private $mIsHtml, $mFormat, $mUnescapeAmps, $mHelp;
private $mBuffer, $mDisabled = false;
private $mIsWrappedHtml = false;
+ private $mHttpStatus = false;
protected $mForceDefaultParams = false;
/**
}
}
+ /**
+ * 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
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 ] ) ) {
$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(),
$response = $this->getRequest()->response();
$headerStr = 'MediaWiki-API-Error: ' . $errCode;
- if ( $e->getCode() === 0 ) {
- $response->header( $headerStr );
- } else {
- $response->header( $headerStr, true, $e->getCode() );
- }
+ $response->header( $headerStr );
// Reset and print just the error message
ob_clean();
$this->createErrorPrinter();
try {
- $this->printResult( true );
+ $this->printResult( $e->getCode() );
} catch ( UsageException $ex ) {
// The error printer itself is failing. Try suppressing its request
// parameters and redo.
$this->mPrinter = null;
$this->createErrorPrinter();
$this->mPrinter->forceDefaultParams();
- $this->printResult( true );
+ if ( $e->getCode() ) {
+ $response->statusHeader( 200 ); // Reset in case the fallback doesn't want a non-200
+ }
+ $this->printResult( $e->getCode() );
}
}
MWDebug::appendDebugInfoToApiResult( $this->getContext(), $this->getResult() );
// Print result data
- $this->printResult( false );
+ $this->printResult();
}
}
/**
* Print results using the current printer
*
- * @param bool $isError
+ * @param int $httpCode HTTP status code, or 0 to not change
*/
- protected function printResult( $isError ) {
+ protected function printResult( $httpCode = 0 ) {
if ( $this->getConfig()->get( 'DebugAPI' ) !== false ) {
$this->setWarning( 'SECURITY WARNING: $wgDebugAPI is enabled' );
}
$printer = $this->mPrinter;
$printer->initPrinter( false );
+ if ( $httpCode ) {
+ $printer->setHttpStatus( $httpCode );
+ }
$printer->execute();
$printer->closePrinter();
}
"api-format-title": "MediaWiki API result",
"api-format-prettyprint-header": "This is the HTML representation of the $1 format. HTML is good for debugging, but is unsuitable for application use.\n\nSpecify the <var>format</var> parameter to change the output format. To see the non-HTML representation of the $1 format, set <kbd>format=$2</kbd>.\n\nSee the [[mw:API|complete documentation]], or the [[Special:ApiHelp/main|API help]] for more information.",
"api-format-prettyprint-header-only-html": "This is an HTML representation intended for debugging, and is unsuitable for application use.\n\nSee the [[mw:API|complete documentation]], or the [[Special:ApiHelp/main|API help]] for more information.",
+ "api-format-prettyprint-status": "This response would be returned with HTTP status $1 $2.",
"api-pageset-param-titles": "A list of titles to work on.",
"api-pageset-param-pageids": "A list of page IDs to work on.",
"api-format-title": "{{technical}}\nPage title when API output is pretty-printed in HTML.",
"api-format-prettyprint-header": "{{technical}} Displayed as a header when API output is pretty-printed in HTML.\n\nParameters:\n* $1 - Format name\n* $2 - Non-pretty-printing module name",
"api-format-prettyprint-header-only-html": "{{technical}} Displayed as a header when API output is pretty-printed in HTML, but there is no non-html module.\n\nParameters:\n* $1 - Format name",
+ "api-format-prettyprint-status": "{{technical}} Displayed as a header when API pretty-printed output is used for a response that uses an unusual HTTP status code.\n\nParameters:\n* $1 - HTTP status code (integer)\n* $2 - Standard English text for the status code.",
"api-pageset-param-titles": "{{doc-apihelp-param|pageset|titles|description=the \"titles\" parameter in pageset-using modules}}",
"api-pageset-param-pageids": "{{doc-apihelp-param|pageset|pageids|description=the \"pageids\" parameter in pageset-using modules}}",
"api-pageset-param-revids": "{{doc-apihelp-param|pageset|revids|description=the \"revids\" parameter in pageset-using modules}}",
'apisandbox-continue',
'apisandbox-continue-clear',
'apisandbox-continue-help',
+ 'api-format-prettyprint-status',
'blanknamespace',
],
],
if ( data.modules.length ) {
mw.loader.load( data.modules );
}
+ if ( data.status && data.status !== 200 ) {
+ $( '<div>' )
+ .addClass( 'api-pretty-header api-pretty-status' )
+ .append(
+ mw.message( 'api-format-prettyprint-status', data.status, data.statustext ).parse()
+ )
+ .appendTo( $result );
+ }
$result.append( Util.parseHTML( data.html ) );
loadTime = data.time;
} else if ( ( m = data.match( /<pre[ >][\s\S]*<\/pre>/ ) ) ) {