MediaWiki  1.34.0
ApiFormatRaw.php
Go to the documentation of this file.
1 <?php
27 class ApiFormatRaw extends ApiFormatBase {
28 
29  private $errorFallback;
30  private $mFailWithHTTPError = false;
31 
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 
110  public function setFailWithHTTPError( $fail ) {
111  $this->mFailWithHTTPError = $fail;
112  }
113 }
ApiFormatRaw\getMimeType
getMimeType()
Overriding class returns the MIME type that should be sent to the client.
Definition: ApiFormatRaw.php:42
ApiFormatRaw\getFilename
getFilename()
Return a filename for this module's output.
Definition: ApiFormatRaw.php:56
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:41
ApiFormatRaw\$mFailWithHTTPError
$mFailWithHTTPError
Definition: ApiFormatRaw.php:30
ApiFormatBase
This is the abstract base class for API formatters.
Definition: ApiFormatBase.php:30
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ApiFormatRaw\$errorFallback
$errorFallback
Definition: ApiFormatRaw.php:29
ApiFormatRaw\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiFormatRaw.php:88
ApiFormatRaw\__construct
__construct(ApiMain $main, ApiFormatBase $errorFallback=null)
Definition: ApiFormatRaw.php:36
ApiFormatRaw\closePrinter
closePrinter()
Finish printing and output buffered data.
Definition: ApiFormatRaw.php:79
ApiFormatRaw
Formatter that spits out anything you like with any desired MIME type.
Definition: ApiFormatRaw.php:27
ApiMain\createPrinterByName
createPrinterByName( $format)
Create an instance of an output formatter by its name.
Definition: ApiMain.php:490
ApiFormatBase\getIsWrappedHtml
getIsWrappedHtml()
Returns true when the special wrapped mode is enabled.
Definition: ApiFormatBase.php:108
ApiFormatRaw\setFailWithHTTPError
setFailWithHTTPError( $fail)
Output HTTP error code 400 when if an error is encountered.
Definition: ApiFormatRaw.php:110
ApiFormatBase\printText
printText( $text)
Append text to the output buffer.
Definition: ApiFormatBase.php:348
ApiBase\getParameter
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:876
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:536
ApiFormatBase\getIsHtml
getIsHtml()
Returns true when the HTML pretty-printer should be used.
Definition: ApiFormatBase.php:99
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:2220
ApiFormatRaw\initPrinter
initPrinter( $unused=false)
Initialize the printer function and prepare the output headers.
Definition: ApiFormatRaw.php:67