Merge "Don't check namespace in SpecialWantedtemplates"
[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 // Get parameters
41 $params = $this->extractRequestParams();
42
43 $this->helpFormat = $params['helpformat'];
44 $this->context = new RequestContext;
45 $this->context->setUser( new User ); // anon to avoid caching issues
46 $this->context->setLanguage( $this->getMain()->getLanguage() );
47
48 if ( is_array( $params['modules'] ) ) {
49 $modules = $params['modules'];
50 } else {
51 $modules = array();
52 }
53
54 if ( is_array( $params['querymodules'] ) ) {
55 $this->logFeatureUsage( 'action=paraminfo&querymodules' );
56 $queryModules = $params['querymodules'];
57 foreach ( $queryModules as $m ) {
58 $modules[] = 'query+' . $m;
59 }
60 } else {
61 $queryModules = array();
62 }
63
64 if ( is_array( $params['formatmodules'] ) ) {
65 $this->logFeatureUsage( 'action=paraminfo&formatmodules' );
66 $formatModules = $params['formatmodules'];
67 foreach ( $formatModules as $m ) {
68 $modules[] = $m;
69 }
70 } else {
71 $formatModules = array();
72 }
73
74 $res = array();
75
76 foreach ( $modules as $m ) {
77 try {
78 $module = $this->getModuleFromPath( $m );
79 } catch ( UsageException $ex ) {
80 $this->setWarning( $ex->getMessage() );
81 continue;
82 }
83 $key = 'modules';
84
85 // Back compat
86 $isBCQuery = false;
87 if ( $module->getParent() && $module->getParent()->getModuleName() == 'query' &&
88 in_array( $module->getModuleName(), $queryModules )
89 ) {
90 $isBCQuery = true;
91 $key = 'querymodules';
92 }
93 if ( in_array( $module->getModuleName(), $formatModules ) ) {
94 $key = 'formatmodules';
95 }
96
97 $item = $this->getModuleInfo( $module );
98 if ( $isBCQuery ) {
99 $item['querytype'] = $item['group'];
100 }
101 $res[$key][] = $item;
102 }
103
104 $result = $this->getResult();
105 $result->addValue( array( $this->getModuleName() ), 'helpformat', $this->helpFormat );
106
107 foreach ( $res as $key => $stuff ) {
108 ApiResult::setIndexedTagName( $res[$key], 'module' );
109 }
110
111 if ( $params['mainmodule'] ) {
112 $this->logFeatureUsage( 'action=paraminfo&mainmodule' );
113 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
114 }
115
116 if ( $params['pagesetmodule'] ) {
117 $this->logFeatureUsage( 'action=paraminfo&pagesetmodule' );
118 $pageSet = new ApiPageSet( $this->getMain()->getModuleManager()->getModule( 'query' ) );
119 $res['pagesetmodule'] = $this->getModuleInfo( $pageSet );
120 unset( $res['pagesetmodule']['name'] );
121 unset( $res['pagesetmodule']['path'] );
122 unset( $res['pagesetmodule']['group'] );
123 }
124
125 $result->addValue( null, $this->getModuleName(), $res );
126 }
127
128 /**
129 * @param array $res Result array
130 * @param string $key Result key
131 * @param Message[] $msgs
132 * @param bool $joinLists
133 */
134 protected function formatHelpMessages( array &$res, $key, array $msgs, $joinLists = false ) {
135 switch ( $this->helpFormat ) {
136 case 'none':
137 break;
138
139 case 'wikitext':
140 $ret = array();
141 foreach ( $msgs as $m ) {
142 $ret[] = $m->setContext( $this->context )->text();
143 }
144 $res[$key] = join( "\n\n", $ret );
145 if ( $joinLists ) {
146 $res[$key] = preg_replace( '!^(([*#:;])[^\n]*)\n\n(?=\2)!m', "$1\n", $res[$key] );
147 }
148 break;
149
150 case 'html':
151 $ret = array();
152 foreach ( $msgs as $m ) {
153 $ret[] = $m->setContext( $this->context )->parseAsBlock();
154 }
155 $ret = join( "\n", $ret );
156 if ( $joinLists ) {
157 $ret = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $ret );
158 }
159 $res[$key] = Parser::stripOuterParagraph( $ret );
160 break;
161
162 case 'raw':
163 $res[$key] = array();
164 foreach ( $msgs as $m ) {
165 $a = array(
166 'key' => $m->getKey(),
167 'params' => $m->getParams(),
168 );
169 if ( $m instanceof ApiHelpParamValueMessage ) {
170 $a['forvalue'] = $m->getParamValue();
171 }
172 $res[$key][] = $a;
173 }
174 ApiResult::setIndexedTagName( $res[$key], 'msg' );
175 break;
176 }
177 }
178
179 /**
180 * @param ApiBase $module
181 * @return ApiResult
182 */
183 private function getModuleInfo( $module ) {
184 $result = $this->getResult();
185 $ret = array();
186 $path = $module->getModulePath();
187
188 $ret['name'] = $module->getModuleName();
189 $ret['classname'] = get_class( $module );
190 $ret['path'] = $path;
191 if ( !$module->isMain() ) {
192 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
193 $module->getModuleName()
194 );
195 }
196 $ret['prefix'] = $module->getModulePrefix();
197
198 $sourceInfo = $module->getModuleSourceInfo();
199 if ( $sourceInfo ) {
200 $ret['source'] = $sourceInfo['name'];
201 if ( isset( $sourceInfo['namemsg'] ) ) {
202 $ret['sourcename'] = $this->context->msg( $sourceInfo['namemsg'] )->text();
203 } else {
204 $ret['sourcename'] = $ret['source'];
205 }
206
207 $link = SpecialPage::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] )->getFullUrl();
208 if ( isset( $sourceInfo['license-name'] ) ) {
209 $ret['licensetag'] = $sourceInfo['license-name'];
210 $ret['licenselink'] = (string)$link;
211 } elseif ( SpecialVersion::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
212 $ret['licenselink'] = (string)$link;
213 }
214 }
215
216 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
217
218 foreach ( $module->getHelpFlags() as $flag ) {
219 $ret[$flag] = true;
220 }
221
222 $ret['helpurls'] = (array)$module->getHelpUrls();
223 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
224 $ret['helpurls'] = array();
225 }
226 ApiResult::setIndexedTagName( $ret['helpurls'], 'helpurl' );
227
228 if ( $this->helpFormat !== 'none' ) {
229 $ret['examples'] = array();
230 $examples = $module->getExamplesMessages();
231 foreach ( $examples as $qs => $msg ) {
232 $item = array(
233 'query' => $qs
234 );
235 $msg = ApiBase::makeMessage( $msg, $this->context, array(
236 $module->getModulePrefix(),
237 $module->getModuleName(),
238 $module->getModulePath()
239 ) );
240 $this->formatHelpMessages( $item, 'description', array( $msg ) );
241 if ( isset( $item['description'] ) ) {
242 if ( is_array( $item['description'] ) ) {
243 $item['description'] = $item['description'][0];
244 } else {
245 ApiResult::setSubelementsList( $item, 'description' );
246 }
247 }
248 $ret['examples'][] = $item;
249 }
250 ApiResult::setIndexedTagName( $ret['examples'], 'example' );
251 }
252
253 $ret['parameters'] = array();
254 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
255 $paramDesc = $module->getFinalParamDescription();
256 foreach ( $params as $name => $settings ) {
257 if ( !is_array( $settings ) ) {
258 $settings = array( ApiBase::PARAM_DFLT => $settings );
259 }
260
261 $item = array(
262 'name' => $name
263 );
264 if ( isset( $paramDesc[$name] ) ) {
265 $this->formatHelpMessages( $item, 'description', $paramDesc[$name], true );
266 }
267
268 $item['required'] = !empty( $settings[ApiBase::PARAM_REQUIRED] );
269
270 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
271 $item['deprecated'] = true;
272 }
273
274 if ( $name === 'token' && $module->needsToken() ) {
275 $item['tokentype'] = $module->needsToken();
276 }
277
278 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
279 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
280 ? $settings[ApiBase::PARAM_DFLT]
281 : null;
282 if ( is_bool( $dflt ) ) {
283 $settings[ApiBase::PARAM_TYPE] = 'boolean';
284 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
285 $settings[ApiBase::PARAM_TYPE] = 'string';
286 } elseif ( is_int( $dflt ) ) {
287 $settings[ApiBase::PARAM_TYPE] = 'integer';
288 }
289 }
290
291 if ( isset( $settings[ApiBase::PARAM_DFLT] ) ) {
292 switch ( $settings[ApiBase::PARAM_TYPE] ) {
293 case 'boolean':
294 $item['default'] = (bool)$settings[ApiBase::PARAM_DFLT];
295 break;
296 case 'string':
297 case 'text':
298 case 'password':
299 $item['default'] = strval( $settings[ApiBase::PARAM_DFLT] );
300 break;
301 case 'integer':
302 case 'limit':
303 $item['default'] = intval( $settings[ApiBase::PARAM_DFLT] );
304 break;
305 case 'timestamp':
306 $item['default'] = wfTimestamp( TS_ISO_8601, $settings[ApiBase::PARAM_DFLT] );
307 break;
308 default:
309 $item['default'] = $settings[ApiBase::PARAM_DFLT];
310 break;
311 }
312 }
313
314 $item['multi'] = !empty( $settings[ApiBase::PARAM_ISMULTI] );
315 if ( $item['multi'] ) {
316 $item['limit'] = $this->getMain()->canApiHighLimits() ?
317 ApiBase::LIMIT_SML2 :
318 ApiBase::LIMIT_SML1;
319 $item['lowlimit'] = ApiBase::LIMIT_SML1;
320 $item['highlimit'] = ApiBase::LIMIT_SML2;
321 }
322
323 if ( !empty( $settings[ApiBase::PARAM_ALLOW_DUPLICATES] ) ) {
324 $item['allowsduplicates'] = true;
325 }
326
327 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
328 if ( $settings[ApiBase::PARAM_TYPE] === 'submodule' ) {
329 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
330 ksort( $settings[ApiBase::PARAM_SUBMODULE_MAP] );
331 $item['type'] = array_keys( $settings[ApiBase::PARAM_SUBMODULE_MAP] );
332 $item['submodules'] = $settings[ApiBase::PARAM_SUBMODULE_MAP];
333 } else {
334 $item['type'] = $module->getModuleManager()->getNames( $name );
335 sort( $item['type'] );
336 $prefix = $module->isMain()
337 ? '' : ( $module->getModulePath() . '+' );
338 $item['submodules'] = array();
339 foreach ( $item['type'] as $v ) {
340 $item['submodules'][$v] = $prefix . $v;
341 }
342 }
343 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX] ) ) {
344 $item['submoduleparamprefix'] = $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX];
345 }
346 } else {
347 $item['type'] = $settings[ApiBase::PARAM_TYPE];
348 }
349 if ( is_array( $item['type'] ) ) {
350 // To prevent sparse arrays from being serialized to JSON as objects
351 $item['type'] = array_values( $item['type'] );
352 ApiResult::setIndexedTagName( $item['type'], 't' );
353 }
354 }
355 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
356 $item['max'] = $settings[ApiBase::PARAM_MAX];
357 }
358 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
359 $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
360 }
361 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
362 $item['min'] = $settings[ApiBase::PARAM_MIN];
363 }
364 if ( !empty( $settings[ApiBase::PARAM_RANGE_ENFORCE] ) ) {
365 $item['enforcerange'] = true;
366 }
367
368 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
369 $item['info'] = array();
370 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
371 $tag = array_shift( $i );
372 $info = array(
373 'name' => $tag,
374 );
375 if ( count( $i ) ) {
376 $info['values'] = $i;
377 ApiResult::setIndexedTagName( $info['values'], 'v' );
378 }
379 $this->formatHelpMessages( $info, 'text', array(
380 $this->context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
381 ->numParams( count( $i ) )
382 ->params( $this->context->getLanguage()->commaList( $i ) )
383 ->params( $module->getModulePrefix() )
384 ) );
385 ApiResult::setSubelementsList( $info, 'text' );
386 $item['info'][] = $info;
387 }
388 ApiResult::setIndexedTagName( $item['info'], 'i' );
389 }
390
391 $ret['parameters'][] = $item;
392 }
393 ApiResult::setIndexedTagName( $ret['parameters'], 'param' );
394
395 return $ret;
396 }
397
398 public function isReadMode() {
399 return false;
400 }
401
402 public function getAllowedParams() {
403 // back compat
404 $querymodules = $this->getMain()->getModuleManager()
405 ->getModule( 'query' )->getModuleManager()->getNames();
406 sort( $querymodules );
407 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
408 sort( $formatmodules );
409
410 return array(
411 'modules' => array(
412 ApiBase::PARAM_ISMULTI => true,
413 ),
414 'helpformat' => array(
415 ApiBase::PARAM_DFLT => 'none',
416 ApiBase::PARAM_TYPE => array( 'html', 'wikitext', 'raw', 'none' ),
417 ),
418
419 'querymodules' => array(
420 ApiBase::PARAM_DEPRECATED => true,
421 ApiBase::PARAM_ISMULTI => true,
422 ApiBase::PARAM_TYPE => $querymodules,
423 ),
424 'mainmodule' => array(
425 ApiBase::PARAM_DEPRECATED => true,
426 ),
427 'pagesetmodule' => array(
428 ApiBase::PARAM_DEPRECATED => true,
429 ),
430 'formatmodules' => array(
431 ApiBase::PARAM_DEPRECATED => true,
432 ApiBase::PARAM_ISMULTI => true,
433 ApiBase::PARAM_TYPE => $formatmodules,
434 )
435 );
436 }
437
438 protected function getExamplesMessages() {
439 return array(
440 'action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo'
441 => 'apihelp-paraminfo-example-1',
442 );
443 }
444
445 public function getHelpUrls() {
446 return 'https://www.mediawiki.org/wiki/API:Parameter_information';
447 }
448 }