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