Implement getRequireOnlyOneParameterErrorMessages, to make the error messages require...
authorSam Reed <reedy@users.mediawiki.org>
Fri, 25 Feb 2011 19:09:39 +0000 (19:09 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Fri, 25 Feb 2011 19:09:39 +0000 (19:09 +0000)
Use in 4 modules for more dynamicness

includes/api/ApiBase.php
includes/api/ApiDelete.php
includes/api/ApiMove.php
includes/api/ApiQueryCategoryMembers.php
includes/api/ApiUpload.php

index e761fbd..d8905a8 100644 (file)
@@ -555,6 +555,22 @@ abstract class ApiBase {
                }
        }
 
+       /**
+        * Generates the possible errors requireOnlyOneParameter() can die with
+        *
+        * @param $params array
+        * @return array
+        */
+       public function getRequireOnlyOneParameterErrorMessages( $params ) {
+               $p = $this->getModulePrefix();
+               $params = implode( ", {$p}", $params );
+
+               return array(
+                       array( 'code' => "{$p}missingparam", 'info' => "One of the parameters {$p}{$params} is required" ),
+                       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 bae27bb..2f6ed2d 100644 (file)
@@ -255,14 +255,15 @@ class ApiDelete extends ApiBase {
        }
 
        public function getPossibleErrors() {
-               return array_merge( parent::getPossibleErrors(), array(
-                       array( 'code' => 'missingparam', 'info' => 'One of the parameters title, pageid is required' ),
-                       array( 'code' => 'invalidparammix', 'info' => 'The parameters title, pageid can not be used together' ),
-                       array( 'invalidtitle', 'title' ),
-                       array( 'nosuchpageid', 'pageid' ),
-                       array( 'notanarticle' ),
-                       array( 'hookaborted', 'error' ),
-               ) );
+               return array_merge( parent::getPossibleErrors(),
+                       $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
+                       array(
+                               array( 'invalidtitle', 'title' ),
+                               array( 'nosuchpageid', 'pageid' ),
+                               array( 'notanarticle' ),
+                               array( 'hookaborted', 'error' ),
+                       )
+               );
        }
 
        public function needsToken() {
index 63fe758..28ad025 100644 (file)
@@ -224,15 +224,16 @@ class ApiMove extends ApiBase {
        }
 
        public function getPossibleErrors() {
-               return array_merge( parent::getPossibleErrors(), array(
-                       array( 'code' => 'missingparam', 'info' => 'One of the parameters from, fromid is required' ),
-                       array( 'code' => 'invalidparammix', 'info' => 'The parameters from, fromid can not be used together' ),
-                       array( 'invalidtitle', 'from' ),
-                       array( 'nosuchpageid', 'fromid' ),
-                       array( 'notanarticle' ),
-                       array( 'invalidtitle', 'to' ),
-                       array( 'sharedfile-exists' ),
-               ) );
+               return array_merge( parent::getPossibleErrors(),
+                       $this->getRequireOnlyOneParameterErrorMessages( array( 'from', 'fromid' ) ),
+                       array(
+                               array( 'invalidtitle', 'from' ),
+                               array( 'nosuchpageid', 'fromid' ),
+                               array( 'notanarticle' ),
+                               array( 'invalidtitle', 'to' ),
+                               array( 'sharedfile-exists' ),
+                       )
+               );
        }
 
        public function needsToken() {
index d245e66..15da5cd 100644 (file)
@@ -326,13 +326,14 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
        }
 
        public function getPossibleErrors() {
-               return array_merge( parent::getPossibleErrors(), array(
-                       array( 'code' => 'cmmissingparam', 'info' => 'One of the parameters title, pageid is required' ),
-                       array( 'code' => 'cminvalidparammix', 'info' => 'The parameters title, pageid can not be used together' ),
-                       array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
-                       array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
-                       array( 'nosuchpageid', 'pageid' ),
-               ) );
+               return array_merge( parent::getPossibleErrors(),
+                       $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
+                       array(
+                               array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
+                               array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
+                               array( 'nosuchpageid', 'pageid' ),
+                       )
+               );
        }
 
        protected function getExamples() {
index 658fa11..3d29b70 100644 (file)
@@ -474,24 +474,25 @@ class ApiUpload extends ApiBase {
        }
 
        public function getPossibleErrors() {
-               return array_merge( parent::getPossibleErrors(), array(
-                       array( 'uploaddisabled' ),
-                       array( 'invalid-session-key' ),
-                       array( 'uploaddisabled' ),
-                       array( 'mustbeloggedin', 'upload' ),
-                       array( 'badaccess-groups' ),
-                       array( 'code' => 'fetchfileerror', 'info' => '' ),
-                       array( 'code' => 'nomodule', 'info' => 'No upload module set' ),
-                       array( 'code' => 'empty-file', 'info' => 'The file you submitted was empty' ),
-                       array( 'code' => 'filetype-missing', 'info' => 'The file is missing an extension' ),
-                       array( 'code' => 'filename-tooshort', 'info' => 'The filename is too short' ),
-                       array( 'code' => 'overwrite', 'info' => 'Overwriting an existing file is not allowed' ),
-                       array( 'code' => 'stashfailed', 'info' => 'Stashing temporary file failed' ),
-                       array( 'code' => 'internal-error', 'info' => 'An internal error occurred' ),
-                       array( 'code' => 'missingparam', 'info' => 'One of the parameters sessionkey, file, url, statuskey is required' ),
-                       array( 'code' => 'invalidparammix', 'info' => 'The parameters sessionkey, file, url, statuskey can not be used together' ),
-                       array( 'code' => 'asynccopyuploaddisabled', 'info' => 'Asynchronous copy uploads disabled' ),
-               ) );
+               return array_merge( parent::getPossibleErrors(),
+                       $this->getRequireOnlyOneParameterErrorMessages( array( 'sessionkey', 'file', 'url', 'statuskey' ) ),
+                       array(
+                               array( 'uploaddisabled' ),
+                               array( 'invalid-session-key' ),
+                               array( 'uploaddisabled' ),
+                               array( 'mustbeloggedin', 'upload' ),
+                               array( 'badaccess-groups' ),
+                               array( 'code' => 'fetchfileerror', 'info' => '' ),
+                               array( 'code' => 'nomodule', 'info' => 'No upload module set' ),
+                               array( 'code' => 'empty-file', 'info' => 'The file you submitted was empty' ),
+                               array( 'code' => 'filetype-missing', 'info' => 'The file is missing an extension' ),
+                               array( 'code' => 'filename-tooshort', 'info' => 'The filename is too short' ),
+                               array( 'code' => 'overwrite', 'info' => 'Overwriting an existing file is not allowed' ),
+                               array( 'code' => 'stashfailed', 'info' => 'Stashing temporary file failed' ),
+                               array( 'code' => 'internal-error', 'info' => 'An internal error occurred' ),
+                               array( 'code' => 'asynccopyuploaddisabled', 'info' => 'Asynchronous copy uploads disabled' ),
+                       )
+               );
        }
 
        public function needsToken() {