API: Split list=deletedrevs into prop=deletedrevisions and list=alldeletedrevisions
[lhc/web/wiklou.git] / includes / api / ApiParamInfo.php
1 <?php
2 /**
3 *
4 *
5 * Created on Dec 01, 2007
6 *
7 * Copyright © 2008 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * @ingroup API
29 */
30 class ApiParamInfo extends ApiBase {
31
32 private $helpFormat;
33 private $context;
34
35 public function __construct( ApiMain $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 public function execute() {
40 global $wgContLang;
41
42 // Get parameters
43 $params = $this->extractRequestParams();
44
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() );
49
50 if ( is_array( $params['modules'] ) ) {
51 $modules = $params['modules'];
52 } else {
53 $modules = array();
54 }
55
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;
61 }
62 } else {
63 $queryModules = array();
64 }
65
66 if ( is_array( $params['formatmodules'] ) ) {
67 $this->logFeatureUsage( 'action=paraminfo&formatmodules' );
68 $formatModules = $params['formatmodules'];
69 foreach ( $formatModules as $m ) {
70 $modules[] = $m;
71 }
72 } else {
73 $formatModules = array();
74 }
75
76 $res = array();
77
78 foreach ( $modules as $m ) {
79 try {
80 $module = $this->getModuleFromPath( $m );
81 } catch ( UsageException $ex ) {
82 $this->setWarning( $ex->getMessage() );
83 continue;
84 }
85 $key = 'modules';
86
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';
94 }
95 if ( in_array( $module->getModuleName(), $formatModules ) ) {
96 $key = 'formatmodules';
97 }
98
99 $item = $this->getModuleInfo( $module );
100 if ( $isBCQuery ) {
101 $item['querytype'] = $item['group'];
102 }
103 $res[$key][] = $item;
104 }
105
106 $result = $this->getResult();
107 $result->addValue( array( $this->getModuleName() ), 'helpformat', $this->helpFormat );
108
109 foreach ( $res as $key => $stuff ) {
110 $result->setIndexedTagName( $res[$key], 'module' );
111 }
112
113 if ( $params['mainmodule'] ) {
114 $this->logFeatureUsage( 'action=paraminfo&mainmodule' );
115 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
116 }
117
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'] );
125 }
126
127 $result->addValue( null, $this->getModuleName(), $res );
128 }
129
130 /**
131 * @param array $res Result array
132 * @param string $key Result key
133 * @param Message[] $msgs
134 */
135 protected function formatHelpMessages( array &$res, $key, array $msgs ) {
136 switch ( $this->helpFormat ) {
137 case 'none':
138 break;
139
140 case 'wikitext':
141 $ret = array();
142 foreach ( $msgs as $m ) {
143 $ret[] = $m->setContext( $this->context )->text();
144 }
145 $res[$key] = join( "\n\n", $ret );
146 break;
147
148 case 'html':
149 $ret = array();
150 foreach ( $msgs as $m ) {
151 $ret[] = $m->setContext( $this->context )->parseAsBlock();
152 }
153 $res[$key] = join( "\n", $ret );
154 break;
155
156 case 'raw':
157 $res[$key] = array();
158 foreach ( $msgs as $m ) {
159 $res[$key][] = array(
160 'key' => $m->getKey(),
161 'params' => $m->getParams(),
162 );
163 }
164 $this->getResult()->setIndexedTagName( $res[$key], 'msg' );
165 break;
166 }
167 }
168
169 /**
170 * @param ApiBase $module
171 * @return ApiResult
172 */
173 private function getModuleInfo( $module ) {
174 $result = $this->getResult();
175 $ret = array();
176 $path = $module->getModulePath();
177
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()
184 );
185 }
186 $ret['prefix'] = $module->getModulePrefix();
187
188 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
189
190 foreach ( $module->getHelpFlags() as $flag ) {
191 $ret[$flag] = '';
192 }
193
194 $ret['helpurls'] = (array)$module->getHelpUrls();
195 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
196 $ret['helpurls'] = array();
197 }
198 $result->setIndexedTagName( $ret['helpurls'], 'helpurl' );
199
200 if ( $this->helpFormat !== 'none' ) {
201 $ret['examples'] = array();
202 $examples = $module->getExamplesMessages();
203 foreach ( $examples as $qs => $msg ) {
204 $item = array(
205 'query' => $qs
206 );
207 $msg = ApiBase::makeMessage( $msg, $this->context, array(
208 $module->getModulePrefix(),
209 $module->getModuleName(),
210 $module->getModulePath()
211 ) );
212 $this->formatHelpMessages( $item, 'description', array( $msg ) );
213 if ( isset( $item['description'] ) ) {
214 if ( is_array( $item['description'] ) ) {
215 $item['description'] = $item['description'][0];
216 } else {
217 $result->setSubelements( $item, 'description' );
218 }
219 }
220 $ret['examples'][] = $item;
221 }
222 $result->setIndexedTagName( $ret['examples'], 'example' );
223 }
224
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 );
231 }
232
233 $item = array(
234 'name' => $name
235 );
236 if ( isset( $paramDesc[$name] ) ) {
237 $this->formatHelpMessages( $item, 'description', $paramDesc[$name] );
238 }
239
240 if ( !empty( $settings[ApiBase::PARAM_REQUIRED] ) ) {
241 $item['required'] = '';
242 }
243
244 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
245 $item['deprecated'] = '';
246 }
247
248 if ( $name === 'token' && $module->needsToken() ) {
249 $item['tokentype'] = $module->needsToken();
250 }
251
252 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
253 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
254 ? $settings[ApiBase::PARAM_DFLT]
255 : null;
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';
262 }
263 }
264
265 if ( isset( $settings[ApiBase::PARAM_DFLT] ) ) {
266 switch ( $settings[ApiBase::PARAM_TYPE] ) {
267 case 'boolean':
268 $item['default'] = ( $settings[ApiBase::PARAM_DFLT] ? 'true' : 'false' );
269 break;
270 case 'string':
271 $item['default'] = strval( $settings[ApiBase::PARAM_DFLT] );
272 break;
273 case 'integer':
274 $item['default'] = intval( $settings[ApiBase::PARAM_DFLT] );
275 break;
276 default:
277 $item['default'] = $settings[ApiBase::PARAM_DFLT];
278 break;
279 }
280 }
281
282 if ( !empty( $settings[ApiBase::PARAM_ISMULTI] ) ) {
283 $item['multi'] = '';
284 $item['limit'] = $this->getMain()->canApiHighLimits() ?
285 ApiBase::LIMIT_SML2 :
286 ApiBase::LIMIT_SML1;
287 $item['lowlimit'] = ApiBase::LIMIT_SML1;
288 $item['highlimit'] = ApiBase::LIMIT_SML2;
289 }
290
291 if ( !empty( $settings[ApiBase::PARAM_ALLOW_DUPLICATES] ) ) {
292 $item['allowsduplicates'] = '';
293 }
294
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'] = '';
300 } else {
301 $item['type'] = $settings[ApiBase::PARAM_TYPE];
302 }
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' );
307 }
308 }
309 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
310 $item['max'] = $settings[ApiBase::PARAM_MAX];
311 }
312 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
313 $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
314 }
315 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
316 $item['min'] = $settings[ApiBase::PARAM_MIN];
317 }
318
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 );
323 $info = array(
324 'name' => $tag,
325 );
326 if ( count( $i ) ) {
327 $info['values'] = $i;
328 $result->setIndexedTagName( $info['values'], 'v' );
329 }
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 ) )
334 ->params( $module->getModulePrefix() )
335 ) );
336 $result->setSubelements( $info, 'text' );
337 $item['info'][] = $info;
338 }
339 $result->setIndexedTagName( $item['info'], 'i' );
340 }
341
342 $ret['parameters'][] = $item;
343 }
344 $result->setIndexedTagName( $ret['parameters'], 'param' );
345
346 return $ret;
347 }
348
349 public function isReadMode() {
350 return false;
351 }
352
353 public function getAllowedParams() {
354 // back compat
355 $querymodules = $this->getMain()->getModuleManager()
356 ->getModule( 'query' )->getModuleManager()->getNames();
357 sort( $querymodules );
358 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
359 sort( $formatmodules );
360
361 return array(
362 'modules' => array(
363 ApiBase::PARAM_ISMULTI => true,
364 ),
365 'helpformat' => array(
366 ApiBase::PARAM_DFLT => 'none',
367 ApiBase::PARAM_TYPE => array( 'html', 'wikitext', 'raw', 'none' ),
368 ),
369
370 'querymodules' => array(
371 ApiBase::PARAM_DEPRECATED => true,
372 ApiBase::PARAM_ISMULTI => true,
373 ApiBase::PARAM_TYPE => $querymodules,
374 ),
375 'mainmodule' => array(
376 ApiBase::PARAM_DEPRECATED => true,
377 ),
378 'pagesetmodule' => array(
379 ApiBase::PARAM_DEPRECATED => true,
380 ),
381 'formatmodules' => array(
382 ApiBase::PARAM_DEPRECATED => true,
383 ApiBase::PARAM_ISMULTI => true,
384 ApiBase::PARAM_TYPE => $formatmodules,
385 )
386 );
387 }
388
389 protected function getExamplesMessages() {
390 return array(
391 'action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo'
392 => 'apihelp-paraminfo-example-1',
393 );
394 }
395
396 public function getHelpUrls() {
397 return 'https://www.mediawiki.org/wiki/API:Parameter_information';
398 }
399 }