PHPSessionHandler: Implement SessionHandlerInterface
[mediawiki.git] / includes / api / ApiParamInfo.php
bloba8e5629a7fa76f1c653b8a94e73beccbb6cb03c4
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 } elseif ( $settings[ApiBase::PARAM_TYPE] === 'tags' ) {
342 $item['type'] = ChangeTags::listExplicitlyDefinedTags();
343 } else {
344 $item['type'] = $settings[ApiBase::PARAM_TYPE];
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' );
352 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
353 $item['max'] = $settings[ApiBase::PARAM_MAX];
355 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
356 $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
358 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
359 $item['min'] = $settings[ApiBase::PARAM_MIN];
361 if ( !empty( $settings[ApiBase::PARAM_RANGE_ENFORCE] ) ) {
362 $item['enforcerange'] = true;
365 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
366 $item['info'] = array();
367 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
368 $tag = array_shift( $i );
369 $info = array(
370 'name' => $tag,
372 if ( count( $i ) ) {
373 $info['values'] = $i;
374 ApiResult::setIndexedTagName( $info['values'], 'v' );
376 $this->formatHelpMessages( $info, 'text', array(
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;
385 ApiResult::setIndexedTagName( $item['info'], 'i' );
388 $ret['parameters'][] = $item;
390 ApiResult::setIndexedTagName( $ret['parameters'], 'param' );
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, array(
398 $module->getModulePrefix(),
399 $module->getModuleName(),
400 $module->getModulePath()
401 ) );
402 $this->formatHelpMessages( $ret, 'dynamicparameters', array( $dynamicParams ) );
406 return $ret;
409 public function isReadMode() {
410 return false;
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 );
421 return array(
422 'modules' => array(
423 ApiBase::PARAM_ISMULTI => true,
425 'helpformat' => array(
426 ApiBase::PARAM_DFLT => 'none',
427 ApiBase::PARAM_TYPE => array( 'html', 'wikitext', 'raw', 'none' ),
430 'querymodules' => array(
431 ApiBase::PARAM_DEPRECATED => true,
432 ApiBase::PARAM_ISMULTI => true,
433 ApiBase::PARAM_TYPE => $querymodules,
435 'mainmodule' => array(
436 ApiBase::PARAM_DEPRECATED => true,
438 'pagesetmodule' => array(
439 ApiBase::PARAM_DEPRECATED => true,
441 'formatmodules' => array(
442 ApiBase::PARAM_DEPRECATED => true,
443 ApiBase::PARAM_ISMULTI => true,
444 ApiBase::PARAM_TYPE => $formatmodules,
449 protected function getExamplesMessages() {
450 return array(
451 'action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo'
452 => 'apihelp-paraminfo-example-1',
456 public function getHelpUrls() {
457 return 'https://www.mediawiki.org/wiki/API:Parameter_information';