Remove unused variable in ProfilerMwprof
[mediawiki.git] / includes / api / ApiParamInfo.php
blob5965a46f5a02960b21683f15f0074df1ae847c04
1 <?php
2 /**
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
24 * @file
27 /**
28 * @ingroup API
30 class ApiParamInfo extends ApiBase {
32 private $helpFormat;
33 private $context;
35 public function __construct( ApiMain $main, $action ) {
36 parent::__construct( $main, $action );
39 public function execute() {
40 global $wgContLang;
42 // Get parameters
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'];
52 } else {
53 $modules = array();
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;
62 } else {
63 $queryModules = array();
66 if ( is_array( $params['formatmodules'] ) ) {
67 $this->logFeatureUsage( 'action=paraminfo&formatmodules' );
68 $formatModules = $params['formatmodules'];
69 foreach ( $formatModules as $m ) {
70 $modules[] = $m;
72 } else {
73 $formatModules = array();
76 $res = array();
78 foreach ( $modules as $m ) {
79 try {
80 $module = $this->getModuleFromPath( $m );
81 } catch ( UsageException $ex ) {
82 $this->setWarning( $ex->getMessage() );
83 continue;
85 $key = 'modules';
87 // Back compat
88 $isBCQuery = false;
89 if ( $module->getParent() && $module->getParent()->getModuleName() == 'query' &&
90 in_array( $module->getModuleName(), $queryModules )
91 ) {
92 $isBCQuery = true;
93 $key = 'querymodules';
95 if ( in_array( $module->getModuleName(), $formatModules ) ) {
96 $key = 'formatmodules';
99 $item = $this->getModuleInfo( $module );
100 if ( $isBCQuery ) {
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 ) {
137 case 'none':
138 break;
140 case 'wikitext':
141 $ret = array();
142 foreach ( $msgs as $m ) {
143 $ret[] = $m->setContext( $this->context )->text();
145 $res[$key] = join( "\n\n", $ret );
146 break;
148 case 'html':
149 $ret = array();
150 foreach ( $msgs as $m ) {
151 $ret[] = $m->setContext( $this->context )->parseAsBlock();
153 $res[$key] = join( "\n", $ret );
154 break;
156 case 'raw':
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' );
165 break;
170 * @param ApiBase $module
171 * @return ApiResult
173 private function getModuleInfo( $module ) {
174 $result = $this->getResult();
175 $ret = array();
177 $ret['name'] = $module->getModuleName();
178 $ret['classname'] = get_class( $module );
179 $ret['path'] = $module->getModulePath();
180 if ( !$module->isMain() ) {
181 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
182 $module->getModuleName()
185 $ret['prefix'] = $module->getModulePrefix();
187 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
189 foreach ( $module->getHelpFlags() as $flag ) {
190 $ret[$flag] = '';
193 $ret['helpurls'] = (array)$module->getHelpUrls();
194 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
195 $ret['helpurls'] = array();
197 $result->setIndexedTagName( $ret['helpurls'], 'helpurl' );
199 if ( $this->helpFormat !== 'none' ) {
200 $ret['examples'] = array();
201 $examples = $module->getExamplesMessages();
202 foreach ( $examples as $qs => $msg ) {
203 $item = array(
204 'query' => $qs
206 $msg = ApiBase::makeMessage( $msg, $this->context, array(
207 $module->getModulePrefix(),
208 $module->getModuleName(),
209 $module->getModulePath()
210 ) );
211 $this->formatHelpMessages( $item, 'description', array( $msg ) );
212 if ( isset( $item['description'] ) ) {
213 if ( is_array( $item['description'] ) ) {
214 $item['description'] = $item['description'][0];
215 } else {
216 $result->setSubelements( $item, 'description' );
219 $ret['examples'][] = $item;
221 $result->setIndexedTagName( $ret['examples'], 'example' );
224 $ret['parameters'] = array();
225 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
226 $paramDesc = $module->getFinalParamDescription();
227 foreach ( $params as $name => $settings ) {
228 if ( !is_array( $settings ) ) {
229 $settings = array( ApiBase::PARAM_DFLT => $settings );
232 $item = array(
233 'name' => $name
235 if ( isset( $paramDesc[$name] ) ) {
236 $this->formatHelpMessages( $item, 'description', $paramDesc[$name] );
239 if ( !empty( $settings[ApiBase::PARAM_REQUIRED] ) ) {
240 $item['required'] = '';
243 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
244 $item['deprecated'] = '';
247 if ( $name === 'token' && $module->needsToken() ) {
248 $item['tokentype'] = $module->needsToken();
251 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
252 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
253 ? $settings[ApiBase::PARAM_DFLT]
254 : null;
255 if ( is_bool( $dflt ) ) {
256 $settings[ApiBase::PARAM_TYPE] = 'boolean';
257 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
258 $settings[ApiBase::PARAM_TYPE] = 'string';
259 } elseif ( is_int( $dflt ) ) {
260 $settings[ApiBase::PARAM_TYPE] = 'integer';
264 if ( isset( $settings[ApiBase::PARAM_DFLT] ) ) {
265 switch ( $settings[ApiBase::PARAM_TYPE] ) {
266 case 'boolean':
267 $item['default'] = ( $settings[ApiBase::PARAM_DFLT] ? 'true' : 'false' );
268 break;
269 case 'string':
270 $item['default'] = strval( $settings[ApiBase::PARAM_DFLT] );
271 break;
272 case 'integer':
273 $item['default'] = intval( $settings[ApiBase::PARAM_DFLT] );
274 break;
275 default:
276 $item['default'] = $settings[ApiBase::PARAM_DFLT];
277 break;
281 if ( !empty( $settings[ApiBase::PARAM_ISMULTI] ) ) {
282 $item['multi'] = '';
283 $item['limit'] = $this->getMain()->canApiHighLimits() ?
284 ApiBase::LIMIT_SML2 :
285 ApiBase::LIMIT_SML1;
286 $item['lowlimit'] = ApiBase::LIMIT_SML1;
287 $item['highlimit'] = ApiBase::LIMIT_SML2;
290 if ( !empty( $settings[ApiBase::PARAM_ALLOW_DUPLICATES] ) ) {
291 $item['allowsduplicates'] = '';
294 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
295 if ( $settings[ApiBase::PARAM_TYPE] === 'submodule' ) {
296 $item['type'] = $module->getModuleManager()->getNames( $name );
297 sort( $item['type'] );
298 $item['submodules'] = '';
299 } else {
300 $item['type'] = $settings[ApiBase::PARAM_TYPE];
302 if ( is_array( $item['type'] ) ) {
303 // To prevent sparse arrays from being serialized to JSON as objects
304 $item['type'] = array_values( $item['type'] );
305 $result->setIndexedTagName( $item['type'], 't' );
308 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
309 $item['max'] = $settings[ApiBase::PARAM_MAX];
311 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
312 $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
314 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
315 $item['min'] = $settings[ApiBase::PARAM_MIN];
317 $ret['parameters'][] = $item;
319 $result->setIndexedTagName( $ret['parameters'], 'param' );
321 return $ret;
324 public function isReadMode() {
325 return false;
328 public function getAllowedParams() {
329 // back compat
330 $querymodules = $this->getMain()->getModuleManager()
331 ->getModule( 'query' )->getModuleManager()->getNames();
332 sort( $querymodules );
333 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
334 sort( $formatmodules );
336 return array(
337 'modules' => array(
338 ApiBase::PARAM_ISMULTI => true,
340 'helpformat' => array(
341 ApiBase::PARAM_DFLT => 'none',
342 ApiBase::PARAM_TYPE => array( 'html', 'wikitext', 'raw', 'none' ),
345 'querymodules' => array(
346 ApiBase::PARAM_DEPRECATED => true,
347 ApiBase::PARAM_ISMULTI => true,
348 ApiBase::PARAM_TYPE => $querymodules,
350 'mainmodule' => array(
351 ApiBase::PARAM_DEPRECATED => true,
353 'pagesetmodule' => array(
354 ApiBase::PARAM_DEPRECATED => true,
356 'formatmodules' => array(
357 ApiBase::PARAM_DEPRECATED => true,
358 ApiBase::PARAM_ISMULTI => true,
359 ApiBase::PARAM_TYPE => $formatmodules,
364 public function getParamDescription() {
365 return array(
366 'modules' => 'List of module names (values of the action= and format= parameters, or "main"). Can specify submodules with a \'+\'',
367 'helpformat' => 'Format of help strings',
369 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
370 'mainmodule' => 'Get information about the main (top-level) module as well',
371 'pagesetmodule' => 'Get information about the pageset module ' .
372 '(providing titles= and friends) as well',
373 'formatmodules' => 'List of format module names (value of format= parameter)',
377 public function getDescription() {
378 return 'Obtain information about certain API parameters and errors.';
381 public function getExamples() {
382 return array(
383 'api.php?action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo'
387 public function getHelpUrls() {
388 return 'https://www.mediawiki.org/wiki/API:Parameter_information';