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 if( $GLOBALS['wgEnableWriteAPI'] )
134 $data['writeapi'] = '';
136 $tz = $GLOBALS['wgLocaltimezone'];
137 $offset = $GLOBALS['wgLocalTZoffset'];
138 if( is_null( $tz ) ) {
141 } elseif( is_null( $offset ) ) {
144 $data['timezone'] = $tz;
145 $data['timeoffset'] = intval($offset);
146 $data['articlepath'] = $GLOBALS['wgArticlePath'];
147 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
148 $data['script'] = $GLOBALS['wgScript'];
149 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
150 $data['server'] = $GLOBALS['wgServer'];
151 $data['wikiid'] = wfWikiID();
153 return $this->getResult()->addValue( 'query', $property, $data );
156 protected function appendNamespaces( $property ) {
159 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
164 ApiResult
:: setContent( $data[$ns], $title );
165 $canonical = MWNamespace
::getCanonicalName( $ns );
167 if( MWNamespace
::hasSubpages( $ns ) )
168 $data[$ns]['subpages'] = '';
171 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
174 $this->getResult()->setIndexedTagName( $data, 'ns' );
175 return $this->getResult()->addValue( 'query', $property, $data );
178 protected function appendNamespaceAliases( $property ) {
179 global $wgNamespaceAliases, $wgContLang;
181 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->namespaceAliases
);
182 $namespaces = $wgContLang->getNamespaces();
184 foreach( $aliases as $title => $ns ) {
185 if( $namespaces[$ns] == $title ) {
186 // Don't list duplicates
192 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
196 $this->getResult()->setIndexedTagName( $data, 'ns' );
197 return $this->getResult()->addValue( 'query', $property, $data );
200 protected function appendSpecialPageAliases( $property ) {
203 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
205 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
206 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
209 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
210 return $this->getResult()->addValue( 'query', $property, $data );
213 protected function appendMagicWords( $property ) {
216 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
218 $caseSensitive = array_shift($aliases);
219 $arr = array('name' => $magicword, 'aliases' => $aliases);
221 $arr['case-sensitive'] = '';
222 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
225 $this->getResult()->setIndexedTagName($data, 'magicword');
226 return $this->getResult()->addValue( 'query', $property, $data );
229 protected function appendInterwikiMap( $property, $filter ) {
230 $this->resetQueryParams();
231 $this->addTables( 'interwiki' );
232 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
234 if( $filter === 'local' )
235 $this->addWhere( 'iw_local = 1' );
236 elseif( $filter === '!local' )
237 $this->addWhere( 'iw_local = 0' );
239 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
241 $this->addOption( 'ORDER BY', 'iw_prefix' );
243 $db = $this->getDB();
244 $res = $this->select( __METHOD__
);
247 $langNames = Language
::getLanguageNames();
248 while( $row = $db->fetchObject($res) )
251 $val['prefix'] = $row->iw_prefix
;
252 if( $row->iw_local
== '1' )
254 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
255 if( isset( $langNames[$row->iw_prefix
] ) )
256 $val['language'] = $langNames[$row->iw_prefix
];
257 $val['url'] = $row->iw_url
;
261 $db->freeResult( $res );
263 $this->getResult()->setIndexedTagName( $data, 'iw' );
264 return $this->getResult()->addValue( 'query', $property, $data );
267 protected function appendDbReplLagInfo( $property, $includeAll ) {
268 global $wgShowHostnames;
271 if ( !$wgShowHostnames )
272 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
275 $lags = $lb->getLagTimes();
276 foreach( $lags as $i => $lag ) {
278 'host' => $lb->getServerName( $i ),
283 list( $host, $lag ) = wfGetLB()->getMaxLag();
285 'host' => $wgShowHostnames ?
$host : '',
286 'lag' => intval( $lag )
290 $result = $this->getResult();
291 $result->setIndexedTagName( $data, 'db' );
292 return $this->getResult()->addValue( 'query', $property, $data );
295 protected function appendStatistics( $property ) {
296 global $wgDisableCounters;
298 $data['pages'] = intval( SiteStats
::pages() );
299 $data['articles'] = intval( SiteStats
::articles() );
300 if ( !$wgDisableCounters ) {
301 $data['views'] = intval( SiteStats
::views() );
303 $data['edits'] = intval( SiteStats
::edits() );
304 $data['images'] = intval( SiteStats
::images() );
305 $data['users'] = intval( SiteStats
::users() );
306 $data['activeusers'] = intval( SiteStats
::activeUsers() );
307 $data['admins'] = intval( SiteStats
::numberingroup('sysop') );
308 $data['jobs'] = intval( SiteStats
::jobs() );
309 return $this->getResult()->addValue( 'query', $property, $data );
312 protected function appendUserGroups( $property ) {
313 global $wgGroupPermissions;
315 foreach( $wgGroupPermissions as $group => $permissions ) {
316 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
317 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
321 $this->getResult()->setIndexedTagName( $data, 'group' );
322 return $this->getResult()->addValue( 'query', $property, $data );
325 protected function appendFileExtensions( $property ) {
326 global $wgFileExtensions;
329 foreach( $wgFileExtensions as $ext ) {
330 $data[] = array( 'ext' => $ext );
332 $this->getResult()->setIndexedTagName( $data, 'fe' );
333 return $this->getResult()->addValue( 'query', $property, $data );
336 protected function appendExtensions( $property ) {
337 global $wgExtensionCredits;
339 foreach ( $wgExtensionCredits as $type => $extensions ) {
340 foreach ( $extensions as $ext ) {
342 $ret['type'] = $type;
343 if ( isset( $ext['name'] ) )
344 $ret['name'] = $ext['name'];
345 if ( isset( $ext['description'] ) )
346 $ret['description'] = $ext['description'];
347 if ( isset( $ext['descriptionmsg'] ) )
348 $ret['descriptionmsg'] = $ext['descriptionmsg'];
349 if ( isset( $ext['author'] ) ) {
350 $ret['author'] = is_array( $ext['author'] ) ?
351 implode( ', ', $ext['author' ] ) : $ext['author'];
353 if ( isset( $ext['version'] ) ) {
354 $ret['version'] = $ext['version'];
355 } elseif ( isset( $ext['svn-revision'] ) &&
356 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
357 $ext['svn-revision'], $m ) )
359 $ret['version'] = 'r' . $m[1];
365 $this->getResult()->setIndexedTagName( $data, 'ext' );
366 return $this->getResult()->addValue( 'query', $property, $data );
370 protected function appendRightsInfo( $property ) {
371 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
372 $title = Title
::newFromText( $wgRightsPage );
373 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
374 $text = $wgRightsText;
375 if( !$text && $title ) {
376 $text = $title->getPrefixedText();
380 'url' => $url ?
$url : '',
381 'text' => $text ?
$text : ''
384 return $this->getResult()->addValue( 'query', $property, $data );
388 public function getAllowedParams() {
391 ApiBase
:: PARAM_DFLT
=> 'general',
392 ApiBase
:: PARAM_ISMULTI
=> true,
393 ApiBase
:: PARAM_TYPE
=> array(
397 'specialpagealiases',
409 ApiBase
:: PARAM_TYPE
=> array(
414 'showalldb' => false,
418 public function getParamDescription() {
421 'Which sysinfo properties to get:',
422 ' general - Overall system information',
423 ' namespaces - List of registered namespaces and their canonical names',
424 ' namespacealiases - List of registered namespace aliases',
425 ' specialpagealiases - List of special page aliases',
426 ' magicwords - List of magic words and their aliases',
427 ' statistics - Returns site statistics',
428 ' interwikimap - Returns interwiki map (optionally filtered)',
429 ' dbrepllag - Returns database server with the highest replication lag',
430 ' usergroups - Returns user groups and the associated permissions',
431 ' extensions - Returns extensions installed on the wiki',
432 ' fileextensions - Returns list of file extensions allowed to be uploaded',
433 ' rightsinfo - Returns wiki rights (license) information if available',
435 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
436 'showalldb' => 'List all database servers, not just the one lagging the most',
440 public function getDescription() {
441 return 'Return general information about the site.';
444 protected function getExamples() {
446 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
447 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
448 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
452 public function getVersion() {
453 return __CLASS__
. ': $Id$';