5 * Created on Sep 25, 2006
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * A query action to return meta information about the wiki site.
37 class ApiQuerySiteinfo
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'si' );
43 public function execute() {
44 $params = $this->extractRequestParams();
47 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 $fit = $this->appendSkins( $p );
93 $fit = $this->appendExtensionTags( $p );
96 $fit = $this->appendFunctionHooks( $p );
99 $fit = $this->appendSubscribedHooks( $p );
102 ApiBase
::dieDebug( __METHOD__
, "Unknown prop=$p" );
105 // Abuse siprop as a query-continue parameter
106 // and set it to all unprocessed props
107 $this->setContinueEnumParameter( 'prop', implode( '|',
108 array_diff( $params['prop'], $done ) ) );
115 protected function appendGeneralInfo( $property ) {
119 $mainPage = Title
::newMainPage();
120 $data['mainpage'] = $mainPage->getPrefixedText();
121 $data['base'] = wfExpandUrl( $mainPage->getFullUrl(), PROTO_CURRENT
);
122 $data['sitename'] = $GLOBALS['wgSitename'];
123 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
124 $data['phpversion'] = phpversion();
125 $data['phpsapi'] = php_sapi_name();
126 $data['dbtype'] = $GLOBALS['wgDBtype'];
127 $data['dbversion'] = $this->getDB()->getServerVersion();
129 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
134 // 'case-insensitive' option is reserved for future
135 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
137 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
138 $data['rightscode'] = $GLOBALS['wgRightsCode'];
140 $data['rights'] = $GLOBALS['wgRightsText'];
141 $data['lang'] = $GLOBALS['wgLanguageCode'];
143 $fallbacks = array();
144 foreach( $wgContLang->getFallbackLanguages() as $code ) {
145 $fallbacks[] = array( 'code' => $code );
147 $data['fallback'] = $fallbacks;
148 $this->getResult()->setIndexedTagName( $data['fallback'], 'lang' );
150 if ( $wgContLang->isRTL() ) {
153 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
155 if ( wfReadOnly() ) {
156 $data['readonly'] = '';
157 $data['readonlyreason'] = wfReadOnlyReason();
159 if ( $GLOBALS['wgEnableWriteAPI'] ) {
160 $data['writeapi'] = '';
163 $tz = $GLOBALS['wgLocaltimezone'];
164 $offset = $GLOBALS['wgLocalTZoffset'];
165 if ( is_null( $tz ) ) {
168 } elseif ( is_null( $offset ) ) {
171 $data['timezone'] = $tz;
172 $data['timeoffset'] = intval( $offset );
173 $data['articlepath'] = $GLOBALS['wgArticlePath'];
174 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
175 $data['script'] = $GLOBALS['wgScript'];
176 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
177 $data['server'] = $GLOBALS['wgServer'];
178 $data['wikiid'] = wfWikiID();
179 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
181 if ( $GLOBALS['wgMiserMode'] ) {
182 $data['misermode'] = '';
185 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
187 return $this->getResult()->addValue( 'query', $property, $data );
190 protected function appendNamespaces( $property ) {
193 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
195 'id' => intval( $ns ),
196 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
198 ApiResult
::setContent( $data[$ns], $title );
199 $canonical = MWNamespace
::getCanonicalName( $ns );
201 if ( MWNamespace
::hasSubpages( $ns ) ) {
202 $data[$ns]['subpages'] = '';
206 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
209 if ( MWNamespace
::isContent( $ns ) ) {
210 $data[$ns]['content'] = '';
214 $this->getResult()->setIndexedTagName( $data, 'ns' );
215 return $this->getResult()->addValue( 'query', $property, $data );
218 protected function appendNamespaceAliases( $property ) {
219 global $wgNamespaceAliases, $wgContLang;
220 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
221 $namespaces = $wgContLang->getNamespaces();
223 foreach ( $aliases as $title => $ns ) {
224 if ( $namespaces[$ns] == $title ) {
225 // Don't list duplicates
229 'id' => intval( $ns )
231 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
235 $this->getResult()->setIndexedTagName( $data, 'ns' );
236 return $this->getResult()->addValue( 'query', $property, $data );
239 protected function appendSpecialPageAliases( $property ) {
242 foreach ( $wgContLang->getSpecialPageAliases() as $specialpage => $aliases ) {
243 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
244 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
247 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
248 return $this->getResult()->addValue( 'query', $property, $data );
251 protected function appendMagicWords( $property ) {
254 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
255 $caseSensitive = array_shift( $aliases );
256 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
257 if ( $caseSensitive ) {
258 $arr['case-sensitive'] = '';
260 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
263 $this->getResult()->setIndexedTagName( $data, 'magicword' );
264 return $this->getResult()->addValue( 'query', $property, $data );
267 protected function appendInterwikiMap( $property, $filter ) {
269 if ( $filter === 'local' ) {
271 } elseif ( $filter === '!local' ) {
273 } elseif ( $filter ) {
274 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
277 $params = $this->extractRequestParams();
278 $langCode = isset( $params['inlanguagecode '] ) ?
$params['inlanguagecode '] : '';
281 $langNames = Language
::getTranslatedLanguageNames( $langCode );
283 $langNames = Language
::getLanguageNames();
286 $getPrefixes = Interwiki
::getAllPrefixes( $local );
289 foreach ( $getPrefixes as $row ) {
290 $prefix = $row['iw_prefix'];
292 $val['prefix'] = $prefix;
293 if ( $row['iw_local'] == '1' ) {
296 // $val['trans'] = intval( $row['iw_trans'] ); // should this be exposed?
297 if ( isset( $langNames[$prefix] ) ) {
298 $val['language'] = $langNames[$prefix];
300 $val['url'] = wfExpandUrl( $row['iw_url'], PROTO_CURRENT
);
301 if( isset( $row['iw_wikiid'] ) ) {
302 $val['wikiid'] = $row['iw_wikiid'];
304 if( isset( $row['iw_api'] ) ) {
305 $val['api'] = $row['iw_api'];
311 $this->getResult()->setIndexedTagName( $data, 'iw' );
312 return $this->getResult()->addValue( 'query', $property, $data );
315 protected function appendDbReplLagInfo( $property, $includeAll ) {
316 global $wgShowHostnames;
320 if ( !$wgShowHostnames ) {
321 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
324 $lags = $lb->getLagTimes();
325 foreach ( $lags as $i => $lag ) {
327 'host' => $lb->getServerName( $i ),
332 list( $host, $lag, $index ) = $lb->getMaxLag();
334 'host' => $wgShowHostnames
335 ?
$lb->getServerName( $index )
337 'lag' => intval( $lag )
341 $result = $this->getResult();
342 $result->setIndexedTagName( $data, 'db' );
343 return $this->getResult()->addValue( 'query', $property, $data );
346 protected function appendStatistics( $property ) {
347 global $wgDisableCounters;
349 $data['pages'] = intval( SiteStats
::pages() );
350 $data['articles'] = intval( SiteStats
::articles() );
351 if ( !$wgDisableCounters ) {
352 $data['views'] = intval( SiteStats
::views() );
354 $data['edits'] = intval( SiteStats
::edits() );
355 $data['images'] = intval( SiteStats
::images() );
356 $data['users'] = intval( SiteStats
::users() );
357 $data['activeusers'] = intval( SiteStats
::activeUsers() );
358 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
359 $data['jobs'] = intval( SiteStats
::jobs() );
360 return $this->getResult()->addValue( 'query', $property, $data );
363 protected function appendUserGroups( $property, $numberInGroup ) {
364 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
367 $result = $this->getResult();
368 foreach ( $wgGroupPermissions as $group => $permissions ) {
371 'rights' => array_keys( $permissions, true ),
374 if ( $numberInGroup ) {
375 global $wgAutopromote;
377 if ( $group == 'user' ) {
378 $arr['number'] = SiteStats
::users();
380 // '*' and autopromote groups have no size
381 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
382 $arr['number'] = SiteStats
::numberInGroup( $group );
387 'add' => $wgAddGroups,
388 'remove' => $wgRemoveGroups,
389 'add-self' => $wgGroupsAddToSelf,
390 'remove-self' => $wgGroupsRemoveFromSelf
393 foreach ( $groupArr as $type => $rights ) {
394 if ( isset( $rights[$group] ) ) {
395 $arr[$type] = $rights[$group];
396 $result->setIndexedTagName( $arr[$type], 'group' );
400 $result->setIndexedTagName( $arr['rights'], 'permission' );
404 $result->setIndexedTagName( $data, 'group' );
405 return $result->addValue( 'query', $property, $data );
408 protected function appendFileExtensions( $property ) {
409 global $wgFileExtensions;
412 foreach ( $wgFileExtensions as $ext ) {
413 $data[] = array( 'ext' => $ext );
415 $this->getResult()->setIndexedTagName( $data, 'fe' );
416 return $this->getResult()->addValue( 'query', $property, $data );
419 protected function appendExtensions( $property ) {
420 global $wgExtensionCredits;
422 foreach ( $wgExtensionCredits as $type => $extensions ) {
423 foreach ( $extensions as $ext ) {
425 $ret['type'] = $type;
426 if ( isset( $ext['name'] ) ) {
427 $ret['name'] = $ext['name'];
429 if ( isset( $ext['description'] ) ) {
430 $ret['description'] = $ext['description'];
432 if ( isset( $ext['descriptionmsg'] ) ) {
433 // Can be a string or array( key, param1, param2, ... )
434 if ( is_array( $ext['descriptionmsg'] ) ) {
435 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
436 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
437 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
439 $ret['descriptionmsg'] = $ext['descriptionmsg'];
442 if ( isset( $ext['author'] ) ) {
443 $ret['author'] = is_array( $ext['author'] ) ?
444 implode( ', ', $ext['author' ] ) : $ext['author'];
446 if ( isset( $ext['url'] ) ) {
447 $ret['url'] = $ext['url'];
449 if ( isset( $ext['version'] ) ) {
450 $ret['version'] = $ext['version'];
451 } elseif ( isset( $ext['svn-revision'] ) &&
452 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
453 $ext['svn-revision'], $m ) )
455 $ret['version'] = 'r' . $m[1];
461 $this->getResult()->setIndexedTagName( $data, 'ext' );
462 return $this->getResult()->addValue( 'query', $property, $data );
465 protected function appendRightsInfo( $property ) {
466 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
467 $title = Title
::newFromText( $wgRightsPage );
468 $url = $title ?
wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
) : $wgRightsUrl;
469 $text = $wgRightsText;
470 if ( !$text && $title ) {
471 $text = $title->getPrefixedText();
475 'url' => $url ?
$url : '',
476 'text' => $text ?
$text : ''
479 return $this->getResult()->addValue( 'query', $property, $data );
482 public function appendLanguages( $property ) {
483 $params = $this->extractRequestParams();
484 $langCode = isset( $params['inlanguagecode '] ) ?
$params['inlanguagecode '] : '';
487 $langNames = Language
::getTranslatedLanguageNames( $langCode );
489 $langNames = Language
::getLanguageNames();
494 foreach ( $langNames as $code => $name ) {
495 $lang = array( 'code' => $code );
496 ApiResult
::setContent( $lang, $name );
499 $this->getResult()->setIndexedTagName( $data, 'lang' );
500 return $this->getResult()->addValue( 'query', $property, $data );
503 public function appendSkins( $property ) {
505 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
506 $skin = array( 'code' => $name );
507 ApiResult
::setContent( $skin, $displayName );
510 $this->getResult()->setIndexedTagName( $data, 'skin' );
511 return $this->getResult()->addValue( 'query', $property, $data );
514 public function appendExtensionTags( $property ) {
516 $wgParser->firstCallInit();
517 $tags = array_map( array( $this, 'formatParserTags'), $wgParser->getTags() );
518 $this->getResult()->setIndexedTagName( $tags, 't' );
519 return $this->getResult()->addValue( 'query', $property, $tags );
522 public function appendFunctionHooks( $property ) {
524 $wgParser->firstCallInit();
525 $hooks = $wgParser->getFunctionHooks();
526 $this->getResult()->setIndexedTagName( $hooks, 'h' );
527 return $this->getResult()->addValue( 'query', $property, $hooks );
530 private function formatParserTags( $item ) {
534 public function appendSubscribedHooks( $property ) {
536 $myWgHooks = $wgHooks;
540 foreach ( $myWgHooks as $hook => $hooks ) {
543 'subscribers' => array_map( array( 'SpecialVersion', 'arrayToString' ), $hooks ),
546 $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
550 $this->getResult()->setIndexedTagName( $data, 'hook' );
551 return $this->getResult()->addValue( 'query', $property, $data );
554 public function getCacheMode( $params ) {
558 public function getAllowedParams() {
561 ApiBase
::PARAM_DFLT
=> 'general',
562 ApiBase
::PARAM_ISMULTI
=> true,
563 ApiBase
::PARAM_TYPE
=> array(
567 'specialpagealiases',
584 ApiBase
::PARAM_TYPE
=> array(
589 'showalldb' => false,
590 'numberingroup' => false,
591 'inlanguagecode ' => null,
595 public function getParamDescription() {
596 $p = $this->getModulePrefix();
599 'Which sysinfo properties to get:',
600 ' general - Overall system information',
601 ' namespaces - List of registered namespaces and their canonical names',
602 ' namespacealiases - List of registered namespace aliases',
603 ' specialpagealiases - List of special page aliases',
604 ' magicwords - List of magic words and their aliases',
605 ' statistics - Returns site statistics',
606 " interwikimap - Returns interwiki map (optionally filtered, (optionally localised by using {$p}inlanguagecode))",
607 ' dbrepllag - Returns database server with the highest replication lag',
608 ' usergroups - Returns user groups and the associated permissions',
609 ' extensions - Returns extensions installed on the wiki',
610 ' fileextensions - Returns list of file extensions allowed to be uploaded',
611 ' rightsinfo - Returns wiki rights (license) information if available',
612 " languages - Returns a list of languages MediaWiki supports (optionally localised by using {$p}inlanguagecode)",
613 ' skins - Returns a list of all enabled skins',
614 ' extensiontags - Returns a list of parser extension tags',
615 ' functionhooks - Returns a list of parser function hooks',
616 ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)'
618 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
619 'showalldb' => 'List all database servers, not just the one lagging the most',
620 'numberingroup' => 'Lists the number of users in user groups',
621 'inlanguagecode ' => 'Language code for localised language names (best effort, use CLDR extension)',
625 public function getDescription() {
626 return 'Return general information about the site';
629 public function getPossibleErrors() {
630 return array_merge( parent
::getPossibleErrors(), array(
631 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
635 public function getExamples() {
637 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
638 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
639 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
643 public function getHelpUrls() {
644 return 'http://www.mediawiki.org/wiki/API:Meta#siteinfo_.2F_si';
647 public function getVersion() {
648 return __CLASS__
. ': $Id$';