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