MediaWiki  1.33.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  if ( $errorFallback === null ) {
39  $this->errorFallback = $main->createPrinterByName( $main->getParameter( 'format' ) );
40  } else {
41  $this->errorFallback = $errorFallback;
42  }
43  }
44 
45  public function getMimeType() {
46  $data = $this->getResult()->getResultData();
47 
48  if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
49  return $this->errorFallback->getMimeType();
50  }
51 
52  if ( !isset( $data['mime'] ) ) {
53  ApiBase::dieDebug( __METHOD__, 'No MIME type set for raw formatter' );
54  }
55 
56  return $data['mime'];
57  }
58 
59  public function getFilename() {
60  $data = $this->getResult()->getResultData();
61  if ( isset( $data['error'] ) ) {
62  return $this->errorFallback->getFilename();
63  } elseif ( !isset( $data['filename'] ) || $this->getIsWrappedHtml() || $this->getIsHtml() ) {
64  return parent::getFilename();
65  } else {
66  return $data['filename'];
67  }
68  }
69 
70  public function initPrinter( $unused = false ) {
71  $data = $this->getResult()->getResultData();
72  if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
73  $this->errorFallback->initPrinter( $unused );
74  if ( $this->mFailWithHTTPError ) {
75  $this->getMain()->getRequest()->response()->statusHeader( 400 );
76  }
77  } else {
78  parent::initPrinter( $unused );
79  }
80  }
81 
82  public function closePrinter() {
83  $data = $this->getResult()->getResultData();
84  if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
85  $this->errorFallback->closePrinter();
86  } else {
87  parent::closePrinter();
88  }
89  }
90 
91  public function execute() {
92  $data = $this->getResult()->getResultData();
93  if ( isset( $data['error'] ) || isset( $data['errors'] ) ) {
94  $this->errorFallback->execute();
95  return;
96  }
97 
98  if ( !isset( $data['text'] ) ) {
99  ApiBase::dieDebug( __METHOD__, 'No text given for raw formatter' );
100  }
101  $this->printText( $data['text'] );
102  }
103 
113  public function setFailWithHTTPError( $fail ) {
114  $this->mFailWithHTTPError = $fail;
115  }
116 }
ApiFormatRaw\getMimeType
getMimeType()
Overriding class returns the MIME type that should be sent to the client.
Definition: ApiFormatRaw.php:45
ApiFormatRaw\getFilename
getFilename()
Return a filename for this module's output.
Definition: ApiFormatRaw.php:59
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:28
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:632
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:91
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
ApiFormatRaw\__construct
__construct(ApiMain $main, ApiFormatBase $errorFallback=null)
Definition: ApiFormatRaw.php:36
ApiFormatRaw\closePrinter
closePrinter()
Finish printing and output buffered data.
Definition: ApiFormatRaw.php:82
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:484
ApiFormatBase\getIsWrappedHtml
getIsWrappedHtml()
Returns true when the special wrapped mode is enabled.
Definition: ApiFormatBase.php:106
ApiFormatRaw\setFailWithHTTPError
setFailWithHTTPError( $fail)
Output HTTP error code 400 when if an error is encountered.
Definition: ApiFormatRaw.php:113
ApiFormatBase\printText
printText( $text)
Append text to the output buffer.
Definition: ApiFormatBase.php:345
ApiBase\getParameter
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:858
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:528
ApiFormatBase\getIsHtml
getIsHtml()
Returns true when the HTML pretty-printer should be used.
Definition: ApiFormatBase.php:97
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:2188
ApiFormatRaw\initPrinter
initPrinter( $unused=false)
Initialize the printer function and prepare the output headers.
Definition: ApiFormatRaw.php:70