API: Added meta=userinfo module to get data about the currently logged-in user.
[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->resetQueryParams();
103 $this->addTables('interwiki');
104 $this->addFields(array('iw_prefix', 'iw_local', 'iw_url'));
105
106 if($filter === 'local')
107 $this->addWhere('iw_local = 1');
108 else if($filter === '!local')
109 $this->addWhere('iw_local = 0');
110 else if($filter !== false)
111 ApiBase :: dieDebug(__METHOD__, "Unknown filter=$filter");
112
113 $this->addOption('ORDER BY', 'iw_prefix');
114
115 $db = $this->getDB();
116 $res = $this->select(__METHOD__);
117
118 $data = array();
119 while($row = $db->fetchObject($res))
120 {
121 $val['prefix'] = $row->iw_prefix;
122 if ($row->iw_local == '1')
123 $val['local'] = '';
124 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
125 $val['url'] = $row->iw_url;
126
127 $data[] = $val;
128 }
129 $db->freeResult($res);
130
131 $this->getResult()->setIndexedTagName($data, 'iw');
132 $this->getResult()->addValue('query', $property, $data);
133 }
134
135 protected function appendDbReplLagInfo($property, $includeAll) {
136 global $wgLoadBalancer, $wgShowHostnames;
137
138 $data = array();
139
140 if ($includeAll) {
141 if (!$wgShowHostnames)
142 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
143
144 global $wgDBservers;
145 $lags = $wgLoadBalancer->getLagTimes();
146 foreach( $lags as $i => $lag ) {
147 $data[] = array (
148 'host' => $wgDBservers[$i]['host'],
149 'lag' => $lag);
150 }
151 } else {
152 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
153 $data[] = array (
154 'host' => $wgShowHostnames ? $host : '',
155 'lag' => $lag);
156 }
157
158 $result = $this->getResult();
159 $result->setIndexedTagName($data, 'db');
160 $result->addValue('query', $property, $data);
161 }
162
163 protected function getAllowedParams() {
164 return array (
165
166 'prop' => array (
167 ApiBase :: PARAM_DFLT => 'general',
168 ApiBase :: PARAM_ISMULTI => true,
169 ApiBase :: PARAM_TYPE => array (
170 'general',
171 'namespaces',
172 'interwikimap',
173 'dbrepllag',
174 )),
175
176 'filteriw' => array (
177 ApiBase :: PARAM_TYPE => array (
178 'local',
179 '!local',
180 )),
181
182 'showalldb' => false,
183 );
184 }
185
186 protected function getParamDescription() {
187 return array (
188 'prop' => array (
189 'Which sysinfo properties to get:',
190 ' "general" - Overall system information',
191 ' "namespaces" - List of registered namespaces (localized)',
192 ' "interwikimap" - Return interwiki map (optionally filtered)',
193 ' "dbrepllag" - Returns DB server with the highest replication lag',
194 ),
195 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
196 'showalldb' => 'List all DB servers, not just the one lagging the most',
197 );
198 }
199
200 protected function getDescription() {
201 return 'Return general information about the site.';
202 }
203
204 protected function getExamples() {
205 return array(
206 'api.php?action=query&meta=siteinfo&siprop=general|namespaces',
207 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
208 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
209 );
210 }
211
212 public function getVersion() {
213 return __CLASS__ . ': $Id$';
214 }
215 }
216