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 $this->errorFallback
= $errorFallback ?
:
39 $main->createPrinterByName( $main->getParameter( 'format' ) );
42 public function getMimeType() {
43 $data = $this->getResult()->getResultData();
45 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
46 return $this->errorFallback
->getMimeType();
49 if ( !isset( $data['mime'] ) ) {
50 ApiBase
::dieDebug( __METHOD__
, 'No MIME type set for raw formatter' );
56 public function getFilename() {
57 $data = $this->getResult()->getResultData();
58 if ( isset( $data['error'] ) ) {
59 return $this->errorFallback
->getFilename();
60 } elseif ( !isset( $data['filename'] ) ||
$this->getIsWrappedHtml() ||
$this->getIsHtml() ) {
61 return parent
::getFilename();
63 return $data['filename'];
67 public function initPrinter( $unused = false ) {
68 $data = $this->getResult()->getResultData();
69 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
70 $this->errorFallback
->initPrinter( $unused );
71 if ( $this->mFailWithHTTPError
) {
72 $this->getMain()->getRequest()->response()->statusHeader( 400 );
75 parent
::initPrinter( $unused );
79 public function closePrinter() {
80 $data = $this->getResult()->getResultData();
81 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
82 $this->errorFallback
->closePrinter();
84 parent
::closePrinter();
88 public function execute() {
89 $data = $this->getResult()->getResultData();
90 if ( isset( $data['error'] ) ||
isset( $data['errors'] ) ) {
91 $this->errorFallback
->execute();
95 if ( !isset( $data['text'] ) ) {
96 ApiBase
::dieDebug( __METHOD__
, 'No text given for raw formatter' );
98 $this->printText( $data['text'] );
102 * Output HTTP error code 400 when if an error is encountered
104 * The purpose is for output formats where the user-agent will
105 * not be able to interpret the validity of the content in any
106 * other way. For example subtitle files read by browser video players.
110 public function setFailWithHTTPError( $fail ) {
111 $this->mFailWithHTTPError
= $fail;