3 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@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
24 * Formatter that spits out anything you like with any desired MIME type
27 class ApiFormatRaw
extends ApiFormatBase
{
29 private $errorFallback;
30 private $mFailWithHTTPError = false;
33 * @param ApiMain $main
34 * @param ApiFormatBase|null $errorFallback Object to fall back on for errors
36 public function __construct( ApiMain
$main, ApiFormatBase
$errorFallback = null ) {
37 parent
::__construct( $main, 'raw' );
38 if ( $errorFallback === null ) {
39 $this->errorFallback
= $main->createPrinterByName( $main->getParameter( 'format' ) );
41 $this->errorFallback
= $errorFallback;
45 public function getMimeType() {
46 $data = $this->getResult()->getResultData();
48 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
49 return $this->errorFallback
->getMimeType();
52 if ( !isset( $data['mime'] ) ) {
53 ApiBase
::dieDebug( __METHOD__
, 'No MIME type set for raw formatter' );
59 public function getFilename() {
60 $data = $this->getResult()->getResultData();
61 if ( isset( $data['error'] ) ) {
62 return $this->errorFallback
->getFilename();
63 } elseif ( !isset( $data['filename'] ) ||
$this->getIsWrappedHtml() ||
$this->getIsHtml() ) {
64 return parent
::getFilename();
66 return $data['filename'];
70 public function initPrinter( $unused = false ) {
71 $data = $this->getResult()->getResultData();
72 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
73 $this->errorFallback
->initPrinter( $unused );
74 if ( $this->mFailWithHTTPError
) {
75 $this->getMain()->getRequest()->response()->statusHeader( 400 );
78 parent
::initPrinter( $unused );
82 public function closePrinter() {
83 $data = $this->getResult()->getResultData();
84 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
85 $this->errorFallback
->closePrinter();
87 parent
::closePrinter();
91 public function execute() {
92 $data = $this->getResult()->getResultData();
93 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
94 $this->errorFallback
->execute();
98 if ( !isset( $data['text'] ) ) {
99 ApiBase
::dieDebug( __METHOD__
, 'No text given for raw formatter' );
101 $this->printText( $data['text'] );
105 * Output HTTP error code 400 when if an error is encountered
107 * The purpose is for output formats where the user-agent will
108 * not be able to interpret the validity of the content in any
109 * other way. For example subtitle files read by browser video players.
113 public function setFailWithHTTPError( $fail ) {
114 $this->mFailWithHTTPError
= $fail;