API PageSet allows generator for non-query modules
[lhc/web/wiklou.git] / includes / api / ApiBase.php
index ff93d2f..abb43e8 100644 (file)
@@ -66,7 +66,15 @@ abstract class ApiBase extends ContextSource {
        const LIMIT_SML1 = 50; // Slow query, std user limit
        const LIMIT_SML2 = 500; // Slow query, bot/sysop limit
 
+       /**
+        * getAllowedParams() flag: When set, the result could take longer to generate,
+        * but should be more thorough. E.g. get the list of generators for ApiSandBox extension
+        * @since 1.21
+        */
+       const GET_VALUES_FOR_HELP = 1;
+
        private $mMainModule, $mModuleName, $mModulePrefix;
+       private $mSlaveDB = null;
        private $mParamCache = array();
 
        /**
@@ -105,15 +113,19 @@ abstract class ApiBase extends ContextSource {
         * The result data should be stored in the ApiResult object available
         * through getResult().
         */
-       public abstract function execute();
+       abstract public function execute();
 
        /**
         * Returns a string that identifies the version of the extending class.
         * Typically includes the class name, the svn revision, timestamp, and
         * last author. Usually done with SVN's Id keyword
         * @return string
+        * @deprecated since 1.21, version string is no longer supported
         */
-       public abstract function getVersion();
+       public function getVersion() {
+               wfDeprecated( __METHOD__, '1.21' );
+               return '';
+       }
 
        /**
         * Get the name of the module being executed by this instance
@@ -123,6 +135,16 @@ abstract class ApiBase extends ContextSource {
                return $this->mModuleName;
        }
 
+
+       /**
+        * Get the module manager, or null if this module has no sub-modules
+        * @since 1.21
+        * @return ApiModuleManager
+        */
+       public function getModuleManager() {
+               return null;
+       }
+
        /**
         * Get parameter prefix (usually two letters or an empty string).
         * @return string
@@ -254,6 +276,8 @@ abstract class ApiBase extends ContextSource {
                        }
                        $msg = $lnPrfx . implode( $lnPrfx, $msg ) . "\n";
 
+                       $msg .= $this->makeHelpArrayToString( $lnPrfx, false, $this->getHelpUrls() );
+
                        if ( $this->isReadMode() ) {
                                $msg .= "\nThis module requires read rights";
                        }
@@ -297,25 +321,6 @@ abstract class ApiBase extends ContextSource {
                                        }
                                }
                        }
-
-                       $msg .= $this->makeHelpArrayToString( $lnPrfx, "Help page", $this->getHelpUrls() );
-
-                       if ( $this->getMain()->getShowVersions() ) {
-                               $versions = $this->getVersion();
-                               $pattern = '/(\$.*) ([0-9a-z_]+\.php) (.*\$)/i';
-                               $callback = array( $this, 'makeHelpMsg_callback' );
-
-                               if ( is_array( $versions ) ) {
-                                       foreach ( $versions as &$v ) {
-                                               $v = preg_replace_callback( $pattern, $callback, $v );
-                                       }
-                                       $versions = implode( "\n  ", $versions );
-                               } else {
-                                       $versions = preg_replace_callback( $pattern, $callback, $versions );
-                               }
-
-                               $msg .= "Version:\n  $versions\n";
-                       }
                }
 
                return $msg;
@@ -340,13 +345,15 @@ abstract class ApiBase extends ContextSource {
                        return '';
                }
                if ( !is_array( $input ) ) {
-                       $input = array(
-                               $input
-                       );
+                       $input = array( $input );
                }
 
                if ( count( $input ) > 0 ) {
-                       $msg = $title . ( count( $input ) > 1 ? 's' : '' ) . ":\n  ";
+                       if ( $title ) {
+                               $msg = $title . ( count( $input ) > 1 ? 's' : '' ) . ":\n  ";
+                       } else {
+                               $msg = '  ';
+                       }
                        $msg .= implode( $prefix, $input ) . "\n";
                        return $msg;
                }
@@ -359,7 +366,7 @@ abstract class ApiBase extends ContextSource {
         * @return string or false
         */
        public function makeHelpMsgParameters() {
-               $params = $this->getFinalParams();
+               $params = $this->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
                if ( $params ) {
 
                        $paramsDescription = $this->getFinalParamDescription();
@@ -486,44 +493,6 @@ abstract class ApiBase extends ContextSource {
                }
        }
 
-       /**
-        * Callback for preg_replace_callback() call in makeHelpMsg().
-        * Replaces a source file name with a link to ViewVC
-        *
-        * @param $matches array
-        * @return string
-        */
-       public function makeHelpMsg_callback( $matches ) {
-               global $wgAutoloadClasses, $wgAutoloadLocalClasses;
-
-               $file = '';
-               if ( isset( $wgAutoloadLocalClasses[get_class( $this )] ) ) {
-                       $file = $wgAutoloadLocalClasses[get_class( $this )];
-               } elseif ( isset( $wgAutoloadClasses[get_class( $this )] ) ) {
-                       $file = $wgAutoloadClasses[get_class( $this )];
-               }
-
-               // Do some guesswork here
-               $path = strstr( $file, 'includes/api/' );
-               if ( $path === false ) {
-                       $path = strstr( $file, 'extensions/' );
-               } else {
-                       $path = 'phase3/' . $path;
-               }
-
-               // Get the filename from $matches[2] instead of $file
-               // If they're not the same file, they're assumed to be in the
-               // same directory
-               // This is necessary to make stuff like ApiMain::getVersion()
-               // returning the version string for ApiBase work
-               if ( $path ) {
-                       return "{$matches[0]}\n   https://svn.wikimedia.org/" .
-                                       "viewvc/mediawiki/trunk/" . dirname( $path ) .
-                                       "/{$matches[2]}";
-               }
-               return $matches[0];
-       }
-
        /**
         * Returns the description string for this module
         * @return mixed string or array of strings
@@ -545,15 +514,22 @@ abstract class ApiBase extends ContextSource {
         * value) or (parameter name) => (array with PARAM_* constants as keys)
         * Don't call this function directly: use getFinalParams() to allow
         * hooks to modify parameters as needed.
+        *
+        * Some derived classes may choose to handle an integer $flags parameter
+        * in the overriding methods. Callers of this method can pass zero or
+        * more OR-ed flags like GET_VALUES_FOR_HELP.
+        *
         * @return array|bool
         */
-       protected function getAllowedParams() {
+       protected function getAllowedParams( /* $flags = 0 */ ) {
+               // int $flags is not declared because it causes "Strict standards"
+               // warning. Most derived classes do not implement it.
                return false;
        }
 
        /**
         * Returns an array of parameter descriptions.
-        * Don't call this functon directly: use getFinalParamDescription() to
+        * Don't call this function directly: use getFinalParamDescription() to
         * allow hooks to modify descriptions as needed.
         * @return array|bool False on no parameter descriptions
         */
@@ -565,11 +541,13 @@ abstract class ApiBase extends ContextSource {
         * Get final list of parameters, after hooks have had a chance to
         * tweak it as needed.
         *
+        * @param $flags int Zero or more flags like GET_VALUES_FOR_HELP
         * @return array|Bool False on no parameters
+        * @since 1.21 $flags param added
         */
-       public function getFinalParams() {
-               $params = $this->getAllowedParams();
-               wfRunHooks( 'APIGetAllowedParams', array( &$this, &$params ) );
+       public function getFinalParams( $flags = 0 ) {
+               $params = $this->getAllowedParams( $flags );
+               wfRunHooks( 'APIGetAllowedParams', array( &$this, &$params, $flags ) );
                return $params;
        }
 
@@ -703,7 +681,7 @@ abstract class ApiBase extends ContextSource {
                        array( $this, "parameterNotEmpty" ) ) ), $required );
 
                if ( count( $intersection ) > 1 ) {
-                       $this->dieUsage( "The parameters {$p}" . implode( ", {$p}",  $intersection ) . ' can not be used together', "{$p}invalidparammix" );
+                       $this->dieUsage( "The parameters {$p}" . implode( ", {$p}", $intersection ) . ' can not be used together', "{$p}invalidparammix" );
                } elseif ( count( $intersection ) == 0 ) {
                        $this->dieUsage( "One of the parameters {$p}" . implode( ", {$p}", $required ) . ' is required', "{$p}missingparam" );
                }
@@ -1352,8 +1330,8 @@ abstract class ApiBase extends ContextSource {
                // uploadMsgs
                'invalid-file-key' => array( 'code' => 'invalid-file-key', 'info' => 'Not a valid file key' ),
                'nouploadmodule' => array( 'code' => 'nouploadmodule', 'info' => 'No upload module set' ),
-               'uploaddisabled' => array( 'code' => 'uploaddisabled', 'info' => 'Uploads are not enabled.  Make sure $wgEnableUploads is set to true in LocalSettings.php and the PHP ini setting file_uploads is true' ),
-               'copyuploaddisabled' => array( 'code' => 'copyuploaddisabled', 'info' => 'Uploads by URL is not enabled.  Make sure $wgAllowCopyUploads is set to true in LocalSettings.php.' ),
+               'uploaddisabled' => array( 'code' => 'uploaddisabled', 'info' => 'Uploads are not enabled. Make sure $wgEnableUploads is set to true in LocalSettings.php and the PHP ini setting file_uploads is true' ),
+               'copyuploaddisabled' => array( 'code' => 'copyuploaddisabled', 'info' => 'Uploads by URL is not enabled. Make sure $wgAllowCopyUploads is set to true in LocalSettings.php.' ),
                'copyuploadbaddomain' => array( 'code' => 'copyuploadbaddomain', 'info' => 'Uploads by URL are not allowed from this domain.' ),
 
                'filename-tooshort' => array( 'code' => 'filename-tooshort', 'info' => 'The filename is too short' ),
@@ -1387,6 +1365,26 @@ abstract class ApiBase extends ContextSource {
                $this->dieUsage( $parsed['info'], $parsed['code'] );
        }
 
+       /**
+        * Will only set a warning instead of failing if the global $wgDebugAPI
+        * is set to true. Otherwise behaves exactly as dieUsageMsg().
+        * @param $error (array|string) Element of a getUserPermissionsErrors()-style array
+        * @since 1.21
+        */
+       public function dieUsageMsgOrDebug( $error ) {
+               global $wgDebugAPI;
+               if( $wgDebugAPI !== true ) {
+                       $this->dieUsageMsg( $error );
+               } else {
+                       if( is_string( $error ) ) {
+                               $error = array( $error );
+                       }
+                       $parsed = $this->parseMsg( $error );
+                       $this->setWarning( '$wgDebugAPI: ' . $parsed['code']
+                               . ' - ' . $parsed['info'] );
+               }
+       }
+
        /**
         * Return the error message related to a certain array
         * @param $error array Element of a getUserPermissionsErrors()-style array
@@ -1398,7 +1396,7 @@ abstract class ApiBase extends ContextSource {
 
                // Check whether the error array was nested
                // array( array( <code>, <params> ), array( <another_code>, <params> ) )
-               if( is_array( $key ) ){
+               if( is_array( $key ) ) {
                        $error = $key;
                        $key = array_shift( $error );
                }
@@ -1669,10 +1667,16 @@ abstract class ApiBase extends ContextSource {
        }
 
        /**
+        * Gets a default slave database connection object
         * @return DatabaseBase
         */
        protected function getDB() {
-               return wfGetDB( DB_SLAVE, 'api' );
+               if ( !isset( $this->mSlaveDB ) ) {
+                       $this->profileDBIn();
+                       $this->mSlaveDB = wfGetDB( DB_SLAVE, 'api' );
+                       $this->profileDBOut();
+               }
+               return $this->mSlaveDB;
        }
 
        /**
@@ -1689,12 +1693,4 @@ abstract class ApiBase extends ContextSource {
                }
                print "\n</pre>\n";
        }
-
-       /**
-        * Returns a string that identifies the version of this class.
-        * @return string
-        */
-       public static function getBaseVersion() {
-               return __CLASS__ . ': $Id$';
-       }
 }