API: Log all deprecated parameter uses to api-feature-usage.log
[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 = array();
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 = array();
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 = array();
70 }
71
72 $res = array();
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( array( $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 = array();
137 foreach ( $msgs as $m ) {
138 $ret[] = $m->setContext( $this->context )->text();
139 }
140 $res[$key] = join( "\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 = array();
148 foreach ( $msgs as $m ) {
149 $ret[] = $m->setContext( $this->context )->parseAsBlock();
150 }
151 $ret = join( "\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] = array();
160 foreach ( $msgs as $m ) {
161 $a = array(
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 $result = $this->getResult();
181 $ret = array();
182 $path = $module->getModulePath();
183
184 $ret['name'] = $module->getModuleName();
185 $ret['classname'] = get_class( $module );
186 $ret['path'] = $path;
187 if ( !$module->isMain() ) {
188 $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup(
189 $module->getModuleName()
190 );
191 }
192 $ret['prefix'] = $module->getModulePrefix();
193
194 $sourceInfo = $module->getModuleSourceInfo();
195 if ( $sourceInfo ) {
196 $ret['source'] = $sourceInfo['name'];
197 if ( isset( $sourceInfo['namemsg'] ) ) {
198 $ret['sourcename'] = $this->context->msg( $sourceInfo['namemsg'] )->text();
199 } else {
200 $ret['sourcename'] = $ret['source'];
201 }
202
203 $link = SpecialPage::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] )->getFullUrl();
204 if ( isset( $sourceInfo['license-name'] ) ) {
205 $ret['licensetag'] = $sourceInfo['license-name'];
206 $ret['licenselink'] = (string)$link;
207 } elseif ( SpecialVersion::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
208 $ret['licenselink'] = (string)$link;
209 }
210 }
211
212 $this->formatHelpMessages( $ret, 'description', $module->getFinalDescription() );
213
214 foreach ( $module->getHelpFlags() as $flag ) {
215 $ret[$flag] = true;
216 }
217
218 $ret['helpurls'] = (array)$module->getHelpUrls();
219 if ( isset( $ret['helpurls'][0] ) && $ret['helpurls'][0] === false ) {
220 $ret['helpurls'] = array();
221 }
222 ApiResult::setIndexedTagName( $ret['helpurls'], 'helpurl' );
223
224 if ( $this->helpFormat !== 'none' ) {
225 $ret['examples'] = array();
226 $examples = $module->getExamplesMessages();
227 foreach ( $examples as $qs => $msg ) {
228 $item = array(
229 'query' => $qs
230 );
231 $msg = ApiBase::makeMessage( $msg, $this->context, array(
232 $module->getModulePrefix(),
233 $module->getModuleName(),
234 $module->getModulePath()
235 ) );
236 $this->formatHelpMessages( $item, 'description', array( $msg ) );
237 if ( isset( $item['description'] ) ) {
238 if ( is_array( $item['description'] ) ) {
239 $item['description'] = $item['description'][0];
240 } else {
241 ApiResult::setSubelementsList( $item, 'description' );
242 }
243 }
244 $ret['examples'][] = $item;
245 }
246 ApiResult::setIndexedTagName( $ret['examples'], 'example' );
247 }
248
249 $ret['parameters'] = array();
250 $params = $module->getFinalParams( ApiBase::GET_VALUES_FOR_HELP );
251 $paramDesc = $module->getFinalParamDescription();
252 foreach ( $params as $name => $settings ) {
253 if ( !is_array( $settings ) ) {
254 $settings = array( ApiBase::PARAM_DFLT => $settings );
255 }
256
257 $item = array(
258 'name' => $name
259 );
260 if ( isset( $paramDesc[$name] ) ) {
261 $this->formatHelpMessages( $item, 'description', $paramDesc[$name], true );
262 }
263
264 $item['required'] = !empty( $settings[ApiBase::PARAM_REQUIRED] );
265
266 if ( !empty( $settings[ApiBase::PARAM_DEPRECATED] ) ) {
267 $item['deprecated'] = true;
268 }
269
270 if ( $name === 'token' && $module->needsToken() ) {
271 $item['tokentype'] = $module->needsToken();
272 }
273
274 if ( !isset( $settings[ApiBase::PARAM_TYPE] ) ) {
275 $dflt = isset( $settings[ApiBase::PARAM_DFLT] )
276 ? $settings[ApiBase::PARAM_DFLT]
277 : null;
278 if ( is_bool( $dflt ) ) {
279 $settings[ApiBase::PARAM_TYPE] = 'boolean';
280 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
281 $settings[ApiBase::PARAM_TYPE] = 'string';
282 } elseif ( is_int( $dflt ) ) {
283 $settings[ApiBase::PARAM_TYPE] = 'integer';
284 }
285 }
286
287 if ( isset( $settings[ApiBase::PARAM_DFLT] ) ) {
288 switch ( $settings[ApiBase::PARAM_TYPE] ) {
289 case 'boolean':
290 $item['default'] = (bool)$settings[ApiBase::PARAM_DFLT];
291 break;
292 case 'string':
293 case 'text':
294 case 'password':
295 $item['default'] = strval( $settings[ApiBase::PARAM_DFLT] );
296 break;
297 case 'integer':
298 case 'limit':
299 $item['default'] = intval( $settings[ApiBase::PARAM_DFLT] );
300 break;
301 case 'timestamp':
302 $item['default'] = wfTimestamp( TS_ISO_8601, $settings[ApiBase::PARAM_DFLT] );
303 break;
304 default:
305 $item['default'] = $settings[ApiBase::PARAM_DFLT];
306 break;
307 }
308 }
309
310 $item['multi'] = !empty( $settings[ApiBase::PARAM_ISMULTI] );
311 if ( $item['multi'] ) {
312 $item['limit'] = $this->getMain()->canApiHighLimits() ?
313 ApiBase::LIMIT_SML2 :
314 ApiBase::LIMIT_SML1;
315 $item['lowlimit'] = ApiBase::LIMIT_SML1;
316 $item['highlimit'] = ApiBase::LIMIT_SML2;
317 }
318
319 if ( !empty( $settings[ApiBase::PARAM_ALLOW_DUPLICATES] ) ) {
320 $item['allowsduplicates'] = true;
321 }
322
323 if ( isset( $settings[ApiBase::PARAM_TYPE] ) ) {
324 if ( $settings[ApiBase::PARAM_TYPE] === 'submodule' ) {
325 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
326 ksort( $settings[ApiBase::PARAM_SUBMODULE_MAP] );
327 $item['type'] = array_keys( $settings[ApiBase::PARAM_SUBMODULE_MAP] );
328 $item['submodules'] = $settings[ApiBase::PARAM_SUBMODULE_MAP];
329 } else {
330 $item['type'] = $module->getModuleManager()->getNames( $name );
331 sort( $item['type'] );
332 $prefix = $module->isMain()
333 ? '' : ( $module->getModulePath() . '+' );
334 $item['submodules'] = array();
335 foreach ( $item['type'] as $v ) {
336 $item['submodules'][$v] = $prefix . $v;
337 }
338 }
339 if ( isset( $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX] ) ) {
340 $item['submoduleparamprefix'] = $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX];
341 }
342 } else {
343 $item['type'] = $settings[ApiBase::PARAM_TYPE];
344 }
345 if ( is_array( $item['type'] ) ) {
346 // To prevent sparse arrays from being serialized to JSON as objects
347 $item['type'] = array_values( $item['type'] );
348 ApiResult::setIndexedTagName( $item['type'], 't' );
349 }
350 }
351 if ( isset( $settings[ApiBase::PARAM_MAX] ) ) {
352 $item['max'] = $settings[ApiBase::PARAM_MAX];
353 }
354 if ( isset( $settings[ApiBase::PARAM_MAX2] ) ) {
355 $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
356 }
357 if ( isset( $settings[ApiBase::PARAM_MIN] ) ) {
358 $item['min'] = $settings[ApiBase::PARAM_MIN];
359 }
360 if ( !empty( $settings[ApiBase::PARAM_RANGE_ENFORCE] ) ) {
361 $item['enforcerange'] = true;
362 }
363
364 if ( !empty( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
365 $item['info'] = array();
366 foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $i ) {
367 $tag = array_shift( $i );
368 $info = array(
369 'name' => $tag,
370 );
371 if ( count( $i ) ) {
372 $info['values'] = $i;
373 ApiResult::setIndexedTagName( $info['values'], 'v' );
374 }
375 $this->formatHelpMessages( $info, 'text', array(
376 $this->context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
377 ->numParams( count( $i ) )
378 ->params( $this->context->getLanguage()->commaList( $i ) )
379 ->params( $module->getModulePrefix() )
380 ) );
381 ApiResult::setSubelementsList( $info, 'text' );
382 $item['info'][] = $info;
383 }
384 ApiResult::setIndexedTagName( $item['info'], 'i' );
385 }
386
387 $ret['parameters'][] = $item;
388 }
389 ApiResult::setIndexedTagName( $ret['parameters'], 'param' );
390
391 return $ret;
392 }
393
394 public function isReadMode() {
395 return false;
396 }
397
398 public function getAllowedParams() {
399 // back compat
400 $querymodules = $this->getMain()->getModuleManager()
401 ->getModule( 'query' )->getModuleManager()->getNames();
402 sort( $querymodules );
403 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
404 sort( $formatmodules );
405
406 return array(
407 'modules' => array(
408 ApiBase::PARAM_ISMULTI => true,
409 ),
410 'helpformat' => array(
411 ApiBase::PARAM_DFLT => 'none',
412 ApiBase::PARAM_TYPE => array( 'html', 'wikitext', 'raw', 'none' ),
413 ),
414
415 'querymodules' => array(
416 ApiBase::PARAM_DEPRECATED => true,
417 ApiBase::PARAM_ISMULTI => true,
418 ApiBase::PARAM_TYPE => $querymodules,
419 ),
420 'mainmodule' => array(
421 ApiBase::PARAM_DEPRECATED => true,
422 ),
423 'pagesetmodule' => array(
424 ApiBase::PARAM_DEPRECATED => true,
425 ),
426 'formatmodules' => array(
427 ApiBase::PARAM_DEPRECATED => true,
428 ApiBase::PARAM_ISMULTI => true,
429 ApiBase::PARAM_TYPE => $formatmodules,
430 )
431 );
432 }
433
434 protected function getExamplesMessages() {
435 return array(
436 'action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo'
437 => 'apihelp-paraminfo-example-1',
438 );
439 }
440
441 public function getHelpUrls() {
442 return 'https://www.mediawiki.org/wiki/API:Parameter_information';
443 }
444 }