Merge "Avoid muliple cache calls to explicitly defined tags"
[lhc/web/wiklou.git] / includes / api / ApiMain.php
index 465025c..a8db20c 100644 (file)
@@ -495,7 +495,22 @@ class ApiMain extends ApiBase {
                // Reset and print just the error message
                ob_clean();
 
-               $this->printResult( true );
+               // Printer may not be initialized if the extractRequestParams() fails for the main module
+               $this->createErrorPrinter();
+
+               try {
+                       $this->printResult( true );
+               } catch ( UsageException $ex ) {
+                       // The error printer itself is failing. Try suppressing its request
+                       // parameters and redo.
+                       $this->setWarning(
+                               'Error printer failed (will retry without params): ' . $ex->getMessage()
+                       );
+                       $this->mPrinter = null;
+                       $this->createErrorPrinter();
+                       $this->mPrinter->forceDefaultParams();
+                       $this->printResult( true );
+               }
        }
 
        /**
@@ -562,8 +577,7 @@ class ApiMain extends ApiBase {
                if ( !in_array( $originParam, $origins ) ) {
                        // origin parameter set but incorrect
                        // Send a 403 response
-                       $message = HttpStatus::getMessage( 403 );
-                       $response->header( "HTTP/1.1 403 $message", true, 403 );
+                       $response->statusHeader( 403 );
                        $response->header( 'Cache-Control: no-cache' );
                        echo "'origin' parameter does not match Origin header\n";
 
@@ -792,22 +806,14 @@ class ApiMain extends ApiBase {
        }
 
        /**
-        * Replace the result data with the information about an exception.
-        * Returns the error code
-        * @param Exception $e
-        * @return string
+        * Create the printer for error output
         */
-       protected function substituteResultWithError( $e ) {
-               $result = $this->getResult();
-
-               // Printer may not be initialized if the extractRequestParams() fails for the main module
+       private function createErrorPrinter() {
                if ( !isset( $this->mPrinter ) ) {
-                       // The printer has not been created yet. Try to manually get formatter value.
                        $value = $this->getRequest()->getVal( 'format', self::API_DEFAULT_FORMAT );
                        if ( !$this->mModuleMgr->isDefined( $value, 'format' ) ) {
                                $value = self::API_DEFAULT_FORMAT;
                        }
-
                        $this->mPrinter = $this->createPrinterByName( $value );
                }
 
@@ -816,10 +822,16 @@ class ApiMain extends ApiBase {
                if ( !$this->mPrinter->canPrintErrors() ) {
                        $this->mPrinter = $this->createPrinterByName( self::API_DEFAULT_FORMAT );
                }
+       }
 
-               // Update raw mode flag for the selected printer.
-               $result->setRawMode( $this->mPrinter->getNeedsRawData() );
-
+       /**
+        * Replace the result data with the information about an exception.
+        * Returns the error code
+        * @param Exception $e
+        * @return string
+        */
+       protected function substituteResultWithError( $e ) {
+               $result = $this->getResult();
                $config = $this->getConfig();
 
                if ( $e instanceof UsageException ) {
@@ -1294,7 +1306,7 @@ class ApiMain extends ApiBase {
                );
        }
 
-       public function modifyHelp( array &$help, array $options ) {
+       public function modifyHelp( array &$help, array $options, array &$tocData ) {
                // Wish PHP had an "array_insert_before". Instead, we have to manually
                // reindex the array to get 'permissions' in the right place.
                $oldHelp = $help;
@@ -1305,6 +1317,7 @@ class ApiMain extends ApiBase {
                        }
                        $help[$k] = $v;
                }
+               $help['datatypes'] = '';
                $help['credits'] = '';
 
                // Fill 'permissions'
@@ -1337,13 +1350,48 @@ class ApiMain extends ApiBase {
                $help['permissions'] .= Html::closeElement( 'dl' );
                $help['permissions'] .= Html::closeElement( 'div' );
 
-               // Fill 'credits', if applicable
+               // Fill 'datatypes' and 'credits', if applicable
                if ( empty( $options['nolead'] ) ) {
-                       $help['credits'] .= Html::element( 'h' . min( 6, $options['headerlevel'] + 1 ),
-                               array( 'id' => '+credits', 'class' => 'apihelp-header' ),
-                               $this->msg( 'api-credits-header' )->parse()
+                       $level = $options['headerlevel'];
+                       $tocnumber = &$options['tocnumber'];
+
+                       $header = $this->msg( 'api-help-datatypes-header' )->parse();
+                       $help['datatypes'] .= Html::rawelement( 'h' . min( 6, $level ),
+                               array( 'id' => 'main/datatypes', 'class' => 'apihelp-header' ),
+                               Html::element( 'span', array( 'id' => Sanitizer::escapeId( 'main/datatypes' ) ) ) .
+                               $header
+                       );
+                       $help['datatypes'] .= $this->msg( 'api-help-datatypes' )->parseAsBlock();
+                       if ( !isset( $tocData['main/datatypes'] ) ) {
+                               $tocnumber[$level]++;
+                               $tocData['main/datatypes'] = array(
+                                       'toclevel' => count( $tocnumber ),
+                                       'level' => $level,
+                                       'anchor' => 'main/datatypes',
+                                       'line' => $header,
+                                       'number' => join( '.', $tocnumber ),
+                                       'index' => false,
+                               );
+                       }
+
+                       $header = $this->msg( 'api-credits-header' )->parse();
+                       $help['credits'] .= Html::rawelement( 'h' . min( 6, $level ),
+                               array( 'id' => 'main/credits', 'class' => 'apihelp-header' ),
+                               Html::element( 'span', array( 'id' => Sanitizer::escapeId( 'main/credits' ) ) ) .
+                               $header
                        );
                        $help['credits'] .= $this->msg( 'api-credits' )->useDatabase( false )->parseAsBlock();
+                       if ( !isset( $tocData['main/credits'] ) ) {
+                               $tocnumber[$level]++;
+                               $tocData['main/credits'] = array(
+                                       'toclevel' => count( $tocnumber ),
+                                       'level' => $level,
+                                       'anchor' => 'main/credits',
+                                       'line' => $header,
+                                       'number' => join( '.', $tocnumber ),
+                                       'index' => false,
+                               );
+                       }
                }
        }