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