r89581 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89580‎ | r89581 | r89582 >
Date:16:45, 6 June 2011
Author:reedy
Status:ok (Comments)
Tags:
Comment:
* (bug 27716) Make a method to do checking of 0 or 1 of the parameters existence (like requireOnlyOneParameter), but without needing one of the parameters
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryBlocks.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQueryBlocks.php
@@ -49,9 +49,7 @@
5050 global $wgUser, $wgContLang;
5151
5252 $params = $this->extractRequestParams();
53 - if ( isset( $params['users'] ) && isset( $params['ip'] ) ) {
54 - $this->dieUsage( 'bkusers and bkip cannot be used together', 'usersandip' );
55 - }
 53+ $this->requireMaxOneParameter( $params, 'users', 'ip' );
5654
5755 $prop = array_flip( $params['prop'] );
5856 $fld_id = isset( $prop['id'] );
@@ -290,7 +288,7 @@
291289
292290 public function getPossibleErrors() {
293291 return array_merge( parent::getPossibleErrors(), array(
294 - array( 'code' => 'usersandip', 'info' => 'bkusers and bkip cannot be used together' ),
 292+ $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ),
295293 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
296294 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
297295 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
Index: trunk/phase3/includes/api/ApiBase.php
@@ -582,6 +582,38 @@
583583 }
584584
585585 /**
 586+ * Die if more than one of a certain set of parameters is set and not false.
 587+ *
 588+ * @param $params array
 589+ */
 590+ public function requireMaxOneParameter( $params ) {
 591+ $required = func_get_args();
 592+ array_shift( $required );
 593+
 594+ $intersection = array_intersect( array_keys( array_filter( $params,
 595+ array( $this, "parameterNotEmpty" ) ) ), $required );
 596+
 597+ if ( count( $intersection ) > 1 ) {
 598+ $this->dieUsage( 'The parameters ' . implode( ', ', $intersection ) . ' can not be used together', 'invalidparammix' );
 599+ }
 600+ }
 601+
 602+ /**
 603+ * Generates the possible error requireMaxOneParameter() can die with
 604+ *
 605+ * @param $params array
 606+ * @return array
 607+ */
 608+ public function getRequireMaxOneParameterErrorMessages( $params ) {
 609+ $p = $this->getModulePrefix();
 610+ $params = implode( ", {$p}", $params );
 611+
 612+ return array(
 613+ array( 'code' => "{$p}invalidparammix", 'info' => "The parameters {$p}{$params} can not be used together" )
 614+ );
 615+ }
 616+
 617+ /**
586618 * Callback function used in requireOnlyOneParameter to check whether reequired parameters are set
587619 *
588620 * @param $x object Parameter to check is not null/false

Comments

#Comment by Catrope (talk | contribs)   19:46, 15 July 2011
+	 * Die if more than one of a certain set of parameters is set and not false.
+	 *
+	 * @param $params array

Should document that varargs is being used here, and that $params means something entirely different in getRequireMaxOneParameterErrorMessage() (which BTW is the craziest method name ever).

Status & tagging log