Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiParamInfo.php
blobb74d52866f8c4666737f088c8b999f6732ec22bf
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 // Get parameters
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'];
50 } else {
51 $modules = array();
54 if ( is_array( $params['querymodules'] ) ) {
55 $this->logFeatureUsage( 'action=paraminfo&querymodules' );
56 $queryModules = $params['querymodules'];
57 foreach ( $queryModules as $m ) {
58 $modules[] = 'query+' . $m;
60 } else {
61 $queryModules = array();
64 if ( is_array( $params['formatmodules'] ) ) {
65 $this->logFeatureUsage( 'action=paraminfo&formatmodules' );
66 $formatModules = $params['formatmodules'];
67 foreach ( $formatModules as $m ) {
68 $modules[] = $m;
70 } else {
71 $formatModules = array();
74 $res = array();
76 foreach ( $modules as $m ) {
77 try {
78 $module = $this->getModuleFromPath( $m );
79 } catch ( UsageException $ex ) {
80 $this->setWarning( $ex->getMessage() );
81 continue;
83 $key = 'modules';
85 // Back compat
86 $isBCQuery = false;
87 if ( $module->getParent() && $module->getParent()->getModuleName() == 'query' &&
88 in_array( $module->getModuleName(), $queryModules )
89 ) {
90 $isBCQuery = true;
91 $key = 'querymodules';
93 if ( in_array( $module->getModuleName(), $formatModules ) ) {
94 $key = 'formatmodules';
97 $item = $this->getModuleInfo( $module );
98 if ( $isBCQuery ) {
99 $item['querytype'] = $item['group'];
101 $res[$key][] = $item;
104 $result = $this->getResult();
105 $result->addValue( array( $this->getModuleName() ), 'helpformat', $this->helpFormat );
107 foreach ( $res as $key => $stuff ) {
108 $result->setIndexedTagName( $res[$key], 'module' );
111 if ( $params['mainmodule'] ) {
112 $this->logFeatureUsage( 'action=paraminfo&mainmodule' );
113 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
116 if ( $params['pagesetmodule'] ) {
117 $this->logFeatureUsage( 'action=paraminfo&pagesetmodule' );
118 $pageSet = new ApiPageSet( $this->getMain()->getModuleManager()->getModule( 'query' ) );
119 $res['pagesetmodule'] = $this->getModuleInfo( $pageSet );
120 unset( $res['pagesetmodule']['name'] );
121 unset( $res['pagesetmodule']['path'] );
122 unset( $res['pagesetmodule']['group'] );
125 $result->addValue( null, $this->getModuleName(), $res );
129 * @param array $res Result array
130 * @param string $key Result key
131 * @param Message[] $msgs
132 * @param bool $joinLists
134 protected function formatHelpMessages( array &$res, $key, array $msgs, $joinLists = false ) {
135 switch ( $this->helpFormat ) {
136 case 'none':
137 break;
139 case 'wikitext':
140 $ret = array();
141 foreach ( $msgs as $m ) {
142 $ret[] = $m->setContext( $this->context )->text();
144 $res[$key] = join( "\n\n", $ret );
145 if ( $joinLists ) {
146 $res[$key] = preg_replace( '!^(([*#:;])[^\n]*)\n\n(?=\2)!m', "$1\n", $res[$key] );
148 break;
150 case 'html':
151 $ret = array();
152 foreach ( $msgs as $m ) {
153 $ret[] = $m->setContext( $this->context )->parseAsBlock();
155 $ret = join( "\n", $ret );
156 if ( $joinLists ) {
157 $ret = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $ret );
159 $res[$key] = $ret;
160 break;
162 case 'raw':
163 $res[$key] = array();
164 foreach ( $msgs as $m ) {
165 $a = array(
166 'key' => $m->getKey(),
167 'params' => $m->getParams(),
169 if ( $m instanceof ApiHelpParamValueMessage ) {
170 $a['forvalue'] = $m->getParamValue();
172 $res[$key][] = $a;
174 $this->getResult()->setIndexedTagName( $res[$key], 'msg' );
175 break;
180 * @param ApiBase $module
181 * @return ApiResult
183 private function getModuleInfo( $module ) {
184 $result = $this->getResult();
185 $ret = array();
186 $path = $module->getModulePath();
188 $ret['name'] = $module->getModuleName();
189 $ret['classname'] = get_class( $module );
190 $ret['path'] = $path;
191 if ( !$module->isMain() ) {
192 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
193 $module->getModuleName()
196 $ret['prefix'] = $module->getModulePrefix();
198 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
200 foreach ( $module->getHelpFlags() as $flag ) {
201 $ret[$flag] = '';
204 $ret['helpurls'] = (array)$module->getHelpUrls();
205 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
206 $ret['helpurls'] = array();
208 $result->setIndexedTagName( $ret['helpurls'], 'helpurl' );
210 if ( $this->helpFormat !== 'none' ) {
211 $ret['examples'] = array();
212 $examples = $module->getExamplesMessages();
213 foreach ( $examples as $qs => $msg ) {
214 $item = array(
215 'query' => $qs
217 $msg = ApiBase::makeMessage( $msg, $this->context, array(
218 $module->getModulePrefix(),
219 $module->getModuleName(),
220 $module->getModulePath()
221 ) );
222 $this->formatHelpMessages( $item, 'description', array( $msg ) );
223 if ( isset( $item['description'] ) ) {
224 if ( is_array( $item['description'] ) ) {
225 $item['description'] = $item['description'][0];
226 } else {
227 $result->setSubelements( $item, 'description' );
230 $ret['examples'][] = $item;
232 $result->setIndexedTagName( $ret['examples'], 'example' );
235 $ret['parameters'] = array();
236 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
237 $paramDesc = $module->getFinalParamDescription();
238 foreach ( $params as $name => $settings ) {
239 if ( !is_array( $settings ) ) {
240 $settings = array( ApiBase::PARAM_DFLT => $settings );
243 $item = array(
244 'name' => $name
246 if ( isset( $paramDesc[$name] ) ) {
247 $this->formatHelpMessages( $item, 'description', $paramDesc[$name], true );
250 if ( !empty( $settings[ApiBase::PARAM_REQUIRED] ) ) {
251 $item['required'] = '';
254 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
255 $item['deprecated'] = '';
258 if ( $name === 'token' && $module->needsToken() ) {
259 $item['tokentype'] = $module->needsToken();
262 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
263 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
264 ? $settings[ApiBase::PARAM_DFLT]
265 : null;
266 if ( is_bool( $dflt ) ) {
267 $settings[ApiBase::PARAM_TYPE] = 'boolean';
268 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
269 $settings[ApiBase::PARAM_TYPE] = 'string';
270 } elseif ( is_int( $dflt ) ) {
271 $settings[ApiBase::PARAM_TYPE] = 'integer';
275 if ( isset( $settings[ApiBase::PARAM_DFLT] ) ) {
276 switch ( $settings[ApiBase::PARAM_TYPE] ) {
277 case 'boolean':
278 $item['default'] = ( $settings[ApiBase::PARAM_DFLT] ? 'true' : 'false' );
279 break;
280 case 'string':
281 $item['default'] = strval( $settings[ApiBase::PARAM_DFLT] );
282 break;
283 case 'integer':
284 $item['default'] = intval( $settings[ApiBase::PARAM_DFLT] );
285 break;
286 default:
287 $item['default'] = $settings[ApiBase::PARAM_DFLT];
288 break;
292 if ( !empty( $settings[ApiBase::PARAM_ISMULTI] ) ) {
293 $item['multi'] = '';
294 $item['limit'] = $this->getMain()->canApiHighLimits() ?
295 ApiBase::LIMIT_SML2 :
296 ApiBase::LIMIT_SML1;
297 $item['lowlimit'] = ApiBase::LIMIT_SML1;
298 $item['highlimit'] = ApiBase::LIMIT_SML2;
301 if ( !empty( $settings[ApiBase::PARAM_ALLOW_DUPLICATES] ) ) {
302 $item['allowsduplicates'] = '';
305 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
306 if ( $settings[ApiBase::PARAM_TYPE] === 'submodule' ) {
307 $item['type'] = $module->getModuleManager()->getNames( $name );
308 sort( $item['type'] );
309 $item['submodules'] = '';
310 } else {
311 $item['type'] = $settings[ApiBase::PARAM_TYPE];
313 if ( is_array( $item['type'] ) ) {
314 // To prevent sparse arrays from being serialized to JSON as objects
315 $item['type'] = array_values( $item['type'] );
316 $result->setIndexedTagName( $item['type'], 't' );
319 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
320 $item['max'] = $settings[ApiBase::PARAM_MAX];
322 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
323 $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
325 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
326 $item['min'] = $settings[ApiBase::PARAM_MIN];
329 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
330 $item['info'] = array();
331 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
332 $tag = array_shift( $i );
333 $info = array(
334 'name' => $tag,
336 if ( count( $i ) ) {
337 $info['values'] = $i;
338 $result->setIndexedTagName( $info['values'], 'v' );
340 $this->formatHelpMessages( $info, 'text', array(
341 $this->context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
342 ->numParams( count( $i ) )
343 ->params( $this->context->getLanguage()->commaList( $i ) )
344 ->params( $module->getModulePrefix() )
345 ) );
346 $result->setSubelements( $info, 'text' );
347 $item['info'][] = $info;
349 $result->setIndexedTagName( $item['info'], 'i' );
352 $ret['parameters'][] = $item;
354 $result->setIndexedTagName( $ret['parameters'], 'param' );
356 return $ret;
359 public function isReadMode() {
360 return false;
363 public function getAllowedParams() {
364 // back compat
365 $querymodules = $this->getMain()->getModuleManager()
366 ->getModule( 'query' )->getModuleManager()->getNames();
367 sort( $querymodules );
368 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
369 sort( $formatmodules );
371 return array(
372 'modules' => array(
373 ApiBase::PARAM_ISMULTI => true,
375 'helpformat' => array(
376 ApiBase::PARAM_DFLT => 'none',
377 ApiBase::PARAM_TYPE => array( 'html', 'wikitext', 'raw', 'none' ),
380 'querymodules' => array(
381 ApiBase::PARAM_DEPRECATED => true,
382 ApiBase::PARAM_ISMULTI => true,
383 ApiBase::PARAM_TYPE => $querymodules,
385 'mainmodule' => array(
386 ApiBase::PARAM_DEPRECATED => true,
388 'pagesetmodule' => array(
389 ApiBase::PARAM_DEPRECATED => true,
391 'formatmodules' => array(
392 ApiBase::PARAM_DEPRECATED => true,
393 ApiBase::PARAM_ISMULTI => true,
394 ApiBase::PARAM_TYPE => $formatmodules,
399 protected function getExamplesMessages() {
400 return array(
401 'action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo'
402 => 'apihelp-paraminfo-example-1',
406 public function getHelpUrls() {
407 return 'https://www.mediawiki.org/wiki/API:Parameter_information';