38 parent::__construct( $main, $format );
39 $this->isRaw = ( $format ===
'rawfm' );
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(
'_' );
54 if ( isset(
$params[
'callback'] ) ) {
55 return 'text/javascript';
58 return 'application/json';
66 $opt |= FormatJson::ALL_OK;
69 switch (
$params[
'formatversion'] ) {
71 $opt |=
$params[
'utf8'] ? FormatJson::ALL_OK : FormatJson::XMLMETA_OK;
74 'Types' => [
'AssocAsObject' => true ],
81 $opt |=
$params[
'ascii'] ? FormatJson::XMLMETA_OK : FormatJson::ALL_OK;
83 'Types' => [
'AssocAsObject' => true ],
91 self::dieDebug( __METHOD__,
'Unknown value for \'formatversion\'' );
95 $data = $this->
getResult()->getResultData(
null, $transform );
96 $json = FormatJson::encode( $data, $this->
getIsHtml(), $opt );
97 if ( $json ===
false ) {
101 self::dieDebug( __METHOD__,
'Unable to encode API result as JSON' );
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)" );
116 if ( $this->isRaw ) {
117 return parent::getAllowedParams();
120 return parent::getAllowedParams() + [
125 ParamValidator::PARAM_DEFAULT =>
false,
129 ParamValidator::PARAM_DEFAULT =>
false,
133 ParamValidator::PARAM_TYPE => [
'1',
'2',
'latest' ],
134 ParamValidator::PARAM_DEFAULT =>
'1',
137 '1' =>
'apihelp-json-paramvalue-formatversion-1',
138 '2' =>
'apihelp-json-paramvalue-formatversion-2',
139 'latest' =>
'apihelp-json-paramvalue-formatversion-latest',
147class_alias( ApiFormatJson::class,
'ApiFormatJson' );
array $params
The job parameters.
This is the main API class, used for both external and internal processing.