MediaWiki master
ApiFormatRaw.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
30
32 private $errorFallback;
34 private $mFailWithHTTPError = false;
35
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
114 public function setFailWithHTTPError( $fail ) {
115 $this->mFailWithHTTPError = $fail;
116 }
117}
118
120class_alias( ApiFormatRaw::class, 'ApiFormatRaw' );
getMain()
Get the main module.
Definition ApiBase.php:589
getResult()
Get the result object.
Definition ApiBase.php:710
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:1808
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:973
This is the abstract base class for API formatters.
getIsWrappedHtml()
Returns true when the special-wrapped mode is enabled.
printText( $text)
Append text to the output buffer.
getIsHtml()
Returns true when the HTML pretty-printer should be used.
Formatter that spits out anything you like with any desired MIME type.
setFailWithHTTPError( $fail)
Output HTTP error code 400 when if an error is encountered.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
initPrinter( $unused=false)
Initialize the printer function and prepare the output headers.
getMimeType()
Overriding class returns the MIME type that should be sent to the client.
__construct(ApiMain $main, ?ApiFormatBase $errorFallback=null)
closePrinter()
Finish printing and output buffered data.
getFilename()
Return a filename for this module's output.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
createPrinterByName( $format)
Create an instance of an output formatter by its name.
Definition ApiMain.php:893