Merge "Convert Special:DeletedContributions to use OOUI."
[lhc/web/wiklou.git] / includes / api / ApiFormatBase.php
index 5011f48..67f54a8 100644 (file)
@@ -33,6 +33,7 @@ abstract class ApiFormatBase extends ApiBase {
        private $mIsHtml, $mFormat, $mUnescapeAmps, $mHelp;
        private $mBuffer, $mDisabled = false;
        private $mIsWrappedHtml = false;
+       private $mHttpStatus = false;
        protected $mForceDefaultParams = false;
 
        /**
@@ -147,6 +148,23 @@ abstract class ApiFormatBase extends ApiBase {
                }
        }
 
+       /**
+        * Set the HTTP status code to be used for the response
+        * @since 1.29
+        * @param int $code
+        */
+       public function setHttpStatus( $code ) {
+               if ( $this->mDisabled ) {
+                       return;
+               }
+
+               if ( $this->getIsHtml() ) {
+                       $this->mHttpStatus = $code;
+               } else {
+                       $this->getMain()->getRequest()->response()->statusHeader( $code );
+               }
+       }
+
        /**
         * Initialize the printer function and prepare the output headers.
         * @param bool $unused Always false since 1.25
@@ -212,6 +230,18 @@ abstract class ApiFormatBase extends ApiBase {
                                                ApiHelp::fixHelpLinks( $header )
                                        )
                                );
+
+                               if ( $this->mHttpStatus && $this->mHttpStatus !== 200 ) {
+                                       $out->addHTML(
+                                               Html::rawElement( 'div', [ 'class' => 'api-pretty-header api-pretty-status' ],
+                                                       $this->msg(
+                                                               'api-format-prettyprint-status',
+                                                               $this->mHttpStatus,
+                                                               HttpStatus::getMessage( $this->mHttpStatus )
+                                                       )->parse()
+                                               )
+                                       );
+                               }
                        }
 
                        if ( Hooks::run( 'ApiFormatHighlight', [ $context, $result, $mime, $format ] ) ) {
@@ -225,12 +255,15 @@ abstract class ApiFormatBase extends ApiBase {
                                $time = microtime( true ) - $this->getConfig()->get( 'RequestTime' );
                                $json = FormatJson::encode(
                                        [
+                                               'status' => (int)( $this->mHttpStatus ?: 200 ),
+                                               'statustext' => HttpStatus::getMessage( $this->mHttpStatus ?: 200 ),
                                                'html' => $out->getHTML(),
                                                'modules' => array_values( array_unique( array_merge(
                                                        $out->getModules(),
                                                        $out->getModuleScripts(),
                                                        $out->getModuleStyles()
                                                ) ) ),
+                                               'continue' => $this->getResult()->getResultData( 'continue' ),
                                                'time' => round( $time * 1000 ),
                                        ],
                                        false, FormatJson::ALL_OK