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 ( UsageException
$ex ) {
70 $this->setWarning( $ex->getMessage() );
72 $submodules = $this->listAllSubmodules( $module, $recursive );
74 $modules = array_merge( $modules, $submodules );
76 $this->setWarning( "Module $path has no submodules" );
86 if ( is_array( $params['querymodules'] ) ) {
87 $queryModules = $params['querymodules'];
88 foreach ( $queryModules as $m ) {
89 $modules[] = 'query+' . $m;
95 if ( is_array( $params['formatmodules'] ) ) {
96 $formatModules = $params['formatmodules'];
97 foreach ( $formatModules as $m ) {
104 $modules = array_unique( $modules );
108 foreach ( $modules as $m ) {
110 $module = $this->getModuleFromPath( $m );
111 } catch ( UsageException
$ex ) {
112 $this->setWarning( $ex->getMessage() );
119 if ( $module->getParent() && $module->getParent()->getModuleName() == 'query' &&
120 in_array( $module->getModuleName(), $queryModules )
123 $key = 'querymodules';
125 if ( in_array( $module->getModuleName(), $formatModules ) ) {
126 $key = 'formatmodules';
129 $item = $this->getModuleInfo( $module );
131 $item['querytype'] = $item['group'];
133 $res[$key][] = $item;
136 $result = $this->getResult();
137 $result->addValue( [ $this->getModuleName() ], 'helpformat', $this->helpFormat
);
139 foreach ( $res as $key => $stuff ) {
140 ApiResult
::setIndexedTagName( $res[$key], 'module' );
143 if ( $params['mainmodule'] ) {
144 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
147 if ( $params['pagesetmodule'] ) {
148 $pageSet = new ApiPageSet( $this->getMain()->getModuleManager()->getModule( 'query' ) );
149 $res['pagesetmodule'] = $this->getModuleInfo( $pageSet );
150 unset( $res['pagesetmodule']['name'] );
151 unset( $res['pagesetmodule']['path'] );
152 unset( $res['pagesetmodule']['group'] );
155 $result->addValue( null, $this->getModuleName(), $res );
159 * List all submodules of a module
160 * @param ApiBase $module
161 * @param boolean $recursive
164 private function listAllSubmodules( ApiBase
$module, $recursive ) {
165 $manager = $module->getModuleManager();
168 $names = $manager->getNames();
170 foreach ( $names as $name ) {
171 $submodule = $manager->getModule( $name );
172 $paths[] = $submodule->getModulePath();
173 if ( $recursive && $submodule->getModuleManager() ) {
174 $paths = array_merge( $paths, $this->listAllSubmodules( $submodule, $recursive ) );
182 * @param array $res Result array
183 * @param string $key Result key
184 * @param Message[] $msgs
185 * @param bool $joinLists
187 protected function formatHelpMessages( array &$res, $key, array $msgs, $joinLists = false ) {
188 switch ( $this->helpFormat
) {
194 foreach ( $msgs as $m ) {
195 $ret[] = $m->setContext( $this->context
)->text();
197 $res[$key] = implode( "\n\n", $ret );
199 $res[$key] = preg_replace( '!^(([*#:;])[^\n]*)\n\n(?=\2)!m', "$1\n", $res[$key] );
205 foreach ( $msgs as $m ) {
206 $ret[] = $m->setContext( $this->context
)->parseAsBlock();
208 $ret = implode( "\n", $ret );
210 $ret = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $ret );
212 $res[$key] = Parser
::stripOuterParagraph( $ret );
217 foreach ( $msgs as $m ) {
219 'key' => $m->getKey(),
220 'params' => $m->getParams(),
222 ApiResult
::setIndexedTagName( $a['params'], 'param' );
223 if ( $m instanceof ApiHelpParamValueMessage
) {
224 $a['forvalue'] = $m->getParamValue();
228 ApiResult
::setIndexedTagName( $res[$key], 'msg' );
234 * @param ApiBase $module
237 private function getModuleInfo( $module ) {
239 $path = $module->getModulePath();
241 $ret['name'] = $module->getModuleName();
242 $ret['classname'] = get_class( $module );
243 $ret['path'] = $path;
244 if ( !$module->isMain() ) {
245 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
246 $module->getModuleName()
249 $ret['prefix'] = $module->getModulePrefix();
251 $sourceInfo = $module->getModuleSourceInfo();
253 $ret['source'] = $sourceInfo['name'];
254 if ( isset( $sourceInfo['namemsg'] ) ) {
255 $ret['sourcename'] = $this->context
->msg( $sourceInfo['namemsg'] )->text();
257 $ret['sourcename'] = $ret['source'];
260 $link = SpecialPage
::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] )->getFullURL();
261 if ( isset( $sourceInfo['license-name'] ) ) {
262 $ret['licensetag'] = $sourceInfo['license-name'];
263 $ret['licenselink'] = (string)$link;
264 } elseif ( SpecialVersion
::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
265 $ret['licenselink'] = (string)$link;
269 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
271 foreach ( $module->getHelpFlags() as $flag ) {
275 $ret['helpurls'] = (array)$module->getHelpUrls();
276 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
277 $ret['helpurls'] = [];
279 ApiResult
::setIndexedTagName( $ret['helpurls'], 'helpurl' );
281 if ( $this->helpFormat
!== 'none' ) {
282 $ret['examples'] = [];
283 $examples = $module->getExamplesMessages();
284 foreach ( $examples as $qs => $msg ) {
288 $msg = ApiBase
::makeMessage( $msg, $this->context
, [
289 $module->getModulePrefix(),
290 $module->getModuleName(),
291 $module->getModulePath()
293 $this->formatHelpMessages( $item, 'description', [ $msg ] );
294 if ( isset( $item['description'] ) ) {
295 if ( is_array( $item['description'] ) ) {
296 $item['description'] = $item['description'][0];
298 ApiResult
::setSubelementsList( $item, 'description' );
301 $ret['examples'][] = $item;
303 ApiResult
::setIndexedTagName( $ret['examples'], 'example' );
306 $ret['parameters'] = [];
307 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
308 $paramDesc = $module->getFinalParamDescription();
309 foreach ( $params as $name => $settings ) {
310 if ( !is_array( $settings ) ) {
311 $settings = [ ApiBase
::PARAM_DFLT
=> $settings ];
317 if ( isset( $paramDesc[$name] ) ) {
318 $this->formatHelpMessages( $item, 'description', $paramDesc[$name], true );
321 $item['required'] = !empty( $settings[ApiBase
::PARAM_REQUIRED
] );
323 if ( !empty( $settings[ApiBase
::PARAM_DEPRECATED
] ) ) {
324 $item['deprecated'] = true;
327 if ( $name === 'token' && $module->needsToken() ) {
328 $item['tokentype'] = $module->needsToken();
331 if ( !isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
332 $dflt = isset( $settings[ApiBase
::PARAM_DFLT
] )
333 ?
$settings[ApiBase
::PARAM_DFLT
]
335 if ( is_bool( $dflt ) ) {
336 $settings[ApiBase
::PARAM_TYPE
] = 'boolean';
337 } elseif ( is_string( $dflt ) ||
is_null( $dflt ) ) {
338 $settings[ApiBase
::PARAM_TYPE
] = 'string';
339 } elseif ( is_int( $dflt ) ) {
340 $settings[ApiBase
::PARAM_TYPE
] = 'integer';
344 if ( isset( $settings[ApiBase
::PARAM_DFLT
] ) ) {
345 switch ( $settings[ApiBase
::PARAM_TYPE
] ) {
347 $item['default'] = (bool)$settings[ApiBase
::PARAM_DFLT
];
352 $item['default'] = strval( $settings[ApiBase
::PARAM_DFLT
] );
356 $item['default'] = intval( $settings[ApiBase
::PARAM_DFLT
] );
359 $item['default'] = wfTimestamp( TS_ISO_8601
, $settings[ApiBase
::PARAM_DFLT
] );
362 $item['default'] = $settings[ApiBase
::PARAM_DFLT
];
367 $item['multi'] = !empty( $settings[ApiBase
::PARAM_ISMULTI
] );
368 if ( $item['multi'] ) {
369 $item['limit'] = $this->getMain()->canApiHighLimits() ?
370 ApiBase
::LIMIT_SML2
:
372 $item['lowlimit'] = ApiBase
::LIMIT_SML1
;
373 $item['highlimit'] = ApiBase
::LIMIT_SML2
;
376 if ( !empty( $settings[ApiBase
::PARAM_ALLOW_DUPLICATES
] ) ) {
377 $item['allowsduplicates'] = true;
380 if ( isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
381 if ( $settings[ApiBase
::PARAM_TYPE
] === 'submodule' ) {
382 if ( isset( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] ) ) {
383 ksort( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] );
384 $item['type'] = array_keys( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] );
385 $item['submodules'] = $settings[ApiBase
::PARAM_SUBMODULE_MAP
];
387 $item['type'] = $module->getModuleManager()->getNames( $name );
388 sort( $item['type'] );
389 $prefix = $module->isMain()
390 ?
'' : ( $module->getModulePath() . '+' );
391 $item['submodules'] = [];
392 foreach ( $item['type'] as $v ) {
393 $item['submodules'][$v] = $prefix . $v;
396 if ( isset( $settings[ApiBase
::PARAM_SUBMODULE_PARAM_PREFIX
] ) ) {
397 $item['submoduleparamprefix'] = $settings[ApiBase
::PARAM_SUBMODULE_PARAM_PREFIX
];
399 } elseif ( $settings[ApiBase
::PARAM_TYPE
] === 'tags' ) {
400 $item['type'] = ChangeTags
::listExplicitlyDefinedTags();
402 $item['type'] = $settings[ApiBase
::PARAM_TYPE
];
404 if ( is_array( $item['type'] ) ) {
405 // To prevent sparse arrays from being serialized to JSON as objects
406 $item['type'] = array_values( $item['type'] );
407 ApiResult
::setIndexedTagName( $item['type'], 't' );
410 if ( isset( $settings[ApiBase
::PARAM_MAX
] ) ) {
411 $item['max'] = $settings[ApiBase
::PARAM_MAX
];
413 if ( isset( $settings[ApiBase
::PARAM_MAX2
] ) ) {
414 $item['highmax'] = $settings[ApiBase
::PARAM_MAX2
];
416 if ( isset( $settings[ApiBase
::PARAM_MIN
] ) ) {
417 $item['min'] = $settings[ApiBase
::PARAM_MIN
];
419 if ( !empty( $settings[ApiBase
::PARAM_RANGE_ENFORCE
] ) ) {
420 $item['enforcerange'] = true;
423 if ( !empty( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] ) ) {
425 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] as $i ) {
426 $tag = array_shift( $i );
431 $info['values'] = $i;
432 ApiResult
::setIndexedTagName( $info['values'], 'v' );
434 $this->formatHelpMessages( $info, 'text', [
435 $this->context
->msg( "apihelp-{$path}-paraminfo-{$tag}" )
436 ->numParams( count( $i ) )
437 ->params( $this->context
->getLanguage()->commaList( $i ) )
438 ->params( $module->getModulePrefix() )
440 ApiResult
::setSubelementsList( $info, 'text' );
441 $item['info'][] = $info;
443 ApiResult
::setIndexedTagName( $item['info'], 'i' );
446 $ret['parameters'][] = $item;
448 ApiResult
::setIndexedTagName( $ret['parameters'], 'param' );
450 $dynamicParams = $module->dynamicParameterDocumentation();
451 if ( $dynamicParams !== null ) {
452 if ( $this->helpFormat
=== 'none' ) {
453 $ret['dynamicparameters'] = true;
455 $dynamicParams = ApiBase
::makeMessage( $dynamicParams, $this->context
, [
456 $module->getModulePrefix(),
457 $module->getModuleName(),
458 $module->getModulePath()
460 $this->formatHelpMessages( $ret, 'dynamicparameters', [ $dynamicParams ] );
467 public function isReadMode() {
471 public function getAllowedParams() {
473 $querymodules = $this->getMain()->getModuleManager()
474 ->getModule( 'query' )->getModuleManager()->getNames();
475 sort( $querymodules );
476 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
477 sort( $formatmodules );
481 ApiBase
::PARAM_ISMULTI
=> true,
484 ApiBase
::PARAM_DFLT
=> 'none',
485 ApiBase
::PARAM_TYPE
=> [ 'html', 'wikitext', 'raw', 'none' ],
489 ApiBase
::PARAM_DEPRECATED
=> true,
490 ApiBase
::PARAM_ISMULTI
=> true,
491 ApiBase
::PARAM_TYPE
=> $querymodules,
494 ApiBase
::PARAM_DEPRECATED
=> true,
497 ApiBase
::PARAM_DEPRECATED
=> true,
500 ApiBase
::PARAM_DEPRECATED
=> true,
501 ApiBase
::PARAM_ISMULTI
=> true,
502 ApiBase
::PARAM_TYPE
=> $formatmodules,
507 protected function getExamplesMessages() {
509 'action=paraminfo&modules=parse|phpfm|query%2Ballpages|query%2Bsiteinfo'
510 => 'apihelp-paraminfo-example-1',
511 'action=paraminfo&modules=query%2B*'
512 => 'apihelp-paraminfo-example-2',
516 public function getHelpUrls() {
517 return 'https://www.mediawiki.org/wiki/API:Parameter_information';