Merge "Mention translatewiki.net on edits only, when edit a default message"
[lhc/web/wiklou.git] / includes / api / ApiParamInfo.php
1 <?php
2 /**
3 *
4 *
5 * Created on Dec 01, 2007
6 *
7 * Copyright © 2008 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * @ingroup API
29 */
30 class ApiParamInfo extends ApiBase {
31
32 private $helpFormat;
33 private $context;
34
35 public function __construct( ApiMain $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 public function execute() {
40 global $wgContLang;
41
42 // Get parameters
43 $params = $this->extractRequestParams();
44
45 $this->helpFormat = $params['helpformat'];
46 $this->context = new RequestContext;
47 $this->context->setUser( new User ); // anon to avoid caching issues
48 $this->context->setLanguage( $this->getMain()->getLanguage() );
49
50 if ( is_array( $params['modules'] ) ) {
51 $modules = $params['modules'];
52 } else {
53 $modules = array();
54 }
55
56 if ( is_array( $params['querymodules'] ) ) {
57 $this->logFeatureUsage( 'action=paraminfo&querymodules' );
58 $queryModules = $params['querymodules'];
59 foreach ( $queryModules as $m ) {
60 $modules[] = 'query+' . $m;
61 }
62 } else {
63 $queryModules = array();
64 }
65
66 if ( is_array( $params['formatmodules'] ) ) {
67 $this->logFeatureUsage( 'action=paraminfo&formatmodules' );
68 $formatModules = $params['formatmodules'];
69 foreach ( $formatModules as $m ) {
70 $modules[] = $m;
71 }
72 } else {
73 $formatModules = array();
74 }
75
76 $res = array();
77
78 foreach ( $modules as $m ) {
79 try {
80 $module = $this->getModuleFromPath( $m );
81 } catch ( UsageException $ex ) {
82 $this->setWarning( $ex->getMessage() );
83 continue;
84 }
85 $key = 'modules';
86
87 // Back compat
88 $isBCQuery = false;
89 if ( $module->getParent() && $module->getParent()->getModuleName() == 'query' &&
90 in_array( $module->getModuleName(), $queryModules )
91 ) {
92 $isBCQuery = true;
93 $key = 'querymodules';
94 }
95 if ( in_array( $module->getModuleName(), $formatModules ) ) {
96 $key = 'formatmodules';
97 }
98
99 $item = $this->getModuleInfo( $module );
100 if ( $isBCQuery ) {
101 $item['querytype'] = $item['group'];
102 }
103 $res[$key][] = $item;
104 }
105
106 $result = $this->getResult();
107 $result->addValue( array( $this->getModuleName() ), 'helpformat', $this->helpFormat );
108
109 foreach ( $res as $key => $stuff ) {
110 $result->setIndexedTagName( $res[$key], 'module' );
111 }
112
113 if ( $params['mainmodule'] ) {
114 $this->logFeatureUsage( 'action=paraminfo&mainmodule' );
115 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
116 }
117
118 if ( $params['pagesetmodule'] ) {
119 $this->logFeatureUsage( 'action=paraminfo&pagesetmodule' );
120 $pageSet = new ApiPageSet( $this->getMain()->getModuleManager()->getModule( 'query' ) );
121 $res['pagesetmodule'] = $this->getModuleInfo( $pageSet );
122 unset( $res['pagesetmodule']['name'] );
123 unset( $res['pagesetmodule']['path'] );
124 unset( $res['pagesetmodule']['group'] );
125 }
126
127 $result->addValue( null, $this->getModuleName(), $res );
128 }
129
130 /**
131 * @param array $res Result array
132 * @param string $key Result key
133 * @param Message[] $msgs
134 */
135 protected function formatHelpMessages( array &$res, $key, array $msgs ) {
136 switch ( $this->helpFormat ) {
137 case 'none':
138 break;
139
140 case 'wikitext':
141 $ret = array();
142 foreach ( $msgs as $m ) {
143 $ret[] = $m->setContext( $this->context )->text();
144 }
145 $res[$key] = join( "\n\n", $ret );
146 break;
147
148 case 'html':
149 $ret = array();
150 foreach ( $msgs as $m ) {
151 $ret[] = $m->setContext( $this->context )->parseAsBlock();
152 }
153 $res[$key] = join( "\n", $ret );
154 break;
155
156 case 'raw':
157 $res[$key] = array();
158 foreach ( $msgs as $m ) {
159 $res[$key][] = array(
160 'key' => $m->getKey(),
161 'params' => $m->getParams(),
162 );
163 }
164 $this->getResult()->setIndexedTagName( $res[$key], 'msg' );
165 break;
166 }
167 }
168
169 /**
170 * @param ApiBase $module
171 * @return ApiResult
172 */
173 private function getModuleInfo( $module ) {
174 $result = $this->getResult();
175 $ret = array();
176
177 $ret['name'] = $module->getModuleName();
178 $ret['classname'] = get_class( $module );
179 $ret['path'] = $module->getModulePath();
180 if ( !$module->isMain() ) {
181 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
182 $module->getModuleName()
183 );
184 }
185 $ret['prefix'] = $module->getModulePrefix();
186
187 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
188
189 foreach ( $module->getHelpFlags() as $flag ) {
190 $ret[$flag] = '';
191 }
192
193 $ret['helpurls'] = (array)$module->getHelpUrls();
194 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
195 $ret['helpurls'] = array();
196 }
197 $result->setIndexedTagName( $ret['helpurls'], 'helpurl' );
198
199 if ( $this->helpFormat !== 'none' ) {
200 $ret['examples'] = array();
201 $examples = $module->getExamplesMessages();
202 foreach ( $examples as $qs => $msg ) {
203 $item = array(
204 'query' => $qs
205 );
206 $msg = ApiBase::makeMessage( $msg, $this->context, array(
207 $module->getModulePrefix(),
208 $module->getModuleName(),
209 $module->getModulePath()
210 ) );
211 $this->formatHelpMessages( $item, 'description', array( $msg ) );
212 if ( isset( $item['description'] ) ) {
213 if ( is_array( $item['description'] ) ) {
214 $item['description'] = $item['description'][0];
215 } else {
216 $result->setSubelements( $item, 'description' );
217 }
218 }
219 $ret['examples'][] = $item;
220 }
221 $result->setIndexedTagName( $ret['examples'], 'example' );
222 }
223
224 $ret['parameters'] = array();
225 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
226 $paramDesc = $module->getFinalParamDescription();
227 foreach ( $params as $name => $settings ) {
228 if ( !is_array( $settings ) ) {
229 $settings = array( ApiBase::PARAM_DFLT => $settings );
230 }
231
232 $item = array(
233 'name' => $name
234 );
235 if ( isset( $paramDesc[$name] ) ) {
236 $this->formatHelpMessages( $item, 'description', $paramDesc[$name] );
237 }
238
239 if ( !empty( $settings[ApiBase::PARAM_REQUIRED] ) ) {
240 $item['required'] = '';
241 }
242
243 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
244 $item['deprecated'] = '';
245 }
246
247 if ( $name === 'token' && $module->needsToken() ) {
248 $item['tokentype'] = $module->needsToken();
249 }
250
251 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
252 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
253 ? $settings[ApiBase::PARAM_DFLT]
254 : null;
255 if ( is_bool( $dflt ) ) {
256 $settings[ApiBase::PARAM_TYPE] = 'boolean';
257 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
258 $settings[ApiBase::PARAM_TYPE] = 'string';
259 } elseif ( is_int( $dflt ) ) {
260 $settings[ApiBase::PARAM_TYPE] = 'integer';
261 }
262 }
263
264 if ( isset( $settings[ApiBase::PARAM_DFLT] ) ) {
265 switch ( $settings[ApiBase::PARAM_TYPE] ) {
266 case 'boolean':
267 $item['default'] = ( $settings[ApiBase::PARAM_DFLT] ? 'true' : 'false' );
268 break;
269 case 'string':
270 $item['default'] = strval( $settings[ApiBase::PARAM_DFLT] );
271 break;
272 case 'integer':
273 $item['default'] = intval( $settings[ApiBase::PARAM_DFLT] );
274 break;
275 default:
276 $item['default'] = $settings[ApiBase::PARAM_DFLT];
277 break;
278 }
279 }
280
281 if ( !empty( $settings[ApiBase::PARAM_ISMULTI] ) ) {
282 $item['multi'] = '';
283 $item['limit'] = $this->getMain()->canApiHighLimits() ?
284 ApiBase::LIMIT_SML2 :
285 ApiBase::LIMIT_SML1;
286 $item['lowlimit'] = ApiBase::LIMIT_SML1;
287 $item['highlimit'] = ApiBase::LIMIT_SML2;
288 }
289
290 if ( !empty( $settings[ApiBase::PARAM_ALLOW_DUPLICATES] ) ) {
291 $item['allowsduplicates'] = '';
292 }
293
294 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
295 if ( $settings[ApiBase::PARAM_TYPE] === 'submodule' ) {
296 $item['type'] = $module->getModuleManager()->getNames( $name );
297 sort( $item['type'] );
298 $item['submodules'] = '';
299 } else {
300 $item['type'] = $settings[ApiBase::PARAM_TYPE];
301 }
302 if ( is_array( $item['type'] ) ) {
303 // To prevent sparse arrays from being serialized to JSON as objects
304 $item['type'] = array_values( $item['type'] );
305 $result->setIndexedTagName( $item['type'], 't' );
306 }
307 }
308 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
309 $item['max'] = $settings[ApiBase::PARAM_MAX];
310 }
311 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
312 $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
313 }
314 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
315 $item['min'] = $settings[ApiBase::PARAM_MIN];
316 }
317 $ret['parameters'][] = $item;
318 }
319 $result->setIndexedTagName( $ret['parameters'], 'param' );
320
321 return $ret;
322 }
323
324 public function isReadMode() {
325 return false;
326 }
327
328 public function getAllowedParams() {
329 // back compat
330 $querymodules = $this->getMain()->getModuleManager()
331 ->getModule( 'query' )->getModuleManager()->getNames();
332 sort( $querymodules );
333 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
334 sort( $formatmodules );
335
336 return array(
337 'modules' => array(
338 ApiBase::PARAM_ISMULTI => true,
339 ),
340 'helpformat' => array(
341 ApiBase::PARAM_DFLT => 'none',
342 ApiBase::PARAM_TYPE => array( 'html', 'wikitext', 'raw', 'none' ),
343 ),
344
345 'querymodules' => array(
346 ApiBase::PARAM_DEPRECATED => true,
347 ApiBase::PARAM_ISMULTI => true,
348 ApiBase::PARAM_TYPE => $querymodules,
349 ),
350 'mainmodule' => array(
351 ApiBase::PARAM_DEPRECATED => true,
352 ),
353 'pagesetmodule' => array(
354 ApiBase::PARAM_DEPRECATED => true,
355 ),
356 'formatmodules' => array(
357 ApiBase::PARAM_DEPRECATED => true,
358 ApiBase::PARAM_ISMULTI => true,
359 ApiBase::PARAM_TYPE => $formatmodules,
360 )
361 );
362 }
363
364 public function getParamDescription() {
365 return array(
366 'modules' => 'List of module names (values of the action= and format= parameters, or "main"). Can specify submodules with a \'+\'',
367 'helpformat' => 'Format of help strings',
368
369 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
370 'mainmodule' => 'Get information about the main (top-level) module as well',
371 'pagesetmodule' => 'Get information about the pageset module ' .
372 '(providing titles= and friends) as well',
373 'formatmodules' => 'List of format module names (value of format= parameter)',
374 );
375 }
376
377 public function getDescription() {
378 return 'Obtain information about certain API parameters and errors.';
379 }
380
381 public function getExamples() {
382 return array(
383 'api.php?action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo'
384 );
385 }
386
387 public function getHelpUrls() {
388 return 'https://www.mediawiki.org/wiki/API:Parameter_information';
389 }
390 }