API: fix copyright symbol, coding style cleanup, more braces
[lhc/web/wiklou.git] / includes / api / ApiParamInfo.php
1 <?php
2
3 /**
4 * Created on Dec 01, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2008 Roan Kattouw <Firstname>.<Lastname>@home.nl
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( "ApiBase.php" );
29 }
30
31 /**
32 * @ingroup API
33 */
34 class ApiParamInfo extends ApiBase {
35
36 public function __construct( $main, $action ) {
37 parent::__construct( $main, $action );
38 }
39
40 public function execute() {
41 // Get parameters
42 $params = $this->extractRequestParams();
43 $result = $this->getResult();
44 $queryObj = new ApiQuery( $this->getMain(), 'query' );
45 $r = array();
46 if ( is_array( $params['modules'] ) ) {
47 $modArr = $this->getMain()->getModules();
48 $r['modules'] = array();
49 foreach ( $params['modules'] as $m ) {
50 if ( !isset( $modArr[$m] ) ) {
51 $r['modules'][] = array( 'name' => $m, 'missing' => '' );
52 continue;
53 }
54 $obj = new $modArr[$m]( $this->getMain(), $m );
55 $a = $this->getClassInfo( $obj );
56 $a['name'] = $m;
57 $r['modules'][] = $a;
58 }
59 $result->setIndexedTagName( $r['modules'], 'module' );
60 }
61 if ( is_array( $params['querymodules'] ) ) {
62 $qmodArr = $queryObj->getModules();
63 $r['querymodules'] = array();
64 foreach ( $params['querymodules'] as $qm ) {
65 if ( !isset( $qmodArr[$qm] ) ) {
66 $r['querymodules'][] = array( 'name' => $qm, 'missing' => '' );
67 continue;
68 }
69 $obj = new $qmodArr[$qm]( $this, $qm );
70 $a = $this->getClassInfo( $obj );
71 $a['name'] = $qm;
72 $r['querymodules'][] = $a;
73 }
74 $result->setIndexedTagName( $r['querymodules'], 'module' );
75 }
76 if ( $params['mainmodule'] ) {
77 $r['mainmodule'] = $this->getClassInfo( $this->getMain() );
78 }
79 if ( $params['pagesetmodule'] ) {
80 $pageSet = new ApiPageSet( $queryObj );
81 $r['pagesetmodule'] = $this->getClassInfo( $pageSet );
82 }
83 $result->addValue( null, $this->getModuleName(), $r );
84 }
85
86 function getClassInfo( $obj ) {
87 $result = $this->getResult();
88 $retval['classname'] = get_class( $obj );
89 $retval['description'] = implode( "\n", (array)$obj->getDescription() );
90 $retval['version'] = implode( "\n", (array)$obj->getVersion() );
91 $retval['prefix'] = $obj->getModulePrefix();
92
93 if ( $obj->isReadMode() ) {
94 $retval['readrights'] = '';
95 }
96 if ( $obj->isWriteMode() ) {
97 $retval['writerights'] = '';
98 }
99 if ( $obj->mustBePosted() ) {
100 $retval['mustbeposted'] = '';
101 }
102 if ( $obj instanceof ApiQueryGeneratorBase ) {
103 $retval['generator'] = '';
104 }
105
106 $allowedParams = $obj->getFinalParams();
107 if ( !is_array( $allowedParams ) ) {
108 return $retval;
109 }
110
111 $retval['parameters'] = array();
112 $paramDesc = $obj->getFinalParamDescription();
113 foreach ( $allowedParams as $n => $p ) {
114 $a = array( 'name' => $n );
115 if ( isset( $paramDesc[$n] ) ) {
116 $a['description'] = implode( "\n", (array)$paramDesc[$n] );
117 }
118 if ( isset( $p[ApiBase::PARAM_DEPRECATED] ) && $p[ApiBase::PARAM_DEPRECATED] ) {
119 $a['deprecated'] = '';
120 }
121 if ( !is_array( $p ) ) {
122 if ( is_bool( $p ) ) {
123 $a['type'] = 'bool';
124 $a['default'] = ( $p ? 'true' : 'false' );
125 } elseif ( is_string( $p ) || is_null( $p ) ) {
126 $a['type'] = 'string';
127 $a['default'] = strval( $p );
128 } elseif ( is_int( $p ) ) {
129 $a['type'] = 'integer';
130 $a['default'] = intval( $p );
131 }
132 $retval['parameters'][] = $a;
133 continue;
134 }
135
136 if ( isset( $p[ApiBase::PARAM_DFLT] ) ) {
137 $a['default'] = $p[ApiBase::PARAM_DFLT];
138 }
139 if ( isset( $p[ApiBase::PARAM_ISMULTI] ) ) {
140 if ( $p[ApiBase::PARAM_ISMULTI] ) {
141 $a['multi'] = '';
142 $a['limit'] = $this->getMain()->canApiHighLimits() ?
143 ApiBase::LIMIT_SML2 :
144 ApiBase::LIMIT_SML1;
145 }
146 }
147
148 if ( isset( $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) ) {
149 if ( $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) {
150 $a['allowsduplicates'] = '';
151 }
152 }
153
154 if ( isset( $p[ApiBase::PARAM_TYPE] ) ) {
155 $a['type'] = $p[ApiBase::PARAM_TYPE];
156 if ( is_array( $a['type'] ) ) {
157 $result->setIndexedTagName( $a['type'], 't' );
158 }
159 }
160 if ( isset( $p[ApiBase::PARAM_MAX] ) ) {
161 $a['max'] = $p[ApiBase::PARAM_MAX];
162 }
163 if ( isset( $p[ApiBase::PARAM_MAX2] ) ) {
164 $a['highmax'] = $p[ApiBase::PARAM_MAX2];
165 }
166 if ( isset( $p[ApiBase::PARAM_MIN] ) ) {
167 $a['min'] = $p[ApiBase::PARAM_MIN];
168 }
169 $retval['parameters'][] = $a;
170 }
171 $result->setIndexedTagName( $retval['parameters'], 'param' );
172
173 // Errors
174 $retval['errors'] = $this->parseErrors( $obj->getPossibleErrors() );
175
176 $result->setIndexedTagName( $retval['errors'], 'error' );
177
178 return $retval;
179 }
180
181 public function isReadMode() {
182 return false;
183 }
184
185 public function getAllowedParams() {
186 return array(
187 'modules' => array(
188 ApiBase::PARAM_ISMULTI => true
189 ),
190 'querymodules' => array(
191 ApiBase::PARAM_ISMULTI => true
192 ),
193 'mainmodule' => false,
194 'pagesetmodule' => false,
195 );
196 }
197
198 public function getParamDescription() {
199 return array(
200 'modules' => 'List of module names (value of the action= parameter)',
201 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
202 'mainmodule' => 'Get information about the main (top-level) module as well',
203 'pagesetmodule' => 'Get information about the pageset module (providing titles= and friends) as well',
204 );
205 }
206
207 public function getDescription() {
208 return 'Obtain information about certain API parameters';
209 }
210
211 protected function getExamples() {
212 return array(
213 'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo'
214 );
215 }
216
217 public function getVersion() {
218 return __CLASS__ . ': $Id$';
219 }
220 }