* Introduced LBFactory -- an abstract class for configuring database load balancers...
[lhc/web/wiklou.git] / includes / api / ApiMain.php
index b33e762..d933710 100644 (file)
@@ -53,10 +53,25 @@ class ApiMain extends ApiBase {
         */
        private static $Modules = array (
                'login' => 'ApiLogin',
+               'logout' => 'ApiLogout',
                'query' => 'ApiQuery',
+               'expandtemplates' => 'ApiExpandTemplates',
+               'parse' => 'ApiParse',
                'opensearch' => 'ApiOpenSearch',
                'feedwatchlist' => 'ApiFeedWatchlist',
                'help' => 'ApiHelp',
+               'paraminfo' => 'ApiParamInfo',
+       );
+       
+       private static $WriteModules = array (
+               'rollback' => 'ApiRollback',
+               'delete' => 'ApiDelete',
+               'undelete' => 'ApiUndelete',
+               'protect' => 'ApiProtect',
+               'block' => 'ApiBlock',
+               'unblock' => 'ApiUnblock',
+               'move' => 'ApiMove',
+               'edit' => 'ApiEditPage',
        );
 
        /**
@@ -73,7 +88,11 @@ class ApiMain extends ApiBase {
                'xmlfm' => 'ApiFormatXml',
                'yaml' => 'ApiFormatYaml',
                'yamlfm' => 'ApiFormatYaml',
-               'rawfm' => 'ApiFormatJson'
+               'rawfm' => 'ApiFormatJson',
+               'txt' => 'ApiFormatTxt',
+               'txtfm' => 'ApiFormatTxt',
+               'dbg' => 'ApiFormatDbg',
+               'dbgfm' => 'ApiFormatDbg'
        );
 
        private $mPrinter, $mModules, $mModuleNames, $mFormats, $mFormatNames;
@@ -98,15 +117,28 @@ class ApiMain extends ApiBase {
                        // If the current user cannot read, 
                        // Remove all modules other than login
                        global $wgUser;
+                       
+                       if( $request->getVal( 'callback' ) !== null ) {
+                               // JSON callback allows cross-site reads.
+                               // For safety, strip user credentials.
+                               wfDebug( "API: stripping user credentials for JSON callback\n" );
+                               $wgUser = new User();
+                       }
+                       
                        if (!$wgUser->isAllowed('read')) {
                                self::$Modules = array(
-                                       'login' => self::$Modules['login'],
-                                       'help' => self::$Modules['help']
+                                       'login'  => self::$Modules['login'],
+                                       'logout' => self::$Modules['logout'],
+                                       'help'   => self::$Modules['help'],
                                        ); 
                        }
                }
 
-               $this->mModules = self :: $Modules;
+               global $wgAPIModules, $wgEnableWriteAPI; // extension modules
+               $this->mModules = $wgAPIModules + self :: $Modules;
+               if($wgEnableWriteAPI)
+                       $this->mModules += self::$WriteModules;
+
                $this->mModuleNames = array_keys($this->mModules); // todo: optimize
                $this->mFormats = self :: $Formats;
                $this->mFormatNames = array_keys($this->mFormats); // todo: optimize
@@ -147,7 +179,7 @@ class ApiMain extends ApiBase {
        public function requestWriteMode() {
                if (!$this->mEnableWrite)
                        $this->dieUsage('Editing of this site is disabled. Make sure the $wgEnableWriteAPI=true; ' .
-                       'statement is included in the site\'s LocalSettings.php file', 'readonly');
+                       'statement is included in the site\'s LocalSettings.php file', 'noapiwrite');
        }
 
        /**
@@ -261,7 +293,7 @@ class ApiMain extends ApiBase {
                                // Something is seriously wrong
                                //
                                $errMessage = array (
-                                       'code' => 'internal_api_error',
+                                       'code' => 'internal_api_error_'. get_class($e),
                                        'info' => "Exception Caught: {$e->getMessage()}"
                                );
                                ApiResult :: setContent($errMessage, "\n\n{$e->getTraceAsString()}\n\n");
@@ -277,16 +309,34 @@ class ApiMain extends ApiBase {
         * Execute the actual module, without any error handling
         */
        protected function executeAction() {
-               
+
                $params = $this->extractRequestParams();
-               
+
                $this->mShowVersions = $params['version'];
                $this->mAction = $params['action'];
 
                // Instantiate the module requested by the user
                $module = new $this->mModules[$this->mAction] ($this, $this->mAction);
-
+               
+               if( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) {
+                       // Check for maxlag
+                       global $wgShowHostnames;
+                       $maxLag = $params['maxlag'];
+                       list( $host, $lag ) = wfGetLB()->getMaxLag();
+                       if ( $lag > $maxLag ) {
+                               if( $wgShowHostnames ) {
+                                       ApiBase :: dieUsage( "Waiting for $host: $lag seconds lagged", 'maxlag' );
+                               } else {
+                                       ApiBase :: dieUsage( "Waiting for a database server: $lag seconds lagged", 'maxlag' );
+                               }
+                               return;
+                       }
+               }
+               
                if (!$this->mInternalMode) {
+                       // Ignore mustBePosted() for internal calls
+                       if($module->mustBePosted() && !$this->mRequest->wasPosted())
+                               $this->dieUsage("The {$this->mAction} module requires a POST request", 'mustbeposted');
 
                        // See if custom printer is used
                        $this->mPrinter = $module->getCustomPrinter();
@@ -316,7 +366,16 @@ class ApiMain extends ApiBase {
        protected function printResult($isError) {
                $printer = $this->mPrinter;
                $printer->profileIn();
+       
+               /* If the help message is requested in the default (xmlfm) format,
+                * tell the printer not to escape ampersands so that our links do
+                * not break. */
+               $params = $this->extractRequestParams();
+               $printer->setUnescapeAmps ( ( $this->mAction == 'help' || $isError ) 
+                               && $params['format'] == ApiMain::API_DEFAULT_FORMAT );
+
                $printer->initPrinter($isError);
+
                $printer->execute();
                $printer->closePrinter();
                $printer->profileOut();
@@ -325,7 +384,7 @@ class ApiMain extends ApiBase {
        /**
         * See ApiBase for description.
         */
-       protected function getAllowedParams() {
+       public function getAllowedParams() {
                return array (
                        'format' => array (
                                ApiBase :: PARAM_DFLT => ApiMain :: API_DEFAULT_FORMAT,
@@ -335,25 +394,29 @@ class ApiMain extends ApiBase {
                                ApiBase :: PARAM_DFLT => 'help',
                                ApiBase :: PARAM_TYPE => $this->mModuleNames
                        ),
-                       'version' => false
+                       'version' => false,
+                       'maxlag'  => array (
+                               ApiBase :: PARAM_TYPE => 'integer'
+                       ),
                );
        }
 
        /**
         * See ApiBase for description.
         */
-       protected function getParamDescription() {
+       public function getParamDescription() {
                return array (
                        'format' => 'The format of the output',
                        'action' => 'What action you would like to perform',
-                       'version' => 'When showing help, include version for each module'
+                       'version' => 'When showing help, include version for each module',
+                       'maxlag' => 'Maximum lag'
                );
        }
 
        /**
         * See ApiBase for description.
         */
-       protected function getDescription() {
+       public function getDescription() {
                return array (
                        '',
                        '',
@@ -386,8 +449,13 @@ class ApiMain extends ApiBase {
         */
        protected function getCredits() {
                return array(
-                       'This API is being implemented by Yuri Astrakhan [[User:Yurik]] / <Firstname><Lastname>@gmail.com',
-                       'Please leave your comments and suggestions at http://www.mediawiki.org/wiki/API'
+                       'API developers:',
+                       '    Roan Kattouw <Firstname>.<Lastname>@home.nl (lead developer Sep 2007-present)',
+                       '    Victor Vasiliev - vasilvv at gee mail dot com',
+                       '    Yuri Astrakhan <Firstname><Lastname>@gmail.com (creator, lead developer Sep 2006-Sep 2007)',
+                       '',
+                       'Please send your comments, suggestions and questions to mediawiki-api@lists.wikimedia.org',
+                       'or file a bug report at http://bugzilla.wikimedia.org/'
                );
        }
 
@@ -395,6 +463,8 @@ class ApiMain extends ApiBase {
         * Override the parent to generate help messages for all available modules.
         */
        public function makeHelpMsg() {
+               
+               $this->mPrinter->setHelp();
 
                // Use parent to make default message for the main module
                $msg = parent :: makeHelpMsg();
@@ -435,11 +505,12 @@ class ApiMain extends ApiBase {
        } 
 
        private $mIsBot = null;
-       
        private $mIsSysop = null;
+       private $mCanApiHighLimits = null;
        
        /**
         * Returns true if the currently logged in user is a bot, false otherwise
+        * OBSOLETE, use canApiHighLimits() instead
         */
        public function isBot() {
                if (!isset ($this->mIsBot)) {
@@ -452,6 +523,7 @@ class ApiMain extends ApiBase {
        /**
         * Similar to isBot(), this method returns true if the logged in user is
         * a sysop, and false if not.
+        * OBSOLETE, use canApiHighLimits() instead
         */
        public function isSysop() {
                if (!isset ($this->mIsSysop)) {
@@ -461,6 +533,15 @@ class ApiMain extends ApiBase {
 
                return $this->mIsSysop;
        }
+       
+       public function canApiHighLimits() {
+               if (!isset($this->mCanApiHighLimits)) {
+                       global $wgUser;
+                       $this->mCanApiHighLimits = $wgUser->isAllowed('apihighlimits');
+               }
+
+               return $this->mCanApiHighLimits;
+       }
 
        public function getShowVersions() {
                return $this->mShowVersions;
@@ -505,6 +586,13 @@ class ApiMain extends ApiBase {
        protected function addFormat( $fmtName, $fmtClass ) {
                $this->mFormats[$fmtName] = $fmtClass;
        }
+       
+       /**
+        * Get the array mapping module names to class names
+        */
+       function getModules() {
+               return $this->mModules;
+       }
 }
 
 /**
@@ -529,3 +617,4 @@ class UsageException extends Exception {
        }
 }
 
+