Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.06% |
33 / 34 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| ApiFormatRaw | |
100.00% |
33 / 33 |
|
100.00% |
7 / 7 |
22 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 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 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Api; |
| 10 | |
| 11 | /** |
| 12 | * Formatter that spits out anything you like with any desired MIME type |
| 13 | * @ingroup API |
| 14 | */ |
| 15 | class ApiFormatRaw extends ApiFormatBase { |
| 16 | |
| 17 | /** @var ApiFormatBase|null */ |
| 18 | private $errorFallback; |
| 19 | /** @var bool */ |
| 20 | private $mFailWithHTTPError = false; |
| 21 | |
| 22 | /** |
| 23 | * @param ApiMain $main |
| 24 | * @param ApiFormatBase|null $errorFallback Object to fall back on for errors |
| 25 | */ |
| 26 | public function __construct( ApiMain $main, ?ApiFormatBase $errorFallback = null ) { |
| 27 | parent::__construct( $main, 'raw' ); |
| 28 | $this->errorFallback = $errorFallback ?? |
| 29 | $main->createPrinterByName( $main->getParameter( 'format' ) ); |
| 30 | } |
| 31 | |
| 32 | /** @inheritDoc */ |
| 33 | public function getMimeType() { |
| 34 | $data = $this->getResult()->getResultData(); |
| 35 | |
| 36 | if ( isset( $data['error'] ) || isset( $data['errors'] ) ) { |
| 37 | return $this->errorFallback->getMimeType(); |
| 38 | } |
| 39 | |
| 40 | if ( !isset( $data['mime'] ) ) { |
| 41 | ApiBase::dieDebug( __METHOD__, 'No MIME type set for raw formatter' ); |
| 42 | } |
| 43 | |
| 44 | return $data['mime']; |
| 45 | } |
| 46 | |
| 47 | /** @inheritDoc */ |
| 48 | public function getFilename() { |
| 49 | $data = $this->getResult()->getResultData(); |
| 50 | if ( isset( $data['error'] ) ) { |
| 51 | return $this->errorFallback->getFilename(); |
| 52 | } elseif ( !isset( $data['filename'] ) || $this->getIsWrappedHtml() || $this->getIsHtml() ) { |
| 53 | return parent::getFilename(); |
| 54 | } else { |
| 55 | return $data['filename']; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** @inheritDoc */ |
| 60 | public function initPrinter( $unused = false ) { |
| 61 | $data = $this->getResult()->getResultData(); |
| 62 | if ( isset( $data['error'] ) || isset( $data['errors'] ) ) { |
| 63 | $this->errorFallback->initPrinter( $unused ); |
| 64 | if ( $this->mFailWithHTTPError ) { |
| 65 | $this->getMain()->getRequest()->response()->statusHeader( 400 ); |
| 66 | } |
| 67 | } else { |
| 68 | parent::initPrinter( $unused ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public function closePrinter() { |
| 73 | $data = $this->getResult()->getResultData(); |
| 74 | if ( isset( $data['error'] ) || isset( $data['errors'] ) ) { |
| 75 | $this->errorFallback->closePrinter(); |
| 76 | } else { |
| 77 | parent::closePrinter(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public function execute() { |
| 82 | $data = $this->getResult()->getResultData(); |
| 83 | if ( isset( $data['error'] ) || isset( $data['errors'] ) ) { |
| 84 | $this->errorFallback->execute(); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | if ( !isset( $data['text'] ) ) { |
| 89 | ApiBase::dieDebug( __METHOD__, 'No text given for raw formatter' ); |
| 90 | } |
| 91 | $this->printText( $data['text'] ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Output HTTP error code 400 when if an error is encountered |
| 96 | * |
| 97 | * The purpose is for output formats where the user-agent will |
| 98 | * not be able to interpret the validity of the content in any |
| 99 | * other way. For example subtitle files read by browser video players. |
| 100 | * |
| 101 | * @param bool $fail |
| 102 | */ |
| 103 | public function setFailWithHTTPError( $fail ) { |
| 104 | $this->mFailWithHTTPError = $fail; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** @deprecated class alias since 1.43 */ |
| 109 | class_alias( ApiFormatRaw::class, 'ApiFormatRaw' ); |