API: Requesting a token you aren't allowed to request no longer dies with an error...
[lhc/web/wiklou.git] / includes / api / ApiQuerySiteinfo.php
1 <?php
2
3 /*
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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 ('ApiQueryBase.php');
29 }
30
31 /**
32 * A query action to return meta information about the wiki site.
33 *
34 * @ingroup API
35 */
36 class ApiQuerySiteinfo extends ApiQueryBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'si');
40 }
41
42 public function execute() {
43
44 $params = $this->extractRequestParams();
45
46 foreach ($params['prop'] as $p) {
47 switch ($p) {
48 default :
49 ApiBase :: dieDebug(__METHOD__, "Unknown prop=$p");
50 case 'general' :
51 $this->appendGeneralInfo($p);
52 break;
53 case 'namespaces' :
54 $this->appendNamespaces($p);
55 break;
56 case 'namespacealiases' :
57 $this->appendNamespaceAliases($p);
58 break;
59 case 'specialpagealiases' :
60 $this->appendSpecialPageAliases($p);
61 break;
62 case 'interwikimap' :
63 $filteriw = isset($params['filteriw']) ? $params['filteriw'] : false;
64 $this->appendInterwikiMap($p, $filteriw);
65 break;
66 case 'dbrepllag' :
67 $this->appendDbReplLagInfo($p, $params['showalldb']);
68 break;
69 case 'statistics' :
70 $this->appendStatistics($p);
71 break;
72 case 'usergroups' :
73 $this->appendUserGroups($p);
74 break;
75 }
76 }
77 }
78
79 protected function appendGeneralInfo($property) {
80 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang;
81
82 $data = array ();
83 $mainPage = Title :: newFromText(wfMsgForContent('mainpage'));
84 $data['mainpage'] = $mainPage->getPrefixedText();
85 $data['base'] = $mainPage->getFullUrl();
86 $data['sitename'] = $wgSitename;
87 $data['generator'] = "MediaWiki $wgVersion";
88
89 $svn = SpecialVersion::getSvnRevision ( $IP );
90 if ( $svn ) $data['rev'] = $svn;
91
92 $data['case'] = $wgCapitalLinks ? 'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
93
94 if (isset($wgRightsCode))
95 $data['rightscode'] = $wgRightsCode;
96 $data['rights'] = $wgRightsText;
97 $data['lang'] = $wgLanguageCode;
98 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
99
100 if ( $wgEnableWriteAPI )
101 $data['writeapi'] = '';
102
103 $this->getResult()->addValue('query', $property, $data);
104 }
105
106 protected function appendNamespaces($property) {
107 global $wgContLang, $wgNamespacesWithSubpages;
108
109 $data = array ();
110 foreach ($wgContLang->getFormattedNamespaces() as $ns => $title) {
111 $data[$ns] = array (
112 'id' => $ns
113 );
114 ApiResult :: setContent($data[$ns], $title);
115 if(@$wgNamespacesWithSubpages[$ns])
116 $data[$ns]['subpages'] = '';
117 }
118
119 $this->getResult()->setIndexedTagName($data, 'ns');
120 $this->getResult()->addValue('query', $property, $data);
121 }
122
123 protected function appendNamespaceAliases($property) {
124 global $wgNamespaceAliases;
125
126 $data = array ();
127 foreach ($wgNamespaceAliases as $title => $ns) {
128 $item = array (
129 'id' => $ns
130 );
131 ApiResult :: setContent($item, strtr($title, '_', ' '));
132 $data[] = $item;
133 }
134
135 $this->getResult()->setIndexedTagName($data, 'ns');
136 $this->getResult()->addValue('query', $property, $data);
137 }
138
139 protected function appendSpecialPageAliases($property)
140 {
141 global $wgLang;
142 $data = array();
143 foreach($wgLang->getSpecialPageAliases() as $specialpage => $aliases)
144 {
145 $arr = array('realname' => $specialpage, 'aliases' => $aliases);
146 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
147 $data[] = $arr;
148 }
149 $this->getResult()->setIndexedTagName($data, 'specialpage');
150 $this->getResult()->addValue('query', $property, $data);
151 }
152
153 protected function appendInterwikiMap($property, $filter) {
154
155 $this->resetQueryParams();
156 $this->addTables('interwiki');
157 $this->addFields(array('iw_prefix', 'iw_local', 'iw_url'));
158
159 if($filter === 'local') {
160 $this->addWhere('iw_local = 1');
161 } elseif($filter === '!local') {
162 $this->addWhere('iw_local = 0');
163 } elseif($filter !== false) {
164 ApiBase :: dieDebug(__METHOD__, "Unknown filter=$filter");
165 }
166
167 $this->addOption('ORDER BY', 'iw_prefix');
168
169 $db = $this->getDB();
170 $res = $this->select(__METHOD__);
171
172 $data = array();
173 $langNames = Language::getLanguageNames();
174 while($row = $db->fetchObject($res))
175 {
176 $val = array();
177 $val['prefix'] = $row->iw_prefix;
178 if($row->iw_local == '1')
179 $val['local'] = '';
180 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
181 if(isset($langNames[$row->iw_prefix]))
182 $val['language'] = $langNames[$row->iw_prefix];
183 $val['url'] = $row->iw_url;
184
185 $data[] = $val;
186 }
187 $db->freeResult($res);
188
189 $this->getResult()->setIndexedTagName($data, 'iw');
190 $this->getResult()->addValue('query', $property, $data);
191 }
192
193 protected function appendDbReplLagInfo($property, $includeAll) {
194 global $wgShowHostnames;
195
196 $data = array();
197
198 if ($includeAll) {
199 if (!$wgShowHostnames)
200 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
201
202 $lb = wfGetLB();
203 $lags = $lb->getLagTimes();
204 foreach( $lags as $i => $lag ) {
205 $data[] = array (
206 'host' => $lb->getServerName( $i ),
207 'lag' => $lag);
208 }
209 } else {
210 list( $host, $lag ) = wfGetLB()->getMaxLag();
211 $data[] = array (
212 'host' => $wgShowHostnames ? $host : '',
213 'lag' => $lag);
214 }
215
216 $result = $this->getResult();
217 $result->setIndexedTagName($data, 'db');
218 $result->addValue('query', $property, $data);
219 }
220
221 protected function appendStatistics($property) {
222 $data = array ();
223 $data['pages'] = intval(SiteStats::pages());
224 $data['articles'] = intval(SiteStats::articles());
225 $data['views'] = intval(SiteStats::views());
226 $data['edits'] = intval(SiteStats::edits());
227 $data['images'] = intval(SiteStats::images());
228 $data['users'] = intval(SiteStats::users());
229 $data['admins'] = intval(SiteStats::admins());
230 $data['jobs'] = intval(SiteStats::jobs());
231 $this->getResult()->addValue('query', $property, $data);
232 }
233
234 protected function appendUserGroups($property) {
235 global $wgGroupPermissions;
236
237 $data = array ();
238 foreach ($wgGroupPermissions as $group => $permissions) {
239 $arr = array ('name' => $group, 'rights' => array_keys($permissions, true));
240 $this->getResult()->setIndexedTagName($arr['rights'], 'permission');
241 $data[] = $arr;
242 }
243
244 $this->getResult()->setIndexedTagName($data, 'group');
245 $this->getResult()->addValue('query', $property, $data);
246 }
247
248 public function getAllowedParams() {
249 return array (
250
251 'prop' => array (
252 ApiBase :: PARAM_DFLT => 'general',
253 ApiBase :: PARAM_ISMULTI => true,
254 ApiBase :: PARAM_TYPE => array (
255 'general',
256 'namespaces',
257 'namespacealiases',
258 'specialpagealiases',
259 'interwikimap',
260 'dbrepllag',
261 'statistics',
262 'usergroups',
263 )),
264
265 'filteriw' => array (
266 ApiBase :: PARAM_TYPE => array (
267 'local',
268 '!local',
269 )),
270
271 'showalldb' => false,
272 );
273 }
274
275 public function getParamDescription() {
276 return array (
277 'prop' => array (
278 'Which sysinfo properties to get:',
279 ' "general" - Overall system information',
280 ' "namespaces" - List of registered namespaces (localized)',
281 ' "namespacealiases" - List of registered namespace aliases',
282 ' "specialpagealiases" - List of special page aliases',
283 ' "statistics" - Returns site statistics',
284 ' "interwikimap" - Returns interwiki map (optionally filtered)',
285 ' "dbrepllag" - Returns database server with the highest replication lag',
286 ' "usergroups" - Returns user groups and the associated permissions',
287 ),
288 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
289 'showalldb' => 'List all database servers, not just the one lagging the most',
290 );
291 }
292
293 public function getDescription() {
294 return 'Return general information about the site.';
295 }
296
297 protected function getExamples() {
298 return array(
299 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
300 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
301 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
302 );
303 }
304
305 public function getVersion() {
306 return __CLASS__ . ': $Id$';
307 }
308 }