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