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() );
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 $fallbackLang = $wgContLang->getFallbackLanguageCode();
144 $fallbackLangArray = array();
145 while( $fallbackLang ) {
146 $fallbackLangArray[] = array( 'code' => $fallbackLang );
147 $fallbackLang = Language
::getFallbackFor( $fallbackLang );
149 $data['fallback'] = $fallbackLangArray;
150 $this->getResult()->setIndexedTagName( $data['fallback'], 'lang' );
152 if ( $wgContLang->isRTL() ) {
155 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
157 if ( wfReadOnly() ) {
158 $data['readonly'] = '';
159 $data['readonlyreason'] = wfReadOnlyReason();
161 if ( $GLOBALS['wgEnableWriteAPI'] ) {
162 $data['writeapi'] = '';
165 $tz = $GLOBALS['wgLocaltimezone'];
166 $offset = $GLOBALS['wgLocalTZoffset'];
167 if ( is_null( $tz ) ) {
170 } elseif ( is_null( $offset ) ) {
173 $data['timezone'] = $tz;
174 $data['timeoffset'] = intval( $offset );
175 $data['articlepath'] = $GLOBALS['wgArticlePath'];
176 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
177 $data['script'] = $GLOBALS['wgScript'];
178 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
179 $data['server'] = $GLOBALS['wgServer'];
180 $data['wikiid'] = wfWikiID();
181 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
183 if ( $GLOBALS['wgMiserMode'] ) {
184 $data['misermode'] = '';
187 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
189 return $this->getResult()->addValue( 'query', $property, $data );
192 protected function appendNamespaces( $property ) {
195 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
197 'id' => intval( $ns ),
198 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
200 ApiResult
::setContent( $data[$ns], $title );
201 $canonical = MWNamespace
::getCanonicalName( $ns );
203 if ( MWNamespace
::hasSubpages( $ns ) ) {
204 $data[$ns]['subpages'] = '';
208 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
211 if ( MWNamespace
::isContent( $ns ) ) {
212 $data[$ns]['content'] = '';
216 $this->getResult()->setIndexedTagName( $data, 'ns' );
217 return $this->getResult()->addValue( 'query', $property, $data );
220 protected function appendNamespaceAliases( $property ) {
221 global $wgNamespaceAliases, $wgContLang;
222 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
223 $namespaces = $wgContLang->getNamespaces();
225 foreach ( $aliases as $title => $ns ) {
226 if ( $namespaces[$ns] == $title ) {
227 // Don't list duplicates
231 'id' => intval( $ns )
233 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
237 $this->getResult()->setIndexedTagName( $data, 'ns' );
238 return $this->getResult()->addValue( 'query', $property, $data );
241 protected function appendSpecialPageAliases( $property ) {
244 foreach ( $wgContLang->getSpecialPageAliases() as $specialpage => $aliases ) {
245 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
246 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
249 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
250 return $this->getResult()->addValue( 'query', $property, $data );
253 protected function appendMagicWords( $property ) {
256 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
257 $caseSensitive = array_shift( $aliases );
258 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
259 if ( $caseSensitive ) {
260 $arr['case-sensitive'] = '';
262 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
265 $this->getResult()->setIndexedTagName( $data, 'magicword' );
266 return $this->getResult()->addValue( 'query', $property, $data );
269 protected function appendInterwikiMap( $property, $filter ) {
271 if ( $filter === 'local' ) {
273 } elseif ( $filter === '!local' ) {
275 } elseif ( $filter ) {
276 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
279 $params = $this->extractRequestParams();
280 $langCode = isset( $params['inlanguagecode '] ) ?
$params['inlanguagecode '] : '';
283 $langNames = Language
::getTranslatedLanguageNames( $langCode );
285 $langNames = Language
::getLanguageNames();
288 $getPrefixes = Interwiki
::getAllPrefixes( $local );
291 foreach ( $getPrefixes as $row ) {
292 $prefix = $row['iw_prefix'];
294 $val['prefix'] = $prefix;
295 if ( $row['iw_local'] == '1' ) {
298 // $val['trans'] = intval( $row['iw_trans'] ); // should this be exposed?
299 if ( isset( $langNames[$prefix] ) ) {
300 $val['language'] = $langNames[$prefix];
302 $val['url'] = wfExpandUrl( $row['iw_url'] );
303 if( isset( $row['iw_wikiid'] ) ) {
304 $val['wikiid'] = $row['iw_wikiid'];
306 if( isset( $row['iw_api'] ) ) {
307 $val['api'] = $row['iw_api'];
313 $this->getResult()->setIndexedTagName( $data, 'iw' );
314 return $this->getResult()->addValue( 'query', $property, $data );
317 protected function appendDbReplLagInfo( $property, $includeAll ) {
318 global $wgShowHostnames;
322 if ( !$wgShowHostnames ) {
323 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
326 $lags = $lb->getLagTimes();
327 foreach ( $lags as $i => $lag ) {
329 'host' => $lb->getServerName( $i ),
334 list( $host, $lag, $index ) = $lb->getMaxLag();
336 'host' => $wgShowHostnames
337 ?
$lb->getServerName( $index )
339 'lag' => intval( $lag )
343 $result = $this->getResult();
344 $result->setIndexedTagName( $data, 'db' );
345 return $this->getResult()->addValue( 'query', $property, $data );
348 protected function appendStatistics( $property ) {
349 global $wgDisableCounters;
351 $data['pages'] = intval( SiteStats
::pages() );
352 $data['articles'] = intval( SiteStats
::articles() );
353 if ( !$wgDisableCounters ) {
354 $data['views'] = intval( SiteStats
::views() );
356 $data['edits'] = intval( SiteStats
::edits() );
357 $data['images'] = intval( SiteStats
::images() );
358 $data['users'] = intval( SiteStats
::users() );
359 $data['activeusers'] = intval( SiteStats
::activeUsers() );
360 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
361 $data['jobs'] = intval( SiteStats
::jobs() );
362 return $this->getResult()->addValue( 'query', $property, $data );
365 protected function appendUserGroups( $property, $numberInGroup ) {
366 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
369 $result = $this->getResult();
370 foreach ( $wgGroupPermissions as $group => $permissions ) {
373 'rights' => array_keys( $permissions, true ),
376 if ( $numberInGroup ) {
377 global $wgAutopromote;
379 if ( $group == 'user' ) {
380 $arr['number'] = SiteStats
::users();
382 // '*' and autopromote groups have no size
383 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
384 $arr['number'] = SiteStats
::numberInGroup( $group );
389 'add' => $wgAddGroups,
390 'remove' => $wgRemoveGroups,
391 'add-self' => $wgGroupsAddToSelf,
392 'remove-self' => $wgGroupsRemoveFromSelf
395 foreach ( $groupArr as $type => $rights ) {
396 if ( isset( $rights[$group] ) ) {
397 $arr[$type] = $rights[$group];
398 $result->setIndexedTagName( $arr[$type], 'group' );
402 $result->setIndexedTagName( $arr['rights'], 'permission' );
406 $result->setIndexedTagName( $data, 'group' );
407 return $result->addValue( 'query', $property, $data );
410 protected function appendFileExtensions( $property ) {
411 global $wgFileExtensions;
414 foreach ( $wgFileExtensions as $ext ) {
415 $data[] = array( 'ext' => $ext );
417 $this->getResult()->setIndexedTagName( $data, 'fe' );
418 return $this->getResult()->addValue( 'query', $property, $data );
421 protected function appendExtensions( $property ) {
422 global $wgExtensionCredits;
424 foreach ( $wgExtensionCredits as $type => $extensions ) {
425 foreach ( $extensions as $ext ) {
427 $ret['type'] = $type;
428 if ( isset( $ext['name'] ) ) {
429 $ret['name'] = $ext['name'];
431 if ( isset( $ext['description'] ) ) {
432 $ret['description'] = $ext['description'];
434 if ( isset( $ext['descriptionmsg'] ) ) {
435 // Can be a string or array( key, param1, param2, ... )
436 if ( is_array( $ext['descriptionmsg'] ) ) {
437 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
438 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
439 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
441 $ret['descriptionmsg'] = $ext['descriptionmsg'];
444 if ( isset( $ext['author'] ) ) {
445 $ret['author'] = is_array( $ext['author'] ) ?
446 implode( ', ', $ext['author' ] ) : $ext['author'];
448 if ( isset( $ext['url'] ) ) {
449 $ret['url'] = $ext['url'];
451 if ( isset( $ext['version'] ) ) {
452 $ret['version'] = $ext['version'];
453 } elseif ( isset( $ext['svn-revision'] ) &&
454 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
455 $ext['svn-revision'], $m ) )
457 $ret['version'] = 'r' . $m[1];
463 $this->getResult()->setIndexedTagName( $data, 'ext' );
464 return $this->getResult()->addValue( 'query', $property, $data );
467 protected function appendRightsInfo( $property ) {
468 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
469 $title = Title
::newFromText( $wgRightsPage );
470 $url = $title ?
wfExpandUrl( $title->getFullURL() ) : $wgRightsUrl;
471 $text = $wgRightsText;
472 if ( !$text && $title ) {
473 $text = $title->getPrefixedText();
477 'url' => $url ?
$url : '',
478 'text' => $text ?
$text : ''
481 return $this->getResult()->addValue( 'query', $property, $data );
484 public function appendLanguages( $property ) {
485 $params = $this->extractRequestParams();
486 $langCode = isset( $params['inlanguagecode '] ) ?
$params['inlanguagecode '] : '';
489 $langNames = Language
::getTranslatedLanguageNames( $langCode );
491 $langNames = Language
::getLanguageNames();
496 foreach ( $langNames as $code => $name ) {
497 $lang = array( 'code' => $code );
498 ApiResult
::setContent( $lang, $name );
501 $this->getResult()->setIndexedTagName( $data, 'lang' );
502 return $this->getResult()->addValue( 'query', $property, $data );
505 public function appendSkins( $property ) {
507 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
508 $skin = array( 'code' => $name );
509 ApiResult
::setContent( $skin, $displayName );
512 $this->getResult()->setIndexedTagName( $data, 'skin' );
513 return $this->getResult()->addValue( 'query', $property, $data );
516 public function appendExtensionTags( $property ) {
518 $wgParser->firstCallInit();
519 $tags = array_map( array( $this, 'formatParserTags'), $wgParser->getTags() );
520 $this->getResult()->setIndexedTagName( $tags, 't' );
521 return $this->getResult()->addValue( 'query', $property, $tags );
524 public function appendFunctionHooks( $property ) {
526 $wgParser->firstCallInit();
527 $hooks = $wgParser->getFunctionHooks();
528 $this->getResult()->setIndexedTagName( $hooks, 'h' );
529 return $this->getResult()->addValue( 'query', $property, $hooks );
532 private function formatParserTags( $item ) {
536 public function appendSubscribedHooks( $property ) {
538 $myWgHooks = $wgHooks;
542 foreach ( $myWgHooks as $hook => $hooks ) {
545 'subscribers' => array_map( array( 'SpecialVersion', 'arrayToString' ), $hooks ),
548 $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
552 $this->getResult()->setIndexedTagName( $data, 'hook' );
553 return $this->getResult()->addValue( 'query', $property, $data );
556 public function getCacheMode( $params ) {
560 public function getAllowedParams() {
563 ApiBase
::PARAM_DFLT
=> 'general',
564 ApiBase
::PARAM_ISMULTI
=> true,
565 ApiBase
::PARAM_TYPE
=> array(
569 'specialpagealiases',
586 ApiBase
::PARAM_TYPE
=> array(
591 'showalldb' => false,
592 'numberingroup' => false,
593 'inlanguagecode ' => null,
597 public function getParamDescription() {
598 $p = $this->getModulePrefix();
601 'Which sysinfo properties to get:',
602 ' general - Overall system information',
603 ' namespaces - List of registered namespaces and their canonical names',
604 ' namespacealiases - List of registered namespace aliases',
605 ' specialpagealiases - List of special page aliases',
606 ' magicwords - List of magic words and their aliases',
607 ' statistics - Returns site statistics',
608 " interwikimap - Returns interwiki map (optionally filtered, (optionally localised by using {$p}inlanguagecode))",
609 ' dbrepllag - Returns database server with the highest replication lag',
610 ' usergroups - Returns user groups and the associated permissions',
611 ' extensions - Returns extensions installed on the wiki',
612 ' fileextensions - Returns list of file extensions allowed to be uploaded',
613 ' rightsinfo - Returns wiki rights (license) information if available',
614 " languages - Returns a list of languages MediaWiki supports (optionally localised by using {$p}inlanguagecode)",
615 ' skins - Returns a list of all enabled skins',
616 ' extensiontags - Returns a list of parser extension tags',
617 ' functionhooks - Returns a list of parser function hooks',
618 ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)'
620 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
621 'showalldb' => 'List all database servers, not just the one lagging the most',
622 'numberingroup' => 'Lists the number of users in user groups',
623 'inlanguagecode ' => 'Language code for localised language names (best effort, use CLDR extension)',
627 public function getDescription() {
628 return 'Return general information about the site';
631 public function getPossibleErrors() {
632 return array_merge( parent
::getPossibleErrors(), array(
633 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
637 public function getExamples() {
639 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
640 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
641 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
645 public function getHelpUrls() {
646 return 'http://www.mediawiki.org/wiki/API:Meta#siteinfo_.2F_si';
649 public function getVersion() {
650 return __CLASS__
. ': $Id$';