X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Fapi%2FApiFormatRaw.php;h=9dbd4a553faaa094c7bf9d8b5f7bcfa6a277740c;hp=81d2f4f5627d4d56f8c83830ebcc73663be4cf9f;hb=ee734d0d3c7533bd9a690dbd71f5151da55c32ad;hpb=30e009794bacc2e3138c372e6ddf876dca2d4a9c diff --git a/includes/api/ApiFormatRaw.php b/includes/api/ApiFormatRaw.php index 81d2f4f562..9dbd4a553f 100644 --- a/includes/api/ApiFormatRaw.php +++ b/includes/api/ApiFormatRaw.php @@ -31,18 +31,24 @@ class ApiFormatRaw extends ApiFormatBase { private $errorFallback; + private $mFailWithHTTPError = false; + /** * @param ApiMain $main - * @param ApiFormatBase $errorFallback Object to fall back on for errors + * @param ApiFormatBase |null $errorFallback Object to fall back on for errors */ - public function __construct( ApiMain $main, ApiFormatBase $errorFallback ) { + public function __construct( ApiMain $main, ApiFormatBase $errorFallback = null ) { parent::__construct( $main, 'raw' ); - $this->errorFallback = $errorFallback; + if ( $errorFallback === null ) { + $this->errorFallback = $main->createPrinterByName( $main->getParameter( 'format' ) ); + } else { + $this->errorFallback = $errorFallback; + } } public function getMimeType() { - $data = $this->getResultData(); + $data = $this->getResult()->getResultData(); if ( isset( $data['error'] ) ) { return $this->errorFallback->getMimeType(); @@ -56,16 +62,19 @@ class ApiFormatRaw extends ApiFormatBase { } public function initPrinter( $unused = false ) { - $data = $this->getResultData(); + $data = $this->getResult()->getResultData(); if ( isset( $data['error'] ) ) { $this->errorFallback->initPrinter( $unused ); + if ( $this->mFailWithHTTPError ) { + $this->getMain()->getRequest()->response()->statusHeader( 400 ); + } } else { parent::initPrinter( $unused ); } } public function closePrinter() { - $data = $this->getResultData(); + $data = $this->getResult()->getResultData(); if ( isset( $data['error'] ) ) { $this->errorFallback->closePrinter(); } else { @@ -74,7 +83,7 @@ class ApiFormatRaw extends ApiFormatBase { } public function execute() { - $data = $this->getResultData(); + $data = $this->getResult()->getResultData(); if ( isset( $data['error'] ) ) { $this->errorFallback->execute(); return; @@ -85,4 +94,17 @@ class ApiFormatRaw extends ApiFormatBase { } $this->printText( $data['text'] ); } + + /** + * Output HTTP error code 400 when if an error is encountered + * + * The purpose is for output formats where the user-agent will + * not be able to interpret the validity of the content in any + * other way. For example subtitle files read by browser video players. + * + * @param bool $fail + */ + public function setFailWithHTTPError( $fail ) { + $this->mFailWithHTTPError = $fail; + } }