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() {
43 $params = $this->extractRequestParams();
45 $this->helpFormat
= $params['helpformat'];
46 $this->context
= new RequestContext
;
47 $this->context
->setUser( new User
); // anon to avoid caching issues
48 $this->context
->setLanguage( $this->getMain()->getLanguage() );
50 if ( is_array( $params['modules'] ) ) {
51 $modules = $params['modules'];
56 if ( is_array( $params['querymodules'] ) ) {
57 $this->logFeatureUsage( 'action=paraminfo&querymodules' );
58 $queryModules = $params['querymodules'];
59 foreach ( $queryModules as $m ) {
60 $modules[] = 'query+' . $m;
63 $queryModules = array();
66 if ( is_array( $params['formatmodules'] ) ) {
67 $this->logFeatureUsage( 'action=paraminfo&formatmodules' );
68 $formatModules = $params['formatmodules'];
69 foreach ( $formatModules as $m ) {
73 $formatModules = array();
78 foreach ( $modules as $m ) {
80 $module = $this->getModuleFromPath( $m );
81 } catch ( UsageException
$ex ) {
82 $this->setWarning( $ex->getMessage() );
89 if ( $module->getParent() && $module->getParent()->getModuleName() == 'query' &&
90 in_array( $module->getModuleName(), $queryModules )
93 $key = 'querymodules';
95 if ( in_array( $module->getModuleName(), $formatModules ) ) {
96 $key = 'formatmodules';
99 $item = $this->getModuleInfo( $module );
101 $item['querytype'] = $item['group'];
103 $res[$key][] = $item;
106 $result = $this->getResult();
107 $result->addValue( array( $this->getModuleName() ), 'helpformat', $this->helpFormat
);
109 foreach ( $res as $key => $stuff ) {
110 $result->setIndexedTagName( $res[$key], 'module' );
113 if ( $params['mainmodule'] ) {
114 $this->logFeatureUsage( 'action=paraminfo&mainmodule' );
115 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
118 if ( $params['pagesetmodule'] ) {
119 $this->logFeatureUsage( 'action=paraminfo&pagesetmodule' );
120 $pageSet = new ApiPageSet( $this->getMain()->getModuleManager()->getModule( 'query' ) );
121 $res['pagesetmodule'] = $this->getModuleInfo( $pageSet );
122 unset( $res['pagesetmodule']['name'] );
123 unset( $res['pagesetmodule']['path'] );
124 unset( $res['pagesetmodule']['group'] );
127 $result->addValue( null, $this->getModuleName(), $res );
131 * @param array $res Result array
132 * @param string $key Result key
133 * @param Message[] $msgs
135 protected function formatHelpMessages( array &$res, $key, array $msgs ) {
136 switch ( $this->helpFormat
) {
142 foreach ( $msgs as $m ) {
143 $ret[] = $m->setContext( $this->context
)->text();
145 $res[$key] = join( "\n\n", $ret );
150 foreach ( $msgs as $m ) {
151 $ret[] = $m->setContext( $this->context
)->parseAsBlock();
153 $res[$key] = join( "\n", $ret );
157 $res[$key] = array();
158 foreach ( $msgs as $m ) {
159 $res[$key][] = array(
160 'key' => $m->getKey(),
161 'params' => $m->getParams(),
164 $this->getResult()->setIndexedTagName( $res[$key], 'msg' );
170 * @param ApiBase $module
173 private function getModuleInfo( $module ) {
174 $result = $this->getResult();
176 $path = $module->getModulePath();
178 $ret['name'] = $module->getModuleName();
179 $ret['classname'] = get_class( $module );
180 $ret['path'] = $path;
181 if ( !$module->isMain() ) {
182 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
183 $module->getModuleName()
186 $ret['prefix'] = $module->getModulePrefix();
188 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
190 foreach ( $module->getHelpFlags() as $flag ) {
194 $ret['helpurls'] = (array)$module->getHelpUrls();
195 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
196 $ret['helpurls'] = array();
198 $result->setIndexedTagName( $ret['helpurls'], 'helpurl' );
200 if ( $this->helpFormat
!== 'none' ) {
201 $ret['examples'] = array();
202 $examples = $module->getExamplesMessages();
203 foreach ( $examples as $qs => $msg ) {
207 $msg = ApiBase
::makeMessage( $msg, $this->context
, array(
208 $module->getModulePrefix(),
209 $module->getModuleName(),
210 $module->getModulePath()
212 $this->formatHelpMessages( $item, 'description', array( $msg ) );
213 if ( isset( $item['description'] ) ) {
214 if ( is_array( $item['description'] ) ) {
215 $item['description'] = $item['description'][0];
217 $result->setSubelements( $item, 'description' );
220 $ret['examples'][] = $item;
222 $result->setIndexedTagName( $ret['examples'], 'example' );
225 $ret['parameters'] = array();
226 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
227 $paramDesc = $module->getFinalParamDescription();
228 foreach ( $params as $name => $settings ) {
229 if ( !is_array( $settings ) ) {
230 $settings = array( ApiBase
::PARAM_DFLT
=> $settings );
236 if ( isset( $paramDesc[$name] ) ) {
237 $this->formatHelpMessages( $item, 'description', $paramDesc[$name] );
240 if ( !empty( $settings[ApiBase
::PARAM_REQUIRED
] ) ) {
241 $item['required'] = '';
244 if ( !empty( $settings[ApiBase
::PARAM_DEPRECATED
] ) ) {
245 $item['deprecated'] = '';
248 if ( $name === 'token' && $module->needsToken() ) {
249 $item['tokentype'] = $module->needsToken();
252 if ( !isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
253 $dflt = isset( $settings[ApiBase
::PARAM_DFLT
] )
254 ?
$settings[ApiBase
::PARAM_DFLT
]
256 if ( is_bool( $dflt ) ) {
257 $settings[ApiBase
::PARAM_TYPE
] = 'boolean';
258 } elseif ( is_string( $dflt ) ||
is_null( $dflt ) ) {
259 $settings[ApiBase
::PARAM_TYPE
] = 'string';
260 } elseif ( is_int( $dflt ) ) {
261 $settings[ApiBase
::PARAM_TYPE
] = 'integer';
265 if ( isset( $settings[ApiBase
::PARAM_DFLT
] ) ) {
266 switch ( $settings[ApiBase
::PARAM_TYPE
] ) {
268 $item['default'] = ( $settings[ApiBase
::PARAM_DFLT
] ?
'true' : 'false' );
271 $item['default'] = strval( $settings[ApiBase
::PARAM_DFLT
] );
274 $item['default'] = intval( $settings[ApiBase
::PARAM_DFLT
] );
277 $item['default'] = $settings[ApiBase
::PARAM_DFLT
];
282 if ( !empty( $settings[ApiBase
::PARAM_ISMULTI
] ) ) {
284 $item['limit'] = $this->getMain()->canApiHighLimits() ?
285 ApiBase
::LIMIT_SML2
:
287 $item['lowlimit'] = ApiBase
::LIMIT_SML1
;
288 $item['highlimit'] = ApiBase
::LIMIT_SML2
;
291 if ( !empty( $settings[ApiBase
::PARAM_ALLOW_DUPLICATES
] ) ) {
292 $item['allowsduplicates'] = '';
295 if ( isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
296 if ( $settings[ApiBase
::PARAM_TYPE
] === 'submodule' ) {
297 $item['type'] = $module->getModuleManager()->getNames( $name );
298 sort( $item['type'] );
299 $item['submodules'] = '';
301 $item['type'] = $settings[ApiBase
::PARAM_TYPE
];
303 if ( is_array( $item['type'] ) ) {
304 // To prevent sparse arrays from being serialized to JSON as objects
305 $item['type'] = array_values( $item['type'] );
306 $result->setIndexedTagName( $item['type'], 't' );
309 if ( isset( $settings[ApiBase
::PARAM_MAX
] ) ) {
310 $item['max'] = $settings[ApiBase
::PARAM_MAX
];
312 if ( isset( $settings[ApiBase
::PARAM_MAX2
] ) ) {
313 $item['highmax'] = $settings[ApiBase
::PARAM_MAX2
];
315 if ( isset( $settings[ApiBase
::PARAM_MIN
] ) ) {
316 $item['min'] = $settings[ApiBase
::PARAM_MIN
];
319 if ( !empty( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] ) ) {
320 $item['info'] = array();
321 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] as $i ) {
322 $tag = array_shift( $i );
327 $info['values'] = $i;
328 $result->setIndexedTagName( $info['values'], 'v' );
330 $this->formatHelpMessages( $info, 'text', array(
331 $this->context
->msg( "apihelp-{$path}-paraminfo-{$tag}" )
332 ->numParams( count( $i ) )
333 ->params( $this->context
->getLanguage()->commaList( $i ) )
335 $result->setSubelements( $info, 'text' );
336 $item['info'][] = $info;
338 $result->setIndexedTagName( $item['info'], 'i' );
341 $ret['parameters'][] = $item;
343 $result->setIndexedTagName( $ret['parameters'], 'param' );
348 public function isReadMode() {
352 public function getAllowedParams() {
354 $querymodules = $this->getMain()->getModuleManager()
355 ->getModule( 'query' )->getModuleManager()->getNames();
356 sort( $querymodules );
357 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
358 sort( $formatmodules );
362 ApiBase
::PARAM_ISMULTI
=> true,
364 'helpformat' => array(
365 ApiBase
::PARAM_DFLT
=> 'none',
366 ApiBase
::PARAM_TYPE
=> array( 'html', 'wikitext', 'raw', 'none' ),
369 'querymodules' => array(
370 ApiBase
::PARAM_DEPRECATED
=> true,
371 ApiBase
::PARAM_ISMULTI
=> true,
372 ApiBase
::PARAM_TYPE
=> $querymodules,
374 'mainmodule' => array(
375 ApiBase
::PARAM_DEPRECATED
=> true,
377 'pagesetmodule' => array(
378 ApiBase
::PARAM_DEPRECATED
=> true,
380 'formatmodules' => array(
381 ApiBase
::PARAM_DEPRECATED
=> true,
382 ApiBase
::PARAM_ISMULTI
=> true,
383 ApiBase
::PARAM_TYPE
=> $formatmodules,
388 protected function getExamplesMessages() {
390 'action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo'
391 => 'apihelp-paraminfo-example-1',
395 public function getHelpUrls() {
396 return 'https://www.mediawiki.org/wiki/API:Parameter_information';