Based on diff to wikia, set more functions consistently public rather than protected
[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 $examples = (array)$obj->getExamples();
97 $retval['examples'] = implode( "\n", $examples );
98 $retval['version'] = implode( "\n", (array)$obj->getVersion() );
99 $retval['prefix'] = $obj->getModulePrefix();
100
101 if ( $obj->isReadMode() ) {
102 $retval['readrights'] = '';
103 }
104 if ( $obj->isWriteMode() ) {
105 $retval['writerights'] = '';
106 }
107 if ( $obj->mustBePosted() ) {
108 $retval['mustbeposted'] = '';
109 }
110 if ( $obj instanceof ApiQueryGeneratorBase ) {
111 $retval['generator'] = '';
112 }
113
114 $allowedParams = $obj->getFinalParams();
115 if ( !is_array( $allowedParams ) ) {
116 return $retval;
117 }
118
119 $retval['helpurls'] = (array)$obj->getHelpUrls();
120 if ( isset( $retval['helpurls'][0] ) && $retval['helpurls'][0] === false ) {
121 $retval['helpurls'] = array();
122 }
123 $result->setIndexedTagName( $retval['helpurls'], 'helpurl' );
124
125 $retval['allexamples'] = $examples;
126 if ( isset( $retval['allexamples'][0] ) && $retval['allexamples'][0] === false ) {
127 $retval['allexamples'] = array();
128 }
129 $result->setIndexedTagName( $retval['allexamples'], 'example' );
130
131 $retval['parameters'] = array();
132 $paramDesc = $obj->getFinalParamDescription();
133 foreach ( $allowedParams as $n => $p ) {
134 $a = array( 'name' => $n );
135 if ( isset( $paramDesc[$n] ) ) {
136 $a['description'] = implode( "\n", (array)$paramDesc[$n] );
137 }
138
139 //handle shorthand
140 if( !is_array( $p ) ) {
141 $p = array(
142 ApiBase::PARAM_DFLT => $p,
143 );
144 }
145
146 //handle missing type
147 if ( !isset( $p[ApiBase::PARAM_TYPE] ) ) {
148 $dflt = isset( $p[ApiBase::PARAM_DFLT] ) ? $p[ApiBase::PARAM_DFLT] : null;
149 if ( is_bool( $dflt ) ) {
150 $p[ApiBase::PARAM_TYPE] = 'boolean';
151 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) {
152 $p[ApiBase::PARAM_TYPE] = 'string';
153 } elseif ( is_int( $dflt ) ) {
154 $p[ApiBase::PARAM_TYPE] = 'integer';
155 }
156 }
157
158 if ( isset( $p[ApiBase::PARAM_DEPRECATED] ) && $p[ApiBase::PARAM_DEPRECATED] ) {
159 $a['deprecated'] = '';
160 }
161 if ( isset( $p[ApiBase::PARAM_REQUIRED] ) && $p[ApiBase::PARAM_REQUIRED] ) {
162 $a['required'] = '';
163 }
164
165 if ( isset( $p[ApiBase::PARAM_DFLT] ) ) {
166 $type = $p[ApiBase::PARAM_TYPE];
167 if( $type === 'boolean' ) {
168 $a['default'] = ( $p[ApiBase::PARAM_DFLT] ? 'true' : 'false' );
169 } elseif( $type === 'string' ) {
170 $a['default'] = strval( $p[ApiBase::PARAM_DFLT] );
171 } elseif( $type === 'integer' ) {
172 $a['default'] = intval( $p[ApiBase::PARAM_DFLT] );
173 } else {
174 $a['default'] = $p[ApiBase::PARAM_DFLT];
175 }
176 }
177 if ( isset( $p[ApiBase::PARAM_ISMULTI] ) && $p[ApiBase::PARAM_ISMULTI] ) {
178 $a['multi'] = '';
179 $a['limit'] = $this->getMain()->canApiHighLimits() ?
180 ApiBase::LIMIT_SML2 :
181 ApiBase::LIMIT_SML1;
182 $a['lowlimit'] = ApiBase::LIMIT_SML1;
183 $a['highlimit'] = ApiBase::LIMIT_SML2;
184 }
185
186 if ( isset( $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) && $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) {
187 $a['allowsduplicates'] = '';
188 }
189
190 if ( isset( $p[ApiBase::PARAM_TYPE] ) ) {
191 $a['type'] = $p[ApiBase::PARAM_TYPE];
192 if ( is_array( $a['type'] ) ) {
193 $a['type'] = array_values( $a['type'] ); // to prevent sparse arrays from being serialized to JSON as objects
194 $result->setIndexedTagName( $a['type'], 't' );
195 }
196 }
197 if ( isset( $p[ApiBase::PARAM_MAX] ) ) {
198 $a['max'] = $p[ApiBase::PARAM_MAX];
199 }
200 if ( isset( $p[ApiBase::PARAM_MAX2] ) ) {
201 $a['highmax'] = $p[ApiBase::PARAM_MAX2];
202 }
203 if ( isset( $p[ApiBase::PARAM_MIN] ) ) {
204 $a['min'] = $p[ApiBase::PARAM_MIN];
205 }
206 $retval['parameters'][] = $a;
207 }
208 $result->setIndexedTagName( $retval['parameters'], 'param' );
209
210 // Errors
211 $retval['errors'] = $this->parseErrors( $obj->getPossibleErrors() );
212 $result->setIndexedTagName( $retval['errors'], 'error' );
213
214 return $retval;
215 }
216
217 public function isReadMode() {
218 return false;
219 }
220
221 public function getAllowedParams() {
222 return array(
223 'modules' => array(
224 ApiBase::PARAM_ISMULTI => true
225 ),
226 'querymodules' => array(
227 ApiBase::PARAM_ISMULTI => true
228 ),
229 'mainmodule' => false,
230 'pagesetmodule' => false,
231 );
232 }
233
234 public function getParamDescription() {
235 return array(
236 'modules' => 'List of module names (value of the action= parameter)',
237 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
238 'mainmodule' => 'Get information about the main (top-level) module as well',
239 'pagesetmodule' => 'Get information about the pageset module (providing titles= and friends) as well',
240 );
241 }
242
243 public function getDescription() {
244 return 'Obtain information about certain API parameters and errors';
245 }
246
247 public function getExamples() {
248 return array(
249 'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo'
250 );
251 }
252
253 public function getHelpUrls() {
254 return 'http://www.mediawiki.org/wiki/API:Parameter_information';
255 }
256
257 public function getVersion() {
258 return __CLASS__ . ': $Id$';
259 }
260 }