* (bug 11206) api.php should honor maxlag
authorVictor Vasiliev <vasilievvv@users.mediawiki.org>
Sun, 18 Nov 2007 09:37:52 +0000 (09:37 +0000)
committerVictor Vasiliev <vasilievvv@users.mediawiki.org>
Sun, 18 Nov 2007 09:37:52 +0000 (09:37 +0000)
* Add wfMaxlagError function
* Add MIME type override option for format=raw

RELEASE-NOTES
includes/GlobalFunctions.php
includes/Wiki.php
includes/api/ApiFormatBase.php
includes/api/ApiMain.php
includes/api/ApiRender.php

index 9cb6c86..bdf4a83 100644 (file)
@@ -194,6 +194,7 @@ Full API documentation is available at http://www.mediawiki.org/wiki/API
 * (bug 11588) Preserve document structure for empty dataset in backlinks query.
 * Outputting list of all user preferences rather than having to request them by name
 * Add raw formatting support. Now several actions like expandtemplates support raw output with format=raw
+* (bug 11206) api.php should honor maxlag
 
 === Languages updated in 1.12 ===
 
index 008a6d2..fa3eb03 100644 (file)
@@ -2335,4 +2335,24 @@ function wfGetNull() {
        return wfIsWindows()
                ? 'NUL'
                : '/dev/null';
+}
+
+/**
+ * Displays a maxlag error
+ * 
+ * @param string $host Server that lags the most
+ * @param int $lag Maxlag (actual)
+ * @param int $maxLag Maxlag (requested)
+ */
+function wfMaxlagError( $host, $lag, $maxLag ) {
+       global $wgShowHostnames;
+       header( 'HTTP/1.1 503 Service Unavailable' );
+       header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
+       header( 'X-Database-Lag: ' . intval( $lag ) );
+       header( 'Content-Type: text/plain' );
+       if( $wgShowHostnames ) {
+               echo "Waiting for $host: $lag seconds lagged\n";
+       } else {
+               echo "Waiting for a database server: $lag seconds lagged\n";
+       }
 }
\ No newline at end of file
index 02f9430..4920ff2 100644 (file)
@@ -57,18 +57,10 @@ class MediaWiki {
        }
 
        function checkMaxLag( $maxLag ) {
-               global $wgLoadBalancer, $wgShowHostnames;
+               global $wgLoadBalancer;
                list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
                if ( $lag > $maxLag ) {
-                       header( 'HTTP/1.1 503 Service Unavailable' );
-                       header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
-                       header( 'X-Database-Lag: ' . intval( $lag ) );
-                       header( 'Content-Type: text/plain' );
-                       if( $wgShowHostnames ) {
-                               echo "Waiting for $host: $lag seconds lagged\n";
-                       } else {
-                               echo "Waiting for a database server: $lag seconds lagged\n";
-                       }
+                       wfMaxlagError( $host, $lag, $maxLag );
                        return false;
                } else {
                        return true;
index 84205b7..0693040 100644 (file)
@@ -287,28 +287,43 @@ class ApiFormatRaw extends ApiFormatBase {
                parent :: __construct($main, $format);
        }
 
-       public static function setRawData( $result, $raw_data ) {
+       public static function setRawData( $result, $raw_data, $raw_type = 'text/plain' ) {
                $data = & $result->getData();
                $data['_raw'] = $raw_data;
+               $data['_raw_mimetype'] = $raw_type;
        }
 
        public function getMimeType() {
-               return 'text/plain';
+               $data = $this->getResultData();
+               if( !isset( $data['_raw_mimetype'] ) && !isset( $data['error'] ) ) {
+                       ApiBase :: dieDebug( 'ApiFormatRaw', 'No raw data is set for this module' );
+                       return;
+               }
+               elseif( isset( $data['error'] ) ) {
+                       $this->executeError( $data );
+                       return;
+               }
+               return $data['_raw_mimetype'];
        }
 
        public function execute() {
                $data = $this->getResultData();
                if( !isset( $data['_raw'] ) && !isset( $data['error'] ) ) {
                        ApiBase :: dieDebug( 'ApiFormatRaw', 'No raw data is set for this module' );
+                       return;
                }
                elseif( isset( $data['error'] ) ) {
-                       header( '500 Internal error' );
-                       echo "{$data['error']['code']}\n";
-                       echo "{$data['error']['info']}\n";
+                       $this->executeError( $data );
                        return;
                }
                $this->printText( $data['_raw'] );
        }
+       
+       private function executeError( $data ) {
+               wfHttpError(500, 'Internal Server Error', '');
+               echo "{$data['error']['code']}\n";
+               echo "{$data['error']['info']}\n";
+       }
 
        public function getNeedsRawData() {
                return true;
index e5fc508..f26f52c 100644 (file)
@@ -186,6 +186,17 @@ 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
@@ -358,7 +369,10 @@ class ApiMain extends ApiBase {
                                ApiBase :: PARAM_DFLT => 'help',
                                ApiBase :: PARAM_TYPE => $this->mModuleNames
                        ),
-                       'version' => false
+                       'version' => false,
+                       'maxlag'  => array (
+                               ApiBase :: PARAM_TYPE => 'integer'
+                       ),
                );
        }
 
@@ -369,7 +383,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'
                );
        }
 
index 1a10138..3540756 100644 (file)
@@ -57,7 +57,7 @@ class ApiRender extends ApiBase {
                // Return result
                $result = $this->getResult();
                if( $this->isRaw() ) {
-                       ApiFormatRaw :: setRawData( $result, $retval );
+                       ApiFormatRaw :: setRawData( $result, $retval, 'text/html' );
                }
                $retval_array = array();
                $result->setContent( $retval_array, $retval );