wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / api / ApiParamInfo.php
bloba808ac00393f395ca0bd1c9aa58750dc74398186
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 $queryModules = $params['querymodules'];
56 foreach ( $queryModules as $m ) {
57 $modules[] = 'query+' . $m;
59 } else {
60 $queryModules = array();
63 if ( is_array( $params['formatmodules'] ) ) {
64 $formatModules = $params['formatmodules'];
65 foreach ( $formatModules as $m ) {
66 $modules[] = $m;
68 } else {
69 $formatModules = array();
72 $res = array();
74 foreach ( $modules as $m ) {
75 try {
76 $module = $this->getModuleFromPath( $m );
77 } catch ( UsageException $ex ) {
78 $this->setWarning( $ex->getMessage() );
79 continue;
81 $key = 'modules';
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';
91 if ( in_array( $module->getModuleName(), $formatModules ) ) {
92 $key = 'formatmodules';
95 $item = $this->getModuleInfo( $module );
96 if ( $isBCQuery ) {
97 $item['querytype'] = $item['group'];
99 $res[$key][] = $item;
102 $result = $this->getResult();
103 $result->addValue( array( $this->getModuleName() ), 'helpformat', $this->helpFormat );
105 foreach ( $res as $key => $stuff ) {
106 ApiResult::setIndexedTagName( $res[$key], 'module' );
109 if ( $params['mainmodule'] ) {
110 $res['mainmodule'] = $this->getModuleInfo( $this->getMain() );
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'] );
121 $result->addValue( null, $this->getModuleName(), $res );
125 * @param array $res Result array
126 * @param string $key Result key
127 * @param Message[] $msgs
128 * @param bool $joinLists
130 protected function formatHelpMessages( array &$res, $key, array $msgs, $joinLists = false ) {
131 switch ( $this->helpFormat ) {
132 case 'none':
133 break;
135 case 'wikitext':
136 $ret = array();
137 foreach ( $msgs as $m ) {
138 $ret[] = $m->setContext( $this->context )->text();
140 $res[$key] = join( "\n\n", $ret );
141 if ( $joinLists ) {
142 $res[$key] = preg_replace( '!^(([*#:;])[^\n]*)\n\n(?=\2)!m', "$1\n", $res[$key] );
144 break;
146 case 'html':
147 $ret = array();
148 foreach ( $msgs as $m ) {
149 $ret[] = $m->setContext( $this->context )->parseAsBlock();
151 $ret = join( "\n", $ret );
152 if ( $joinLists ) {
153 $ret = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $ret );
155 $res[$key] = Parser::stripOuterParagraph( $ret );
156 break;
158 case 'raw':
159 $res[$key] = array();
160 foreach ( $msgs as $m ) {
161 $a = array(
162 'key' => $m->getKey(),
163 'params' => $m->getParams(),
165 if ( $m instanceof ApiHelpParamValueMessage ) {
166 $a['forvalue'] = $m->getParamValue();
168 $res[$key][] = $a;
170 ApiResult::setIndexedTagName( $res[$key], 'msg' );
171 break;
176 * @param ApiBase $module
177 * @return ApiResult
179 private function getModuleInfo( $module ) {
180 $ret = array();
181 $path = $module->getModulePath();
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()
191 $ret['prefix'] = $module->getModulePrefix();
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'];
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;
211 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
213 foreach ( $module->getHelpFlags() as $flag ) {
214 $ret[$flag] = true;
217 $ret['helpurls'] = (array)$module->getHelpUrls();
218 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
219 $ret['helpurls'] = array();
221 ApiResult::setIndexedTagName( $ret['helpurls'], 'helpurl' );
223 if ( $this->helpFormat !== 'none' ) {
224 $ret['examples'] = array();
225 $examples = $module->getExamplesMessages();
226 foreach ( $examples as $qs => $msg ) {
227 $item = array(
228 'query' => $qs
230 $msg = ApiBase::makeMessage( $msg, $this->context, array(
231 $module->getModulePrefix(),
232 $module->getModuleName(),
233 $module->getModulePath()
234 ) );
235 $this->formatHelpMessages( $item, 'description', array( $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' );
243 $ret['examples'][] = $item;
245 ApiResult::setIndexedTagName( $ret['examples'], 'example' );
248 $ret['parameters'] = array();
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 = array( ApiBase::PARAM_DFLT => $settings );
256 $item = array(
257 'name' => $name
259 if ( isset( $paramDesc[$name] ) ) {
260 $this->formatHelpMessages( $item, 'description', $paramDesc[$name], true );
263 $item['required'] = !empty( $settings[ApiBase::PARAM_REQUIRED] );
265 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
266 $item['deprecated'] = true;
269 if ( $name === 'token' && $module->needsToken() ) {
270 $item['tokentype'] = $module->needsToken();
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';
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;
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;
318 if ( !empty( $settings[ApiBase::PARAM_ALLOW_DUPLICATES] ) ) {
319 $item['allowsduplicates'] = true;
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'] = array();
334 foreach ( $item['type'] as $v ) {
335 $item['submodules'][$v] = $prefix . $v;
338 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX] ) ) {
339 $item['submoduleparamprefix'] = $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX];
341 } else {
342 $item['type'] = $settings[ApiBase::PARAM_TYPE];
344 if ( is_array( $item['type'] ) ) {
345 // To prevent sparse arrays from being serialized to JSON as objects
346 $item['type'] = array_values( $item['type'] );
347 ApiResult::setIndexedTagName( $item['type'], 't' );
350 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
351 $item['max'] = $settings[ApiBase::PARAM_MAX];
353 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
354 $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
356 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
357 $item['min'] = $settings[ApiBase::PARAM_MIN];
359 if ( !empty( $settings[ApiBase::PARAM_RANGE_ENFORCE] ) ) {
360 $item['enforcerange'] = true;
363 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
364 $item['info'] = array();
365 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
366 $tag = array_shift( $i );
367 $info = array(
368 'name' => $tag,
370 if ( count( $i ) ) {
371 $info['values'] = $i;
372 ApiResult::setIndexedTagName( $info['values'], 'v' );
374 $this->formatHelpMessages( $info, 'text', array(
375 $this->context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
376 ->numParams( count( $i ) )
377 ->params( $this->context->getLanguage()->commaList( $i ) )
378 ->params( $module->getModulePrefix() )
379 ) );
380 ApiResult::setSubelementsList( $info, 'text' );
381 $item['info'][] = $info;
383 ApiResult::setIndexedTagName( $item['info'], 'i' );
386 $ret['parameters'][] = $item;
388 ApiResult::setIndexedTagName( $ret['parameters'], 'param' );
390 return $ret;
393 public function isReadMode() {
394 return false;
397 public function getAllowedParams() {
398 // back compat
399 $querymodules = $this->getMain()->getModuleManager()
400 ->getModule( 'query' )->getModuleManager()->getNames();
401 sort( $querymodules );
402 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
403 sort( $formatmodules );
405 return array(
406 'modules' => array(
407 ApiBase::PARAM_ISMULTI => true,
409 'helpformat' => array(
410 ApiBase::PARAM_DFLT => 'none',
411 ApiBase::PARAM_TYPE => array( 'html', 'wikitext', 'raw', 'none' ),
414 'querymodules' => array(
415 ApiBase::PARAM_DEPRECATED => true,
416 ApiBase::PARAM_ISMULTI => true,
417 ApiBase::PARAM_TYPE => $querymodules,
419 'mainmodule' => array(
420 ApiBase::PARAM_DEPRECATED => true,
422 'pagesetmodule' => array(
423 ApiBase::PARAM_DEPRECATED => true,
425 'formatmodules' => array(
426 ApiBase::PARAM_DEPRECATED => true,
427 ApiBase::PARAM_ISMULTI => true,
428 ApiBase::PARAM_TYPE => $formatmodules,
433 protected function getExamplesMessages() {
434 return array(
435 'action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo'
436 => 'apihelp-paraminfo-example-1',
440 public function getHelpUrls() {
441 return 'https://www.mediawiki.org/wiki/API:Parameter_information';