Follow-up r97303: sort module names in paraminfo's paraminfo
[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 /**
33 * @var ApiQuery
34 */
35 protected $queryObj;
36
37 public function __construct( $main, $action ) {
38 parent::__construct( $main, $action );
39 $this->queryObj = new ApiQuery( $this->getMain(), 'query' );
40 }
41
42 public function execute() {
43 // Get parameters
44 $params = $this->extractRequestParams();
45 $result = $this->getResult();
46
47 $res = array();
48 if ( is_array( $params['modules'] ) ) {
49 $modules = $this->getMain()->getModules();
50 $res['modules'] = array();
51 foreach ( $params['modules'] as $mod ) {
52 if ( !isset( $modules[$mod] ) ) {
53 $res['modules'][] = array( 'name' => $mod, 'missing' => '' );
54 continue;
55 }
56 $obj = new $modules[$mod]( $this->getMain(), $mod );
57
58 $item = $this->getClassInfo( $obj );
59 $item['name'] = $mod;
60 $res['modules'][] = $item;
61 }
62 $result->setIndexedTagName( $res['modules'], 'module' );
63 }
64
65 if ( is_array( $params['querymodules'] ) ) {
66 $queryModules = $this->queryObj->getModules();
67 $res['querymodules'] = array();
68 foreach ( $params['querymodules'] as $qm ) {
69 if ( !isset( $queryModules[$qm] ) ) {
70 $res['querymodules'][] = array( 'name' => $qm, 'missing' => '' );
71 continue;
72 }
73 $obj = new $queryModules[$qm]( $this, $qm );
74 $item = $this->getClassInfo( $obj );
75 $item['name'] = $qm;
76 $item['querytype'] = $this->queryObj->getModuleType( $qm );
77 $res['querymodules'][] = $item;
78 }
79 $result->setIndexedTagName( $res['querymodules'], 'module' );
80 }
81
82 if ( $params['mainmodule'] ) {
83 $res['mainmodule'] = $this->getClassInfo( $this->getMain() );
84 }
85
86 if ( $params['pagesetmodule'] ) {
87 $pageSet = new ApiPageSet( $this->queryObj );
88 $res['pagesetmodule'] = $this->getClassInfo( $pageSet );
89 }
90
91 if ( is_array( $params['formatmodules'] ) ) {
92 $formats = $this->getMain()->getFormats();
93 $res['formatmodules'] = array();
94 foreach ( $params['formatmodules'] as $f ) {
95 if ( !isset( $formats[$f] ) ) {
96 $res['formatmodules'][] = array( 'name' => $f, 'missing' => '' );
97 continue;
98 }
99 $obj = new $formats[$f]( $this, $f );
100 $item = $this->getClassInfo( $obj );
101 $item['name'] = $f;
102 $res['formatmodules'][] = $item;
103 }
104 $result->setIndexedTagName( $res['formatmodules'], 'module' );
105 }
106 $result->addValue( null, $this->getModuleName(), $res );
107 }
108
109 /**
110 * @param $obj ApiBase
111 * @return ApiResult
112 */
113 function getClassInfo( $obj ) {
114 $result = $this->getResult();
115 $retval['classname'] = get_class( $obj );
116 $retval['description'] = implode( "\n", (array)$obj->getFinalDescription() );
117
118 $retval['examples'] = '';
119
120 $retval['version'] = implode( "\n", (array)$obj->getVersion() );
121 $retval['prefix'] = $obj->getModulePrefix();
122
123 if ( $obj->isReadMode() ) {
124 $retval['readrights'] = '';
125 }
126 if ( $obj->isWriteMode() ) {
127 $retval['writerights'] = '';
128 }
129 if ( $obj->mustBePosted() ) {
130 $retval['mustbeposted'] = '';
131 }
132 if ( $obj instanceof ApiQueryGeneratorBase ) {
133 $retval['generator'] = '';
134 }
135
136 $allowedParams = $obj->getFinalParams();
137 if ( !is_array( $allowedParams ) ) {
138 return $retval;
139 }
140
141 $retval['helpurls'] = (array)$obj->getHelpUrls();
142 if ( isset( $retval['helpurls'][0] ) && $retval['helpurls'][0] === false ) {
143 $retval['helpurls'] = array();
144 }
145 $result->setIndexedTagName( $retval['helpurls'], 'helpurl' );
146
147 $examples = $obj->getExamples();
148 $retval['allexamples'] = array();
149 if ( $examples !== false ) {
150 foreach( $examples as $k => $v ) {
151 if ( strlen( $retval['examples'] ) ) {
152 $retval['examples'] .= ' ';
153 }
154 $item = array();
155 if ( is_numeric( $k ) ) {
156 $retval['examples'] .= $v;
157 $result->setContent( $item, $v );
158 } else {
159 if ( !is_array( $v ) ) {
160 $item['description'] = $v;
161 } else {
162 $item['description'] = implode( $v, "\n" );
163 }
164 $retval['examples'] .= $item['description'] . ' ' . $k;
165 $result->setContent( $item, $k );
166 }
167 $retval['allexamples'][] = $item;
168 }
169 }
170 $result->setIndexedTagName( $retval['allexamples'], 'example' );
171
172 $retval['parameters'] = array();
173 $paramDesc = $obj->getFinalParamDescription();
174 foreach ( $allowedParams as $n => $p ) {
175 $a = array( 'name' => $n );
176 if ( isset( $paramDesc[$n] ) ) {
177 $a['description'] = implode( "\n", (array)$paramDesc[$n] );
178 }
179
180 //handle shorthand
181 if( !is_array( $p ) ) {
182 $p = array(
183 ApiBase::PARAM_DFLT => $p,
184 );
185 }
186
187 //handle missing type
188 if ( !isset( $p[ApiBase::PARAM_TYPE] ) ) {
189 $dflt = isset( $p[ApiBase::PARAM_DFLT] ) ? $p[ApiBase::PARAM_DFLT] : null;
190 if ( is_bool( $dflt ) ) {
191 $p[ApiBase::PARAM_TYPE] = 'boolean';
192 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
193 $p[ApiBase::PARAM_TYPE] = 'string';
194 } elseif ( is_int( $dflt ) ) {
195 $p[ApiBase::PARAM_TYPE] = 'integer';
196 }
197 }
198
199 if ( isset( $p[ApiBase::PARAM_DEPRECATED] ) && $p[ApiBase::PARAM_DEPRECATED] ) {
200 $a['deprecated'] = '';
201 }
202 if ( isset( $p[ApiBase::PARAM_REQUIRED] ) && $p[ApiBase::PARAM_REQUIRED] ) {
203 $a['required'] = '';
204 }
205
206 if ( isset( $p[ApiBase::PARAM_DFLT] ) ) {
207 $type = $p[ApiBase::PARAM_TYPE];
208 if( $type === 'boolean' ) {
209 $a['default'] = ( $p[ApiBase::PARAM_DFLT] ? 'true' : 'false' );
210 } elseif( $type === 'string' ) {
211 $a['default'] = strval( $p[ApiBase::PARAM_DFLT] );
212 } elseif( $type === 'integer' ) {
213 $a['default'] = intval( $p[ApiBase::PARAM_DFLT] );
214 } else {
215 $a['default'] = $p[ApiBase::PARAM_DFLT];
216 }
217 }
218 if ( isset( $p[ApiBase::PARAM_ISMULTI] ) && $p[ApiBase::PARAM_ISMULTI] ) {
219 $a['multi'] = '';
220 $a['limit'] = $this->getMain()->canApiHighLimits() ?
221 ApiBase::LIMIT_SML2 :
222 ApiBase::LIMIT_SML1;
223 $a['lowlimit'] = ApiBase::LIMIT_SML1;
224 $a['highlimit'] = ApiBase::LIMIT_SML2;
225 }
226
227 if ( isset( $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) && $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) {
228 $a['allowsduplicates'] = '';
229 }
230
231 if ( isset( $p[ApiBase::PARAM_TYPE] ) ) {
232 $a['type'] = $p[ApiBase::PARAM_TYPE];
233 if ( is_array( $a['type'] ) ) {
234 $a['type'] = array_values( $a['type'] ); // to prevent sparse arrays from being serialized to JSON as objects
235 $result->setIndexedTagName( $a['type'], 't' );
236 }
237 }
238 if ( isset( $p[ApiBase::PARAM_MAX] ) ) {
239 $a['max'] = $p[ApiBase::PARAM_MAX];
240 }
241 if ( isset( $p[ApiBase::PARAM_MAX2] ) ) {
242 $a['highmax'] = $p[ApiBase::PARAM_MAX2];
243 }
244 if ( isset( $p[ApiBase::PARAM_MIN] ) ) {
245 $a['min'] = $p[ApiBase::PARAM_MIN];
246 }
247 $retval['parameters'][] = $a;
248 }
249 $result->setIndexedTagName( $retval['parameters'], 'param' );
250
251 // Errors
252 $retval['errors'] = $this->parseErrors( $obj->getPossibleErrors() );
253 $result->setIndexedTagName( $retval['errors'], 'error' );
254
255 return $retval;
256 }
257
258 public function isReadMode() {
259 return false;
260 }
261
262 public function getAllowedParams() {
263 $modules = array_keys( $this->getMain()->getModules() );
264 sort( $modules );
265 $querymodules = array_keys( $this->queryObj->getModules() );
266 sort( $querymodules );
267 $formatmodules = array_keys( $this->getMain()->getFormats() );
268 sort( $formatmodules );
269 return array(
270 'modules' => array(
271 ApiBase::PARAM_ISMULTI => true,
272 ApiBase::PARAM_TYPE => $modules,
273 ),
274 'querymodules' => array(
275 ApiBase::PARAM_ISMULTI => true,
276 ApiBase::PARAM_TYPE => $querymodules,
277 ),
278 'mainmodule' => false,
279 'pagesetmodule' => false,
280 'formatmodules' => array(
281 ApiBase::PARAM_ISMULTI => true,
282 ApiBase::PARAM_TYPE => $formatmodules,
283 )
284 );
285 }
286
287 public function getParamDescription() {
288 return array(
289 'modules' => 'List of module names (value of the action= parameter)',
290 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
291 'mainmodule' => 'Get information about the main (top-level) module as well',
292 'pagesetmodule' => 'Get information about the pageset module (providing titles= and friends) as well',
293 'formatmodules' => 'List of format module names (value of format= parameter)',
294 );
295 }
296
297 public function getDescription() {
298 return 'Obtain information about certain API parameters and errors';
299 }
300
301 public function getExamples() {
302 return array(
303 'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo'
304 );
305 }
306
307 public function getHelpUrls() {
308 return 'https://www.mediawiki.org/wiki/API:Parameter_information';
309 }
310
311 public function getVersion() {
312 return __CLASS__ . ': $Id$';
313 }
314 }