MediaWiki master
ApiFormatJson.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
13
19
21 private $isRaw;
22
23 public function __construct( ApiMain $main, string $format ) {
24 parent::__construct( $main, $format );
25 $this->isRaw = ( $format === 'rawfm' );
26
27 if ( $this->getMain()->getCheck( 'callback' ) ) {
28 # T94015: jQuery appends a useless '_' parameter in jsonp mode.
29 # Mark the parameter as used in that case to avoid a warning that's
30 # outside the control of the end user.
31 # (and do it here because ApiMain::reportUnusedParams() gets called
32 # before our ::execute())
33 $this->getMain()->markParamsUsed( '_' );
34 }
35 }
36
38 public function getMimeType() {
39 $params = $this->extractRequestParams();
40 // callback:
41 if ( isset( $params['callback'] ) ) {
42 return 'text/javascript';
43 }
44
45 return 'application/json';
46 }
47
48 public function execute() {
49 $params = $this->extractRequestParams();
50
51 $opt = 0;
52 if ( $this->isRaw ) {
53 $opt |= FormatJson::ALL_OK;
54 $transform = [];
55 } else {
56 switch ( $params['formatversion'] ) {
57 case 1:
58 $opt |= $params['utf8'] ? FormatJson::ALL_OK : FormatJson::XMLMETA_OK;
59 $transform = [
60 'BC' => [],
61 'Types' => [ 'AssocAsObject' => true ],
62 'Strip' => 'all',
63 ];
64 break;
65
66 case 2:
67 case 'latest':
68 $opt |= $params['ascii'] ? FormatJson::XMLMETA_OK : FormatJson::ALL_OK;
69 $transform = [
70 'Types' => [ 'AssocAsObject' => true ],
71 'Strip' => 'all',
72 ];
73 break;
74
75 default:
76 // Should have been caught during parameter validation
77 // @codeCoverageIgnoreStart
78 self::dieDebug( __METHOD__, 'Unknown value for \'formatversion\'' );
79 // @codeCoverageIgnoreEnd
80 }
81 }
82 $data = $this->getResult()->getResultData( null, $transform );
83 $json = FormatJson::encode( $data, $this->getIsHtml(), $opt );
84 if ( $json === false ) {
85 // This should never happen, but it's a bug which could crop up
86 // if you use ApiResult::NO_VALIDATE for instance.
87 // @codeCoverageIgnoreStart
88 self::dieDebug( __METHOD__, 'Unable to encode API result as JSON' );
89 // @codeCoverageIgnoreEnd
90 }
91
92 if ( isset( $params['callback'] ) ) {
93 $callback = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $params['callback'] );
94 # Prepend a comment to try to avoid attacks against content
95 # sniffers, such as T70187.
96 $this->printText( "/**/$callback($json)" );
97 } else {
98 $this->printText( $json );
99 }
100 }
101
103 public function getAllowedParams() {
104 if ( $this->isRaw ) {
105 return parent::getAllowedParams();
106 }
107
108 return parent::getAllowedParams() + [
109 'callback' => [
110 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-callback',
111 ],
112 'utf8' => [
113 ParamValidator::PARAM_DEFAULT => false,
114 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-utf8',
115 ],
116 'ascii' => [
117 ParamValidator::PARAM_DEFAULT => false,
118 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-ascii',
119 ],
120 'formatversion' => [
121 ParamValidator::PARAM_TYPE => [ '1', '2', 'latest' ],
122 ParamValidator::PARAM_DEFAULT => '1',
123 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-formatversion',
125 '1' => 'apihelp-json-paramvalue-formatversion-1',
126 '2' => 'apihelp-json-paramvalue-formatversion-2',
127 'latest' => 'apihelp-json-paramvalue-formatversion-latest',
128 ],
129 ],
130 ];
131 }
132}
133
135class_alias( ApiFormatJson::class, 'ApiFormatJson' );
getMain()
Get the main module.
Definition ApiBase.php:561
getResult()
Get the result object.
Definition ApiBase.php:682
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:207
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:1744
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:167
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
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.When getIsHtml() returns tru...
__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:67
JSON formatter wrapper class.
Service for formatting and validating API parameters.