| 1 |
<?php |
| 2 |
|
| 3 |
" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\Json\FormatJson; |
| 26 |
use Wikimedia\ParamValidator\ParamValidator; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class ApiFormatJson extends ApiFormatBase { |
| 33 |
|
| 34 |
|
| 35 |
private $isRaw; |
| 36 |
|
| 37 |
public function __construct( ApiMain $main, string $format ) { |
| 38 |
parent::__construct( $main, $format ); |
| 39 |
$this->isRaw = ( $format === 'rawfm' ); |
| 40 |
|
| 41 |
if ( $this->getMain()->getCheck( 'callback' ) ) { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
$this->getMain()->markParamsUsed( '_' ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
public function getMimeType() { |
| 52 |
$params = $this->extractRequestParams(); |
| 53 |
|
| 54 |
if ( isset( $params['callback'] ) ) { |
| 55 |
return 'text/javascript'; |
| 56 |
} |
| 57 |
|
| 58 |
return 'application/json'; |
| 59 |
} |
| 60 |
|
| 61 |
public function execute() { |
| 62 |
$params = $this->extractRequestParams(); |
| 63 |
|
| 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', |
| 76 |
]; |
| 77 |
break; |
| 78 |
|
| 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', |
| 85 |
]; |
| 86 |
break; |
| 87 |
|
| 88 |
default: |
| 89 |
|
| 90 |
|
| 91 |
self::dieDebug( __METHOD__, 'Unknown value for \'formatversion\'' ); |
| 92 |
|
| 93 |
} |
| 94 |
} |
| 95 |
$data = $this->getResult()->getResultData( null, $transform ); |
| 96 |
$json = FormatJson::encode( $data, $this->getIsHtml(), $opt ); |
| 97 |
if ( $json === false ) { |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
self::dieDebug( __METHOD__, 'Unable to encode API result as JSON' ); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
if ( isset( $params['callback'] ) ) { |
| 106 |
$callback = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $params['callback'] ); |
| 107 |
|
| 108 |
|
| 109 |
$this->printText( "/**/$callback($json)" ); |
| 110 |
} else { |
| 111 |
$this->printText( $json ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public function getAllowedParams() { |
| 116 |
if ( $this->isRaw ) { |
| 117 |
return parent::getAllowedParams(); |
| 118 |
} |
| 119 |
|
| 120 |
return parent::getAllowedParams() + [ |
| 121 |
'callback' => [ |
| 122 |
ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-callback', |
| 123 |
], |
| 124 |
'utf8' => [ |
| 125 |
ParamValidator::PARAM_DEFAULT => false, |
| 126 |
ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-utf8', |
| 127 |
], |
| 128 |
'ascii' => [ |
| 129 |
ParamValidator::PARAM_DEFAULT => false, |
| 130 |
ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-ascii', |
| 131 |
], |
| 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', |
| 140 |
], |
| 141 |
], |
| 142 |
]; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
class_alias( ApiFormatJson::class, 'ApiFormatJson' ); |