Fix 2 usages of 'bool' when 'boolean' is used elsewhere
[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 public function __construct( $main, $action ) {
38 parent::__construct( $main, $action );
39 }
40
41 public function execute() {
42 // Get parameters
43 $params = $this->extractRequestParams();
44 $result = $this->getResult();
45 $queryObj = new ApiQuery( $this->getMain(), 'query' );
46 $r = array();
47 if ( is_array( $params['modules'] ) ) {
48 $modArr = $this->getMain()->getModules();
49 $r['modules'] = array();
50 foreach ( $params['modules'] as $m ) {
51 if ( !isset( $modArr[$m] ) ) {
52 $r['modules'][] = array( 'name' => $m, 'missing' => '' );
53 continue;
54 }
55 $obj = new $modArr[$m]( $this->getMain(), $m );
56 $a = $this->getClassInfo( $obj );
57 $a['name'] = $m;
58 $r['modules'][] = $a;
59 }
60 $result->setIndexedTagName( $r['modules'], 'module' );
61 }
62 if ( is_array( $params['querymodules'] ) ) {
63 $qmodArr = $queryObj->getModules();
64 $r['querymodules'] = array();
65 foreach ( $params['querymodules'] as $qm ) {
66 if ( !isset( $qmodArr[$qm] ) ) {
67 $r['querymodules'][] = array( 'name' => $qm, 'missing' => '' );
68 continue;
69 }
70 $obj = new $qmodArr[$qm]( $this, $qm );
71 $a = $this->getClassInfo( $obj );
72 $a['name'] = $qm;
73 $a['querytype'] = $queryObj->getModuleType( $qm );
74 $r['querymodules'][] = $a;
75 }
76 $result->setIndexedTagName( $r['querymodules'], 'module' );
77 }
78 if ( $params['mainmodule'] ) {
79 $r['mainmodule'] = $this->getClassInfo( $this->getMain() );
80 }
81 if ( $params['pagesetmodule'] ) {
82 $pageSet = new ApiPageSet( $queryObj );
83 $r['pagesetmodule'] = $this->getClassInfo( $pageSet );
84 }
85 $result->addValue( null, $this->getModuleName(), $r );
86 }
87
88 /**
89 * @param $obj ApiBase
90 * @return ApiResult
91 */
92 function getClassInfo( $obj ) {
93 $result = $this->getResult();
94 $retval['classname'] = get_class( $obj );
95 $retval['description'] = implode( "\n", (array)$obj->getDescription() );
96 $retval['examples'] = implode( "\n", (array)$obj->getExamples() );
97 $retval['version'] = implode( "\n", (array)$obj->getVersion() );
98 $retval['prefix'] = $obj->getModulePrefix();
99
100 if ( $obj->isReadMode() ) {
101 $retval['readrights'] = '';
102 }
103 if ( $obj->isWriteMode() ) {
104 $retval['writerights'] = '';
105 }
106 if ( $obj->mustBePosted() ) {
107 $retval['mustbeposted'] = '';
108 }
109 if ( $obj instanceof ApiQueryGeneratorBase ) {
110 $retval['generator'] = '';
111 }
112
113 $allowedParams = $obj->getFinalParams();
114 if ( !is_array( $allowedParams ) ) {
115 return $retval;
116 }
117
118 $retval['parameters'] = array();
119 $paramDesc = $obj->getFinalParamDescription();
120 foreach ( $allowedParams as $n => $p ) {
121 $a = array( 'name' => $n );
122 if ( isset( $paramDesc[$n] ) ) {
123 $a['description'] = implode( "\n", (array)$paramDesc[$n] );
124 }
125
126 //handle shorthand
127 if( !is_array( $p ) ) {
128 $p = array(
129 ApiBase::PARAM_DFLT => $p,
130 );
131 }
132
133 //handle missing type
134 if ( !isset( $p[ApiBase::PARAM_TYPE] ) ) {
135 $dflt = isset( $p[ApiBase::PARAM_DFLT] ) ? $p[ApiBase::PARAM_DFLT] : null;
136 if ( is_bool( $dflt ) ) {
137 $p[ApiBase::PARAM_TYPE] = 'boolean';
138 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
139 $p[ApiBase::PARAM_TYPE] = 'string';
140 } elseif ( is_int( $dflt ) ) {
141 $p[ApiBase::PARAM_TYPE] = 'integer';
142 }
143 }
144
145 if ( isset( $p[ApiBase::PARAM_DEPRECATED] ) && $p[ApiBase::PARAM_DEPRECATED] ) {
146 $a['deprecated'] = '';
147 }
148 if ( isset( $p[ApiBase::PARAM_REQUIRED] ) && $p[ApiBase::PARAM_REQUIRED] ) {
149 $a['required'] = '';
150 }
151
152 if ( isset( $p[ApiBase::PARAM_DFLT] ) ) {
153 $type = $p[ApiBase::PARAM_TYPE];
154 if( $type === 'boolean' ) {
155 $a['default'] = ( $p[ApiBase::PARAM_DFLT] ? 'true' : 'false' );
156 } elseif( $type === 'string' ) {
157 $a['default'] = strval( $p[ApiBase::PARAM_DFLT] );
158 } elseif( $type === 'integer' ) {
159 $a['default'] = intval( $p[ApiBase::PARAM_DFLT] );
160 } else {
161 $a['default'] = $p[ApiBase::PARAM_DFLT];
162 }
163 }
164 if ( isset( $p[ApiBase::PARAM_ISMULTI] ) && $p[ApiBase::PARAM_ISMULTI] ) {
165 $a['multi'] = '';
166 $a['limit'] = $this->getMain()->canApiHighLimits() ?
167 ApiBase::LIMIT_SML2 :
168 ApiBase::LIMIT_SML1;
169 $a['lowlimit'] = ApiBase::LIMIT_SML1;
170 $a['highlimit'] = ApiBase::LIMIT_SML2;
171 }
172
173 if ( isset( $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) && $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) {
174 $a['allowsduplicates'] = '';
175 }
176
177 if ( isset( $p[ApiBase::PARAM_TYPE] ) ) {
178 $a['type'] = $p[ApiBase::PARAM_TYPE];
179 if ( is_array( $a['type'] ) ) {
180 $a['type'] = array_values( $a['type'] ); // to prevent sparse arrays from being serialized to JSON as objects
181 $result->setIndexedTagName( $a['type'], 't' );
182 }
183 }
184 if ( isset( $p[ApiBase::PARAM_MAX] ) ) {
185 $a['max'] = $p[ApiBase::PARAM_MAX];
186 }
187 if ( isset( $p[ApiBase::PARAM_MAX2] ) ) {
188 $a['highmax'] = $p[ApiBase::PARAM_MAX2];
189 }
190 if ( isset( $p[ApiBase::PARAM_MIN] ) ) {
191 $a['min'] = $p[ApiBase::PARAM_MIN];
192 }
193 $retval['parameters'][] = $a;
194 }
195 $result->setIndexedTagName( $retval['parameters'], 'param' );
196
197 // Errors
198 $retval['errors'] = $this->parseErrors( $obj->getPossibleErrors() );
199
200 $result->setIndexedTagName( $retval['errors'], 'error' );
201
202 return $retval;
203 }
204
205 public function isReadMode() {
206 return false;
207 }
208
209 public function getAllowedParams() {
210 return array(
211 'modules' => array(
212 ApiBase::PARAM_ISMULTI => true
213 ),
214 'querymodules' => array(
215 ApiBase::PARAM_ISMULTI => true
216 ),
217 'mainmodule' => false,
218 'pagesetmodule' => false,
219 );
220 }
221
222 public function getParamDescription() {
223 return array(
224 'modules' => 'List of module names (value of the action= parameter)',
225 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
226 'mainmodule' => 'Get information about the main (top-level) module as well',
227 'pagesetmodule' => 'Get information about the pageset module (providing titles= and friends) as well',
228 );
229 }
230
231 public function getDescription() {
232 return 'Obtain information about certain API parameters and errors';
233 }
234
235 protected function getExamples() {
236 return array(
237 'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo'
238 );
239 }
240
241 public function getVersion() {
242 return __CLASS__ . ': $Id$';
243 }
244 }