X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2Fapi%2FApiMain.php;h=2e16312eaaa7811f602a1dda55e6dd62e4d55ef2;hb=b5993f884a3c4b0012fca120d3625452408c159d;hp=1a11b527a0bdbe941396225af80424bc2fdb761b;hpb=c2665e4b6e615c80e4126f1ff343e7f9f3018dcc;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 1a11b527a0..2e16312eaa 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -56,6 +56,7 @@ class ApiMain extends ApiBase { 'parse' => 'ApiParse', 'opensearch' => 'ApiOpenSearch', 'feedcontributions' => 'ApiFeedContributions', + 'feedrecentchanges' => 'ApiFeedRecentChanges', 'feedwatchlist' => 'ApiFeedWatchlist', 'help' => 'ApiHelp', 'paraminfo' => 'ApiParamInfo', @@ -67,7 +68,6 @@ class ApiMain extends ApiBase { 'purge' => 'ApiPurge', 'setnotificationtimestamp' => 'ApiSetNotificationTimestamp', 'rollback' => 'ApiRollback', - 'runjobs' => 'ApiRunJobs', 'delete' => 'ApiDelete', 'undelete' => 'ApiUndelete', 'protect' => 'ApiProtect', @@ -362,37 +362,7 @@ class ApiMain extends ApiBase { try { $this->executeAction(); } catch ( Exception $e ) { - // Allow extra cleanup and logging - wfRunHooks( 'ApiMain::onException', array( $this, $e ) ); - - // Log it - if ( !( $e instanceof UsageException ) ) { - MWExceptionHandler::logException( $e ); - } - - // Handle any kind of exception by outputting properly formatted error message. - // If this fails, an unhandled exception should be thrown so that global error - // handler will process and log it. - - $errCode = $this->substituteResultWithError( $e ); - - // Error results should not be cached - $this->setCacheMode( 'private' ); - - $response = $this->getRequest()->response(); - $headerStr = 'MediaWiki-API-Error: ' . $errCode; - if ( $e->getCode() === 0 ) { - $response->header( $headerStr ); - } else { - $response->header( $headerStr, true, $e->getCode() ); - } - - // 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 ); + $this->handleException( $e ); } // Log the request whether or not there was an error @@ -409,6 +379,76 @@ class ApiMain extends ApiBase { ob_end_flush(); } + /** + * Handle an exception as an API response + * + * @since 1.23 + * @param Exception $e + */ + protected function handleException( Exception $e ) { + // Bug 63145: Rollback any open database transactions + MWExceptionHandler::rollbackMasterChangesAndLog( $e ); + + // Allow extra cleanup and logging + wfRunHooks( 'ApiMain::onException', array( $this, $e ) ); + + // Log it + if ( !( $e instanceof UsageException ) ) { + MWExceptionHandler::logException( $e ); + } + + // Handle any kind of exception by outputting properly formatted error message. + // If this fails, an unhandled exception should be thrown so that global error + // handler will process and log it. + + $errCode = $this->substituteResultWithError( $e ); + + // Error results should not be cached + $this->setCacheMode( 'private' ); + + $response = $this->getRequest()->response(); + $headerStr = 'MediaWiki-API-Error: ' . $errCode; + if ( $e->getCode() === 0 ) { + $response->header( $headerStr ); + } else { + $response->header( $headerStr, true, $e->getCode() ); + } + + // 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 ); + } + + /** + * Handle an exception from the ApiBeforeMain hook. + * + * This tries to print the exception as an API response, to be more + * friendly to clients. If it fails, it will rethrow the exception. + * + * @since 1.23 + * @param Exception $e + */ + public static function handleApiBeforeMainException( Exception $e ) { + ob_start(); + + try { + $main = new self( RequestContext::getMain(), false ); + $main->handleException( $e ); + } catch ( Exception $e2 ) { + // Nope, even that didn't work. Punt. + throw $e; + } + + // Log the request and reset cache headers + $main->logRequest( 0 ); + $main->sendCacheHeaders(); + + ob_end_flush(); + } + /** * Check the &origin= query parameter against the Origin: HTTP header and respond appropriately. * @@ -604,6 +644,7 @@ class ApiMain extends ApiBase { global $wgShowHostnames; $result = $this->getResult(); + // Printer may not be initialized if the extractRequestParams() fails for the main module if ( !isset( $this->mPrinter ) ) { // The printer has not been created yet. Try to manually get formatter value. @@ -613,11 +654,18 @@ class ApiMain extends ApiBase { } $this->mPrinter = $this->createPrinterByName( $value ); - if ( $this->mPrinter->getNeedsRawData() ) { - $result->setRawMode(); - } } + // 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 ); + } + + // Update raw mode flag for the selected printer. + $result->setRawMode( $this->mPrinter->getNeedsRawData() ); + if ( $e instanceof UsageException ) { // User entered incorrect parameters - print usage screen $errMessage = $e->getMessageArray();