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:484
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:41
__construct(ApiMain $main, ApiFormatBase $errorFallback=null)
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
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
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:2212