3 * Copyright © 2009 Roan Kattouw <roan.kattouw@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Api
;
26 * Formatter that spits out anything you like with any desired MIME type
29 class ApiFormatRaw
extends ApiFormatBase
{
31 /** @var ApiFormatBase|null */
32 private $errorFallback;
34 private $mFailWithHTTPError = false;
37 * @param ApiMain $main
38 * @param ApiFormatBase|null $errorFallback Object to fall back on for errors
40 public function __construct( ApiMain
$main, ?ApiFormatBase
$errorFallback = null ) {
41 parent
::__construct( $main, 'raw' );
42 $this->errorFallback
= $errorFallback ?
:
43 $main->createPrinterByName( $main->getParameter( 'format' ) );
46 public function getMimeType() {
47 $data = $this->getResult()->getResultData();
49 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
50 return $this->errorFallback
->getMimeType();
53 if ( !isset( $data['mime'] ) ) {
54 ApiBase
::dieDebug( __METHOD__
, 'No MIME type set for raw formatter' );
60 public function getFilename() {
61 $data = $this->getResult()->getResultData();
62 if ( isset( $data['error'] ) ) {
63 return $this->errorFallback
->getFilename();
64 } elseif ( !isset( $data['filename'] ) ||
$this->getIsWrappedHtml() ||
$this->getIsHtml() ) {
65 return parent
::getFilename();
67 return $data['filename'];
71 public function initPrinter( $unused = false ) {
72 $data = $this->getResult()->getResultData();
73 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
74 $this->errorFallback
->initPrinter( $unused );
75 if ( $this->mFailWithHTTPError
) {
76 $this->getMain()->getRequest()->response()->statusHeader( 400 );
79 parent
::initPrinter( $unused );
83 public function closePrinter() {
84 $data = $this->getResult()->getResultData();
85 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
86 $this->errorFallback
->closePrinter();
88 parent
::closePrinter();
92 public function execute() {
93 $data = $this->getResult()->getResultData();
94 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
95 $this->errorFallback
->execute();
99 if ( !isset( $data['text'] ) ) {
100 ApiBase
::dieDebug( __METHOD__
, 'No text given for raw formatter' );
102 $this->printText( $data['text'] );
106 * Output HTTP error code 400 when if an error is encountered
108 * The purpose is for output formats where the user-agent will
109 * not be able to interpret the validity of the content in any
110 * other way. For example subtitle files read by browser video players.
114 public function setFailWithHTTPError( $fail ) {
115 $this->mFailWithHTTPError
= $fail;
119 /** @deprecated class alias since 1.43 */
120 class_alias( ApiFormatRaw
::class, 'ApiFormatRaw' );