MediaWiki master
ApiFormatJson.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
27
33
35 private $isRaw;
36
37 public function __construct( ApiMain $main, string $format ) {
38 parent::__construct( $main, $format );
39 $this->isRaw = ( $format === 'rawfm' );
40
41 if ( $this->getMain()->getCheck( 'callback' ) ) {
42 # T94015: jQuery appends a useless '_' parameter in jsonp mode.
43 # Mark the parameter as used in that case to avoid a warning that's
44 # outside the control of the end user.
45 # (and do it here because ApiMain::reportUnusedParams() gets called
46 # before our ::execute())
47 $this->getMain()->markParamsUsed( '_' );
48 }
49 }
50
51 public function getMimeType() {
53 // callback:
54 if ( isset( $params['callback'] ) ) {
55 return 'text/javascript';
56 }
57
58 return 'application/json';
59 }
60
61 public function execute() {
63
64 $opt = 0;
65 if ( $this->isRaw ) {
66 $opt |= FormatJson::ALL_OK;
67 $transform = [];
68 } else {
69 switch ( $params['formatversion'] ) {
70 case 1:
71 $opt |= $params['utf8'] ? FormatJson::ALL_OK : FormatJson::XMLMETA_OK;
72 $transform = [
73 'BC' => [],
74 'Types' => [ 'AssocAsObject' => true ],
75 'Strip' => 'all',
76 ];
77 break;
78
79 case 2:
80 case 'latest':
81 $opt |= $params['ascii'] ? FormatJson::XMLMETA_OK : FormatJson::ALL_OK;
82 $transform = [
83 'Types' => [ 'AssocAsObject' => true ],
84 'Strip' => 'all',
85 ];
86 break;
87
88 default:
89 // Should have been caught during parameter validation
90 // @codeCoverageIgnoreStart
91 self::dieDebug( __METHOD__, 'Unknown value for \'formatversion\'' );
92 // @codeCoverageIgnoreEnd
93 }
94 }
95 $data = $this->getResult()->getResultData( null, $transform );
96 $json = FormatJson::encode( $data, $this->getIsHtml(), $opt );
97 if ( $json === false ) {
98 // This should never happen, but it's a bug which could crop up
99 // if you use ApiResult::NO_VALIDATE for instance.
100 // @codeCoverageIgnoreStart
101 self::dieDebug( __METHOD__, 'Unable to encode API result as JSON' );
102 // @codeCoverageIgnoreEnd
103 }
104
105 if ( isset( $params['callback'] ) ) {
106 $callback = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $params['callback'] );
107 # Prepend a comment to try to avoid attacks against content
108 # sniffers, such as T70187.
109 $this->printText( "/**/$callback($json)" );
110 } else {
111 $this->printText( $json );
112 }
113 }
114
115 public function getAllowedParams() {
116 if ( $this->isRaw ) {
117 return parent::getAllowedParams();
118 }
119
120 return parent::getAllowedParams() + [
121 'callback' => [
122 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-callback',
123 ],
124 'utf8' => [
125 ParamValidator::PARAM_DEFAULT => false,
126 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-utf8',
127 ],
128 'ascii' => [
129 ParamValidator::PARAM_DEFAULT => false,
130 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-ascii',
131 ],
132 'formatversion' => [
133 ParamValidator::PARAM_TYPE => [ '1', '2', 'latest' ],
134 ParamValidator::PARAM_DEFAULT => '1',
135 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-formatversion',
137 '1' => 'apihelp-json-paramvalue-formatversion-1',
138 '2' => 'apihelp-json-paramvalue-formatversion-2',
139 'latest' => 'apihelp-json-paramvalue-formatversion-latest',
140 ],
141 ],
142 ];
143 }
144}
145
147class_alias( ApiFormatJson::class, 'ApiFormatJson' );
array $params
The job parameters.
getMain()
Get the main module.
Definition ApiBase.php:589
getResult()
Get the result object.
Definition ApiBase.php:710
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:224
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:1774
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:184
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
This is the abstract base class for API formatters.
printText( $text)
Append text to the output buffer.
getIsHtml()
Returns true when the HTML pretty-printer should be used.
API JSON output formatter.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getMimeType()
Overriding class returns the MIME type that should be sent to the client.
__construct(ApiMain $main, string $format)
If $format ends with 'fm', pretty-print the output in HTML.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
JSON formatter wrapper class.
Service for formatting and validating API parameters.