4 * Created on Sep 25, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * A query action to return meta information about the wiki site.
36 class ApiQuerySiteinfo
extends ApiQueryBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'si');
42 public function execute() {
44 $params = $this->extractRequestParams();
46 foreach ($params['prop'] as $p) {
49 ApiBase
:: dieDebug(__METHOD__
, "Unknown prop=$p");
51 $this->appendGeneralInfo($p);
54 $this->appendNamespaces($p);
56 case 'namespacealiases' :
57 $this->appendNamespaceAliases($p);
59 case 'specialpagealiases' :
60 $this->appendSpecialPageAliases($p);
63 $filteriw = isset($params['filteriw']) ?
$params['filteriw'] : false;
64 $this->appendInterwikiMap($p, $filteriw);
67 $this->appendDbReplLagInfo($p, $params['showalldb']);
70 $this->appendStatistics($p);
76 protected function appendGeneralInfo($property) {
77 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang;
80 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
81 $data['mainpage'] = $mainPage->getPrefixedText();
82 $data['base'] = $mainPage->getFullUrl();
83 $data['sitename'] = $wgSitename;
84 $data['generator'] = "MediaWiki $wgVersion";
86 $svn = SpecialVersion
::getSvnRevision ( $IP );
87 if ( $svn ) $data['rev'] = $svn;
89 $data['case'] = $wgCapitalLinks ?
'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
91 if (isset($wgRightsCode))
92 $data['rightscode'] = $wgRightsCode;
93 $data['rights'] = $wgRightsText;
94 $data['lang'] = $wgLanguageCode;
95 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
97 if ( $wgEnableWriteAPI )
98 $data['writeapi'] = '';
100 $this->getResult()->addValue('query', $property, $data);
103 protected function appendNamespaces($property) {
104 global $wgContLang, $wgNamespacesWithSubpages;
107 foreach ($wgContLang->getFormattedNamespaces() as $ns => $title) {
111 ApiResult
:: setContent($data[$ns], $title);
112 if(@$wgNamespacesWithSubpages[$ns])
113 $data[$ns]['subpages'] = '';
116 $this->getResult()->setIndexedTagName($data, 'ns');
117 $this->getResult()->addValue('query', $property, $data);
120 protected function appendNamespaceAliases($property) {
121 global $wgNamespaceAliases;
124 foreach ($wgNamespaceAliases as $title => $ns) {
128 ApiResult
:: setContent($item, strtr($title, '_', ' '));
132 $this->getResult()->setIndexedTagName($data, 'ns');
133 $this->getResult()->addValue('query', $property, $data);
136 protected function appendSpecialPageAliases($property)
140 foreach($wgLang->getSpecialPageAliases() as $specialpage => $aliases)
142 $arr = array('realname' => $specialpage, 'aliases' => $aliases);
143 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
146 $this->getResult()->setIndexedTagName($data, 'specialpage');
147 $this->getResult()->addValue('query', $property, $data);
150 protected function appendInterwikiMap($property, $filter) {
152 $this->resetQueryParams();
153 $this->addTables('interwiki');
154 $this->addFields(array('iw_prefix', 'iw_local', 'iw_url'));
156 if($filter === 'local') {
157 $this->addWhere('iw_local = 1');
158 } elseif($filter === '!local') {
159 $this->addWhere('iw_local = 0');
160 } elseif($filter !== false) {
161 ApiBase
:: dieDebug(__METHOD__
, "Unknown filter=$filter");
164 $this->addOption('ORDER BY', 'iw_prefix');
166 $db = $this->getDB();
167 $res = $this->select(__METHOD__
);
170 while($row = $db->fetchObject($res))
173 $val['prefix'] = $row->iw_prefix
;
174 if ($row->iw_local
== '1')
176 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
177 $val['url'] = $row->iw_url
;
181 $db->freeResult($res);
183 $this->getResult()->setIndexedTagName($data, 'iw');
184 $this->getResult()->addValue('query', $property, $data);
187 protected function appendDbReplLagInfo($property, $includeAll) {
188 global $wgShowHostnames;
193 if (!$wgShowHostnames)
194 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
197 $lags = wfGetLB()->getLagTimes();
198 foreach( $lags as $i => $lag ) {
200 'host' => $wgDBservers[$i]['host'],
204 list( $host, $lag ) = wfGetLB()->getMaxLag();
206 'host' => $wgShowHostnames ?
$host : '',
210 $result = $this->getResult();
211 $result->setIndexedTagName($data, 'db');
212 $result->addValue('query', $property, $data);
215 protected function appendStatistics($property) {
217 $data['pages'] = intval(SiteStats
::pages());
218 $data['articles'] = intval(SiteStats
::articles());
219 $data['views'] = intval(SiteStats
::views());
220 $data['edits'] = intval(SiteStats
::edits());
221 $data['images'] = intval(SiteStats
::images());
222 $data['users'] = intval(SiteStats
::users());
223 $data['admins'] = intval(SiteStats
::admins());
224 $data['jobs'] = intval(SiteStats
::jobs());
225 $this->getResult()->addValue('query', $property, $data);
228 public function getAllowedParams() {
232 ApiBase
:: PARAM_DFLT
=> 'general',
233 ApiBase
:: PARAM_ISMULTI
=> true,
234 ApiBase
:: PARAM_TYPE
=> array (
238 'specialpagealiases',
244 'filteriw' => array (
245 ApiBase
:: PARAM_TYPE
=> array (
250 'showalldb' => false,
254 public function getParamDescription() {
257 'Which sysinfo properties to get:',
258 ' "general" - Overall system information',
259 ' "namespaces" - List of registered namespaces (localized)',
260 ' "namespacealiases" - List of registered namespace aliases',
261 ' "specialpagealiases" - List of special page aliases',
262 ' "statistics" - Returns site statistics',
263 ' "interwikimap" - Returns interwiki map (optionally filtered)',
264 ' "dbrepllag" - Returns database server with the highest replication lag',
266 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
267 'showalldb' => 'List all database servers, not just the one lagging the most',
271 public function getDescription() {
272 return 'Return general information about the site.';
275 protected function getExamples() {
277 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
278 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
279 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
283 public function getVersion() {
284 return __CLASS__
. ': $Id$';