Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiParamInfo.php
blob67983e7a3aa080340cd9551f1dfd2fa5d3d7b1e2
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 = [];
50 foreach ( $params['modules'] as $path ) {
51 if ( $path === '*' || $path === '**' ) {
52 $path = "main+$path";
54 if ( substr( $path, -2 ) === '+*' || substr( $path, -2 ) === ' *' ) {
55 $submodules = true;
56 $path = substr( $path, 0, -2 );
57 $recursive = false;
58 } elseif ( substr( $path, -3 ) === '+**' || substr( $path, -3 ) === ' **' ) {
59 $submodules = true;
60 $path = substr( $path, 0, -3 );
61 $recursive = true;
62 } else {
63 $submodules = false;
66 if ( $submodules ) {
67 try {
68 $module = $this->getModuleFromPath( $path );
69 } catch ( ApiUsageException $ex ) {
70 foreach ( $ex->getStatusValue()->getErrors() as $error ) {
71 $this->addWarning( $error );
73 continue;
75 $submodules = $this->listAllSubmodules( $module, $recursive );
76 if ( $submodules ) {
77 $modules = array_merge( $modules, $submodules );
78 } else {
79 $this->addWarning( [ 'apierror-badmodule-nosubmodules', $path ], 'badmodule' );
81 } else {
82 $modules[] = $path;
85 } else {
86 $modules = [];
89 if ( is_array( $params['querymodules'] ) ) {
90 $queryModules = $params['querymodules'];
91 foreach ( $queryModules as $m ) {
92 $modules[] = 'query+' . $m;
94 } else {
95 $queryModules = [];
98 if ( is_array( $params['formatmodules'] ) ) {
99 $formatModules = $params['formatmodules'];
100 foreach ( $formatModules as $m ) {
101 $modules[] = $m;
103 } else {
104 $formatModules = [];
107 $modules = array_unique( $modules );
109 $res = [];
111 foreach ( $modules as $m ) {
112 try {
113 $module = $this->getModuleFromPath( $m );
114 } catch ( ApiUsageException $ex ) {
115 foreach ( $ex->getStatusValue()->getErrors() as $error ) {
116 $this->addWarning( $error );
118 continue;
120 $key = 'modules';
122 // Back compat
123 $isBCQuery = false;
124 if ( $module->getParent() && $module->getParent()->getModuleName() == 'query' &&
125 in_array( $module->getModuleName(), $queryModules )
127 $isBCQuery = true;
128 $key = 'querymodules';
130 if ( in_array( $module->getModuleName(), $formatModules ) ) {
131 $key = 'formatmodules';
134 $item = $this->getModuleInfo( $module );
135 if ( $isBCQuery ) {
136 $item['querytype'] = $item['group'];
138 $res[$key][] = $item;
141 $result = $this->getResult();
142 $result->addValue( [ $this->getModuleName() ], 'helpformat', $this->helpFormat );
144 foreach ( $res as $key => $stuff ) {
145 ApiResult::setIndexedTagName( $res[$key], 'module' );
148 if ( $params['mainmodule'] ) {
149 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
152 if ( $params['pagesetmodule'] ) {
153 $pageSet = new ApiPageSet( $this->getMain()->getModuleManager()->getModule( 'query' ) );
154 $res['pagesetmodule'] = $this->getModuleInfo( $pageSet );
155 unset( $res['pagesetmodule']['name'] );
156 unset( $res['pagesetmodule']['path'] );
157 unset( $res['pagesetmodule']['group'] );
160 $result->addValue( null, $this->getModuleName(), $res );
164 * List all submodules of a module
165 * @param ApiBase $module
166 * @param boolean $recursive
167 * @return string[]
169 private function listAllSubmodules( ApiBase $module, $recursive ) {
170 $manager = $module->getModuleManager();
171 if ( $manager ) {
172 $paths = [];
173 $names = $manager->getNames();
174 sort( $names );
175 foreach ( $names as $name ) {
176 $submodule = $manager->getModule( $name );
177 $paths[] = $submodule->getModulePath();
178 if ( $recursive && $submodule->getModuleManager() ) {
179 $paths = array_merge( $paths, $this->listAllSubmodules( $submodule, $recursive ) );
183 return $paths;
187 * @param array $res Result array
188 * @param string $key Result key
189 * @param Message[] $msgs
190 * @param bool $joinLists
192 protected function formatHelpMessages( array &$res, $key, array $msgs, $joinLists = false ) {
193 switch ( $this->helpFormat ) {
194 case 'none':
195 break;
197 case 'wikitext':
198 $ret = [];
199 foreach ( $msgs as $m ) {
200 $ret[] = $m->setContext( $this->context )->text();
202 $res[$key] = implode( "\n\n", $ret );
203 if ( $joinLists ) {
204 $res[$key] = preg_replace( '!^(([*#:;])[^\n]*)\n\n(?=\2)!m', "$1\n", $res[$key] );
206 break;
208 case 'html':
209 $ret = [];
210 foreach ( $msgs as $m ) {
211 $ret[] = $m->setContext( $this->context )->parseAsBlock();
213 $ret = implode( "\n", $ret );
214 if ( $joinLists ) {
215 $ret = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $ret );
217 $res[$key] = Parser::stripOuterParagraph( $ret );
218 break;
220 case 'raw':
221 $res[$key] = [];
222 foreach ( $msgs as $m ) {
223 $a = [
224 'key' => $m->getKey(),
225 'params' => $m->getParams(),
227 ApiResult::setIndexedTagName( $a['params'], 'param' );
228 if ( $m instanceof ApiHelpParamValueMessage ) {
229 $a['forvalue'] = $m->getParamValue();
231 $res[$key][] = $a;
233 ApiResult::setIndexedTagName( $res[$key], 'msg' );
234 break;
239 * @param ApiBase $module
240 * @return array
242 private function getModuleInfo( $module ) {
243 $ret = [];
244 $path = $module->getModulePath();
246 $ret['name'] = $module->getModuleName();
247 $ret['classname'] = get_class( $module );
248 $ret['path'] = $path;
249 if ( !$module->isMain() ) {
250 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
251 $module->getModuleName()
254 $ret['prefix'] = $module->getModulePrefix();
256 $sourceInfo = $module->getModuleSourceInfo();
257 if ( $sourceInfo ) {
258 $ret['source'] = $sourceInfo['name'];
259 if ( isset( $sourceInfo['namemsg'] ) ) {
260 $ret['sourcename'] = $this->context->msg( $sourceInfo['namemsg'] )->text();
261 } else {
262 $ret['sourcename'] = $ret['source'];
265 $link = SpecialPage::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] )->getFullURL();
266 if ( isset( $sourceInfo['license-name'] ) ) {
267 $ret['licensetag'] = $sourceInfo['license-name'];
268 $ret['licenselink'] = (string)$link;
269 } elseif ( SpecialVersion::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
270 $ret['licenselink'] = (string)$link;
274 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
276 foreach ( $module->getHelpFlags() as $flag ) {
277 $ret[$flag] = true;
280 $ret['helpurls'] = (array)$module->getHelpUrls();
281 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
282 $ret['helpurls'] = [];
284 ApiResult::setIndexedTagName( $ret['helpurls'], 'helpurl' );
286 if ( $this->helpFormat !== 'none' ) {
287 $ret['examples'] = [];
288 $examples = $module->getExamplesMessages();
289 foreach ( $examples as $qs => $msg ) {
290 $item = [
291 'query' => $qs
293 $msg = ApiBase::makeMessage( $msg, $this->context, [
294 $module->getModulePrefix(),
295 $module->getModuleName(),
296 $module->getModulePath()
297 ] );
298 $this->formatHelpMessages( $item, 'description', [ $msg ] );
299 if ( isset( $item['description'] ) ) {
300 if ( is_array( $item['description'] ) ) {
301 $item['description'] = $item['description'][0];
302 } else {
303 ApiResult::setSubelementsList( $item, 'description' );
306 $ret['examples'][] = $item;
308 ApiResult::setIndexedTagName( $ret['examples'], 'example' );
311 $ret['parameters'] = [];
312 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
313 $paramDesc = $module->getFinalParamDescription();
314 foreach ( $params as $name => $settings ) {
315 if ( !is_array( $settings ) ) {
316 $settings = [ ApiBase::PARAM_DFLT => $settings ];
319 $item = [
320 'name' => $name
322 if ( isset( $paramDesc[$name] ) ) {
323 $this->formatHelpMessages( $item, 'description', $paramDesc[$name], true );
326 $item['required'] = !empty( $settings[ApiBase::PARAM_REQUIRED] );
328 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
329 $item['deprecated'] = true;
332 if ( $name === 'token' && $module->needsToken() ) {
333 $item['tokentype'] = $module->needsToken();
336 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
337 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
338 ? $settings[ApiBase::PARAM_DFLT]
339 : null;
340 if ( is_bool( $dflt ) ) {
341 $settings[ApiBase::PARAM_TYPE] = 'boolean';
342 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
343 $settings[ApiBase::PARAM_TYPE] = 'string';
344 } elseif ( is_int( $dflt ) ) {
345 $settings[ApiBase::PARAM_TYPE] = 'integer';
349 if ( isset( $settings[ApiBase::PARAM_DFLT] ) ) {
350 switch ( $settings[ApiBase::PARAM_TYPE] ) {
351 case 'boolean':
352 $item['default'] = (bool)$settings[ApiBase::PARAM_DFLT];
353 break;
354 case 'string':
355 case 'text':
356 case 'password':
357 $item['default'] = strval( $settings[ApiBase::PARAM_DFLT] );
358 break;
359 case 'integer':
360 case 'limit':
361 $item['default'] = intval( $settings[ApiBase::PARAM_DFLT] );
362 break;
363 case 'timestamp':
364 $item['default'] = wfTimestamp( TS_ISO_8601, $settings[ApiBase::PARAM_DFLT] );
365 break;
366 default:
367 $item['default'] = $settings[ApiBase::PARAM_DFLT];
368 break;
372 $item['multi'] = !empty( $settings[ApiBase::PARAM_ISMULTI] );
373 if ( $item['multi'] ) {
374 $item['limit'] = $this->getMain()->canApiHighLimits() ?
375 ApiBase::LIMIT_SML2 :
376 ApiBase::LIMIT_SML1;
377 $item['lowlimit'] = ApiBase::LIMIT_SML1;
378 $item['highlimit'] = ApiBase::LIMIT_SML2;
381 if ( !empty( $settings[ApiBase::PARAM_ALLOW_DUPLICATES] ) ) {
382 $item['allowsduplicates'] = true;
385 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
386 if ( $settings[ApiBase::PARAM_TYPE] === 'submodule' ) {
387 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
388 ksort( $settings[ApiBase::PARAM_SUBMODULE_MAP] );
389 $item['type'] = array_keys( $settings[ApiBase::PARAM_SUBMODULE_MAP] );
390 $item['submodules'] = $settings[ApiBase::PARAM_SUBMODULE_MAP];
391 } else {
392 $item['type'] = $module->getModuleManager()->getNames( $name );
393 sort( $item['type'] );
394 $prefix = $module->isMain()
395 ? '' : ( $module->getModulePath() . '+' );
396 $item['submodules'] = [];
397 foreach ( $item['type'] as $v ) {
398 $item['submodules'][$v] = $prefix . $v;
401 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX] ) ) {
402 $item['submoduleparamprefix'] = $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX];
404 } elseif ( $settings[ApiBase::PARAM_TYPE] === 'tags' ) {
405 $item['type'] = ChangeTags::listExplicitlyDefinedTags();
406 } else {
407 $item['type'] = $settings[ApiBase::PARAM_TYPE];
409 if ( is_array( $item['type'] ) ) {
410 // To prevent sparse arrays from being serialized to JSON as objects
411 $item['type'] = array_values( $item['type'] );
412 ApiResult::setIndexedTagName( $item['type'], 't' );
415 // Add 'allspecifier' if applicable
416 if ( $item['type'] === 'namespace' ) {
417 $allowAll = true;
418 $allSpecifier = ApiBase::ALL_DEFAULT_STRING;
419 } else {
420 $allowAll = isset( $settings[ApiBase::PARAM_ALL] )
421 ? $settings[ApiBase::PARAM_ALL]
422 : false;
423 $allSpecifier = ( is_string( $allowAll ) ? $allowAll : ApiBase::ALL_DEFAULT_STRING );
425 if ( $allowAll && $item['multi'] &&
426 ( is_array( $item['type'] ) || $item['type'] === 'namespace' ) ) {
427 $item['allspecifier'] = $allSpecifier;
430 if ( $item['type'] === 'namespace' &&
431 isset( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ) &&
432 is_array( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] )
434 $item['extranamespaces'] = $settings[ApiBase::PARAM_EXTRA_NAMESPACES];
435 ApiResult::setArrayType( $item['extranamespaces'], 'array' );
436 ApiResult::setIndexedTagName( $item['extranamespaces'], 'ns' );
439 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
440 $item['max'] = $settings[ApiBase::PARAM_MAX];
442 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
443 $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
445 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
446 $item['min'] = $settings[ApiBase::PARAM_MIN];
448 if ( !empty( $settings[ApiBase::PARAM_RANGE_ENFORCE] ) ) {
449 $item['enforcerange'] = true;
452 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
453 $item['info'] = [];
454 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
455 $tag = array_shift( $i );
456 $info = [
457 'name' => $tag,
459 if ( count( $i ) ) {
460 $info['values'] = $i;
461 ApiResult::setIndexedTagName( $info['values'], 'v' );
463 $this->formatHelpMessages( $info, 'text', [
464 $this->context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
465 ->numParams( count( $i ) )
466 ->params( $this->context->getLanguage()->commaList( $i ) )
467 ->params( $module->getModulePrefix() )
468 ] );
469 ApiResult::setSubelementsList( $info, 'text' );
470 $item['info'][] = $info;
472 ApiResult::setIndexedTagName( $item['info'], 'i' );
475 $ret['parameters'][] = $item;
477 ApiResult::setIndexedTagName( $ret['parameters'], 'param' );
479 $dynamicParams = $module->dynamicParameterDocumentation();
480 if ( $dynamicParams !== null ) {
481 if ( $this->helpFormat === 'none' ) {
482 $ret['dynamicparameters'] = true;
483 } else {
484 $dynamicParams = ApiBase::makeMessage( $dynamicParams, $this->context, [
485 $module->getModulePrefix(),
486 $module->getModuleName(),
487 $module->getModulePath()
488 ] );
489 $this->formatHelpMessages( $ret, 'dynamicparameters', [ $dynamicParams ] );
493 return $ret;
496 public function isReadMode() {
497 return false;
500 public function getAllowedParams() {
501 // back compat
502 $querymodules = $this->getMain()->getModuleManager()
503 ->getModule( 'query' )->getModuleManager()->getNames();
504 sort( $querymodules );
505 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
506 sort( $formatmodules );
508 return [
509 'modules' => [
510 ApiBase::PARAM_ISMULTI => true,
512 'helpformat' => [
513 ApiBase::PARAM_DFLT => 'none',
514 ApiBase::PARAM_TYPE => [ 'html', 'wikitext', 'raw', 'none' ],
517 'querymodules' => [
518 ApiBase::PARAM_DEPRECATED => true,
519 ApiBase::PARAM_ISMULTI => true,
520 ApiBase::PARAM_TYPE => $querymodules,
522 'mainmodule' => [
523 ApiBase::PARAM_DEPRECATED => true,
525 'pagesetmodule' => [
526 ApiBase::PARAM_DEPRECATED => true,
528 'formatmodules' => [
529 ApiBase::PARAM_DEPRECATED => true,
530 ApiBase::PARAM_ISMULTI => true,
531 ApiBase::PARAM_TYPE => $formatmodules,
536 protected function getExamplesMessages() {
537 return [
538 'action=paraminfo&modules=parse|phpfm|query%2Ballpages|query%2Bsiteinfo'
539 => 'apihelp-paraminfo-example-1',
540 'action=paraminfo&modules=query%2B*'
541 => 'apihelp-paraminfo-example-2',
545 public function getHelpUrls() {
546 return 'https://www.mediawiki.org/wiki/API:Parameter_information';