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