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);
60 $filteriw = isset($params['filteriw']) ?
$params['filteriw'] : false;
61 $this->appendInterwikiMap($p, $filteriw);
64 $this->appendDbReplLagInfo($p, $params['showalldb']);
67 $this->appendStatistics($p);
73 protected function appendGeneralInfo($property) {
74 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgLanguageCode, $IP;
77 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
78 $data['mainpage'] = $mainPage->getPrefixedText();
79 $data['base'] = $mainPage->getFullUrl();
80 $data['sitename'] = $wgSitename;
81 $data['generator'] = "MediaWiki $wgVersion";
83 $svn = SpecialVersion
::getSvnRevision ( $IP );
84 if ( $svn ) $data['rev'] = $svn;
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;
92 $this->getResult()->addValue('query', $property, $data);
95 protected function appendNamespaces($property) {
96 global $wgContLang, $wgNamespacesWithSubpages;
99 foreach ($wgContLang->getFormattedNamespaces() as $ns => $title) {
103 ApiResult
:: setContent($data[$ns], $title);
104 if(@$wgNamespacesWithSubpages[$ns])
105 $data[$ns]['subpages'] = '';
108 $this->getResult()->setIndexedTagName($data, 'ns');
109 $this->getResult()->addValue('query', $property, $data);
112 protected function appendNamespaceAliases($property) {
113 global $wgNamespaceAliases;
116 foreach ($wgNamespaceAliases as $title => $ns) {
120 ApiResult
:: setContent($item, strtr($title, '_', ' '));
124 $this->getResult()->setIndexedTagName($data, 'ns');
125 $this->getResult()->addValue('query', $property, $data);
128 protected function appendInterwikiMap($property, $filter) {
130 $this->resetQueryParams();
131 $this->addTables('interwiki');
132 $this->addFields(array('iw_prefix', 'iw_local', 'iw_url'));
134 if($filter === 'local') {
135 $this->addWhere('iw_local = 1');
136 } elseif($filter === '!local') {
137 $this->addWhere('iw_local = 0');
138 } elseif($filter !== false) {
139 ApiBase
:: dieDebug(__METHOD__
, "Unknown filter=$filter");
142 $this->addOption('ORDER BY', 'iw_prefix');
144 $db = $this->getDB();
145 $res = $this->select(__METHOD__
);
148 while($row = $db->fetchObject($res))
150 $val['prefix'] = $row->iw_prefix
;
151 if ($row->iw_local
== '1')
153 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
154 $val['url'] = $row->iw_url
;
158 $db->freeResult($res);
160 $this->getResult()->setIndexedTagName($data, 'iw');
161 $this->getResult()->addValue('query', $property, $data);
164 protected function appendDbReplLagInfo($property, $includeAll) {
165 global $wgLoadBalancer, $wgShowHostnames;
170 if (!$wgShowHostnames)
171 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
174 $lags = $wgLoadBalancer->getLagTimes();
175 foreach( $lags as $i => $lag ) {
177 'host' => $wgDBservers[$i]['host'],
181 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
183 'host' => $wgShowHostnames ?
$host : '',
187 $result = $this->getResult();
188 $result->setIndexedTagName($data, 'db');
189 $result->addValue('query', $property, $data);
192 protected function appendStatistics($property) {
194 $data['pages'] = intval(SiteStats
::pages());
195 $data['articles'] = intval(SiteStats
::articles());
196 $data['views'] = intval(SiteStats
::views());
197 $data['edits'] = intval(SiteStats
::edits());
198 $data['images'] = intval(SiteStats
::images());
199 $data['users'] = intval(SiteStats
::users());
200 $data['admins'] = intval(SiteStats
::admins());
201 $data['jobs'] = intval(SiteStats
::jobs());
202 $this->getResult()->addValue('query', $property, $data);
205 public function getAllowedParams() {
209 ApiBase
:: PARAM_DFLT
=> 'general',
210 ApiBase
:: PARAM_ISMULTI
=> true,
211 ApiBase
:: PARAM_TYPE
=> array (
220 'filteriw' => array (
221 ApiBase
:: PARAM_TYPE
=> array (
226 'showalldb' => false,
230 public function getParamDescription() {
233 'Which sysinfo properties to get:',
234 ' "general" - Overall system information',
235 ' "namespaces" - List of registered namespaces (localized)',
236 ' "namespacealiases" - List of registered namespace aliases',
237 ' "statistics" - Returns site statistics',
238 ' "interwikimap" - Returns interwiki map (optionally filtered)',
239 ' "dbrepllag" - Returns database server with the highest replication lag',
241 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
242 'showalldb' => 'List all database servers, not just the one lagging the most',
246 public function getDescription() {
247 return 'Return general information about the site.';
250 protected function getExamples() {
252 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
253 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
254 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
258 public function getVersion() {
259 return __CLASS__
. ': $Id$';