* (bug 27716) Make a method to do checking of 0 or 1 of the parameters existence...
authorSam Reed <reedy@users.mediawiki.org>
Mon, 6 Jun 2011 16:45:40 +0000 (16:45 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Mon, 6 Jun 2011 16:45:40 +0000 (16:45 +0000)
includes/api/ApiBase.php
includes/api/ApiQueryBlocks.php

index ce7fd48..572c3c4 100644 (file)
@@ -581,6 +581,38 @@ abstract class ApiBase {
                );
        }
 
+       /**
+        * Die if more than one of a certain set of parameters is set and not false.
+        *
+        * @param $params array
+        */
+       public function requireMaxOneParameter( $params ) {
+               $required = func_get_args();
+               array_shift( $required );
+
+               $intersection = array_intersect( array_keys( array_filter( $params,
+                               array( $this, "parameterNotEmpty" ) ) ), $required );
+
+               if ( count( $intersection ) > 1 ) {
+                       $this->dieUsage( 'The parameters ' . implode( ', ', $intersection ) . ' can not be used together', 'invalidparammix' );
+               }
+       }
+
+       /**
+        * Generates the possible error requireMaxOneParameter() can die with
+        *
+        * @param $params array
+        * @return array
+        */
+       public function getRequireMaxOneParameterErrorMessages( $params ) {
+               $p = $this->getModulePrefix();
+               $params = implode( ", {$p}", $params );
+
+               return array(
+                       array( 'code' => "{$p}invalidparammix", 'info' => "The parameters {$p}{$params} can not be used together" )
+               );
+       }
+
        /**
         * Callback function used in requireOnlyOneParameter to check whether reequired parameters are set
         *
index 878a9ed..d96452c 100644 (file)
@@ -49,9 +49,7 @@ class ApiQueryBlocks extends ApiQueryBase {
                global $wgUser, $wgContLang;
 
                $params = $this->extractRequestParams();
-               if ( isset( $params['users'] ) && isset( $params['ip'] ) ) {
-                       $this->dieUsage( 'bkusers and bkip cannot be used together', 'usersandip' );
-               }
+               $this->requireMaxOneParameter( $params, 'users', 'ip' );
 
                $prop = array_flip( $params['prop'] );
                $fld_id = isset( $prop['id'] );
@@ -290,7 +288,7 @@ class ApiQueryBlocks extends ApiQueryBase {
 
        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
-                       array( 'code' => 'usersandip', 'info' => 'bkusers and bkip cannot be used together' ),
+                       $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ),
                        array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
                        array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
                        array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),