Fix 'Tags' padding to keep it farther from the edge and document the source of the...
[lhc/web/wiklou.git] / includes / api / ApiParamInfo.php
1 <?php
2 /**
3 * Copyright © 2008 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * @ingroup API
25 */
26 class ApiParamInfo extends ApiBase {
27
28 private $helpFormat;
29 private $context;
30
31 public function __construct( ApiMain $main, $action ) {
32 parent::__construct( $main, $action );
33 }
34
35 public function execute() {
36 // Get parameters
37 $params = $this->extractRequestParams();
38
39 $this->helpFormat = $params['helpformat'];
40 $this->context = new RequestContext;
41 $this->context->setUser( new User ); // anon to avoid caching issues
42 $this->context->setLanguage( $this->getMain()->getLanguage() );
43
44 if ( is_array( $params['modules'] ) ) {
45 $modules = [];
46 foreach ( $params['modules'] as $path ) {
47 if ( $path === '*' || $path === '**' ) {
48 $path = "main+$path";
49 }
50 if ( substr( $path, -2 ) === '+*' || substr( $path, -2 ) === ' *' ) {
51 $submodules = true;
52 $path = substr( $path, 0, -2 );
53 $recursive = false;
54 } elseif ( substr( $path, -3 ) === '+**' || substr( $path, -3 ) === ' **' ) {
55 $submodules = true;
56 $path = substr( $path, 0, -3 );
57 $recursive = true;
58 } else {
59 $submodules = false;
60 }
61
62 if ( $submodules ) {
63 try {
64 $module = $this->getModuleFromPath( $path );
65 } catch ( ApiUsageException $ex ) {
66 foreach ( $ex->getStatusValue()->getErrors() as $error ) {
67 $this->addWarning( $error );
68 }
69 continue;
70 }
71 $submodules = $this->listAllSubmodules( $module, $recursive );
72 if ( $submodules ) {
73 $modules = array_merge( $modules, $submodules );
74 } else {
75 $this->addWarning( [ 'apierror-badmodule-nosubmodules', $path ], 'badmodule' );
76 }
77 } else {
78 $modules[] = $path;
79 }
80 }
81 } else {
82 $modules = [];
83 }
84
85 if ( is_array( $params['querymodules'] ) ) {
86 $queryModules = $params['querymodules'];
87 foreach ( $queryModules as $m ) {
88 $modules[] = 'query+' . $m;
89 }
90 } else {
91 $queryModules = [];
92 }
93
94 if ( is_array( $params['formatmodules'] ) ) {
95 $formatModules = $params['formatmodules'];
96 foreach ( $formatModules as $m ) {
97 $modules[] = $m;
98 }
99 } else {
100 $formatModules = [];
101 }
102
103 $modules = array_unique( $modules );
104
105 $res = [];
106
107 foreach ( $modules as $m ) {
108 try {
109 $module = $this->getModuleFromPath( $m );
110 } catch ( ApiUsageException $ex ) {
111 foreach ( $ex->getStatusValue()->getErrors() as $error ) {
112 $this->addWarning( $error );
113 }
114 continue;
115 }
116 $key = 'modules';
117
118 // Back compat
119 $isBCQuery = false;
120 if ( $module->getParent() && $module->getParent()->getModuleName() == 'query' &&
121 in_array( $module->getModuleName(), $queryModules )
122 ) {
123 $isBCQuery = true;
124 $key = 'querymodules';
125 }
126 if ( in_array( $module->getModuleName(), $formatModules ) ) {
127 $key = 'formatmodules';
128 }
129
130 $item = $this->getModuleInfo( $module );
131 if ( $isBCQuery ) {
132 $item['querytype'] = $item['group'];
133 }
134 $res[$key][] = $item;
135 }
136
137 $result = $this->getResult();
138 $result->addValue( [ $this->getModuleName() ], 'helpformat', $this->helpFormat );
139
140 foreach ( $res as $key => $stuff ) {
141 ApiResult::setIndexedTagName( $res[$key], 'module' );
142 }
143
144 if ( $params['mainmodule'] ) {
145 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
146 }
147
148 if ( $params['pagesetmodule'] ) {
149 $pageSet = new ApiPageSet( $this->getMain()->getModuleManager()->getModule( 'query' ) );
150 $res['pagesetmodule'] = $this->getModuleInfo( $pageSet );
151 unset( $res['pagesetmodule']['name'] );
152 unset( $res['pagesetmodule']['path'] );
153 unset( $res['pagesetmodule']['group'] );
154 }
155
156 $result->addValue( null, $this->getModuleName(), $res );
157 }
158
159 /**
160 * List all submodules of a module
161 * @param ApiBase $module
162 * @param bool $recursive
163 * @return string[]
164 */
165 private function listAllSubmodules( ApiBase $module, $recursive ) {
166 $manager = $module->getModuleManager();
167 if ( $manager ) {
168 $paths = [];
169 $names = $manager->getNames();
170 sort( $names );
171 foreach ( $names as $name ) {
172 $submodule = $manager->getModule( $name );
173 $paths[] = $submodule->getModulePath();
174 if ( $recursive && $submodule->getModuleManager() ) {
175 $paths = array_merge( $paths, $this->listAllSubmodules( $submodule, $recursive ) );
176 }
177 }
178 }
179 return $paths;
180 }
181
182 /**
183 * @param array &$res Result array
184 * @param string $key Result key
185 * @param Message[] $msgs
186 * @param bool $joinLists
187 */
188 protected function formatHelpMessages( array &$res, $key, array $msgs, $joinLists = false ) {
189 switch ( $this->helpFormat ) {
190 case 'none':
191 break;
192
193 case 'wikitext':
194 $ret = [];
195 foreach ( $msgs as $m ) {
196 $ret[] = $m->setContext( $this->context )->text();
197 }
198 $res[$key] = implode( "\n\n", $ret );
199 if ( $joinLists ) {
200 $res[$key] = preg_replace( '!^(([*#:;])[^\n]*)\n\n(?=\2)!m', "$1\n", $res[$key] );
201 }
202 break;
203
204 case 'html':
205 $ret = [];
206 foreach ( $msgs as $m ) {
207 $ret[] = $m->setContext( $this->context )->parseAsBlock();
208 }
209 $ret = implode( "\n", $ret );
210 if ( $joinLists ) {
211 $ret = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $ret );
212 }
213 $res[$key] = Parser::stripOuterParagraph( $ret );
214 break;
215
216 case 'raw':
217 $res[$key] = [];
218 foreach ( $msgs as $m ) {
219 $a = [
220 'key' => $m->getKey(),
221 'params' => $m->getParams(),
222 ];
223 ApiResult::setIndexedTagName( $a['params'], 'param' );
224 if ( $m instanceof ApiHelpParamValueMessage ) {
225 $a['forvalue'] = $m->getParamValue();
226 }
227 $res[$key][] = $a;
228 }
229 ApiResult::setIndexedTagName( $res[$key], 'msg' );
230 break;
231 }
232 }
233
234 /**
235 * @param ApiBase $module
236 * @return array
237 */
238 private function getModuleInfo( $module ) {
239 $ret = [];
240 $path = $module->getModulePath();
241
242 $ret['name'] = $module->getModuleName();
243 $ret['classname'] = get_class( $module );
244 $ret['path'] = $path;
245 if ( !$module->isMain() ) {
246 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
247 $module->getModuleName()
248 );
249 }
250 $ret['prefix'] = $module->getModulePrefix();
251
252 $sourceInfo = $module->getModuleSourceInfo();
253 if ( $sourceInfo ) {
254 $ret['source'] = $sourceInfo['name'];
255 if ( isset( $sourceInfo['namemsg'] ) ) {
256 $ret['sourcename'] = $this->context->msg( $sourceInfo['namemsg'] )->text();
257 } else {
258 $ret['sourcename'] = $ret['source'];
259 }
260
261 $link = SpecialPage::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] )->getFullURL();
262 if ( isset( $sourceInfo['license-name'] ) ) {
263 $ret['licensetag'] = $sourceInfo['license-name'];
264 $ret['licenselink'] = (string)$link;
265 } elseif ( SpecialVersion::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
266 $ret['licenselink'] = (string)$link;
267 }
268 }
269
270 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
271
272 foreach ( $module->getHelpFlags() as $flag ) {
273 $ret[$flag] = true;
274 }
275
276 $ret['helpurls'] = (array)$module->getHelpUrls();
277 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
278 $ret['helpurls'] = [];
279 }
280 ApiResult::setIndexedTagName( $ret['helpurls'], 'helpurl' );
281
282 if ( $this->helpFormat !== 'none' ) {
283 $ret['examples'] = [];
284 $examples = $module->getExamplesMessages();
285 foreach ( $examples as $qs => $msg ) {
286 $item = [
287 'query' => $qs
288 ];
289 $msg = ApiBase::makeMessage( $msg, $this->context, [
290 $module->getModulePrefix(),
291 $module->getModuleName(),
292 $module->getModulePath()
293 ] );
294 $this->formatHelpMessages( $item, 'description', [ $msg ] );
295 if ( isset( $item['description'] ) ) {
296 if ( is_array( $item['description'] ) ) {
297 $item['description'] = $item['description'][0];
298 } else {
299 ApiResult::setSubelementsList( $item, 'description' );
300 }
301 }
302 $ret['examples'][] = $item;
303 }
304 ApiResult::setIndexedTagName( $ret['examples'], 'example' );
305 }
306
307 $ret['parameters'] = [];
308 $ret['templatedparameters'] = [];
309 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
310 $paramDesc = $module->getFinalParamDescription();
311 $index = 0;
312 foreach ( $params as $name => $settings ) {
313 if ( !is_array( $settings ) ) {
314 $settings = [ ApiBase::PARAM_DFLT => $settings ];
315 }
316
317 $item = [
318 'index' => ++$index,
319 'name' => $name,
320 ];
321
322 if ( !empty( $settings[ApiBase::PARAM_TEMPLATE_VARS] ) ) {
323 $item['templatevars'] = $settings[ApiBase::PARAM_TEMPLATE_VARS];
324 ApiResult::setIndexedTagName( $item['templatevars'], 'var' );
325 }
326
327 if ( isset( $paramDesc[$name] ) ) {
328 $this->formatHelpMessages( $item, 'description', $paramDesc[$name], true );
329 }
330
331 $item['required'] = !empty( $settings[ApiBase::PARAM_REQUIRED] );
332
333 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
334 $item['deprecated'] = true;
335 }
336
337 if ( $name === 'token' && $module->needsToken() ) {
338 $item['tokentype'] = $module->needsToken();
339 }
340
341 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
342 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
343 ? $settings[ApiBase::PARAM_DFLT]
344 : null;
345 if ( is_bool( $dflt ) ) {
346 $settings[ApiBase::PARAM_TYPE] = 'boolean';
347 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
348 $settings[ApiBase::PARAM_TYPE] = 'string';
349 } elseif ( is_int( $dflt ) ) {
350 $settings[ApiBase::PARAM_TYPE] = 'integer';
351 }
352 }
353
354 if ( isset( $settings[ApiBase::PARAM_DFLT] ) ) {
355 switch ( $settings[ApiBase::PARAM_TYPE] ) {
356 case 'boolean':
357 $item['default'] = (bool)$settings[ApiBase::PARAM_DFLT];
358 break;
359 case 'string':
360 case 'text':
361 case 'password':
362 $item['default'] = strval( $settings[ApiBase::PARAM_DFLT] );
363 break;
364 case 'integer':
365 case 'limit':
366 $item['default'] = intval( $settings[ApiBase::PARAM_DFLT] );
367 break;
368 case 'timestamp':
369 $item['default'] = wfTimestamp( TS_ISO_8601, $settings[ApiBase::PARAM_DFLT] );
370 break;
371 default:
372 $item['default'] = $settings[ApiBase::PARAM_DFLT];
373 break;
374 }
375 }
376
377 $item['multi'] = !empty( $settings[ApiBase::PARAM_ISMULTI] );
378 if ( $item['multi'] ) {
379 $item['lowlimit'] = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT1] )
380 ? $settings[ApiBase::PARAM_ISMULTI_LIMIT1]
381 : ApiBase::LIMIT_SML1;
382 $item['highlimit'] = !empty( $settings[ApiBase::PARAM_ISMULTI_LIMIT2] )
383 ? $settings[ApiBase::PARAM_ISMULTI_LIMIT2]
384 : ApiBase::LIMIT_SML2;
385 $item['limit'] = $this->getMain()->canApiHighLimits()
386 ? $item['highlimit']
387 : $item['lowlimit'];
388 }
389
390 if ( !empty( $settings[ApiBase::PARAM_ALLOW_DUPLICATES] ) ) {
391 $item['allowsduplicates'] = true;
392 }
393
394 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
395 if ( $settings[ApiBase::PARAM_TYPE] === 'submodule' ) {
396 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
397 ksort( $settings[ApiBase::PARAM_SUBMODULE_MAP] );
398 $item['type'] = array_keys( $settings[ApiBase::PARAM_SUBMODULE_MAP] );
399 $item['submodules'] = $settings[ApiBase::PARAM_SUBMODULE_MAP];
400 } else {
401 $item['type'] = $module->getModuleManager()->getNames( $name );
402 sort( $item['type'] );
403 $prefix = $module->isMain()
404 ? '' : ( $module->getModulePath() . '+' );
405 $item['submodules'] = [];
406 foreach ( $item['type'] as $v ) {
407 $item['submodules'][$v] = $prefix . $v;
408 }
409 }
410 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX] ) ) {
411 $item['submoduleparamprefix'] = $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX];
412 }
413
414 $deprecatedSubmodules = [];
415 foreach ( $item['submodules'] as $v => $submodulePath ) {
416 try {
417 $submod = $this->getModuleFromPath( $submodulePath );
418 if ( $submod && $submod->isDeprecated() ) {
419 $deprecatedSubmodules[] = $v;
420 }
421 } catch ( ApiUsageException $ex ) {
422 // Ignore
423 }
424 }
425 if ( $deprecatedSubmodules ) {
426 $item['type'] = array_merge(
427 array_diff( $item['type'], $deprecatedSubmodules ),
428 $deprecatedSubmodules
429 );
430 $item['deprecatedvalues'] = $deprecatedSubmodules;
431 }
432 } elseif ( $settings[ApiBase::PARAM_TYPE] === 'tags' ) {
433 $item['type'] = ChangeTags::listExplicitlyDefinedTags();
434 } else {
435 $item['type'] = $settings[ApiBase::PARAM_TYPE];
436 }
437 if ( is_array( $item['type'] ) ) {
438 // To prevent sparse arrays from being serialized to JSON as objects
439 $item['type'] = array_values( $item['type'] );
440 ApiResult::setIndexedTagName( $item['type'], 't' );
441 }
442
443 // Add 'allspecifier' if applicable
444 if ( $item['type'] === 'namespace' ) {
445 $allowAll = true;
446 $allSpecifier = ApiBase::ALL_DEFAULT_STRING;
447 } else {
448 $allowAll = isset( $settings[ApiBase::PARAM_ALL] )
449 ? $settings[ApiBase::PARAM_ALL]
450 : false;
451 $allSpecifier = ( is_string( $allowAll ) ? $allowAll : ApiBase::ALL_DEFAULT_STRING );
452 }
453 if ( $allowAll && $item['multi'] &&
454 ( is_array( $item['type'] ) || $item['type'] === 'namespace' ) ) {
455 $item['allspecifier'] = $allSpecifier;
456 }
457
458 if ( $item['type'] === 'namespace' &&
459 isset( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ) &&
460 is_array( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] )
461 ) {
462 $item['extranamespaces'] = $settings[ApiBase::PARAM_EXTRA_NAMESPACES];
463 ApiResult::setArrayType( $item['extranamespaces'], 'array' );
464 ApiResult::setIndexedTagName( $item['extranamespaces'], 'ns' );
465 }
466 }
467 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
468 $item['max'] = $settings[ApiBase::PARAM_MAX];
469 }
470 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
471 $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
472 }
473 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
474 $item['min'] = $settings[ApiBase::PARAM_MIN];
475 }
476 if ( !empty( $settings[ApiBase::PARAM_RANGE_ENFORCE] ) ) {
477 $item['enforcerange'] = true;
478 }
479 if ( isset( $settings[self::PARAM_MAX_BYTES] ) ) {
480 $item['maxbytes'] = $settings[self::PARAM_MAX_BYTES];
481 }
482 if ( isset( $settings[self::PARAM_MAX_CHARS] ) ) {
483 $item['maxchars'] = $settings[self::PARAM_MAX_CHARS];
484 }
485 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED_VALUES] ) ) {
486 $deprecatedValues = array_keys( $settings[ApiBase::PARAM_DEPRECATED_VALUES] );
487 if ( is_array( $item['type'] ) ) {
488 $deprecatedValues = array_intersect( $deprecatedValues, $item['type'] );
489 }
490 if ( $deprecatedValues ) {
491 $item['deprecatedvalues'] = array_values( $deprecatedValues );
492 ApiResult::setIndexedTagName( $item['deprecatedvalues'], 'v' );
493 }
494 }
495
496 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
497 $item['info'] = [];
498 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
499 $tag = array_shift( $i );
500 $info = [
501 'name' => $tag,
502 ];
503 if ( count( $i ) ) {
504 $info['values'] = $i;
505 ApiResult::setIndexedTagName( $info['values'], 'v' );
506 }
507 $this->formatHelpMessages( $info, 'text', [
508 $this->context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
509 ->numParams( count( $i ) )
510 ->params( $this->context->getLanguage()->commaList( $i ) )
511 ->params( $module->getModulePrefix() )
512 ] );
513 ApiResult::setSubelementsList( $info, 'text' );
514 $item['info'][] = $info;
515 }
516 ApiResult::setIndexedTagName( $item['info'], 'i' );
517 }
518
519 $key = empty( $settings[ApiBase::PARAM_TEMPLATE_VARS] ) ? 'parameters' : 'templatedparameters';
520 $ret[$key][] = $item;
521 }
522 ApiResult::setIndexedTagName( $ret['parameters'], 'param' );
523 ApiResult::setIndexedTagName( $ret['templatedparameters'], 'param' );
524
525 $dynamicParams = $module->dynamicParameterDocumentation();
526 if ( $dynamicParams !== null ) {
527 if ( $this->helpFormat === 'none' ) {
528 $ret['dynamicparameters'] = true;
529 } else {
530 $dynamicParams = ApiBase::makeMessage( $dynamicParams, $this->context, [
531 $module->getModulePrefix(),
532 $module->getModuleName(),
533 $module->getModulePath()
534 ] );
535 $this->formatHelpMessages( $ret, 'dynamicparameters', [ $dynamicParams ] );
536 }
537 }
538
539 return $ret;
540 }
541
542 public function isReadMode() {
543 return false;
544 }
545
546 public function getAllowedParams() {
547 // back compat
548 $querymodules = $this->getMain()->getModuleManager()
549 ->getModule( 'query' )->getModuleManager()->getNames();
550 sort( $querymodules );
551 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
552 sort( $formatmodules );
553
554 return [
555 'modules' => [
556 ApiBase::PARAM_ISMULTI => true,
557 ],
558 'helpformat' => [
559 ApiBase::PARAM_DFLT => 'none',
560 ApiBase::PARAM_TYPE => [ 'html', 'wikitext', 'raw', 'none' ],
561 ],
562
563 'querymodules' => [
564 ApiBase::PARAM_DEPRECATED => true,
565 ApiBase::PARAM_ISMULTI => true,
566 ApiBase::PARAM_TYPE => $querymodules,
567 ],
568 'mainmodule' => [
569 ApiBase::PARAM_DEPRECATED => true,
570 ],
571 'pagesetmodule' => [
572 ApiBase::PARAM_DEPRECATED => true,
573 ],
574 'formatmodules' => [
575 ApiBase::PARAM_DEPRECATED => true,
576 ApiBase::PARAM_ISMULTI => true,
577 ApiBase::PARAM_TYPE => $formatmodules,
578 ]
579 ];
580 }
581
582 protected function getExamplesMessages() {
583 return [
584 'action=paraminfo&modules=parse|phpfm|query%2Ballpages|query%2Bsiteinfo'
585 => 'apihelp-paraminfo-example-1',
586 'action=paraminfo&modules=query%2B*'
587 => 'apihelp-paraminfo-example-2',
588 ];
589 }
590
591 public function getHelpUrls() {
592 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Parameter_information';
593 }
594 }