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