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