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