5 * Created on Dec 01, 2007
7 * Copyright © 2008 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
30 class ApiParamInfo
extends ApiBase
{
35 public function __construct( ApiMain
$main, $action ) {
36 parent
::__construct( $main, $action );
39 public function execute() {
41 $params = $this->extractRequestParams();
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() );
48 if ( is_array( $params['modules'] ) ) {
50 foreach ( $params['modules'] as $path ) {
51 if ( $path === '*' ||
$path === '**' ) {
54 if ( substr( $path, -2 ) === '+*' ||
substr( $path, -2 ) === ' *' ) {
56 $path = substr( $path, 0, -2 );
58 } elseif ( substr( $path, -3 ) === '+**' ||
substr( $path, -3 ) === ' **' ) {
60 $path = substr( $path, 0, -3 );
68 $module = $this->getModuleFromPath( $path );
69 } catch ( ApiUsageException
$ex ) {
70 foreach ( $ex->getStatusValue()->getErrors() as $error ) {
71 $this->addWarning( $error );
75 $submodules = $this->listAllSubmodules( $module, $recursive );
77 $modules = array_merge( $modules, $submodules );
79 $this->addWarning( [ 'apierror-badmodule-nosubmodules', $path ], 'badmodule' );
89 if ( is_array( $params['querymodules'] ) ) {
90 $queryModules = $params['querymodules'];
91 foreach ( $queryModules as $m ) {
92 $modules[] = 'query+' . $m;
98 if ( is_array( $params['formatmodules'] ) ) {
99 $formatModules = $params['formatmodules'];
100 foreach ( $formatModules as $m ) {
107 $modules = array_unique( $modules );
111 foreach ( $modules as $m ) {
113 $module = $this->getModuleFromPath( $m );
114 } catch ( ApiUsageException
$ex ) {
115 foreach ( $ex->getStatusValue()->getErrors() as $error ) {
116 $this->addWarning( $error );
124 if ( $module->getParent() && $module->getParent()->getModuleName() == 'query' &&
125 in_array( $module->getModuleName(), $queryModules )
128 $key = 'querymodules';
130 if ( in_array( $module->getModuleName(), $formatModules ) ) {
131 $key = 'formatmodules';
134 $item = $this->getModuleInfo( $module );
136 $item['querytype'] = $item['group'];
138 $res[$key][] = $item;
141 $result = $this->getResult();
142 $result->addValue( [ $this->getModuleName() ], 'helpformat', $this->helpFormat
);
144 foreach ( $res as $key => $stuff ) {
145 ApiResult
::setIndexedTagName( $res[$key], 'module' );
148 if ( $params['mainmodule'] ) {
149 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
152 if ( $params['pagesetmodule'] ) {
153 $pageSet = new ApiPageSet( $this->getMain()->getModuleManager()->getModule( 'query' ) );
154 $res['pagesetmodule'] = $this->getModuleInfo( $pageSet );
155 unset( $res['pagesetmodule']['name'] );
156 unset( $res['pagesetmodule']['path'] );
157 unset( $res['pagesetmodule']['group'] );
160 $result->addValue( null, $this->getModuleName(), $res );
164 * List all submodules of a module
165 * @param ApiBase $module
166 * @param boolean $recursive
169 private function listAllSubmodules( ApiBase
$module, $recursive ) {
170 $manager = $module->getModuleManager();
173 $names = $manager->getNames();
175 foreach ( $names as $name ) {
176 $submodule = $manager->getModule( $name );
177 $paths[] = $submodule->getModulePath();
178 if ( $recursive && $submodule->getModuleManager() ) {
179 $paths = array_merge( $paths, $this->listAllSubmodules( $submodule, $recursive ) );
187 * @param array $res Result array
188 * @param string $key Result key
189 * @param Message[] $msgs
190 * @param bool $joinLists
192 protected function formatHelpMessages( array &$res, $key, array $msgs, $joinLists = false ) {
193 switch ( $this->helpFormat
) {
199 foreach ( $msgs as $m ) {
200 $ret[] = $m->setContext( $this->context
)->text();
202 $res[$key] = implode( "\n\n", $ret );
204 $res[$key] = preg_replace( '!^(([*#:;])[^\n]*)\n\n(?=\2)!m', "$1\n", $res[$key] );
210 foreach ( $msgs as $m ) {
211 $ret[] = $m->setContext( $this->context
)->parseAsBlock();
213 $ret = implode( "\n", $ret );
215 $ret = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $ret );
217 $res[$key] = Parser
::stripOuterParagraph( $ret );
222 foreach ( $msgs as $m ) {
224 'key' => $m->getKey(),
225 'params' => $m->getParams(),
227 ApiResult
::setIndexedTagName( $a['params'], 'param' );
228 if ( $m instanceof ApiHelpParamValueMessage
) {
229 $a['forvalue'] = $m->getParamValue();
233 ApiResult
::setIndexedTagName( $res[$key], 'msg' );
239 * @param ApiBase $module
242 private function getModuleInfo( $module ) {
244 $path = $module->getModulePath();
246 $ret['name'] = $module->getModuleName();
247 $ret['classname'] = get_class( $module );
248 $ret['path'] = $path;
249 if ( !$module->isMain() ) {
250 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
251 $module->getModuleName()
254 $ret['prefix'] = $module->getModulePrefix();
256 $sourceInfo = $module->getModuleSourceInfo();
258 $ret['source'] = $sourceInfo['name'];
259 if ( isset( $sourceInfo['namemsg'] ) ) {
260 $ret['sourcename'] = $this->context
->msg( $sourceInfo['namemsg'] )->text();
262 $ret['sourcename'] = $ret['source'];
265 $link = SpecialPage
::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] )->getFullURL();
266 if ( isset( $sourceInfo['license-name'] ) ) {
267 $ret['licensetag'] = $sourceInfo['license-name'];
268 $ret['licenselink'] = (string)$link;
269 } elseif ( SpecialVersion
::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
270 $ret['licenselink'] = (string)$link;
274 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
276 foreach ( $module->getHelpFlags() as $flag ) {
280 $ret['helpurls'] = (array)$module->getHelpUrls();
281 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
282 $ret['helpurls'] = [];
284 ApiResult
::setIndexedTagName( $ret['helpurls'], 'helpurl' );
286 if ( $this->helpFormat
!== 'none' ) {
287 $ret['examples'] = [];
288 $examples = $module->getExamplesMessages();
289 foreach ( $examples as $qs => $msg ) {
293 $msg = ApiBase
::makeMessage( $msg, $this->context
, [
294 $module->getModulePrefix(),
295 $module->getModuleName(),
296 $module->getModulePath()
298 $this->formatHelpMessages( $item, 'description', [ $msg ] );
299 if ( isset( $item['description'] ) ) {
300 if ( is_array( $item['description'] ) ) {
301 $item['description'] = $item['description'][0];
303 ApiResult
::setSubelementsList( $item, 'description' );
306 $ret['examples'][] = $item;
308 ApiResult
::setIndexedTagName( $ret['examples'], 'example' );
311 $ret['parameters'] = [];
312 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
313 $paramDesc = $module->getFinalParamDescription();
314 foreach ( $params as $name => $settings ) {
315 if ( !is_array( $settings ) ) {
316 $settings = [ ApiBase
::PARAM_DFLT
=> $settings ];
322 if ( isset( $paramDesc[$name] ) ) {
323 $this->formatHelpMessages( $item, 'description', $paramDesc[$name], true );
326 $item['required'] = !empty( $settings[ApiBase
::PARAM_REQUIRED
] );
328 if ( !empty( $settings[ApiBase
::PARAM_DEPRECATED
] ) ) {
329 $item['deprecated'] = true;
332 if ( $name === 'token' && $module->needsToken() ) {
333 $item['tokentype'] = $module->needsToken();
336 if ( !isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
337 $dflt = isset( $settings[ApiBase
::PARAM_DFLT
] )
338 ?
$settings[ApiBase
::PARAM_DFLT
]
340 if ( is_bool( $dflt ) ) {
341 $settings[ApiBase
::PARAM_TYPE
] = 'boolean';
342 } elseif ( is_string( $dflt ) ||
is_null( $dflt ) ) {
343 $settings[ApiBase
::PARAM_TYPE
] = 'string';
344 } elseif ( is_int( $dflt ) ) {
345 $settings[ApiBase
::PARAM_TYPE
] = 'integer';
349 if ( isset( $settings[ApiBase
::PARAM_DFLT
] ) ) {
350 switch ( $settings[ApiBase
::PARAM_TYPE
] ) {
352 $item['default'] = (bool)$settings[ApiBase
::PARAM_DFLT
];
357 $item['default'] = strval( $settings[ApiBase
::PARAM_DFLT
] );
361 $item['default'] = intval( $settings[ApiBase
::PARAM_DFLT
] );
364 $item['default'] = wfTimestamp( TS_ISO_8601
, $settings[ApiBase
::PARAM_DFLT
] );
367 $item['default'] = $settings[ApiBase
::PARAM_DFLT
];
372 $item['multi'] = !empty( $settings[ApiBase
::PARAM_ISMULTI
] );
373 if ( $item['multi'] ) {
374 $item['limit'] = $this->getMain()->canApiHighLimits() ?
375 ApiBase
::LIMIT_SML2
:
377 $item['lowlimit'] = ApiBase
::LIMIT_SML1
;
378 $item['highlimit'] = ApiBase
::LIMIT_SML2
;
381 if ( !empty( $settings[ApiBase
::PARAM_ALLOW_DUPLICATES
] ) ) {
382 $item['allowsduplicates'] = true;
385 if ( isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
386 if ( $settings[ApiBase
::PARAM_TYPE
] === 'submodule' ) {
387 if ( isset( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] ) ) {
388 ksort( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] );
389 $item['type'] = array_keys( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] );
390 $item['submodules'] = $settings[ApiBase
::PARAM_SUBMODULE_MAP
];
392 $item['type'] = $module->getModuleManager()->getNames( $name );
393 sort( $item['type'] );
394 $prefix = $module->isMain()
395 ?
'' : ( $module->getModulePath() . '+' );
396 $item['submodules'] = [];
397 foreach ( $item['type'] as $v ) {
398 $item['submodules'][$v] = $prefix . $v;
401 if ( isset( $settings[ApiBase
::PARAM_SUBMODULE_PARAM_PREFIX
] ) ) {
402 $item['submoduleparamprefix'] = $settings[ApiBase
::PARAM_SUBMODULE_PARAM_PREFIX
];
404 } elseif ( $settings[ApiBase
::PARAM_TYPE
] === 'tags' ) {
405 $item['type'] = ChangeTags
::listExplicitlyDefinedTags();
407 $item['type'] = $settings[ApiBase
::PARAM_TYPE
];
409 if ( is_array( $item['type'] ) ) {
410 // To prevent sparse arrays from being serialized to JSON as objects
411 $item['type'] = array_values( $item['type'] );
412 ApiResult
::setIndexedTagName( $item['type'], 't' );
415 // Add 'allspecifier' if applicable
416 if ( $item['type'] === 'namespace' ) {
418 $allSpecifier = ApiBase
::ALL_DEFAULT_STRING
;
420 $allowAll = isset( $settings[ApiBase
::PARAM_ALL
] )
421 ?
$settings[ApiBase
::PARAM_ALL
]
423 $allSpecifier = ( is_string( $allowAll ) ?
$allowAll : ApiBase
::ALL_DEFAULT_STRING
);
425 if ( $allowAll && $item['multi'] &&
426 ( is_array( $item['type'] ) ||
$item['type'] === 'namespace' ) ) {
427 $item['allspecifier'] = $allSpecifier;
430 if ( $item['type'] === 'namespace' &&
431 isset( $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] ) &&
432 is_array( $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] )
434 $item['extranamespaces'] = $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
];
435 ApiResult
::setArrayType( $item['extranamespaces'], 'array' );
436 ApiResult
::setIndexedTagName( $item['extranamespaces'], 'ns' );
439 if ( isset( $settings[ApiBase
::PARAM_MAX
] ) ) {
440 $item['max'] = $settings[ApiBase
::PARAM_MAX
];
442 if ( isset( $settings[ApiBase
::PARAM_MAX2
] ) ) {
443 $item['highmax'] = $settings[ApiBase
::PARAM_MAX2
];
445 if ( isset( $settings[ApiBase
::PARAM_MIN
] ) ) {
446 $item['min'] = $settings[ApiBase
::PARAM_MIN
];
448 if ( !empty( $settings[ApiBase
::PARAM_RANGE_ENFORCE
] ) ) {
449 $item['enforcerange'] = true;
452 if ( !empty( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] ) ) {
454 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] as $i ) {
455 $tag = array_shift( $i );
460 $info['values'] = $i;
461 ApiResult
::setIndexedTagName( $info['values'], 'v' );
463 $this->formatHelpMessages( $info, 'text', [
464 $this->context
->msg( "apihelp-{$path}-paraminfo-{$tag}" )
465 ->numParams( count( $i ) )
466 ->params( $this->context
->getLanguage()->commaList( $i ) )
467 ->params( $module->getModulePrefix() )
469 ApiResult
::setSubelementsList( $info, 'text' );
470 $item['info'][] = $info;
472 ApiResult
::setIndexedTagName( $item['info'], 'i' );
475 $ret['parameters'][] = $item;
477 ApiResult
::setIndexedTagName( $ret['parameters'], 'param' );
479 $dynamicParams = $module->dynamicParameterDocumentation();
480 if ( $dynamicParams !== null ) {
481 if ( $this->helpFormat
=== 'none' ) {
482 $ret['dynamicparameters'] = true;
484 $dynamicParams = ApiBase
::makeMessage( $dynamicParams, $this->context
, [
485 $module->getModulePrefix(),
486 $module->getModuleName(),
487 $module->getModulePath()
489 $this->formatHelpMessages( $ret, 'dynamicparameters', [ $dynamicParams ] );
496 public function isReadMode() {
500 public function getAllowedParams() {
502 $querymodules = $this->getMain()->getModuleManager()
503 ->getModule( 'query' )->getModuleManager()->getNames();
504 sort( $querymodules );
505 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
506 sort( $formatmodules );
510 ApiBase
::PARAM_ISMULTI
=> true,
513 ApiBase
::PARAM_DFLT
=> 'none',
514 ApiBase
::PARAM_TYPE
=> [ 'html', 'wikitext', 'raw', 'none' ],
518 ApiBase
::PARAM_DEPRECATED
=> true,
519 ApiBase
::PARAM_ISMULTI
=> true,
520 ApiBase
::PARAM_TYPE
=> $querymodules,
523 ApiBase
::PARAM_DEPRECATED
=> true,
526 ApiBase
::PARAM_DEPRECATED
=> true,
529 ApiBase
::PARAM_DEPRECATED
=> true,
530 ApiBase
::PARAM_ISMULTI
=> true,
531 ApiBase
::PARAM_TYPE
=> $formatmodules,
536 protected function getExamplesMessages() {
538 'action=paraminfo&modules=parse|phpfm|query%2Ballpages|query%2Bsiteinfo'
539 => 'apihelp-paraminfo-example-1',
540 'action=paraminfo&modules=query%2B*'
541 => 'apihelp-paraminfo-example-2',
545 public function getHelpUrls() {
546 return 'https://www.mediawiki.org/wiki/API:Parameter_information';