X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fapi%2FApiFormatBase.php;h=67f54a8068ea628a27c64aa90c33f0d9284a0bd4;hb=e758226c91935a1df2b6fd3ed1f18922d8bfb45b;hp=5011f4875673f2412cab979599f73d0f030d5b3e;hpb=60c869b070ea78594a91d3fc9c9420e4e7ca8960;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/api/ApiFormatBase.php b/includes/api/ApiFormatBase.php index 5011f48756..67f54a8068 100644 --- a/includes/api/ApiFormatBase.php +++ b/includes/api/ApiFormatBase.php @@ -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