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 $this->appendMagicWords( $p );
64 $filteriw = isset( $params['filteriw'] ) ?
$params['filteriw'] : false;
65 $this->appendInterwikiMap( $p, $filteriw );
68 $this->appendDbReplLagInfo( $p, $params['showalldb'] );
71 $this->appendStatistics( $p );
74 $this->appendUserGroups( $p );
77 ApiBase
:: dieDebug( __METHOD__
, "Unknown prop=$p" );
82 protected function appendGeneralInfo( $property ) {
83 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgContLang;
84 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
87 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
88 $data['mainpage'] = $mainPage->getPrefixedText();
89 $data['base'] = $mainPage->getFullUrl();
90 $data['sitename'] = $wgSitename;
91 $data['generator'] = "MediaWiki $wgVersion";
93 $svn = SpecialVersion
::getSvnRevision( $IP );
97 $data['case'] = $wgCapitalLinks ?
'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
99 if( isset( $wgRightsCode ) )
100 $data['rightscode'] = $wgRightsCode;
101 $data['rights'] = $wgRightsText;
102 $data['lang'] = $wgLanguageCode;
103 if( $wgContLang->isRTL() )
105 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
108 $data['readonly'] = '';
109 if( $wgEnableWriteAPI )
110 $data['writeapi'] = '';
112 $tz = $wgLocaltimezone;
113 $offset = $wgLocalTZoffset;
114 if( is_null( $tz ) ) {
117 } elseif( is_null( $offset ) ) {
120 $data['timezone'] = $tz;
121 $data['timeoffset'] = $offset;
123 $this->getResult()->addValue( 'query', $property, $data );
126 protected function appendNamespaces( $property ) {
129 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
134 ApiResult
:: setContent( $data[$ns], $title );
135 if( MWNamespace
::hasSubpages($ns) )
136 $data[$ns]['subpages'] = '';
139 $this->getResult()->setIndexedTagName( $data, 'ns' );
140 $this->getResult()->addValue( 'query', $property, $data );
143 protected function appendNamespaceAliases( $property ) {
144 global $wgNamespaceAliases;
146 foreach( $wgNamespaceAliases as $title => $ns ) {
150 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
154 $this->getResult()->setIndexedTagName( $data, 'ns' );
155 $this->getResult()->addValue( 'query', $property, $data );
158 protected function appendSpecialPageAliases( $property ) {
161 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
163 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
164 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
167 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
168 $this->getResult()->addValue( 'query', $property, $data );
171 protected function appendMagicWords( $property ) {
174 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
176 $caseSensitive = array_shift($aliases);
177 $arr = array('name' => $magicword, 'aliases' => $aliases);
179 $arr['case-sensitive'] = '';
180 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
183 $this->getResult()->setIndexedTagName($data, 'magicword');
184 $this->getResult()->addValue('query', $property, $data);
188 protected function appendInterwikiMap( $property, $filter ) {
189 $this->resetQueryParams();
190 $this->addTables( 'interwiki' );
191 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
193 if( $filter === 'local' )
194 $this->addWhere( 'iw_local = 1' );
195 elseif( $filter === '!local' )
196 $this->addWhere( 'iw_local = 0' );
198 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
200 $this->addOption( 'ORDER BY', 'iw_prefix' );
202 $db = $this->getDB();
203 $res = $this->select( __METHOD__
);
206 $langNames = Language
::getLanguageNames();
207 while( $row = $db->fetchObject($res) )
210 $val['prefix'] = $row->iw_prefix
;
211 if( $row->iw_local
== '1' )
213 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
214 if( isset( $langNames[$row->iw_prefix
] ) )
215 $val['language'] = $langNames[$row->iw_prefix
];
216 $val['url'] = $row->iw_url
;
220 $db->freeResult( $res );
222 $this->getResult()->setIndexedTagName( $data, 'iw' );
223 $this->getResult()->addValue( 'query', $property, $data );
226 protected function appendDbReplLagInfo( $property, $includeAll ) {
227 global $wgShowHostnames;
230 if ( !$wgShowHostnames )
231 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
234 $lags = $lb->getLagTimes();
235 foreach( $lags as $i => $lag ) {
237 'host' => $lb->getServerName( $i ),
242 list( $host, $lag ) = wfGetLB()->getMaxLag();
244 'host' => $wgShowHostnames ?
$host : '',
249 $result = $this->getResult();
250 $result->setIndexedTagName( $data, 'db' );
251 $result->addValue( 'query', $property, $data );
254 protected function appendStatistics( $property ) {
256 $data['pages'] = intval( SiteStats
::pages() );
257 $data['articles'] = intval( SiteStats
::articles() );
258 $data['views'] = intval( SiteStats
::views() );
259 $data['edits'] = intval( SiteStats
::edits() );
260 $data['images'] = intval( SiteStats
::images() );
261 $data['users'] = intval( SiteStats
::users() );
262 $data['activeusers'] = intval( SiteStats
::activeUsers() );
263 $data['admins'] = intval( SiteStats
::numberingroup('sysop') );
264 $data['jobs'] = intval( SiteStats
::jobs() );
265 $this->getResult()->addValue( 'query', $property, $data );
268 protected function appendUserGroups( $property ) {
269 global $wgGroupPermissions;
271 foreach( $wgGroupPermissions as $group => $permissions ) {
272 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
273 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
277 $this->getResult()->setIndexedTagName( $data, 'group' );
278 $this->getResult()->addValue( 'query', $property, $data );
281 public function getAllowedParams() {
284 ApiBase
:: PARAM_DFLT
=> 'general',
285 ApiBase
:: PARAM_ISMULTI
=> true,
286 ApiBase
:: PARAM_TYPE
=> array(
290 'specialpagealiases',
299 ApiBase
:: PARAM_TYPE
=> array(
304 'showalldb' => false,
308 public function getParamDescription() {
311 'Which sysinfo properties to get:',
312 ' "general" - Overall system information',
313 ' "namespaces" - List of registered namespaces (localized)',
314 ' "namespacealiases" - List of registered namespace aliases',
315 ' "specialpagealiases" - List of special page aliases',
316 ' "magicwords" - List of magic words and their aliases',
317 ' "statistics" - Returns site statistics',
318 ' "interwikimap" - Returns interwiki map (optionally filtered)',
319 ' "dbrepllag" - Returns database server with the highest replication lag',
320 ' "usergroups" - Returns user groups and the associated permissions',
322 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
323 'showalldb' => 'List all database servers, not just the one lagging the most',
327 public function getDescription() {
328 return 'Return general information about the site.';
331 protected function getExamples() {
333 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
334 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
335 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
339 public function getVersion() {
340 return __CLASS__
. ': $Id$';