MediaWiki master
ApiFormatRaw.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
16
18 private $errorFallback;
20 private $mFailWithHTTPError = false;
21
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
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
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
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
103 public function setFailWithHTTPError( $fail ) {
104 $this->mFailWithHTTPError = $fail;
105 }
106}
107
109class_alias( ApiFormatRaw::class, 'ApiFormatRaw' );
getMain()
Get the main module.
Definition ApiBase.php:561
getResult()
Get the result object.
Definition ApiBase.php:682
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:1748
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:944
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.When getIsHtml() returns tru...
__construct(ApiMain $main, ?ApiFormatBase $errorFallback=null)
closePrinter()
Finish printing and output buffered data.
getFilename()
Return a filename for this module's output.If $this->getIsWrappedHtml() || $this->getIsHtml(),...
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
createPrinterByName( $format)
Create an instance of an output formatter by its name.
Definition ApiMain.php:916