Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.06% covered (success)
97.06%
33 / 34
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
ApiFormatRaw
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
7 / 7
23
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getMimeType
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 getFilename
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 initPrinter
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 closePrinter
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 execute
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 setFailWithHTTPError
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Copyright © 2009 Roan Kattouw <roan.kattouw@gmail.com>
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23namespace MediaWiki\Api;
24
25/**
26 * Formatter that spits out anything you like with any desired MIME type
27 * @ingroup API
28 */
29class ApiFormatRaw extends ApiFormatBase {
30
31    /** @var ApiFormatBase|null */
32    private $errorFallback;
33    /** @var bool */
34    private $mFailWithHTTPError = false;
35
36    /**
37     * @param ApiMain $main
38     * @param ApiFormatBase|null $errorFallback Object to fall back on for errors
39     */
40    public function __construct( ApiMain $main, ?ApiFormatBase $errorFallback = null ) {
41        parent::__construct( $main, 'raw' );
42        $this->errorFallback = $errorFallback ?:
43            $main->createPrinterByName( $main->getParameter( 'format' ) );
44    }
45
46    public function getMimeType() {
47        $data = $this->getResult()->getResultData();
48
49        if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
50            return $this->errorFallback->getMimeType();
51        }
52
53        if ( !isset( $data['mime'] ) ) {
54            ApiBase::dieDebug( __METHOD__, 'No MIME type set for raw formatter' );
55        }
56
57        return $data['mime'];
58    }
59
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'];
68        }
69    }
70
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 );
77            }
78        } else {
79            parent::initPrinter( $unused );
80        }
81    }
82
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();
89        }
90    }
91
92    public function execute() {
93        $data = $this->getResult()->getResultData();
94        if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
95            $this->errorFallback->execute();
96            return;
97        }
98
99        if ( !isset( $data['text'] ) ) {
100            ApiBase::dieDebug( __METHOD__, 'No text given for raw formatter' );
101        }
102        $this->printText( $data['text'] );
103    }
104
105    /**
106     * Output HTTP error code 400 when if an error is encountered
107     *
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.
111     *
112     * @param bool $fail
113     */
114    public function setFailWithHTTPError( $fail ) {
115        $this->mFailWithHTTPError = $fail;
116    }
117}
118
119/** @deprecated class alias since 1.43 */
120class_alias( ApiFormatRaw::class, 'ApiFormatRaw' );