Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiFormatJson.php
blob7675efec7061c834404922f3edd1caa7919c4400
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\Json\FormatJson;
26 use Wikimedia\ParamValidator\ParamValidator;
28 /**
29 * API JSON output formatter
30 * @ingroup API
32 class ApiFormatJson extends ApiFormatBase {
34 /** @var bool */
35 private $isRaw;
37 public function __construct( ApiMain $main, string $format ) {
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( '_' );
51 public function getMimeType() {
52 $params = $this->extractRequestParams();
53 // callback:
54 if ( isset( $params['callback'] ) ) {
55 return 'text/javascript';
58 return 'application/json';
61 public function execute() {
62 $params = $this->extractRequestParams();
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',
77 break;
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',
86 break;
88 default:
89 // Should have been caught during parameter validation
90 // @codeCoverageIgnoreStart
91 self::dieDebug( __METHOD__, 'Unknown value for \'formatversion\'' );
92 // @codeCoverageIgnoreEnd
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
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 );
115 public function getAllowedParams() {
116 if ( $this->isRaw ) {
117 return parent::getAllowedParams();
120 return parent::getAllowedParams() + [
121 'callback' => [
122 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-callback',
124 'utf8' => [
125 ParamValidator::PARAM_DEFAULT => false,
126 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-utf8',
128 'ascii' => [
129 ParamValidator::PARAM_DEFAULT => false,
130 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-ascii',
132 'formatversion' => [
133 ParamValidator::PARAM_TYPE => [ '1', '2', 'latest' ],
134 ParamValidator::PARAM_DEFAULT => '1',
135 ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-formatversion',
136 ApiBase::PARAM_HELP_MSG_PER_VALUE => [
137 '1' => 'apihelp-json-paramvalue-formatversion-1',
138 '2' => 'apihelp-json-paramvalue-formatversion-2',
139 'latest' => 'apihelp-json-paramvalue-formatversion-latest',
146 /** @deprecated class alias since 1.43 */
147 class_alias( ApiFormatJson::class, 'ApiFormatJson' );