| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class ApiFormatRaw extends ApiFormatBase { |
| 30 |
|
| 31 |
|
| 32 |
private $errorFallback; |
| 33 |
|
| 34 |
private $mFailWithHTTPError = false; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function setFailWithHTTPError( $fail ) { |
| 115 |
$this->mFailWithHTTPError = $fail; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
class_alias( ApiFormatRaw::class, 'ApiFormatRaw' ); |