5d51582990859b61d13dc203d8731da97b2738aa
[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 * @addtogroup 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 'interwikimap' :
60 $filteriw = isset($params['filteriw']) ? $params['filteriw'] : false;
61 $this->appendInterwikiMap($p, $filteriw);
62 break;
63 case 'dbrepllag' :
64 $this->appendDbReplLagInfo($p, $params['showalldb']);
65 break;
66 case 'statistics' :
67 $this->appendStatistics($p);
68 break;
69 }
70 }
71 }
72
73 protected function appendGeneralInfo($property) {
74 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgLanguageCode, $IP;
75
76 $data = array ();
77 $mainPage = Title :: newFromText(wfMsgForContent('mainpage'));
78 $data['mainpage'] = $mainPage->getPrefixedText();
79 $data['base'] = $mainPage->getFullUrl();
80 $data['sitename'] = $wgSitename;
81 $data['generator'] = "MediaWiki $wgVersion";
82
83 $svn = SpecialVersion::getSvnRevision ( $IP );
84 if ( $svn ) $data['rev'] = $svn;
85
86 $data['case'] = $wgCapitalLinks ? 'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
87 if (isset($wgRightsCode))
88 $data['rightscode'] = $wgRightsCode;
89 $data['rights'] = $wgRightsText;
90 $data['lang'] = $wgLanguageCode;
91
92 $this->getResult()->addValue('query', $property, $data);
93 }
94
95 protected function appendNamespaces($property) {
96 global $wgContLang, $wgNamespacesWithSubpages;
97
98 $data = array ();
99 foreach ($wgContLang->getFormattedNamespaces() as $ns => $title) {
100 $data[$ns] = array (
101 'id' => $ns
102 );
103 ApiResult :: setContent($data[$ns], $title);
104 if(@$wgNamespacesWithSubpages[$ns])
105 $data[$ns]['subpages'] = '';
106 }
107
108 $this->getResult()->setIndexedTagName($data, 'ns');
109 $this->getResult()->addValue('query', $property, $data);
110 }
111
112 protected function appendNamespaceAliases($property) {
113 global $wgNamespaceAliases;
114
115 $data = array ();
116 foreach ($wgNamespaceAliases as $title => $ns) {
117 $item = array (
118 'id' => $ns
119 );
120 ApiResult :: setContent($item, strtr($title, '_', ' '));
121 $data[] = $item;
122 }
123
124 $this->getResult()->setIndexedTagName($data, 'ns');
125 $this->getResult()->addValue('query', $property, $data);
126 }
127
128 protected function appendInterwikiMap($property, $filter) {
129
130 $this->resetQueryParams();
131 $this->addTables('interwiki');
132 $this->addFields(array('iw_prefix', 'iw_local', 'iw_url'));
133
134 if($filter === 'local') {
135 $this->addWhere('iw_local = 1');
136 } elseif($filter === '!local') {
137 $this->addWhere('iw_local = 0');
138 } elseif($filter !== false) {
139 ApiBase :: dieDebug(__METHOD__, "Unknown filter=$filter");
140 }
141
142 $this->addOption('ORDER BY', 'iw_prefix');
143
144 $db = $this->getDB();
145 $res = $this->select(__METHOD__);
146
147 $data = array();
148 while($row = $db->fetchObject($res))
149 {
150 $val['prefix'] = $row->iw_prefix;
151 if ($row->iw_local == '1')
152 $val['local'] = '';
153 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
154 $val['url'] = $row->iw_url;
155
156 $data[] = $val;
157 }
158 $db->freeResult($res);
159
160 $this->getResult()->setIndexedTagName($data, 'iw');
161 $this->getResult()->addValue('query', $property, $data);
162 }
163
164 protected function appendDbReplLagInfo($property, $includeAll) {
165 global $wgLoadBalancer, $wgShowHostnames;
166
167 $data = array();
168
169 if ($includeAll) {
170 if (!$wgShowHostnames)
171 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
172
173 global $wgDBservers;
174 $lags = $wgLoadBalancer->getLagTimes();
175 foreach( $lags as $i => $lag ) {
176 $data[] = array (
177 'host' => $wgDBservers[$i]['host'],
178 'lag' => $lag);
179 }
180 } else {
181 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
182 $data[] = array (
183 'host' => $wgShowHostnames ? $host : '',
184 'lag' => $lag);
185 }
186
187 $result = $this->getResult();
188 $result->setIndexedTagName($data, 'db');
189 $result->addValue('query', $property, $data);
190 }
191
192 protected function appendStatistics($property) {
193 $data = array ();
194 $data['pages'] = intval(SiteStats::pages());
195 $data['articles'] = intval(SiteStats::articles());
196 $data['views'] = intval(SiteStats::views());
197 $data['edits'] = intval(SiteStats::edits());
198 $data['images'] = intval(SiteStats::images());
199 $data['users'] = intval(SiteStats::users());
200 $data['admins'] = intval(SiteStats::admins());
201 $data['jobs'] = intval(SiteStats::jobs());
202 $this->getResult()->addValue('query', $property, $data);
203 }
204
205 public function getAllowedParams() {
206 return array (
207
208 'prop' => array (
209 ApiBase :: PARAM_DFLT => 'general',
210 ApiBase :: PARAM_ISMULTI => true,
211 ApiBase :: PARAM_TYPE => array (
212 'general',
213 'namespaces',
214 'namespacealiases',
215 'interwikimap',
216 'dbrepllag',
217 'statistics',
218 )),
219
220 'filteriw' => array (
221 ApiBase :: PARAM_TYPE => array (
222 'local',
223 '!local',
224 )),
225
226 'showalldb' => false,
227 );
228 }
229
230 public function getParamDescription() {
231 return array (
232 'prop' => array (
233 'Which sysinfo properties to get:',
234 ' "general" - Overall system information',
235 ' "namespaces" - List of registered namespaces (localized)',
236 ' "namespacealiases" - List of registered namespace aliases',
237 ' "statistics" - Returns site statistics',
238 ' "interwikimap" - Returns interwiki map (optionally filtered)',
239 ' "dbrepllag" - Returns database server with the highest replication lag',
240 ),
241 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
242 'showalldb' => 'List all database servers, not just the one lagging the most',
243 );
244 }
245
246 public function getDescription() {
247 return 'Return general information about the site.';
248 }
249
250 protected function getExamples() {
251 return array(
252 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
253 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
254 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
255 );
256 }
257
258 public function getVersion() {
259 return __CLASS__ . ': $Id$';
260 }
261 }