Adding apihighlimits permission. Users with this permission can request 10 times...
[lhc/web/wiklou.git] / includes / api / ApiMain.php
index c1ef461..acf3e50 100644 (file)
@@ -289,6 +289,21 @@ class ApiMain extends ApiBase {
 
                // 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 $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' );
+                               }
+                               return;
+                       }
+               }
 
                if (!$this->mInternalMode) {
 
@@ -325,7 +340,7 @@ class ApiMain extends ApiBase {
                 * tell the printer not to escape ampersands so that our links do
                 * not break. */
                $params = $this->extractRequestParams();
-               $printer->setUnescapeAmps ( $this->mAction == 'help' 
+               $printer->setUnescapeAmps ( ( $this->mAction == 'help' || $isError ) 
                                && $params['format'] == ApiMain::API_DEFAULT_FORMAT );
 
                $printer->initPrinter($isError);
@@ -348,7 +363,10 @@ class ApiMain extends ApiBase {
                                ApiBase :: PARAM_DFLT => 'help',
                                ApiBase :: PARAM_TYPE => $this->mModuleNames
                        ),
-                       'version' => false
+                       'version' => false,
+                       'maxlag'  => array (
+                               ApiBase :: PARAM_TYPE => 'integer'
+                       ),
                );
        }
 
@@ -359,7 +377,8 @@ class ApiMain extends ApiBase {
                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'
                );
        }
 
@@ -448,11 +467,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)) {
@@ -465,6 +485,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)) {
@@ -474,6 +495,15 @@ class ApiMain extends ApiBase {
 
                return $this->mIsSysop;
        }
+       
+       public function canApiHighLimits() {
+               if (!is_null ($this->mCanApiHighLimits)) {
+                       global $wgUser;
+                       $this->mCanApiHighLimits = $wgUser->isAllowed('apihighlimits');
+               }
+
+               return $this->mCanApiHighLimits;
+       }
 
        public function getShowVersions() {
                return $this->mShowVersions;