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'] ) ) {
49 $modules = $params['modules'];
54 if ( is_array( $params['querymodules'] ) ) {
55 $queryModules = $params['querymodules'];
56 foreach ( $queryModules as $m ) {
57 $modules[] = 'query+' . $m;
63 if ( is_array( $params['formatmodules'] ) ) {
64 $formatModules = $params['formatmodules'];
65 foreach ( $formatModules as $m ) {
74 foreach ( $modules as $m ) {
76 $module = $this->getModuleFromPath( $m );
77 } catch ( UsageException
$ex ) {
78 $this->setWarning( $ex->getMessage() );
85 if ( $module->getParent() && $module->getParent()->getModuleName() == 'query' &&
86 in_array( $module->getModuleName(), $queryModules )
89 $key = 'querymodules';
91 if ( in_array( $module->getModuleName(), $formatModules ) ) {
92 $key = 'formatmodules';
95 $item = $this->getModuleInfo( $module );
97 $item['querytype'] = $item['group'];
102 $result = $this->getResult();
103 $result->addValue( [ $this->getModuleName() ], 'helpformat', $this->helpFormat
);
105 foreach ( $res as $key => $stuff ) {
106 ApiResult
::setIndexedTagName( $res[$key], 'module' );
109 if ( $params['mainmodule'] ) {
110 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
113 if ( $params['pagesetmodule'] ) {
114 $pageSet = new ApiPageSet( $this->getMain()->getModuleManager()->getModule( 'query' ) );
115 $res['pagesetmodule'] = $this->getModuleInfo( $pageSet );
116 unset( $res['pagesetmodule']['name'] );
117 unset( $res['pagesetmodule']['path'] );
118 unset( $res['pagesetmodule']['group'] );
121 $result->addValue( null, $this->getModuleName(), $res );
125 * @param array $res Result array
126 * @param string $key Result key
127 * @param Message[] $msgs
128 * @param bool $joinLists
130 protected function formatHelpMessages( array &$res, $key, array $msgs, $joinLists = false ) {
131 switch ( $this->helpFormat
) {
137 foreach ( $msgs as $m ) {
138 $ret[] = $m->setContext( $this->context
)->text();
140 $res[$key] = implode( "\n\n", $ret );
142 $res[$key] = preg_replace( '!^(([*#:;])[^\n]*)\n\n(?=\2)!m', "$1\n", $res[$key] );
148 foreach ( $msgs as $m ) {
149 $ret[] = $m->setContext( $this->context
)->parseAsBlock();
151 $ret = implode( "\n", $ret );
153 $ret = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $ret );
155 $res[$key] = Parser
::stripOuterParagraph( $ret );
160 foreach ( $msgs as $m ) {
162 'key' => $m->getKey(),
163 'params' => $m->getParams(),
165 ApiResult
::setIndexedTagName( $a['params'], 'param' );
166 if ( $m instanceof ApiHelpParamValueMessage
) {
167 $a['forvalue'] = $m->getParamValue();
171 ApiResult
::setIndexedTagName( $res[$key], 'msg' );
177 * @param ApiBase $module
180 private function getModuleInfo( $module ) {
182 $path = $module->getModulePath();
184 $ret['name'] = $module->getModuleName();
185 $ret['classname'] = get_class( $module );
186 $ret['path'] = $path;
187 if ( !$module->isMain() ) {
188 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
189 $module->getModuleName()
192 $ret['prefix'] = $module->getModulePrefix();
194 $sourceInfo = $module->getModuleSourceInfo();
196 $ret['source'] = $sourceInfo['name'];
197 if ( isset( $sourceInfo['namemsg'] ) ) {
198 $ret['sourcename'] = $this->context
->msg( $sourceInfo['namemsg'] )->text();
200 $ret['sourcename'] = $ret['source'];
203 $link = SpecialPage
::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] )->getFullURL();
204 if ( isset( $sourceInfo['license-name'] ) ) {
205 $ret['licensetag'] = $sourceInfo['license-name'];
206 $ret['licenselink'] = (string)$link;
207 } elseif ( SpecialVersion
::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
208 $ret['licenselink'] = (string)$link;
212 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
214 foreach ( $module->getHelpFlags() as $flag ) {
218 $ret['helpurls'] = (array)$module->getHelpUrls();
219 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
220 $ret['helpurls'] = [];
222 ApiResult
::setIndexedTagName( $ret['helpurls'], 'helpurl' );
224 if ( $this->helpFormat
!== 'none' ) {
225 $ret['examples'] = [];
226 $examples = $module->getExamplesMessages();
227 foreach ( $examples as $qs => $msg ) {
231 $msg = ApiBase
::makeMessage( $msg, $this->context
, [
232 $module->getModulePrefix(),
233 $module->getModuleName(),
234 $module->getModulePath()
236 $this->formatHelpMessages( $item, 'description', [ $msg ] );
237 if ( isset( $item['description'] ) ) {
238 if ( is_array( $item['description'] ) ) {
239 $item['description'] = $item['description'][0];
241 ApiResult
::setSubelementsList( $item, 'description' );
244 $ret['examples'][] = $item;
246 ApiResult
::setIndexedTagName( $ret['examples'], 'example' );
249 $ret['parameters'] = [];
250 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
251 $paramDesc = $module->getFinalParamDescription();
252 foreach ( $params as $name => $settings ) {
253 if ( !is_array( $settings ) ) {
254 $settings = [ ApiBase
::PARAM_DFLT
=> $settings ];
260 if ( isset( $paramDesc[$name] ) ) {
261 $this->formatHelpMessages( $item, 'description', $paramDesc[$name], true );
264 $item['required'] = !empty( $settings[ApiBase
::PARAM_REQUIRED
] );
266 if ( !empty( $settings[ApiBase
::PARAM_DEPRECATED
] ) ) {
267 $item['deprecated'] = true;
270 if ( $name === 'token' && $module->needsToken() ) {
271 $item['tokentype'] = $module->needsToken();
274 if ( !isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
275 $dflt = isset( $settings[ApiBase
::PARAM_DFLT
] )
276 ?
$settings[ApiBase
::PARAM_DFLT
]
278 if ( is_bool( $dflt ) ) {
279 $settings[ApiBase
::PARAM_TYPE
] = 'boolean';
280 } elseif ( is_string( $dflt ) ||
is_null( $dflt ) ) {
281 $settings[ApiBase
::PARAM_TYPE
] = 'string';
282 } elseif ( is_int( $dflt ) ) {
283 $settings[ApiBase
::PARAM_TYPE
] = 'integer';
287 if ( isset( $settings[ApiBase
::PARAM_DFLT
] ) ) {
288 switch ( $settings[ApiBase
::PARAM_TYPE
] ) {
290 $item['default'] = (bool)$settings[ApiBase
::PARAM_DFLT
];
295 $item['default'] = strval( $settings[ApiBase
::PARAM_DFLT
] );
299 $item['default'] = intval( $settings[ApiBase
::PARAM_DFLT
] );
302 $item['default'] = wfTimestamp( TS_ISO_8601
, $settings[ApiBase
::PARAM_DFLT
] );
305 $item['default'] = $settings[ApiBase
::PARAM_DFLT
];
310 $item['multi'] = !empty( $settings[ApiBase
::PARAM_ISMULTI
] );
311 if ( $item['multi'] ) {
312 $item['limit'] = $this->getMain()->canApiHighLimits() ?
313 ApiBase
::LIMIT_SML2
:
315 $item['lowlimit'] = ApiBase
::LIMIT_SML1
;
316 $item['highlimit'] = ApiBase
::LIMIT_SML2
;
319 if ( !empty( $settings[ApiBase
::PARAM_ALLOW_DUPLICATES
] ) ) {
320 $item['allowsduplicates'] = true;
323 if ( isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
324 if ( $settings[ApiBase
::PARAM_TYPE
] === 'submodule' ) {
325 if ( isset( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] ) ) {
326 ksort( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] );
327 $item['type'] = array_keys( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] );
328 $item['submodules'] = $settings[ApiBase
::PARAM_SUBMODULE_MAP
];
330 $item['type'] = $module->getModuleManager()->getNames( $name );
331 sort( $item['type'] );
332 $prefix = $module->isMain()
333 ?
'' : ( $module->getModulePath() . '+' );
334 $item['submodules'] = [];
335 foreach ( $item['type'] as $v ) {
336 $item['submodules'][$v] = $prefix . $v;
339 if ( isset( $settings[ApiBase
::PARAM_SUBMODULE_PARAM_PREFIX
] ) ) {
340 $item['submoduleparamprefix'] = $settings[ApiBase
::PARAM_SUBMODULE_PARAM_PREFIX
];
342 } elseif ( $settings[ApiBase
::PARAM_TYPE
] === 'tags' ) {
343 $item['type'] = ChangeTags
::listExplicitlyDefinedTags();
345 $item['type'] = $settings[ApiBase
::PARAM_TYPE
];
347 if ( is_array( $item['type'] ) ) {
348 // To prevent sparse arrays from being serialized to JSON as objects
349 $item['type'] = array_values( $item['type'] );
350 ApiResult
::setIndexedTagName( $item['type'], 't' );
353 if ( isset( $settings[ApiBase
::PARAM_MAX
] ) ) {
354 $item['max'] = $settings[ApiBase
::PARAM_MAX
];
356 if ( isset( $settings[ApiBase
::PARAM_MAX2
] ) ) {
357 $item['highmax'] = $settings[ApiBase
::PARAM_MAX2
];
359 if ( isset( $settings[ApiBase
::PARAM_MIN
] ) ) {
360 $item['min'] = $settings[ApiBase
::PARAM_MIN
];
362 if ( !empty( $settings[ApiBase
::PARAM_RANGE_ENFORCE
] ) ) {
363 $item['enforcerange'] = true;
366 if ( !empty( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] ) ) {
368 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] as $i ) {
369 $tag = array_shift( $i );
374 $info['values'] = $i;
375 ApiResult
::setIndexedTagName( $info['values'], 'v' );
377 $this->formatHelpMessages( $info, 'text', [
378 $this->context
->msg( "apihelp-{$path}-paraminfo-{$tag}" )
379 ->numParams( count( $i ) )
380 ->params( $this->context
->getLanguage()->commaList( $i ) )
381 ->params( $module->getModulePrefix() )
383 ApiResult
::setSubelementsList( $info, 'text' );
384 $item['info'][] = $info;
386 ApiResult
::setIndexedTagName( $item['info'], 'i' );
389 $ret['parameters'][] = $item;
391 ApiResult
::setIndexedTagName( $ret['parameters'], 'param' );
393 $dynamicParams = $module->dynamicParameterDocumentation();
394 if ( $dynamicParams !== null ) {
395 if ( $this->helpFormat
=== 'none' ) {
396 $ret['dynamicparameters'] = true;
398 $dynamicParams = ApiBase
::makeMessage( $dynamicParams, $this->context
, [
399 $module->getModulePrefix(),
400 $module->getModuleName(),
401 $module->getModulePath()
403 $this->formatHelpMessages( $ret, 'dynamicparameters', [ $dynamicParams ] );
410 public function isReadMode() {
414 public function getAllowedParams() {
416 $querymodules = $this->getMain()->getModuleManager()
417 ->getModule( 'query' )->getModuleManager()->getNames();
418 sort( $querymodules );
419 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
420 sort( $formatmodules );
424 ApiBase
::PARAM_ISMULTI
=> true,
427 ApiBase
::PARAM_DFLT
=> 'none',
428 ApiBase
::PARAM_TYPE
=> [ 'html', 'wikitext', 'raw', 'none' ],
432 ApiBase
::PARAM_DEPRECATED
=> true,
433 ApiBase
::PARAM_ISMULTI
=> true,
434 ApiBase
::PARAM_TYPE
=> $querymodules,
437 ApiBase
::PARAM_DEPRECATED
=> true,
440 ApiBase
::PARAM_DEPRECATED
=> true,
443 ApiBase
::PARAM_DEPRECATED
=> true,
444 ApiBase
::PARAM_ISMULTI
=> true,
445 ApiBase
::PARAM_TYPE
=> $formatmodules,
450 protected function getExamplesMessages() {
452 'action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo'
453 => 'apihelp-paraminfo-example-1',
457 public function getHelpUrls() {
458 return 'https://www.mediawiki.org/wiki/API:Parameter_information';