Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiFormatRaw.php
blobb3e1f291c4e43a84e928ec47638e49d4f162ee7b
1 <?php
2 /**
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
20 * @file
23 namespace MediaWiki\Api;
25 /**
26 * Formatter that spits out anything you like with any desired MIME type
27 * @ingroup API
29 class ApiFormatRaw extends ApiFormatBase {
31 /** @var ApiFormatBase|null */
32 private $errorFallback;
33 /** @var bool */
34 private $mFailWithHTTPError = false;
36 /**
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' );
57 return $data['mime'];
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();
66 } else {
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 );
78 } else {
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();
87 } else {
88 parent::closePrinter();
92 public function execute() {
93 $data = $this->getResult()->getResultData();
94 if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
95 $this->errorFallback->execute();
96 return;
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.
112 * @param bool $fail
114 public function setFailWithHTTPError( $fail ) {
115 $this->mFailWithHTTPError = $fail;
119 /** @deprecated class alias since 1.43 */
120 class_alias( ApiFormatRaw::class, 'ApiFormatRaw' );