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