API: Warn when unsupported PHP array syntax is used
authorBrad Jorsch <bjorsch@wikimedia.org>
Thu, 17 Apr 2014 14:24:30 +0000 (10:24 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Thu, 17 Apr 2014 14:24:30 +0000 (10:24 -0400)
The API takes multi-valued parameters as key=value1|value2|value3, not
key[]=value1&key[]=value2&key[]=value3, in part because the latter is
overly verbose when the API encourages use of large arrays.

But when someone, not knowing this, does accidentally use the verbose
syntax, they should get a warning instead of having the parameter be
silently ignored.

Bug: 64057
Change-Id: I32a16efb8028d7f6d120d20dfc886f08ed9ec97d

includes/api/ApiMain.php

index 2e16312..5cdcd51 100644 (file)
@@ -980,7 +980,18 @@ class ApiMain extends ApiBase {
        public function getVal( $name, $default = null ) {
                $this->mParamsUsed[$name] = true;
 
-               return $this->getRequest()->getVal( $name, $default );
+               $ret = $this->getRequest()->getVal( $name );
+               if ( $ret === null ) {
+                       if ( $this->getRequest()->getArray( $name ) !== null ) {
+                               // See bug 10262 for why we don't just join( '|', ... ) the
+                               // array.
+                               $this->setWarning(
+                                       "Parameter '$name' uses unsupported PHP array syntax"
+                               );
+                       }
+                       $ret = $default;
+               }
+               return $ret;
        }
 
        /**
@@ -988,9 +999,7 @@ class ApiMain extends ApiBase {
         * was used, for logging.
         */
        public function getCheck( $name ) {
-               $this->mParamsUsed[$name] = true;
-
-               return $this->getRequest()->getCheck( $name );
+               return $this->getVal( $name, null ) !== null;
        }
 
        /**