24 parent::__construct( $main, $format );
25 $this->isRaw = ( $format ===
'rawfm' );
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(
'_' );
41 if ( isset( $params[
'callback'] ) ) {
42 return 'text/javascript';
45 return 'application/json';
53 $opt |= FormatJson::ALL_OK;
56 switch ( $params[
'formatversion'] ) {
58 $opt |= $params[
'utf8'] ? FormatJson::ALL_OK : FormatJson::XMLMETA_OK;
61 'Types' => [
'AssocAsObject' => true ],
68 $opt |= $params[
'ascii'] ? FormatJson::XMLMETA_OK : FormatJson::ALL_OK;
70 'Types' => [
'AssocAsObject' => true ],
78 self::dieDebug( __METHOD__,
'Unknown value for \'formatversion\'' );
82 $data = $this->
getResult()->getResultData(
null, $transform );
83 $json = FormatJson::encode( $data, $this->
getIsHtml(), $opt );
84 if ( $json ===
false ) {
88 self::dieDebug( __METHOD__,
'Unable to encode API result as JSON' );
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)" );
104 if ( $this->isRaw ) {
105 return parent::getAllowedParams();
108 return parent::getAllowedParams() + [
113 ParamValidator::PARAM_DEFAULT =>
false,
117 ParamValidator::PARAM_DEFAULT =>
false,
121 ParamValidator::PARAM_TYPE => [
'1',
'2',
'latest' ],
122 ParamValidator::PARAM_DEFAULT =>
'1',
125 '1' =>
'apihelp-json-paramvalue-formatversion-1',
126 '2' =>
'apihelp-json-paramvalue-formatversion-2',
127 'latest' =>
'apihelp-json-paramvalue-formatversion-latest',
135class_alias( ApiFormatJson::class,
'ApiFormatJson' );
This is the main API class, used for both external and internal processing.