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 $this->appendExtensions( $p );
79 case 'fileextensions':
80 $this->appendFileExtensions( $p );
83 ApiBase
:: dieDebug( __METHOD__
, "Unknown prop=$p" );
88 protected function appendGeneralInfo( $property ) {
89 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgContLang;
90 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
93 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
94 $data['mainpage'] = $mainPage->getPrefixedText();
95 $data['base'] = $mainPage->getFullUrl();
96 $data['sitename'] = $wgSitename;
97 $data['generator'] = "MediaWiki $wgVersion";
99 $svn = SpecialVersion
::getSvnRevision( $IP );
103 $data['case'] = $wgCapitalLinks ?
'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
105 if( isset( $wgRightsCode ) )
106 $data['rightscode'] = $wgRightsCode;
107 $data['rights'] = $wgRightsText;
108 $data['lang'] = $wgLanguageCode;
109 if( $wgContLang->isRTL() )
111 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
114 $data['readonly'] = '';
115 if( $wgEnableWriteAPI )
116 $data['writeapi'] = '';
118 $tz = $wgLocaltimezone;
119 $offset = $wgLocalTZoffset;
120 if( is_null( $tz ) ) {
123 } elseif( is_null( $offset ) ) {
126 $data['timezone'] = $tz;
127 $data['timeoffset'] = $offset;
129 $this->getResult()->addValue( 'query', $property, $data );
132 protected function appendNamespaces( $property ) {
135 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
140 ApiResult
:: setContent( $data[$ns], $title );
141 $canonical = MWNamespace
::getCanonicalName( $ns );
143 if( MWNamespace
::hasSubpages( $ns ) )
144 $data[$ns]['subpages'] = '';
147 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
150 $this->getResult()->setIndexedTagName( $data, 'ns' );
151 $this->getResult()->addValue( 'query', $property, $data );
154 protected function appendNamespaceAliases( $property ) {
155 global $wgNamespaceAliases, $wgContLang;
157 $aliases = array_merge($wgNamespaceAliases, $wgContLang->namespaceAliases
);
159 foreach( $aliases as $title => $ns ) {
163 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
167 $this->getResult()->setIndexedTagName( $data, 'ns' );
168 $this->getResult()->addValue( 'query', $property, $data );
171 protected function appendSpecialPageAliases( $property ) {
174 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
176 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
177 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
180 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
181 $this->getResult()->addValue( 'query', $property, $data );
184 protected function appendMagicWords( $property ) {
187 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
189 $caseSensitive = array_shift($aliases);
190 $arr = array('name' => $magicword, 'aliases' => $aliases);
192 $arr['case-sensitive'] = '';
193 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
196 $this->getResult()->setIndexedTagName($data, 'magicword');
197 $this->getResult()->addValue('query', $property, $data);
200 protected function appendInterwikiMap( $property, $filter ) {
201 $this->resetQueryParams();
202 $this->addTables( 'interwiki' );
203 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
205 if( $filter === 'local' )
206 $this->addWhere( 'iw_local = 1' );
207 elseif( $filter === '!local' )
208 $this->addWhere( 'iw_local = 0' );
210 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
212 $this->addOption( 'ORDER BY', 'iw_prefix' );
214 $db = $this->getDB();
215 $res = $this->select( __METHOD__
);
218 $langNames = Language
::getLanguageNames();
219 while( $row = $db->fetchObject($res) )
222 $val['prefix'] = $row->iw_prefix
;
223 if( $row->iw_local
== '1' )
225 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
226 if( isset( $langNames[$row->iw_prefix
] ) )
227 $val['language'] = $langNames[$row->iw_prefix
];
228 $val['url'] = $row->iw_url
;
232 $db->freeResult( $res );
234 $this->getResult()->setIndexedTagName( $data, 'iw' );
235 $this->getResult()->addValue( 'query', $property, $data );
238 protected function appendDbReplLagInfo( $property, $includeAll ) {
239 global $wgShowHostnames;
242 if ( !$wgShowHostnames )
243 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
246 $lags = $lb->getLagTimes();
247 foreach( $lags as $i => $lag ) {
249 'host' => $lb->getServerName( $i ),
254 list( $host, $lag ) = wfGetLB()->getMaxLag();
256 'host' => $wgShowHostnames ?
$host : '',
261 $result = $this->getResult();
262 $result->setIndexedTagName( $data, 'db' );
263 $result->addValue( 'query', $property, $data );
266 protected function appendStatistics( $property ) {
268 $data['pages'] = intval( SiteStats
::pages() );
269 $data['articles'] = intval( SiteStats
::articles() );
270 $data['views'] = intval( SiteStats
::views() );
271 $data['edits'] = intval( SiteStats
::edits() );
272 $data['images'] = intval( SiteStats
::images() );
273 $data['users'] = intval( SiteStats
::users() );
274 $data['activeusers'] = intval( SiteStats
::activeUsers() );
275 $data['admins'] = intval( SiteStats
::numberingroup('sysop') );
276 $data['jobs'] = intval( SiteStats
::jobs() );
277 $this->getResult()->addValue( 'query', $property, $data );
280 protected function appendUserGroups( $property ) {
281 global $wgGroupPermissions;
283 foreach( $wgGroupPermissions as $group => $permissions ) {
284 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
285 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
289 $this->getResult()->setIndexedTagName( $data, 'group' );
290 $this->getResult()->addValue( 'query', $property, $data );
293 protected function appendFileExtensions( $property ) {
294 global $wgFileExtensions;
297 foreach( $wgFileExtensions as $ext ) {
298 $data[] = array( 'ext' => $ext );
300 $this->getResult()->setIndexedTagName( $data, 'fe' );
301 $this->getResult()->addValue( 'query', $property, $data );
304 protected function appendExtensions( $property ) {
305 global $wgExtensionCredits;
307 foreach ( $wgExtensionCredits as $type => $extensions ) {
308 foreach ( $extensions as $ext ) {
310 $ret['type'] = $type;
311 if ( isset( $ext['name'] ) )
312 $ret['name'] = $ext['name'];
313 if ( isset( $ext['description'] ) )
314 $ret['description'] = $ext['description'];
315 if ( isset( $ext['descriptionmsg'] ) )
316 $ret['descriptionmsg'] = $ext['descriptionmsg'];
317 if ( isset( $ext['author'] ) ) {
318 $ret['author'] = is_array( $ext['author'] ) ?
319 implode( ', ', $ext['author' ] ) : $ext['author'];
321 if ( isset( $ext['version'] ) ) {
322 $ret['version'] = $ext['version'];
323 } elseif ( isset( $ext['svn-revision'] ) &&
324 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
325 $ext['svn-revision'], $m ) )
327 $ret['version'] = 'r' . $m[1];
333 $this->getResult()->setIndexedTagName( $data, 'ext' );
334 $this->getResult()->addValue( 'query', $property, $data );
338 public function getAllowedParams() {
341 ApiBase
:: PARAM_DFLT
=> 'general',
342 ApiBase
:: PARAM_ISMULTI
=> true,
343 ApiBase
:: PARAM_TYPE
=> array(
347 'specialpagealiases',
358 ApiBase
:: PARAM_TYPE
=> array(
363 'showalldb' => false,
367 public function getParamDescription() {
370 'Which sysinfo properties to get:',
371 ' "general" - Overall system information',
372 ' "namespaces" - List of registered namespaces and their canonical names',
373 ' "namespacealiases" - List of registered namespace aliases',
374 ' "specialpagealiases" - List of special page aliases',
375 ' "magicwords" - List of magic words and their aliases',
376 ' "statistics" - Returns site statistics',
377 ' "interwikimap" - Returns interwiki map (optionally filtered)',
378 ' "dbrepllag" - Returns database server with the highest replication lag',
379 ' "usergroups" - Returns user groups and the associated permissions',
380 ' "extensions" - Returns extensions installed on the wiki',
381 ' "fileextensions" - Returns list of file extensions allowed to be uploaded',
383 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
384 'showalldb' => 'List all database servers, not just the one lagging the most',
388 public function getDescription() {
389 return 'Return general information about the site.';
392 protected function getExamples() {
394 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
395 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
396 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
400 public function getVersion() {
401 return __CLASS__
. ': $Id$';