API: Don't use a nonexistent message name
[lhc/web/wiklou.git] / includes / api / ApiMain.php
index f26f52c..6a57427 100644 (file)
@@ -53,12 +53,27 @@ class ApiMain extends ApiBase {
         */
        private static $Modules = array (
                'login' => 'ApiLogin',
+               'logout' => 'ApiLogout',
                'query' => 'ApiQuery',
                'expandtemplates' => 'ApiExpandTemplates',
-               'render' => 'ApiRender',
+               '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',
+               #'changerights' => 'ApiChangeRights'
+               # Disabled for now
        );
 
        /**
@@ -75,8 +90,11 @@ class ApiMain extends ApiBase {
                'xmlfm' => 'ApiFormatXml',
                'yaml' => 'ApiFormatYaml',
                'yamlfm' => 'ApiFormatYaml',
-               'raw' => 'ApiFormatRaw',
-               'rawfm' => 'ApiFormatJson'
+               'rawfm' => 'ApiFormatJson',
+               'txt' => 'ApiFormatTxt',
+               'txtfm' => 'ApiFormatTxt',
+               'dbg' => 'ApiFormatDbg',
+               'dbgfm' => 'ApiFormatDbg'
        );
 
        private $mPrinter, $mModules, $mModuleNames, $mFormats, $mFormatNames;
@@ -101,16 +119,27 @@ 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'],
                                        ); 
                        }
                }
 
-               global $wgAPIModules; // extension 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;
@@ -152,7 +181,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');
        }
 
        /**
@@ -186,17 +215,6 @@ class ApiMain extends ApiBase {
         * have been accumulated, and replace it with an error message and a help screen.
         */
        protected function executeActionWithErrorHandling() {
-               $params = $this->extractRequestParams();
-               if( isset( $params['maxlag'] ) ) {
-                       // Check for maxlag
-                       global $wgLoadBalancer;
-                       $maxLag = $params['maxlag'];
-                       list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
-                       if ( $lag > $maxLag ) {
-                               wfMaxlagError( $host, $lag, $maxLag );
-                               return;
-                       }
-               }
 
                // In case an error occurs during data output,
                // clear the output buffer and print just the error information
@@ -293,25 +311,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 (!$this->mInternalMode) {
-
-                       //Check usage of raw printer
-                       if( $params['format'] == 'raw' ) {
-                               if( !$module->supportRaw() ) {
-                                       ApiBase :: dieUsage( 'This module doesn\'t support format=raw', 'rawnotsupported' );
-                                       return;
+               
+               if( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) {
+                       // Check for maxlag
+                       global $wgLoadBalancer, $wgShowHostnames;
+                       $maxLag = $params['maxlag'];
+                       list( $host, $lag ) = $wgLoadBalancer->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' );
                                }
-                               $module->setRaw();
+                               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();
@@ -359,7 +386,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,
@@ -379,7 +406,7 @@ class ApiMain extends ApiBase {
        /**
         * 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',
@@ -391,7 +418,7 @@ class ApiMain extends ApiBase {
        /**
         * See ApiBase for description.
         */
-       protected function getDescription() {
+       public function getDescription() {
                return array (
                        '',
                        '',
@@ -424,8 +451,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/'
                );
        }
 
@@ -433,6 +465,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();
@@ -473,11 +507,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)) {
@@ -490,6 +525,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)) {
@@ -499,6 +535,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;
@@ -543,6 +588,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;
+       }
 }
 
 /**
@@ -568,4 +620,3 @@ class UsageException extends Exception {
 }
 
 
-