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();
46 foreach ( $params['prop'] as $p ) {
49 $fit = $this->appendGeneralInfo( $p );
52 $fit = $this->appendNamespaces( $p );
54 case 'namespacealiases':
55 $fit = $this->appendNamespaceAliases( $p );
57 case 'specialpagealiases':
58 $fit = $this->appendSpecialPageAliases( $p );
61 $fit = $this->appendMagicWords( $p );
64 $filteriw = isset( $params['filteriw'] ) ?
$params['filteriw'] : false;
65 $fit = $this->appendInterwikiMap( $p, $filteriw );
68 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
71 $fit = $this->appendStatistics( $p );
74 $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
77 $fit = $this->appendExtensions( $p );
79 case 'fileextensions':
80 $fit = $this->appendFileExtensions( $p );
83 $fit = $this->appendRightsInfo( $p );
86 $fit = $this->appendLanguages( $p );
89 $fit = $this->appendSkins( $p );
92 $fit = $this->appendExtensionTags( $p );
95 $fit = $this->appendFunctionHooks( $p );
98 $fit = $this->appendSubscribedHooks( $p );
101 ApiBase
::dieDebug( __METHOD__
, "Unknown prop=$p" );
104 // Abuse siprop as a query-continue parameter
105 // and set it to all unprocessed props
106 $this->setContinueEnumParameter( 'prop', implode( '|',
107 array_diff( $params['prop'], $done ) ) );
114 protected function appendGeneralInfo( $property ) {
118 $mainPage = Title
::newMainPage();
119 $data['mainpage'] = $mainPage->getPrefixedText();
120 $data['base'] = wfExpandUrl( $mainPage->getFullUrl() );
121 $data['sitename'] = $GLOBALS['wgSitename'];
122 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
123 $data['phpversion'] = phpversion();
124 $data['phpsapi'] = php_sapi_name();
125 $data['dbtype'] = $GLOBALS['wgDBtype'];
126 $data['dbversion'] = $this->getDB()->getServerVersion();
128 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
133 // 'case-insensitive' option is reserved for future
134 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
136 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
137 $data['rightscode'] = $GLOBALS['wgRightsCode'];
139 $data['rights'] = $GLOBALS['wgRightsText'];
140 $data['lang'] = $GLOBALS['wgLanguageCode'];
142 $fallbackLang = $wgContLang->getFallbackLanguageCode();
143 $fallbackLangArray = array();
144 while( $fallbackLang ) {
145 $fallbackLangArray[] = array( 'code' => $fallbackLang );
146 $fallbackLang = Language
::getFallbackFor( $fallbackLang );
148 $data['fallback'] = $fallbackLangArray;
149 $this->getResult()->setIndexedTagName( $data['fallback'], 'lang' );
151 if ( $wgContLang->isRTL() ) {
154 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
156 if ( wfReadOnly() ) {
157 $data['readonly'] = '';
158 $data['readonlyreason'] = wfReadOnlyReason();
160 if ( $GLOBALS['wgEnableWriteAPI'] ) {
161 $data['writeapi'] = '';
164 $tz = $GLOBALS['wgLocaltimezone'];
165 $offset = $GLOBALS['wgLocalTZoffset'];
166 if ( is_null( $tz ) ) {
169 } elseif ( is_null( $offset ) ) {
172 $data['timezone'] = $tz;
173 $data['timeoffset'] = intval( $offset );
174 $data['articlepath'] = $GLOBALS['wgArticlePath'];
175 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
176 $data['script'] = $GLOBALS['wgScript'];
177 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
178 $data['server'] = $GLOBALS['wgServer'];
179 $data['wikiid'] = wfWikiID();
180 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
182 if ( $GLOBALS['wgMiserMode'] ) {
183 $data['misermode'] = '';
186 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
188 return $this->getResult()->addValue( 'query', $property, $data );
191 protected function appendNamespaces( $property ) {
194 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
196 'id' => intval( $ns ),
197 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
199 ApiResult
::setContent( $data[$ns], $title );
200 $canonical = MWNamespace
::getCanonicalName( $ns );
202 if ( MWNamespace
::hasSubpages( $ns ) ) {
203 $data[$ns]['subpages'] = '';
207 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
210 if ( MWNamespace
::isContent( $ns ) ) {
211 $data[$ns]['content'] = '';
215 $this->getResult()->setIndexedTagName( $data, 'ns' );
216 return $this->getResult()->addValue( 'query', $property, $data );
219 protected function appendNamespaceAliases( $property ) {
220 global $wgNamespaceAliases, $wgContLang;
221 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
222 $namespaces = $wgContLang->getNamespaces();
224 foreach ( $aliases as $title => $ns ) {
225 if ( $namespaces[$ns] == $title ) {
226 // Don't list duplicates
230 'id' => intval( $ns )
232 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
236 $this->getResult()->setIndexedTagName( $data, 'ns' );
237 return $this->getResult()->addValue( 'query', $property, $data );
240 protected function appendSpecialPageAliases( $property ) {
243 foreach ( $wgContLang->getSpecialPageAliases() as $specialpage => $aliases ) {
244 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
245 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
248 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
249 return $this->getResult()->addValue( 'query', $property, $data );
252 protected function appendMagicWords( $property ) {
255 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
256 $caseSensitive = array_shift( $aliases );
257 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
258 if ( $caseSensitive ) {
259 $arr['case-sensitive'] = '';
261 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
264 $this->getResult()->setIndexedTagName( $data, 'magicword' );
265 return $this->getResult()->addValue( 'query', $property, $data );
268 protected function appendInterwikiMap( $property, $filter ) {
270 if ( $filter === 'local' ) {
272 } elseif ( $filter === '!local' ) {
274 } elseif ( $filter ) {
275 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
278 $params = $this->extractRequestParams();
279 $langCode = isset( $params['inlanguagecode '] ) ?
$params['inlanguagecode '] : '';
282 $langNames = Language
::getTranslatedLanguageNames( $langCode );
284 $langNames = Language
::getLanguageNames();
287 $getPrefixes = Interwiki
::getAllPrefixes( $local );
290 foreach ( $getPrefixes as $row ) {
291 $prefix = $row['iw_prefix'];
293 $val['prefix'] = $prefix;
294 if ( $row['iw_local'] == '1' ) {
297 // $val['trans'] = intval( $row['iw_trans'] ); // should this be exposed?
298 if ( isset( $langNames[$prefix] ) ) {
299 $val['language'] = $langNames[$prefix];
301 $val['url'] = wfExpandUrl( $row['iw_url'] );
302 if( isset( $row['iw_wikiid'] ) ) {
303 $val['wikiid'] = $row['iw_wikiid'];
305 if( isset( $row['iw_api'] ) ) {
306 $val['api'] = $row['iw_api'];
312 $this->getResult()->setIndexedTagName( $data, 'iw' );
313 return $this->getResult()->addValue( 'query', $property, $data );
316 protected function appendDbReplLagInfo( $property, $includeAll ) {
317 global $wgShowHostnames;
321 if ( !$wgShowHostnames ) {
322 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
325 $lags = $lb->getLagTimes();
326 foreach ( $lags as $i => $lag ) {
328 'host' => $lb->getServerName( $i ),
333 list( $host, $lag, $index ) = $lb->getMaxLag();
335 'host' => $wgShowHostnames
336 ?
$lb->getServerName( $index )
338 'lag' => intval( $lag )
342 $result = $this->getResult();
343 $result->setIndexedTagName( $data, 'db' );
344 return $this->getResult()->addValue( 'query', $property, $data );
347 protected function appendStatistics( $property ) {
348 global $wgDisableCounters;
350 $data['pages'] = intval( SiteStats
::pages() );
351 $data['articles'] = intval( SiteStats
::articles() );
352 if ( !$wgDisableCounters ) {
353 $data['views'] = intval( SiteStats
::views() );
355 $data['edits'] = intval( SiteStats
::edits() );
356 $data['images'] = intval( SiteStats
::images() );
357 $data['users'] = intval( SiteStats
::users() );
358 $data['activeusers'] = intval( SiteStats
::activeUsers() );
359 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
360 $data['jobs'] = intval( SiteStats
::jobs() );
361 return $this->getResult()->addValue( 'query', $property, $data );
364 protected function appendUserGroups( $property, $numberInGroup ) {
365 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
368 $result = $this->getResult();
369 foreach ( $wgGroupPermissions as $group => $permissions ) {
372 'rights' => array_keys( $permissions, true ),
375 if ( $numberInGroup ) {
376 global $wgAutopromote;
378 if ( $group == 'user' ) {
379 $arr['number'] = SiteStats
::users();
381 // '*' and autopromote groups have no size
382 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
383 $arr['number'] = SiteStats
::numberInGroup( $group );
388 'add' => $wgAddGroups,
389 'remove' => $wgRemoveGroups,
390 'add-self' => $wgGroupsAddToSelf,
391 'remove-self' => $wgGroupsRemoveFromSelf
394 foreach ( $groupArr as $type => $rights ) {
395 if ( isset( $rights[$group] ) ) {
396 $arr[$type] = $rights[$group];
397 $result->setIndexedTagName( $arr[$type], 'group' );
401 $result->setIndexedTagName( $arr['rights'], 'permission' );
405 $result->setIndexedTagName( $data, 'group' );
406 return $result->addValue( 'query', $property, $data );
409 protected function appendFileExtensions( $property ) {
410 global $wgFileExtensions;
413 foreach ( $wgFileExtensions as $ext ) {
414 $data[] = array( 'ext' => $ext );
416 $this->getResult()->setIndexedTagName( $data, 'fe' );
417 return $this->getResult()->addValue( 'query', $property, $data );
420 protected function appendExtensions( $property ) {
421 global $wgExtensionCredits;
423 foreach ( $wgExtensionCredits as $type => $extensions ) {
424 foreach ( $extensions as $ext ) {
426 $ret['type'] = $type;
427 if ( isset( $ext['name'] ) ) {
428 $ret['name'] = $ext['name'];
430 if ( isset( $ext['description'] ) ) {
431 $ret['description'] = $ext['description'];
433 if ( isset( $ext['descriptionmsg'] ) ) {
434 // Can be a string or array( key, param1, param2, ... )
435 if ( is_array( $ext['descriptionmsg'] ) ) {
436 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
437 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
438 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
440 $ret['descriptionmsg'] = $ext['descriptionmsg'];
443 if ( isset( $ext['author'] ) ) {
444 $ret['author'] = is_array( $ext['author'] ) ?
445 implode( ', ', $ext['author' ] ) : $ext['author'];
447 if ( isset( $ext['url'] ) ) {
448 $ret['url'] = $ext['url'];
450 if ( isset( $ext['version'] ) ) {
451 $ret['version'] = $ext['version'];
452 } elseif ( isset( $ext['svn-revision'] ) &&
453 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
454 $ext['svn-revision'], $m ) )
456 $ret['version'] = 'r' . $m[1];
462 $this->getResult()->setIndexedTagName( $data, 'ext' );
463 return $this->getResult()->addValue( 'query', $property, $data );
466 protected function appendRightsInfo( $property ) {
467 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
468 $title = Title
::newFromText( $wgRightsPage );
469 $url = $title ?
wfExpandUrl( $title->getFullURL() ) : $wgRightsUrl;
470 $text = $wgRightsText;
471 if ( !$text && $title ) {
472 $text = $title->getPrefixedText();
476 'url' => $url ?
$url : '',
477 'text' => $text ?
$text : ''
480 return $this->getResult()->addValue( 'query', $property, $data );
483 public function appendLanguages( $property ) {
484 $params = $this->extractRequestParams();
485 $langCode = isset( $params['inlanguagecode '] ) ?
$params['inlanguagecode '] : '';
488 $langNames = Language
::getTranslatedLanguageNames( $langCode );
490 $langNames = Language
::getLanguageNames();
495 foreach ( $langNames as $code => $name ) {
496 $lang = array( 'code' => $code );
497 ApiResult
::setContent( $lang, $name );
500 $this->getResult()->setIndexedTagName( $data, 'lang' );
501 return $this->getResult()->addValue( 'query', $property, $data );
504 public function appendSkins( $property ) {
506 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
507 $skin = array( 'code' => $name );
508 ApiResult
::setContent( $skin, $displayName );
511 $this->getResult()->setIndexedTagName( $data, 'skin' );
512 return $this->getResult()->addValue( 'query', $property, $data );
515 public function appendExtensionTags( $property ) {
517 $wgParser->firstCallInit();
518 $tags = array_map( array( $this, 'formatParserTags'), $wgParser->getTags() );
519 $this->getResult()->setIndexedTagName( $tags, 't' );
520 return $this->getResult()->addValue( 'query', $property, $tags );
523 public function appendFunctionHooks( $property ) {
525 $wgParser->firstCallInit();
526 $hooks = $wgParser->getFunctionHooks();
527 $this->getResult()->setIndexedTagName( $hooks, 'h' );
528 return $this->getResult()->addValue( 'query', $property, $hooks );
531 private function formatParserTags( $item ) {
535 public function appendSubscribedHooks( $property ) {
537 $myWgHooks = $wgHooks;
541 foreach ( $myWgHooks as $hook => $hooks ) {
544 'subscribers' => array_map( array( 'SpecialVersion', 'arrayToString' ), $hooks ),
547 $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
551 $this->getResult()->setIndexedTagName( $data, 'hook' );
552 return $this->getResult()->addValue( 'query', $property, $data );
555 public function getCacheMode( $params ) {
559 public function getAllowedParams() {
562 ApiBase
::PARAM_DFLT
=> 'general',
563 ApiBase
::PARAM_ISMULTI
=> true,
564 ApiBase
::PARAM_TYPE
=> array(
568 'specialpagealiases',
585 ApiBase
::PARAM_TYPE
=> array(
590 'showalldb' => false,
591 'numberingroup' => false,
592 'inlanguagecode ' => null,
596 public function getParamDescription() {
597 $p = $this->getModulePrefix();
600 'Which sysinfo properties to get:',
601 ' general - Overall system information',
602 ' namespaces - List of registered namespaces and their canonical names',
603 ' namespacealiases - List of registered namespace aliases',
604 ' specialpagealiases - List of special page aliases',
605 ' magicwords - List of magic words and their aliases',
606 ' statistics - Returns site statistics',
607 " interwikimap - Returns interwiki map (optionally filtered, (optionally localised by using {$p}inlanguagecode))",
608 ' dbrepllag - Returns database server with the highest replication lag',
609 ' usergroups - Returns user groups and the associated permissions',
610 ' extensions - Returns extensions installed on the wiki',
611 ' fileextensions - Returns list of file extensions allowed to be uploaded',
612 ' rightsinfo - Returns wiki rights (license) information if available',
613 " languages - Returns a list of languages MediaWiki supports (optionally localised by using {$p}inlanguagecode)",
614 ' skins - Returns a list of all enabled skins',
615 ' extensiontags - Returns a list of parser extension tags',
616 ' functionhooks - Returns a list of parser function hooks',
617 ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)'
619 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
620 'showalldb' => 'List all database servers, not just the one lagging the most',
621 'numberingroup' => 'Lists the number of users in user groups',
622 'inlanguagecode ' => 'Language code for localised language names (best effort, use CLDR extension)',
626 public function getDescription() {
627 return 'Return general information about the site';
630 public function getPossibleErrors() {
631 return array_merge( parent
::getPossibleErrors(), array(
632 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
636 public function getExamples() {
638 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
639 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
640 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
644 public function getHelpUrls() {
645 return 'http://www.mediawiki.org/wiki/API:Meta#siteinfo_.2F_si';
648 public function getVersion() {
649 return __CLASS__
. ': $Id$';