X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fapi%2FApiFormatRaw.php;h=b1a9c984f06c7ada4458ee03db89648e2c33aed2;hb=6b3e5511fb848890f174690885e748b90389c0b8;hp=7bb2453d2caf9ea0baa46bb27841b971423930f0;hpb=c3ff90433d426820af03dfa6c512f2ed90f0671e;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/api/ApiFormatRaw.php b/includes/api/ApiFormatRaw.php index 7bb2453d2c..9ec4a2c3fb 100644 --- a/includes/api/ApiFormatRaw.php +++ b/includes/api/ApiFormatRaw.php @@ -1,9 +1,5 @@ .@gmail.com" * * This program is free software; you can redistribute it and/or modify @@ -31,20 +27,25 @@ 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->getResult()->getResultData(); - if ( isset( $data['error'] ) ) { + if ( isset( $data['error'] ) || isset( $data['errors'] ) ) { return $this->errorFallback->getMimeType(); } @@ -55,10 +56,24 @@ class ApiFormatRaw extends ApiFormatBase { return $data['mime']; } - public function initPrinter( $unused = false ) { + public function getFilename() { $data = $this->getResult()->getResultData(); if ( isset( $data['error'] ) ) { + return $this->errorFallback->getFilename(); + } elseif ( !isset( $data['filename'] ) || $this->getIsWrappedHtml() || $this->getIsHtml() ) { + return parent::getFilename(); + } else { + return $data['filename']; + } + } + + public function initPrinter( $unused = false ) { + $data = $this->getResult()->getResultData(); + if ( isset( $data['error'] ) || isset( $data['errors'] ) ) { $this->errorFallback->initPrinter( $unused ); + if ( $this->mFailWithHTTPError ) { + $this->getMain()->getRequest()->response()->statusHeader( 400 ); + } } else { parent::initPrinter( $unused ); } @@ -66,7 +81,7 @@ class ApiFormatRaw extends ApiFormatBase { public function closePrinter() { $data = $this->getResult()->getResultData(); - if ( isset( $data['error'] ) ) { + if ( isset( $data['error'] ) || isset( $data['errors'] ) ) { $this->errorFallback->closePrinter(); } else { parent::closePrinter(); @@ -75,7 +90,7 @@ class ApiFormatRaw extends ApiFormatBase { public function execute() { $data = $this->getResult()->getResultData(); - if ( isset( $data['error'] ) ) { + if ( isset( $data['error'] ) || isset( $data['errors'] ) ) { $this->errorFallback->execute(); return; } @@ -85,4 +100,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; + } }