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() {
43 $params = $this->extractRequestParams();
44 foreach($params['prop'] as $p)
49 $this->appendGeneralInfo($p);
52 $this->appendNamespaces($p);
54 case 'namespacealiases' :
55 $this->appendNamespaceAliases($p);
57 case 'specialpagealiases' :
58 $this->appendSpecialPageAliases($p);
61 $filteriw = isset($params['filteriw']) ?
$params['filteriw'] : false;
62 $this->appendInterwikiMap($p, $filteriw);
65 $this->appendDbReplLagInfo($p, $params['showalldb']);
68 $this->appendStatistics($p);
71 $this->appendUserGroups($p);
74 ApiBase
:: dieDebug(__METHOD__
, "Unknown prop=$p");
79 protected function appendGeneralInfo($property) {
80 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang;
83 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
84 $data['mainpage'] = $mainPage->getPrefixedText();
85 $data['base'] = $mainPage->getFullUrl();
86 $data['sitename'] = $wgSitename;
87 $data['generator'] = "MediaWiki $wgVersion";
89 $svn = SpecialVersion
::getSvnRevision($IP);
93 $data['case'] = $wgCapitalLinks ?
'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
95 if(isset($wgRightsCode))
96 $data['rightscode'] = $wgRightsCode;
97 $data['rights'] = $wgRightsText;
98 $data['lang'] = $wgLanguageCode;
99 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
102 $data['readonly'] = '';
103 if($wgEnableWriteAPI)
104 $data['writeapi'] = '';
106 $this->getResult()->addValue('query', $property, $data);
109 protected function appendNamespaces($property) {
112 foreach($wgContLang->getFormattedNamespaces() as $ns => $title)
117 ApiResult
:: setContent($data[$ns], $title);
118 if(MWNamespace
::hasSubpages($ns))
119 $data[$ns]['subpages'] = '';
122 $this->getResult()->setIndexedTagName($data, 'ns');
123 $this->getResult()->addValue('query', $property, $data);
126 protected function appendNamespaceAliases($property) {
127 global $wgNamespaceAliases;
129 foreach($wgNamespaceAliases as $title => $ns) {
133 ApiResult
:: setContent($item, strtr($title, '_', ' '));
137 $this->getResult()->setIndexedTagName($data, 'ns');
138 $this->getResult()->addValue('query', $property, $data);
141 protected function appendSpecialPageAliases($property)
145 foreach($wgLang->getSpecialPageAliases() as $specialpage => $aliases)
147 $arr = array('realname' => $specialpage, 'aliases' => $aliases);
148 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
151 $this->getResult()->setIndexedTagName($data, 'specialpage');
152 $this->getResult()->addValue('query', $property, $data);
155 protected function appendInterwikiMap($property, $filter) {
157 $this->resetQueryParams();
158 $this->addTables('interwiki');
159 $this->addFields(array('iw_prefix', 'iw_local', 'iw_url'));
161 if($filter === 'local')
162 $this->addWhere('iw_local = 1');
163 elseif($filter === '!local')
164 $this->addWhere('iw_local = 0');
165 elseif($filter !== false)
166 ApiBase
:: dieDebug(__METHOD__
, "Unknown filter=$filter");
168 $this->addOption('ORDER BY', 'iw_prefix');
170 $db = $this->getDB();
171 $res = $this->select(__METHOD__
);
174 $langNames = Language
::getLanguageNames();
175 while($row = $db->fetchObject($res))
178 $val['prefix'] = $row->iw_prefix
;
179 if($row->iw_local
== '1')
181 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
182 if(isset($langNames[$row->iw_prefix
]))
183 $val['language'] = $langNames[$row->iw_prefix
];
184 $val['url'] = $row->iw_url
;
188 $db->freeResult($res);
190 $this->getResult()->setIndexedTagName($data, 'iw');
191 $this->getResult()->addValue('query', $property, $data);
194 protected function appendDbReplLagInfo($property, $includeAll) {
195 global $wgShowHostnames;
198 if (!$wgShowHostnames)
199 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
202 $lags = $lb->getLagTimes();
203 foreach( $lags as $i => $lag ) {
205 'host' => $lb->getServerName( $i ),
209 list( $host, $lag ) = wfGetLB()->getMaxLag();
211 'host' => $wgShowHostnames ?
$host : '',
215 $result = $this->getResult();
216 $result->setIndexedTagName($data, 'db');
217 $result->addValue('query', $property, $data);
220 protected function appendStatistics($property) {
222 $data['pages'] = intval(SiteStats
::pages());
223 $data['articles'] = intval(SiteStats
::articles());
224 $data['views'] = intval(SiteStats
::views());
225 $data['edits'] = intval(SiteStats
::edits());
226 $data['images'] = intval(SiteStats
::images());
227 $data['users'] = intval(SiteStats
::users());
228 $data['admins'] = intval(SiteStats
::admins());
229 $data['jobs'] = intval(SiteStats
::jobs());
230 $this->getResult()->addValue('query', $property, $data);
233 protected function appendUserGroups($property) {
234 global $wgGroupPermissions;
236 foreach ($wgGroupPermissions as $group => $permissions) {
237 $arr = array ('name' => $group, 'rights' => array_keys($permissions, true));
238 $this->getResult()->setIndexedTagName($arr['rights'], 'permission');
242 $this->getResult()->setIndexedTagName($data, 'group');
243 $this->getResult()->addValue('query', $property, $data);
246 public function getAllowedParams() {
249 ApiBase
:: PARAM_DFLT
=> 'general',
250 ApiBase
:: PARAM_ISMULTI
=> true,
251 ApiBase
:: PARAM_TYPE
=> array (
255 'specialpagealiases',
262 'filteriw' => array (
263 ApiBase
:: PARAM_TYPE
=> array (
268 'showalldb' => false,
272 public function getParamDescription() {
275 'Which sysinfo properties to get:',
276 ' "general" - Overall system information',
277 ' "namespaces" - List of registered namespaces (localized)',
278 ' "namespacealiases" - List of registered namespace aliases',
279 ' "specialpagealiases" - List of special page aliases',
280 ' "statistics" - Returns site statistics',
281 ' "interwikimap" - Returns interwiki map (optionally filtered)',
282 ' "dbrepllag" - Returns database server with the highest replication lag',
283 ' "usergroups" - Returns user groups and the associated permissions',
285 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
286 'showalldb' => 'List all database servers, not just the one lagging the most',
290 public function getDescription() {
291 return 'Return general information about the site.';
294 protected function getExamples() {
296 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
297 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
298 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
302 public function getVersion() {
303 return __CLASS__
. ': $Id$';