MediaWiki  master
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 }
getResult()
Get the result object.
Definition: ApiBase.php:640
getIsWrappedHtml()
Returns true when the special wrapped mode is enabled.
getMain()
Get the main module.
Definition: ApiBase.php:536
Formatter that spits out anything you like with any desired MIME type.
printText( $text)
Append text to the output buffer.
This is the abstract base class for API formatters.
setFailWithHTTPError( $fail)
Output HTTP error code 400 when if an error is encountered.
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:876
createPrinterByName( $format)
Create an instance of an output formatter by its name.
Definition: ApiMain.php:492
getIsHtml()
Returns true when the HTML pretty-printer should be used.
initPrinter( $unused=false)
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:42
__construct(ApiMain $main, ApiFormatBase $errorFallback=null)
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:2212