Merge "Http::getProxy() method to get proxy configuration"
[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 // Get parameters
41 $params = $this->extractRequestParams();
42
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() );
47
48 if ( is_array( $params['modules'] ) ) {
49 $modules = $params['modules'];
50 } else {
51 $modules = [];
52 }
53
54 if ( is_array( $params['querymodules'] ) ) {
55 $queryModules = $params['querymodules'];
56 foreach ( $queryModules as $m ) {
57 $modules[] = 'query+' . $m;
58 }
59 } else {
60 $queryModules = [];
61 }
62
63 if ( is_array( $params['formatmodules'] ) ) {
64 $formatModules = $params['formatmodules'];
65 foreach ( $formatModules as $m ) {
66 $modules[] = $m;
67 }
68 } else {
69 $formatModules = [];
70 }
71
72 $res = [];
73
74 foreach ( $modules as $m ) {
75 try {
76 $module = $this->getModuleFromPath( $m );
77 } catch ( UsageException $ex ) {
78 $this->setWarning( $ex->getMessage() );
79 continue;
80 }
81 $key = 'modules';
82
83 // Back compat
84 $isBCQuery = false;
85 if ( $module->getParent() && $module->getParent()->getModuleName() == 'query' &&
86 in_array( $module->getModuleName(), $queryModules )
87 ) {
88 $isBCQuery = true;
89 $key = 'querymodules';
90 }
91 if ( in_array( $module->getModuleName(), $formatModules ) ) {
92 $key = 'formatmodules';
93 }
94
95 $item = $this->getModuleInfo( $module );
96 if ( $isBCQuery ) {
97 $item['querytype'] = $item['group'];
98 }
99 $res[$key][] = $item;
100 }
101
102 $result = $this->getResult();
103 $result->addValue( [ $this->getModuleName() ], 'helpformat', $this->helpFormat );
104
105 foreach ( $res as $key => $stuff ) {
106 ApiResult::setIndexedTagName( $res[$key], 'module' );
107 }
108
109 if ( $params['mainmodule'] ) {
110 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
111 }
112
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'] );
119 }
120
121 $result->addValue( null, $this->getModuleName(), $res );
122 }
123
124 /**
125 * @param array $res Result array
126 * @param string $key Result key
127 * @param Message[] $msgs
128 * @param bool $joinLists
129 */
130 protected function formatHelpMessages( array &$res, $key, array $msgs, $joinLists = false ) {
131 switch ( $this->helpFormat ) {
132 case 'none':
133 break;
134
135 case 'wikitext':
136 $ret = [];
137 foreach ( $msgs as $m ) {
138 $ret[] = $m->setContext( $this->context )->text();
139 }
140 $res[$key] = implode( "\n\n", $ret );
141 if ( $joinLists ) {
142 $res[$key] = preg_replace( '!^(([*#:;])[^\n]*)\n\n(?=\2)!m', "$1\n", $res[$key] );
143 }
144 break;
145
146 case 'html':
147 $ret = [];
148 foreach ( $msgs as $m ) {
149 $ret[] = $m->setContext( $this->context )->parseAsBlock();
150 }
151 $ret = implode( "\n", $ret );
152 if ( $joinLists ) {
153 $ret = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $ret );
154 }
155 $res[$key] = Parser::stripOuterParagraph( $ret );
156 break;
157
158 case 'raw':
159 $res[$key] = [];
160 foreach ( $msgs as $m ) {
161 $a = [
162 'key' => $m->getKey(),
163 'params' => $m->getParams(),
164 ];
165 if ( $m instanceof ApiHelpParamValueMessage ) {
166 $a['forvalue'] = $m->getParamValue();
167 }
168 $res[$key][] = $a;
169 }
170 ApiResult::setIndexedTagName( $res[$key], 'msg' );
171 break;
172 }
173 }
174
175 /**
176 * @param ApiBase $module
177 * @return ApiResult
178 */
179 private function getModuleInfo( $module ) {
180 $ret = [];
181 $path = $module->getModulePath();
182
183 $ret['name'] = $module->getModuleName();
184 $ret['classname'] = get_class( $module );
185 $ret['path'] = $path;
186 if ( !$module->isMain() ) {
187 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
188 $module->getModuleName()
189 );
190 }
191 $ret['prefix'] = $module->getModulePrefix();
192
193 $sourceInfo = $module->getModuleSourceInfo();
194 if ( $sourceInfo ) {
195 $ret['source'] = $sourceInfo['name'];
196 if ( isset( $sourceInfo['namemsg'] ) ) {
197 $ret['sourcename'] = $this->context->msg( $sourceInfo['namemsg'] )->text();
198 } else {
199 $ret['sourcename'] = $ret['source'];
200 }
201
202 $link = SpecialPage::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] )->getFullURL();
203 if ( isset( $sourceInfo['license-name'] ) ) {
204 $ret['licensetag'] = $sourceInfo['license-name'];
205 $ret['licenselink'] = (string)$link;
206 } elseif ( SpecialVersion::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
207 $ret['licenselink'] = (string)$link;
208 }
209 }
210
211 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
212
213 foreach ( $module->getHelpFlags() as $flag ) {
214 $ret[$flag] = true;
215 }
216
217 $ret['helpurls'] = (array)$module->getHelpUrls();
218 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
219 $ret['helpurls'] = [];
220 }
221 ApiResult::setIndexedTagName( $ret['helpurls'], 'helpurl' );
222
223 if ( $this->helpFormat !== 'none' ) {
224 $ret['examples'] = [];
225 $examples = $module->getExamplesMessages();
226 foreach ( $examples as $qs => $msg ) {
227 $item = [
228 'query' => $qs
229 ];
230 $msg = ApiBase::makeMessage( $msg, $this->context, [
231 $module->getModulePrefix(),
232 $module->getModuleName(),
233 $module->getModulePath()
234 ] );
235 $this->formatHelpMessages( $item, 'description', [ $msg ] );
236 if ( isset( $item['description'] ) ) {
237 if ( is_array( $item['description'] ) ) {
238 $item['description'] = $item['description'][0];
239 } else {
240 ApiResult::setSubelementsList( $item, 'description' );
241 }
242 }
243 $ret['examples'][] = $item;
244 }
245 ApiResult::setIndexedTagName( $ret['examples'], 'example' );
246 }
247
248 $ret['parameters'] = [];
249 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
250 $paramDesc = $module->getFinalParamDescription();
251 foreach ( $params as $name => $settings ) {
252 if ( !is_array( $settings ) ) {
253 $settings = [ ApiBase::PARAM_DFLT => $settings ];
254 }
255
256 $item = [
257 'name' => $name
258 ];
259 if ( isset( $paramDesc[$name] ) ) {
260 $this->formatHelpMessages( $item, 'description', $paramDesc[$name], true );
261 }
262
263 $item['required'] = !empty( $settings[ApiBase::PARAM_REQUIRED] );
264
265 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
266 $item['deprecated'] = true;
267 }
268
269 if ( $name === 'token' && $module->needsToken() ) {
270 $item['tokentype'] = $module->needsToken();
271 }
272
273 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
274 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
275 ? $settings[ApiBase::PARAM_DFLT]
276 : null;
277 if ( is_bool( $dflt ) ) {
278 $settings[ApiBase::PARAM_TYPE] = 'boolean';
279 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
280 $settings[ApiBase::PARAM_TYPE] = 'string';
281 } elseif ( is_int( $dflt ) ) {
282 $settings[ApiBase::PARAM_TYPE] = 'integer';
283 }
284 }
285
286 if ( isset( $settings[ApiBase::PARAM_DFLT] ) ) {
287 switch ( $settings[ApiBase::PARAM_TYPE] ) {
288 case 'boolean':
289 $item['default'] = (bool)$settings[ApiBase::PARAM_DFLT];
290 break;
291 case 'string':
292 case 'text':
293 case 'password':
294 $item['default'] = strval( $settings[ApiBase::PARAM_DFLT] );
295 break;
296 case 'integer':
297 case 'limit':
298 $item['default'] = intval( $settings[ApiBase::PARAM_DFLT] );
299 break;
300 case 'timestamp':
301 $item['default'] = wfTimestamp( TS_ISO_8601, $settings[ApiBase::PARAM_DFLT] );
302 break;
303 default:
304 $item['default'] = $settings[ApiBase::PARAM_DFLT];
305 break;
306 }
307 }
308
309 $item['multi'] = !empty( $settings[ApiBase::PARAM_ISMULTI] );
310 if ( $item['multi'] ) {
311 $item['limit'] = $this->getMain()->canApiHighLimits() ?
312 ApiBase::LIMIT_SML2 :
313 ApiBase::LIMIT_SML1;
314 $item['lowlimit'] = ApiBase::LIMIT_SML1;
315 $item['highlimit'] = ApiBase::LIMIT_SML2;
316 }
317
318 if ( !empty( $settings[ApiBase::PARAM_ALLOW_DUPLICATES] ) ) {
319 $item['allowsduplicates'] = true;
320 }
321
322 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
323 if ( $settings[ApiBase::PARAM_TYPE] === 'submodule' ) {
324 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
325 ksort( $settings[ApiBase::PARAM_SUBMODULE_MAP] );
326 $item['type'] = array_keys( $settings[ApiBase::PARAM_SUBMODULE_MAP] );
327 $item['submodules'] = $settings[ApiBase::PARAM_SUBMODULE_MAP];
328 } else {
329 $item['type'] = $module->getModuleManager()->getNames( $name );
330 sort( $item['type'] );
331 $prefix = $module->isMain()
332 ? '' : ( $module->getModulePath() . '+' );
333 $item['submodules'] = [];
334 foreach ( $item['type'] as $v ) {
335 $item['submodules'][$v] = $prefix . $v;
336 }
337 }
338 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX] ) ) {
339 $item['submoduleparamprefix'] = $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX];
340 }
341 } elseif ( $settings[ApiBase::PARAM_TYPE] === 'tags' ) {
342 $item['type'] = ChangeTags::listExplicitlyDefinedTags();
343 } else {
344 $item['type'] = $settings[ApiBase::PARAM_TYPE];
345 }
346 if ( is_array( $item['type'] ) ) {
347 // To prevent sparse arrays from being serialized to JSON as objects
348 $item['type'] = array_values( $item['type'] );
349 ApiResult::setIndexedTagName( $item['type'], 't' );
350 }
351 }
352 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
353 $item['max'] = $settings[ApiBase::PARAM_MAX];
354 }
355 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
356 $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
357 }
358 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
359 $item['min'] = $settings[ApiBase::PARAM_MIN];
360 }
361 if ( !empty( $settings[ApiBase::PARAM_RANGE_ENFORCE] ) ) {
362 $item['enforcerange'] = true;
363 }
364
365 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
366 $item['info'] = [];
367 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
368 $tag = array_shift( $i );
369 $info = [
370 'name' => $tag,
371 ];
372 if ( count( $i ) ) {
373 $info['values'] = $i;
374 ApiResult::setIndexedTagName( $info['values'], 'v' );
375 }
376 $this->formatHelpMessages( $info, 'text', [
377 $this->context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
378 ->numParams( count( $i ) )
379 ->params( $this->context->getLanguage()->commaList( $i ) )
380 ->params( $module->getModulePrefix() )
381 ] );
382 ApiResult::setSubelementsList( $info, 'text' );
383 $item['info'][] = $info;
384 }
385 ApiResult::setIndexedTagName( $item['info'], 'i' );
386 }
387
388 $ret['parameters'][] = $item;
389 }
390 ApiResult::setIndexedTagName( $ret['parameters'], 'param' );
391
392 $dynamicParams = $module->dynamicParameterDocumentation();
393 if ( $dynamicParams !== null ) {
394 if ( $this->helpFormat === 'none' ) {
395 $ret['dynamicparameters'] = true;
396 } else {
397 $dynamicParams = ApiBase::makeMessage( $dynamicParams, $this->context, [
398 $module->getModulePrefix(),
399 $module->getModuleName(),
400 $module->getModulePath()
401 ] );
402 $this->formatHelpMessages( $ret, 'dynamicparameters', [ $dynamicParams ] );
403 }
404 }
405
406 return $ret;
407 }
408
409 public function isReadMode() {
410 return false;
411 }
412
413 public function getAllowedParams() {
414 // back compat
415 $querymodules = $this->getMain()->getModuleManager()
416 ->getModule( 'query' )->getModuleManager()->getNames();
417 sort( $querymodules );
418 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
419 sort( $formatmodules );
420
421 return [
422 'modules' => [
423 ApiBase::PARAM_ISMULTI => true,
424 ],
425 'helpformat' => [
426 ApiBase::PARAM_DFLT => 'none',
427 ApiBase::PARAM_TYPE => [ 'html', 'wikitext', 'raw', 'none' ],
428 ],
429
430 'querymodules' => [
431 ApiBase::PARAM_DEPRECATED => true,
432 ApiBase::PARAM_ISMULTI => true,
433 ApiBase::PARAM_TYPE => $querymodules,
434 ],
435 'mainmodule' => [
436 ApiBase::PARAM_DEPRECATED => true,
437 ],
438 'pagesetmodule' => [
439 ApiBase::PARAM_DEPRECATED => true,
440 ],
441 'formatmodules' => [
442 ApiBase::PARAM_DEPRECATED => true,
443 ApiBase::PARAM_ISMULTI => true,
444 ApiBase::PARAM_TYPE => $formatmodules,
445 ]
446 ];
447 }
448
449 protected function getExamplesMessages() {
450 return [
451 'action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo'
452 => 'apihelp-paraminfo-example-1',
453 ];
454 }
455
456 public function getHelpUrls() {
457 return 'https://www.mediawiki.org/wiki/API:Parameter_information';
458 }
459 }