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