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