41925d1d4f5a9f3309afffc1e63614e538fd159d
[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 (C) 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 {
48 $modArr = $this->getMain()->getModules();
49 $r['modules'] = array();
50 foreach($params['modules'] as $m)
51 {
52 if(!isset($modArr[$m]))
53 {
54 $r['modules'][] = array('name' => $m, 'missing' => '');
55 continue;
56 }
57 $obj = new $modArr[$m]($this->getMain(), $m);
58 $a = $this->getClassInfo($obj);
59 $a['name'] = $m;
60 $r['modules'][] = $a;
61 }
62 $result->setIndexedTagName($r['modules'], 'module');
63 }
64 if(is_array($params['querymodules']))
65 {
66 $qmodArr = $queryObj->getModules();
67 $r['querymodules'] = array();
68 foreach($params['querymodules'] as $qm)
69 {
70 if(!isset($qmodArr[$qm]))
71 {
72 $r['querymodules'][] = array('name' => $qm, 'missing' => '');
73 continue;
74 }
75 $obj = new $qmodArr[$qm]($this, $qm);
76 $a = $this->getClassInfo($obj);
77 $a['name'] = $qm;
78 $r['querymodules'][] = $a;
79 }
80 $result->setIndexedTagName($r['querymodules'], 'module');
81 }
82 if($params['mainmodule'])
83 $r['mainmodule'] = $this->getClassInfo($this->getMain());
84 if($params['pagesetmodule'])
85 {
86 $pageSet = new ApiPageSet($queryObj);
87 $r['pagesetmodule'] = $this->getClassInfo($pageSet);
88 }
89 $result->addValue(null, $this->getModuleName(), $r);
90 }
91
92 function getClassInfo($obj)
93 {
94 $result = $this->getResult();
95 $retval['classname'] = get_class($obj);
96 $retval['description'] = implode("\n", (array)$obj->getDescription());
97 $retval['version'] = $obj->getVersion();
98 $retval['prefix'] = $obj->getModulePrefix();
99 if($obj->isReadMode())
100 $retval['readrights'] = '';
101 if($obj->isWriteMode())
102 $retval['writerights'] = '';
103 if($obj->mustBePosted())
104 $retval['mustbeposted'] = '';
105 $allowedParams = $obj->getFinalParams();
106 if(!is_array($allowedParams))
107 return $retval;
108 $retval['parameters'] = array();
109 $paramDesc = $obj->getFinalParamDescription();
110 foreach($allowedParams as $n => $p)
111 {
112 $a = array('name' => $n);
113 if(isset($paramDesc[$n]))
114 $a['description'] = implode("\n", (array)$paramDesc[$n]);
115 if(!is_array($p))
116 {
117 if(is_bool($p))
118 {
119 $a['type'] = 'bool';
120 $a['default'] = ($p ? 'true' : 'false');
121 }
122 else if(is_string($p) || is_null($p))
123 {
124 $a['type'] = 'string';
125 $a['default'] = strval($p);
126 }
127 else if(is_int($p))
128 {
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 if(isset($p[ApiBase::PARAM_ISMULTI]))
139 if($p[ApiBase::PARAM_ISMULTI])
140 {
141 $a['multi'] = '';
142 $a['limit'] = $this->getMain()->canApiHighLimits() ?
143 ApiBase::LIMIT_SML2 :
144 ApiBase::LIMIT_SML1;
145 }
146 if(isset($p[ApiBase::PARAM_ALLOW_DUPLICATES]))
147 if($p[ApiBase::PARAM_ALLOW_DUPLICATES])
148 $a['allowsduplicates'] = '';
149 if(isset($p[ApiBase::PARAM_TYPE]))
150 {
151 $a['type'] = $p[ApiBase::PARAM_TYPE];
152 if(is_array($a['type']))
153 $result->setIndexedTagName($a['type'], 't');
154 }
155 if(isset($p[ApiBase::PARAM_MAX]))
156 $a['max'] = $p[ApiBase::PARAM_MAX];
157 if(isset($p[ApiBase::PARAM_MAX2]))
158 $a['highmax'] = $p[ApiBase::PARAM_MAX2];
159 if(isset($p[ApiBase::PARAM_MIN]))
160 $a['min'] = $p[ApiBase::PARAM_MIN];
161 $retval['parameters'][] = $a;
162 }
163 $result->setIndexedTagName($retval['parameters'], 'param');
164 return $retval;
165 }
166
167 public function isReadMode() {
168 return false;
169 }
170
171 public function getAllowedParams() {
172 return array (
173 'modules' => array(
174 ApiBase :: PARAM_ISMULTI => true
175 ),
176 'querymodules' => array(
177 ApiBase :: PARAM_ISMULTI => true
178 ),
179 'mainmodule' => false,
180 'pagesetmodule' => false,
181 );
182 }
183
184 public function getParamDescription() {
185 return array (
186 'modules' => 'List of module names (value of the action= parameter)',
187 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
188 'mainmodule' => 'Get information about the main (top-level) module as well',
189 'pagesetmodule' => 'Get information about the pageset module (providing titles= and friends) as well',
190 );
191 }
192
193 public function getDescription() {
194 return 'Obtain information about certain API parameters';
195 }
196
197 protected function getExamples() {
198 return array (
199 'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo'
200 );
201 }
202
203 public function getVersion() {
204 return __CLASS__ . ': $Id$';
205 }
206 }