MediaWiki master
ApiFormatRaw.php
Go to the documentation of this file.
1<?php
28
30 private $errorFallback;
32 private $mFailWithHTTPError = false;
33
38 public function __construct( ApiMain $main, ApiFormatBase $errorFallback = null ) {
39 parent::__construct( $main, 'raw' );
40 $this->errorFallback = $errorFallback ?:
41 $main->createPrinterByName( $main->getParameter( 'format' ) );
42 }
43
44 public function getMimeType() {
45 $data = $this->getResult()->getResultData();
46
47 if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
48 return $this->errorFallback->getMimeType();
49 }
50
51 if ( !isset( $data['mime'] ) ) {
52 ApiBase::dieDebug( __METHOD__, 'No MIME type set for raw formatter' );
53 }
54
55 return $data['mime'];
56 }
57
58 public function getFilename() {
59 $data = $this->getResult()->getResultData();
60 if ( isset( $data['error'] ) ) {
61 return $this->errorFallback->getFilename();
62 } elseif ( !isset( $data['filename'] ) || $this->getIsWrappedHtml() || $this->getIsHtml() ) {
63 return parent::getFilename();
64 } else {
65 return $data['filename'];
66 }
67 }
68
69 public function initPrinter( $unused = false ) {
70 $data = $this->getResult()->getResultData();
71 if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
72 $this->errorFallback->initPrinter( $unused );
73 if ( $this->mFailWithHTTPError ) {
74 $this->getMain()->getRequest()->response()->statusHeader( 400 );
75 }
76 } else {
77 parent::initPrinter( $unused );
78 }
79 }
80
81 public function closePrinter() {
82 $data = $this->getResult()->getResultData();
83 if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
84 $this->errorFallback->closePrinter();
85 } else {
86 parent::closePrinter();
87 }
88 }
89
90 public function execute() {
91 $data = $this->getResult()->getResultData();
92 if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
93 $this->errorFallback->execute();
94 return;
95 }
96
97 if ( !isset( $data['text'] ) ) {
98 ApiBase::dieDebug( __METHOD__, 'No text given for raw formatter' );
99 }
100 $this->printText( $data['text'] );
101 }
102
112 public function setFailWithHTTPError( $fail ) {
113 $this->mFailWithHTTPError = $fail;
114 }
115}
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:964
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:1811
getMain()
Get the main module.
Definition ApiBase.php:580
getResult()
Get the result object.
Definition ApiBase.php:701
This is the abstract base class for API formatters.
printText( $text)
Append text to the output buffer.
getIsWrappedHtml()
Returns true when the special-wrapped mode is enabled.
getIsHtml()
Returns true when the HTML pretty-printer should be used.
Formatter that spits out anything you like with any desired MIME type.
initPrinter( $unused=false)
Initialize the printer function and prepare the output headers.
closePrinter()
Finish printing and output buffered data.
setFailWithHTTPError( $fail)
Output HTTP error code 400 when if an error is encountered.
__construct(ApiMain $main, ApiFormatBase $errorFallback=null)
getMimeType()
Overriding class returns the MIME type that should be sent to the client.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
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:68
createPrinterByName( $format)
Create an instance of an output formatter by its name.
Definition ApiMain.php:884