Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
33 / 33 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
ApiFormatRaw | |
100.00% |
33 / 33 |
|
100.00% |
7 / 7 |
23 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getMimeType | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
getFilename | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
initPrinter | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
closePrinter | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
execute | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
setFailWithHTTPError | |
100.00% |
1 / 1 |
|
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 | |
23 | /** |
24 | * Formatter that spits out anything you like with any desired MIME type |
25 | * @ingroup API |
26 | */ |
27 | class ApiFormatRaw extends ApiFormatBase { |
28 | |
29 | private $errorFallback; |
30 | private $mFailWithHTTPError = false; |
31 | |
32 | /** |
33 | * @param ApiMain $main |
34 | * @param ApiFormatBase|null $errorFallback Object to fall back on for errors |
35 | */ |
36 | public function __construct( ApiMain $main, ApiFormatBase $errorFallback = null ) { |
37 | parent::__construct( $main, 'raw' ); |
38 | $this->errorFallback = $errorFallback ?: |
39 | $main->createPrinterByName( $main->getParameter( 'format' ) ); |
40 | } |
41 | |
42 | public function getMimeType() { |
43 | $data = $this->getResult()->getResultData(); |
44 | |
45 | if ( isset( $data['error'] ) || isset( $data['errors'] ) ) { |
46 | return $this->errorFallback->getMimeType(); |
47 | } |
48 | |
49 | if ( !isset( $data['mime'] ) ) { |
50 | ApiBase::dieDebug( __METHOD__, 'No MIME type set for raw formatter' ); |
51 | } |
52 | |
53 | return $data['mime']; |
54 | } |
55 | |
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(); |
62 | } else { |
63 | return $data['filename']; |
64 | } |
65 | } |
66 | |
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 ); |
73 | } |
74 | } else { |
75 | parent::initPrinter( $unused ); |
76 | } |
77 | } |
78 | |
79 | public function closePrinter() { |
80 | $data = $this->getResult()->getResultData(); |
81 | if ( isset( $data['error'] ) || isset( $data['errors'] ) ) { |
82 | $this->errorFallback->closePrinter(); |
83 | } else { |
84 | parent::closePrinter(); |
85 | } |
86 | } |
87 | |
88 | public function execute() { |
89 | $data = $this->getResult()->getResultData(); |
90 | if ( isset( $data['error'] ) || isset( $data['errors'] ) ) { |
91 | $this->errorFallback->execute(); |
92 | return; |
93 | } |
94 | |
95 | if ( !isset( $data['text'] ) ) { |
96 | ApiBase::dieDebug( __METHOD__, 'No text given for raw formatter' ); |
97 | } |
98 | $this->printText( $data['text'] ); |
99 | } |
100 | |
101 | /** |
102 | * Output HTTP error code 400 when if an error is encountered |
103 | * |
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. |
107 | * |
108 | * @param bool $fail |
109 | */ |
110 | public function setFailWithHTTPError( $fail ) { |
111 | $this->mFailWithHTTPError = $fail; |
112 | } |
113 | } |