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, $params['numberingroup'] );
78 $fit = $this->appendExtensions( $p );
80 case 'fileextensions':
81 $fit = $this->appendFileExtensions( $p );
84 $fit = $this->appendRightsInfo( $p );
87 $fit = $this->appendLanguages( $p );
90 ApiBase
:: dieDebug( __METHOD__
, "Unknown prop=$p" );
94 // Abuse siprop as a query-continue parameter
95 // and set it to all unprocessed props
96 $this->setContinueEnumParameter( 'prop', implode( '|',
97 array_diff( $params['prop'], $done ) ) );
104 protected function appendGeneralInfo( $property ) {
109 $mainPage = Title
:: newFromText( wfMsgForContent( 'mainpage' ) );
110 $data['mainpage'] = $mainPage->getPrefixedText();
111 $data['base'] = $mainPage->getFullUrl();
112 $data['sitename'] = $GLOBALS['wgSitename'];
113 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
114 $data['phpversion'] = phpversion();
115 $data['phpsapi'] = php_sapi_name();
116 $data['dbtype'] = $GLOBALS['wgDBtype'];
117 $data['dbversion'] = $this->getDB()->getServerVersion();
119 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
123 // 'case-insensitive' option is reserved for future
124 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
126 if ( isset( $GLOBALS['wgRightsCode'] ) )
127 $data['rightscode'] = $GLOBALS['wgRightsCode'];
128 $data['rights'] = $GLOBALS['wgRightsText'];
129 $data['lang'] = $GLOBALS['wgLanguageCode'];
130 if ( $wgContLang->isRTL() )
132 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
134 if ( wfReadOnly() ) {
135 $data['readonly'] = '';
136 $data['readonlyreason'] = wfReadOnlyReason();
138 if ( $GLOBALS['wgEnableWriteAPI'] )
139 $data['writeapi'] = '';
141 $tz = $GLOBALS['wgLocaltimezone'];
142 $offset = $GLOBALS['wgLocalTZoffset'];
143 if ( is_null( $tz ) ) {
146 } elseif ( is_null( $offset ) ) {
149 $data['timezone'] = $tz;
150 $data['timeoffset'] = intval( $offset );
151 $data['articlepath'] = $GLOBALS['wgArticlePath'];
152 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
153 $data['script'] = $GLOBALS['wgScript'];
154 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
155 $data['server'] = $GLOBALS['wgServer'];
156 $data['wikiid'] = wfWikiID();
157 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
159 return $this->getResult()->addValue( 'query', $property, $data );
162 protected function appendNamespaces( $property ) {
165 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title )
168 'id' => intval( $ns ),
169 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
171 ApiResult
:: setContent( $data[$ns], $title );
172 $canonical = MWNamespace
::getCanonicalName( $ns );
174 if ( MWNamespace
::hasSubpages( $ns ) )
175 $data[$ns]['subpages'] = '';
178 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
180 if ( MWNamespace
::isContent( $ns ) )
181 $data[$ns]['content'] = '';
184 $this->getResult()->setIndexedTagName( $data, 'ns' );
185 return $this->getResult()->addValue( 'query', $property, $data );
188 protected function appendNamespaceAliases( $property ) {
189 global $wgNamespaceAliases, $wgContLang;
190 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
191 $namespaces = $wgContLang->getNamespaces();
193 foreach ( $aliases as $title => $ns ) {
194 if ( $namespaces[$ns] == $title ) {
195 // Don't list duplicates
199 'id' => intval( $ns )
201 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
205 $this->getResult()->setIndexedTagName( $data, 'ns' );
206 return $this->getResult()->addValue( 'query', $property, $data );
209 protected function appendSpecialPageAliases( $property ) {
212 foreach ( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
214 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
215 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
218 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
219 return $this->getResult()->addValue( 'query', $property, $data );
222 protected function appendMagicWords( $property ) {
225 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases )
227 $caseSensitive = array_shift( $aliases );
228 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
229 if ( $caseSensitive )
230 $arr['case-sensitive'] = '';
231 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
234 $this->getResult()->setIndexedTagName( $data, 'magicword' );
235 return $this->getResult()->addValue( 'query', $property, $data );
238 protected function appendInterwikiMap( $property, $filter ) {
239 $this->resetQueryParams();
240 $this->addTables( 'interwiki' );
241 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
243 if ( $filter === 'local' )
244 $this->addWhere( 'iw_local = 1' );
245 elseif ( $filter === '!local' )
246 $this->addWhere( 'iw_local = 0' );
248 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
250 $this->addOption( 'ORDER BY', 'iw_prefix' );
252 $db = $this->getDB();
253 $res = $this->select( __METHOD__
);
256 $langNames = Language
::getLanguageNames();
257 while ( $row = $db->fetchObject( $res ) )
260 $val['prefix'] = $row->iw_prefix
;
261 if ( $row->iw_local
== '1' )
263 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
264 if ( isset( $langNames[$row->iw_prefix
] ) )
265 $val['language'] = $langNames[$row->iw_prefix
];
266 $val['url'] = $row->iw_url
;
270 $db->freeResult( $res );
272 $this->getResult()->setIndexedTagName( $data, 'iw' );
273 return $this->getResult()->addValue( 'query', $property, $data );
276 protected function appendDbReplLagInfo( $property, $includeAll ) {
277 global $wgShowHostnames;
280 if ( !$wgShowHostnames )
281 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
284 $lags = $lb->getLagTimes();
285 foreach ( $lags as $i => $lag ) {
287 'host' => $lb->getServerName( $i ),
292 list( $host, $lag ) = wfGetLB()->getMaxLag();
294 'host' => $wgShowHostnames ?
$host : '',
295 'lag' => intval( $lag )
299 $result = $this->getResult();
300 $result->setIndexedTagName( $data, 'db' );
301 return $this->getResult()->addValue( 'query', $property, $data );
304 protected function appendStatistics( $property ) {
305 global $wgDisableCounters;
307 $data['pages'] = intval( SiteStats
::pages() );
308 $data['articles'] = intval( SiteStats
::articles() );
309 if ( !$wgDisableCounters ) {
310 $data['views'] = intval( SiteStats
::views() );
312 $data['edits'] = intval( SiteStats
::edits() );
313 $data['images'] = intval( SiteStats
::images() );
314 $data['users'] = intval( SiteStats
::users() );
315 $data['activeusers'] = intval( SiteStats
::activeUsers() );
316 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
317 $data['jobs'] = intval( SiteStats
::jobs() );
318 return $this->getResult()->addValue( 'query', $property, $data );
321 protected function appendUserGroups( $property, $numberInGroup ) {
322 global $wgGroupPermissions;
324 foreach ( $wgGroupPermissions as $group => $permissions ) {
327 'rights' => array_keys( $permissions, true ),
329 if ( $numberInGroup )
330 $arr['number'] = SiteStats
::numberInGroup( $group );
332 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
336 $this->getResult()->setIndexedTagName( $data, 'group' );
337 return $this->getResult()->addValue( 'query', $property, $data );
340 protected function appendFileExtensions( $property ) {
341 global $wgFileExtensions;
344 foreach ( $wgFileExtensions as $ext ) {
345 $data[] = array( 'ext' => $ext );
347 $this->getResult()->setIndexedTagName( $data, 'fe' );
348 return $this->getResult()->addValue( 'query', $property, $data );
351 protected function appendExtensions( $property ) {
352 global $wgExtensionCredits;
354 foreach ( $wgExtensionCredits as $type => $extensions ) {
355 foreach ( $extensions as $ext ) {
357 $ret['type'] = $type;
358 if ( isset( $ext['name'] ) )
359 $ret['name'] = $ext['name'];
360 if ( isset( $ext['description'] ) )
361 $ret['description'] = $ext['description'];
362 if ( isset( $ext['descriptionmsg'] ) ) {
363 // Can be a string or array( key, param1, param2, ... )
364 if ( is_array( $ext['descriptionmsg'] ) ) {
365 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
366 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
367 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
369 $ret['descriptionmsg'] = $ext['descriptionmsg'];
372 if ( isset( $ext['author'] ) ) {
373 $ret['author'] = is_array( $ext['author'] ) ?
374 implode( ', ', $ext['author' ] ) : $ext['author'];
376 if ( isset( $ext['url'] ) ) {
377 $ret['url'] = $ext['url'];
379 if ( isset( $ext['version'] ) ) {
380 $ret['version'] = $ext['version'];
381 } elseif ( isset( $ext['svn-revision'] ) &&
382 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
383 $ext['svn-revision'], $m ) )
385 $ret['version'] = 'r' . $m[1];
391 $this->getResult()->setIndexedTagName( $data, 'ext' );
392 return $this->getResult()->addValue( 'query', $property, $data );
396 protected function appendRightsInfo( $property ) {
397 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
398 $title = Title
::newFromText( $wgRightsPage );
399 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
400 $text = $wgRightsText;
401 if ( !$text && $title ) {
402 $text = $title->getPrefixedText();
406 'url' => $url ?
$url : '',
407 'text' => $text ?
$text : ''
410 return $this->getResult()->addValue( 'query', $property, $data );
413 public function appendLanguages( $property ) {
415 foreach ( Language
::getLanguageNames() as $code => $name ) {
416 $lang = array( 'code' => $code );
417 ApiResult
::setContent( $lang, $name );
420 $this->getResult()->setIndexedTagName( $data, 'lang' );
421 return $this->getResult()->addValue( 'query', $property, $data );
425 public function getAllowedParams() {
428 ApiBase
:: PARAM_DFLT
=> 'general',
429 ApiBase
:: PARAM_ISMULTI
=> true,
430 ApiBase
:: PARAM_TYPE
=> array(
434 'specialpagealiases',
447 ApiBase
:: PARAM_TYPE
=> array(
452 'showalldb' => false,
453 'numberingroup' => false,
457 public function getParamDescription() {
460 'Which sysinfo properties to get:',
461 ' general - Overall system information',
462 ' namespaces - List of registered namespaces and their canonical names',
463 ' namespacealiases - List of registered namespace aliases',
464 ' specialpagealiases - List of special page aliases',
465 ' magicwords - List of magic words and their aliases',
466 ' statistics - Returns site statistics',
467 ' interwikimap - Returns interwiki map (optionally filtered)',
468 ' dbrepllag - Returns database server with the highest replication lag',
469 ' usergroups - Returns user groups and the associated permissions',
470 ' extensions - Returns extensions installed on the wiki',
471 ' fileextensions - Returns list of file extensions allowed to be uploaded',
472 ' rightsinfo - Returns wiki rights (license) information if available',
473 ' languages - Returns a list of languages MediaWiki supports',
475 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
476 'showalldb' => 'List all database servers, not just the one lagging the most',
477 'numberingroup' => 'Lists the number of users in user groups',
481 public function getDescription() {
482 return 'Return general information about the site.';
485 public function getPossibleErrors() {
486 return array_merge( parent
::getPossibleErrors(), array(
487 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true', ),
491 protected function getExamples() {
493 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
494 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
495 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
499 public function getVersion() {
500 return __CLASS__
. ': $Id$';