Merge "Add special pages aliases for Western Balochi (bgn) from translatewiki"
[lhc/web/wiklou.git] / includes / api / ApiMain.php
index 34ed523..2978453 100644 (file)
@@ -181,10 +181,10 @@ class ApiMain extends ApiBase {
                        // Remove all modules other than login
                        global $wgUser;
 
-                       if ( $this->getVal( 'callback' ) !== null ) {
-                               // JSON callback allows cross-site reads.
-                               // For safety, strip user credentials.
-                               wfDebug( "API: stripping user credentials for JSON callback\n" );
+                       if ( $this->lacksSameOriginSecurity() ) {
+                               // If we're in a mode that breaks the same-origin policy, strip
+                               // user credentials for security.
+                               wfDebug( "API: stripping user credentials when the same-origin policy is not applied\n" );
                                $wgUser = new User();
                                $this->getContext()->setUser( $wgUser );
                        }
@@ -215,6 +215,8 @@ class ApiMain extends ApiBase {
                $this->mModuleMgr->addModules( self::$Formats, 'format' );
                $this->mModuleMgr->addModules( $config->get( 'APIFormatModules' ), 'format' );
 
+               Hooks::run( 'ApiMain::moduleManager', array( $this->mModuleMgr ) );
+
                $this->mResult = new ApiResult( $this );
                $this->mEnableWrite = $enableWrite;
 
@@ -359,14 +361,11 @@ class ApiMain extends ApiBase {
         * Execute api request. Any errors will be handled if the API was called by the remote client.
         */
        public function execute() {
-               $this->profileIn();
                if ( $this->mInternalMode ) {
                        $this->executeAction();
                } else {
                        $this->executeActionWithErrorHandling();
                }
-
-               $this->profileOut();
        }
 
        /**
@@ -417,7 +416,13 @@ class ApiMain extends ApiBase {
                // Bug 63145: Rollback any open database transactions
                if ( !( $e instanceof UsageException ) ) {
                        // UsageExceptions are intentional, so don't rollback if that's the case
-                       MWExceptionHandler::rollbackMasterChangesAndLog( $e );
+                       try {
+                               MWExceptionHandler::rollbackMasterChangesAndLog( $e );
+                       } catch ( DBError $e2 ) {
+                               // Rollback threw an exception too. Log it, but don't interrupt
+                               // our regularly scheduled exception handling.
+                               MWExceptionHandler::logException( $e2 );
+                       }
                }
 
                // Allow extra cleanup and logging
@@ -448,8 +453,6 @@ class ApiMain extends ApiBase {
                // Reset and print just the error message
                ob_clean();
 
-               // If the error occurred during printing, do a printer->profileOut()
-               $this->mPrinter->safeProfileOut();
                $this->printResult( true );
        }
 
@@ -656,8 +659,24 @@ class ApiMain extends ApiBase {
                        $out->addVaryHeader( 'X-Forwarded-Proto' );
                }
 
+               // The logic should be:
+               // $this->mCacheControl['max-age'] is set?
+               //    Use it, the module knows better than our guess.
+               // !$this->mModule || $this->mModule->isWriteMode(), and mCacheMode is private?
+               //    Use 0 because we can guess caching is probably the wrong thing to do.
+               // Use $this->getParameter( 'maxage' ), which already defaults to 0.
+               $maxage = 0;
+               if ( isset( $this->mCacheControl['max-age'] ) ) {
+                       $maxage = $this->mCacheControl['max-age'];
+               } elseif ( ( $this->mModule && !$this->mModule->isWriteMode() ) ||
+                       $this->mCacheMode !== 'private'
+               ) {
+                       $maxage = $this->getParameter( 'maxage' );
+               }
+               $privateCache = 'private, must-revalidate, max-age=' . $maxage;
+
                if ( $this->mCacheMode == 'private' ) {
-                       $response->header( 'Cache-Control: private' );
+                       $response->header( "Cache-Control: $privateCache" );
                        return;
                }
 
@@ -669,14 +688,14 @@ class ApiMain extends ApiBase {
                                $response->header( $out->getXVO() );
                                if ( $out->haveCacheVaryCookies() ) {
                                        // Logged in, mark this request private
-                                       $response->header( 'Cache-Control: private' );
+                                       $response->header( "Cache-Control: $privateCache" );
                                        return;
                                }
                                // Logged out, send normal public headers below
                        } elseif ( session_id() != '' ) {
                                // Logged in or otherwise has session (e.g. anonymous users who have edited)
                                // Mark request private
-                               $response->header( 'Cache-Control: private' );
+                               $response->header( "Cache-Control: $privateCache" );
 
                                return;
                        } // else no XVO and anonymous, send public headers below
@@ -700,7 +719,7 @@ class ApiMain extends ApiBase {
                        // Public cache not requested
                        // Sending a Vary header in this case is harmless, and protects us
                        // against conditional calls of setCacheMaxAge().
-                       $response->header( 'Cache-Control: private' );
+                       $response->header( "Cache-Control: $privateCache" );
 
                        return;
                }
@@ -753,7 +772,6 @@ class ApiMain extends ApiBase {
                // Printer may not be able to handle errors. This is particularly
                // likely if the module returns something for getCustomPrinter().
                if ( !$this->mPrinter->canPrintErrors() ) {
-                       $this->mPrinter->safeProfileOut();
                        $this->mPrinter = $this->createPrinterByName( self::API_DEFAULT_FORMAT );
                }
 
@@ -1022,10 +1040,8 @@ class ApiMain extends ApiBase {
                $this->checkAsserts( $params );
 
                // Execute
-               $module->profileIn();
                $module->execute();
                Hooks::run( 'APIAfterExecute', array( &$module ) );
-               $module->profileOut();
 
                $this->reportUnusedParams();
 
@@ -1176,13 +1192,10 @@ class ApiMain extends ApiBase {
 
                $this->getResult()->cleanUpUTF8();
                $printer = $this->mPrinter;
-               $printer->profileIn();
 
                $printer->initPrinter( false );
-
                $printer->execute();
                $printer->closePrinter();
-               $printer->profileOut();
        }
 
        /**