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();
45 foreach( $params['prop'] as $p )
50 $fit = $this->appendGeneralInfo( $p );
53 $fit = $this->appendNamespaces( $p );
55 case 'namespacealiases':
56 $fit = $this->appendNamespaceAliases( $p );
58 case 'specialpagealiases':
59 $fit = $this->appendSpecialPageAliases( $p );
62 $fit = $this->appendMagicWords( $p );
65 $filteriw = isset( $params['filteriw'] ) ?
$params['filteriw'] : false;
66 $fit = $this->appendInterwikiMap( $p, $filteriw );
69 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
72 $fit = $this->appendStatistics( $p );
75 $fit = $this->appendUserGroups( $p );
78 $fit = $this->appendExtensions( $p );
80 case 'fileextensions':
81 $fit = $this->appendFileExtensions( $p );
84 $fit = $this->appendRightsInfo( $p );
87 ApiBase
:: dieDebug( __METHOD__
, "Unknown prop=$p" );
91 # Abuse siprop as a query-continue parameter
92 # and set it to all unprocessed props
93 $this->setContinueEnumParameter('prop', implode('|',
94 array_diff($params['prop'], $done)));
101 protected function appendGeneralInfo( $property ) {
106 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
107 $data['mainpage'] = $mainPage->getPrefixedText();
108 $data['base'] = $mainPage->getFullUrl();
109 $data['sitename'] = $GLOBALS['wgSitename'];
110 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
111 $data['phpversion'] = phpversion();
112 $data['phpsapi'] = php_sapi_name();
113 $data['dbclass'] = get_class($this->getDB());
114 $data['dbversion'] = $this->getDB()->getServerVersion();
116 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
120 // 'case-insensitive' option is reserved for future
121 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
123 if( isset( $GLOBALS['wgRightsCode'] ) )
124 $data['rightscode'] = $GLOBALS['wgRightsCode'];
125 $data['rights'] = $GLOBALS['wgRightsText'];
126 $data['lang'] = $GLOBALS['wgLanguageCode'];
127 if( $wgContLang->isRTL() )
129 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
132 $data['readonly'] = '';
133 $data['readonlyreason'] = wfReadOnlyReason();
135 if( $GLOBALS['wgEnableWriteAPI'] )
136 $data['writeapi'] = '';
138 $tz = $GLOBALS['wgLocaltimezone'];
139 $offset = $GLOBALS['wgLocalTZoffset'];
140 if( is_null( $tz ) ) {
143 } elseif( is_null( $offset ) ) {
146 $data['timezone'] = $tz;
147 $data['timeoffset'] = intval($offset);
148 $data['articlepath'] = $GLOBALS['wgArticlePath'];
149 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
150 $data['script'] = $GLOBALS['wgScript'];
151 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
152 $data['server'] = $GLOBALS['wgServer'];
153 $data['wikiid'] = wfWikiID();
155 return $this->getResult()->addValue( 'query', $property, $data );
158 protected function appendNamespaces( $property ) {
161 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
166 ApiResult
:: setContent( $data[$ns], $title );
167 $canonical = MWNamespace
::getCanonicalName( $ns );
169 if( MWNamespace
::hasSubpages( $ns ) )
170 $data[$ns]['subpages'] = '';
173 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
176 $this->getResult()->setIndexedTagName( $data, 'ns' );
177 return $this->getResult()->addValue( 'query', $property, $data );
180 protected function appendNamespaceAliases( $property ) {
181 global $wgNamespaceAliases, $wgContLang;
183 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->namespaceAliases
);
184 $namespaces = $wgContLang->getNamespaces();
186 foreach( $aliases as $title => $ns ) {
187 if( $namespaces[$ns] == $title ) {
188 // Don't list duplicates
194 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
198 $this->getResult()->setIndexedTagName( $data, 'ns' );
199 return $this->getResult()->addValue( 'query', $property, $data );
202 protected function appendSpecialPageAliases( $property ) {
205 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
207 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
208 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
211 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
212 return $this->getResult()->addValue( 'query', $property, $data );
215 protected function appendMagicWords( $property ) {
218 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
220 $caseSensitive = array_shift($aliases);
221 $arr = array('name' => $magicword, 'aliases' => $aliases);
223 $arr['case-sensitive'] = '';
224 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
227 $this->getResult()->setIndexedTagName($data, 'magicword');
228 return $this->getResult()->addValue( 'query', $property, $data );
231 protected function appendInterwikiMap( $property, $filter ) {
232 $this->resetQueryParams();
233 $this->addTables( 'interwiki' );
234 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
236 if( $filter === 'local' )
237 $this->addWhere( 'iw_local = 1' );
238 elseif( $filter === '!local' )
239 $this->addWhere( 'iw_local = 0' );
241 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
243 $this->addOption( 'ORDER BY', 'iw_prefix' );
245 $db = $this->getDB();
246 $res = $this->select( __METHOD__
);
249 $langNames = Language
::getLanguageNames();
250 while( $row = $db->fetchObject($res) )
253 $val['prefix'] = $row->iw_prefix
;
254 if( $row->iw_local
== '1' )
256 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
257 if( isset( $langNames[$row->iw_prefix
] ) )
258 $val['language'] = $langNames[$row->iw_prefix
];
259 $val['url'] = $row->iw_url
;
263 $db->freeResult( $res );
265 $this->getResult()->setIndexedTagName( $data, 'iw' );
266 return $this->getResult()->addValue( 'query', $property, $data );
269 protected function appendDbReplLagInfo( $property, $includeAll ) {
270 global $wgShowHostnames;
273 if ( !$wgShowHostnames )
274 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
277 $lags = $lb->getLagTimes();
278 foreach( $lags as $i => $lag ) {
280 'host' => $lb->getServerName( $i ),
285 list( $host, $lag ) = wfGetLB()->getMaxLag();
287 'host' => $wgShowHostnames ?
$host : '',
288 'lag' => intval( $lag )
292 $result = $this->getResult();
293 $result->setIndexedTagName( $data, 'db' );
294 return $this->getResult()->addValue( 'query', $property, $data );
297 protected function appendStatistics( $property ) {
298 global $wgDisableCounters;
300 $data['pages'] = intval( SiteStats
::pages() );
301 $data['articles'] = intval( SiteStats
::articles() );
302 if ( !$wgDisableCounters ) {
303 $data['views'] = intval( SiteStats
::views() );
305 $data['edits'] = intval( SiteStats
::edits() );
306 $data['images'] = intval( SiteStats
::images() );
307 $data['users'] = intval( SiteStats
::users() );
308 $data['activeusers'] = intval( SiteStats
::activeUsers() );
309 $data['admins'] = intval( SiteStats
::numberingroup('sysop') );
310 $data['jobs'] = intval( SiteStats
::jobs() );
311 return $this->getResult()->addValue( 'query', $property, $data );
314 protected function appendUserGroups( $property ) {
315 global $wgGroupPermissions;
317 foreach( $wgGroupPermissions as $group => $permissions ) {
318 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
319 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
323 $this->getResult()->setIndexedTagName( $data, 'group' );
324 return $this->getResult()->addValue( 'query', $property, $data );
327 protected function appendFileExtensions( $property ) {
328 global $wgFileExtensions;
331 foreach( $wgFileExtensions as $ext ) {
332 $data[] = array( 'ext' => $ext );
334 $this->getResult()->setIndexedTagName( $data, 'fe' );
335 return $this->getResult()->addValue( 'query', $property, $data );
338 protected function appendExtensions( $property ) {
339 global $wgExtensionCredits;
341 foreach ( $wgExtensionCredits as $type => $extensions ) {
342 foreach ( $extensions as $ext ) {
344 $ret['type'] = $type;
345 if ( isset( $ext['name'] ) )
346 $ret['name'] = $ext['name'];
347 if ( isset( $ext['description'] ) )
348 $ret['description'] = $ext['description'];
349 if ( isset( $ext['descriptionmsg'] ) )
350 $ret['descriptionmsg'] = $ext['descriptionmsg'];
351 if ( isset( $ext['author'] ) ) {
352 $ret['author'] = is_array( $ext['author'] ) ?
353 implode( ', ', $ext['author' ] ) : $ext['author'];
355 if ( isset( $ext['version'] ) ) {
356 $ret['version'] = $ext['version'];
357 } elseif ( isset( $ext['svn-revision'] ) &&
358 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
359 $ext['svn-revision'], $m ) )
361 $ret['version'] = 'r' . $m[1];
367 $this->getResult()->setIndexedTagName( $data, 'ext' );
368 return $this->getResult()->addValue( 'query', $property, $data );
372 protected function appendRightsInfo( $property ) {
373 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
374 $title = Title
::newFromText( $wgRightsPage );
375 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
376 $text = $wgRightsText;
377 if( !$text && $title ) {
378 $text = $title->getPrefixedText();
382 'url' => $url ?
$url : '',
383 'text' => $text ?
$text : ''
386 return $this->getResult()->addValue( 'query', $property, $data );
390 public function getAllowedParams() {
393 ApiBase
:: PARAM_DFLT
=> 'general',
394 ApiBase
:: PARAM_ISMULTI
=> true,
395 ApiBase
:: PARAM_TYPE
=> array(
399 'specialpagealiases',
411 ApiBase
:: PARAM_TYPE
=> array(
416 'showalldb' => false,
420 public function getParamDescription() {
423 'Which sysinfo properties to get:',
424 ' general - Overall system information',
425 ' namespaces - List of registered namespaces and their canonical names',
426 ' namespacealiases - List of registered namespace aliases',
427 ' specialpagealiases - List of special page aliases',
428 ' magicwords - List of magic words and their aliases',
429 ' statistics - Returns site statistics',
430 ' interwikimap - Returns interwiki map (optionally filtered)',
431 ' dbrepllag - Returns database server with the highest replication lag',
432 ' usergroups - Returns user groups and the associated permissions',
433 ' extensions - Returns extensions installed on the wiki',
434 ' fileextensions - Returns list of file extensions allowed to be uploaded',
435 ' rightsinfo - Returns wiki rights (license) information if available',
437 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
438 'showalldb' => 'List all database servers, not just the one lagging the most',
442 public function getDescription() {
443 return 'Return general information about the site.';
446 protected function getExamples() {
448 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
449 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
450 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
454 public function getVersion() {
455 return __CLASS__
. ': $Id$';