Add warning for unused params inside api
authorumherirrender <umherirrender_de.wp@web.de>
Wed, 26 Sep 2012 08:30:53 +0000 (10:30 +0200)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 3 Oct 2012 00:06:13 +0000 (00:06 +0000)
Now MediaWiki knows all used params, it can be helpful to get a list of
unused params. This can be spelling issues or the missing 'g' for
generator modules. When the client knows about this, he can correct
them.

Change-Id: I221b7cd02bed4a38aba161dc3f85eb8fd2aad52c

includes/api/ApiMain.php

index bfc4933..87a287b 100644 (file)
@@ -821,6 +821,8 @@ class ApiMain extends ApiBase {
                $module->profileOut();
 
                if ( !$this->mInternalMode ) {
+                       // Report unused params
+                       $this->reportUnusedParams();
 
                        //append Debug information
                        MWDebug::appendDebugInfoToApiResult( $this->getContext(), $this->getResult() );
@@ -897,6 +899,21 @@ class ApiMain extends ApiBase {
                return $this->getRequest()->getCheck( $name );          
        }
 
+       /**
+        * Report unused parameters, so the client gets a hint in case it gave us parameters we don't know,
+        * for example in case of spelling mistakes or a missing 'g' prefix for generators.
+        */
+       protected function reportUnusedParams() {
+               $paramsUsed = $this->getParamsUsed();
+               $allParams = $this->getRequest()->getValueNames();
+
+               $unusedParams = array_diff( $allParams, $paramsUsed );
+               if( count( $unusedParams ) ) {
+                       $s = count( $unusedParams ) > 1 ? 's' : '';
+                       $this->setWarning( "Unrecognized parameter$s: '" . implode( $unusedParams, "', '" ) . "'" );
+               }
+       }
+
        /**
         * Print results using the current printer
         *