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