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, $wgContLang;
81 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
84 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
85 $data['mainpage'] = $mainPage->getPrefixedText();
86 $data['base'] = $mainPage->getFullUrl();
87 $data['sitename'] = $wgSitename;
88 $data['generator'] = "MediaWiki $wgVersion";
90 $svn = SpecialVersion
::getSvnRevision( $IP );
94 $data['case'] = $wgCapitalLinks ?
'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
96 if( isset( $wgRightsCode ) )
97 $data['rightscode'] = $wgRightsCode;
98 $data['rights'] = $wgRightsText;
99 $data['lang'] = $wgLanguageCode;
100 if( $wgContLang->isRTL() )
102 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
105 $data['readonly'] = '';
106 if( $wgEnableWriteAPI )
107 $data['writeapi'] = '';
109 $tz = $wgLocaltimezone;
110 $offset = $wgLocalTZoffset;
111 if( is_null( $tz ) ) {
114 } elseif( is_null( $offset ) ) {
117 $data['timezone'] = $tz;
118 $data['timeoffset'] = $offset;
120 $this->getResult()->addValue( 'query', $property, $data );
123 protected function appendNamespaces( $property ) {
126 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
131 ApiResult
:: setContent( $data[$ns], $title );
132 if( MWNamespace
::hasSubpages($ns) )
133 $data[$ns]['subpages'] = '';
136 $this->getResult()->setIndexedTagName( $data, 'ns' );
137 $this->getResult()->addValue( 'query', $property, $data );
140 protected function appendNamespaceAliases( $property ) {
141 global $wgNamespaceAliases;
143 foreach( $wgNamespaceAliases as $title => $ns ) {
147 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
151 $this->getResult()->setIndexedTagName( $data, 'ns' );
152 $this->getResult()->addValue( 'query', $property, $data );
155 protected function appendSpecialPageAliases( $property ) {
158 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
160 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
161 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
164 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
165 $this->getResult()->addValue( 'query', $property, $data );
168 protected function appendInterwikiMap( $property, $filter ) {
169 $this->resetQueryParams();
170 $this->addTables( 'interwiki' );
171 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
173 if( $filter === 'local' )
174 $this->addWhere( 'iw_local = 1' );
175 elseif( $filter === '!local' )
176 $this->addWhere( 'iw_local = 0' );
177 elseif( $filter !== false )
178 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
180 $this->addOption( 'ORDER BY', 'iw_prefix' );
182 $db = $this->getDB();
183 $res = $this->select( __METHOD__
);
186 $langNames = Language
::getLanguageNames();
187 while( $row = $db->fetchObject($res) )
190 $val['prefix'] = $row->iw_prefix
;
191 if( $row->iw_local
== '1' )
193 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
194 if( isset( $langNames[$row->iw_prefix
] ) )
195 $val['language'] = $langNames[$row->iw_prefix
];
196 $val['url'] = $row->iw_url
;
200 $db->freeResult( $res );
202 $this->getResult()->setIndexedTagName( $data, 'iw' );
203 $this->getResult()->addValue( 'query', $property, $data );
206 protected function appendDbReplLagInfo( $property, $includeAll ) {
207 global $wgShowHostnames;
210 if ( !$wgShowHostnames )
211 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
214 $lags = $lb->getLagTimes();
215 foreach( $lags as $i => $lag ) {
217 'host' => $lb->getServerName( $i ),
222 list( $host, $lag ) = wfGetLB()->getMaxLag();
224 'host' => $wgShowHostnames ?
$host : '',
229 $result = $this->getResult();
230 $result->setIndexedTagName( $data, 'db' );
231 $result->addValue( 'query', $property, $data );
234 protected function appendStatistics( $property ) {
236 $data['pages'] = intval( SiteStats
::pages() );
237 $data['articles'] = intval( SiteStats
::articles() );
238 $data['views'] = intval( SiteStats
::views() );
239 $data['edits'] = intval( SiteStats
::edits() );
240 $data['images'] = intval( SiteStats
::images() );
241 $data['users'] = intval( SiteStats
::users() );
242 $data['admins'] = intval( SiteStats
::numberingroup('sysop') );
243 $data['jobs'] = intval( SiteStats
::jobs() );
244 $this->getResult()->addValue( 'query', $property, $data );
247 protected function appendUserGroups( $property ) {
248 global $wgGroupPermissions;
250 foreach( $wgGroupPermissions as $group => $permissions ) {
251 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
252 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
256 $this->getResult()->setIndexedTagName( $data, 'group' );
257 $this->getResult()->addValue( 'query', $property, $data );
260 public function getAllowedParams() {
263 ApiBase
:: PARAM_DFLT
=> 'general',
264 ApiBase
:: PARAM_ISMULTI
=> true,
265 ApiBase
:: PARAM_TYPE
=> array(
269 'specialpagealiases',
277 ApiBase
:: PARAM_TYPE
=> array(
282 'showalldb' => false,
286 public function getParamDescription() {
289 'Which sysinfo properties to get:',
290 ' "general" - Overall system information',
291 ' "namespaces" - List of registered namespaces (localized)',
292 ' "namespacealiases" - List of registered namespace aliases',
293 ' "specialpagealiases" - List of special page aliases',
294 ' "statistics" - Returns site statistics',
295 ' "interwikimap" - Returns interwiki map (optionally filtered)',
296 ' "dbrepllag" - Returns database server with the highest replication lag',
297 ' "usergroups" - Returns user groups and the associated permissions',
299 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
300 'showalldb' => 'List all database servers, not just the one lagging the most',
304 public function getDescription() {
305 return 'Return general information about the site.';
308 protected function getExamples() {
310 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
311 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
312 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
316 public function getVersion() {
317 return __CLASS__
. ': $Id$';