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
28 * A query action to return meta information about the wiki site.
32 class ApiQuerySiteinfo
extends ApiQueryBase
{
34 public function __construct( $query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'si' );
38 public function execute() {
39 $params = $this->extractRequestParams();
42 foreach ( $params['prop'] as $p ) {
45 $fit = $this->appendGeneralInfo( $p );
48 $fit = $this->appendNamespaces( $p );
50 case 'namespacealiases':
51 $fit = $this->appendNamespaceAliases( $p );
53 case 'specialpagealiases':
54 $fit = $this->appendSpecialPageAliases( $p );
57 $fit = $this->appendMagicWords( $p );
60 $filteriw = isset( $params['filteriw'] ) ?
$params['filteriw'] : false;
61 $fit = $this->appendInterwikiMap( $p, $filteriw );
64 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
67 $fit = $this->appendStatistics( $p );
70 $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
73 $fit = $this->appendExtensions( $p );
75 case 'fileextensions':
76 $fit = $this->appendFileExtensions( $p );
79 $fit = $this->appendRightsInfo( $p );
82 $fit = $this->appendLanguages( $p );
85 $fit = $this->appendSkins( $p );
88 $fit = $this->appendExtensionTags( $p );
91 $fit = $this->appendFunctionHooks( $p );
94 $fit = $this->appendSubscribedHooks( $p );
97 $fit = $this->appendVariables( $p );
100 $fit = $this->appendProtocols( $p );
103 ApiBase
::dieDebug( __METHOD__
, "Unknown prop=$p" );
106 // Abuse siprop as a query-continue parameter
107 // and set it to all unprocessed props
108 $this->setContinueEnumParameter( 'prop', implode( '|',
109 array_diff( $params['prop'], $done ) ) );
116 protected function appendGeneralInfo( $property ) {
118 $wgDisableLangConversion,
119 $wgDisableTitleConversion;
122 $mainPage = Title
::newMainPage();
123 $data['mainpage'] = $mainPage->getPrefixedText();
124 $data['base'] = wfExpandUrl( $mainPage->getFullUrl(), PROTO_CURRENT
);
125 $data['sitename'] = $GLOBALS['wgSitename'];
126 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
127 $data['phpversion'] = phpversion();
128 $data['phpsapi'] = PHP_SAPI
;
129 $data['dbtype'] = $GLOBALS['wgDBtype'];
130 $data['dbversion'] = $this->getDB()->getServerVersion();
132 if ( !$wgDisableLangConversion ) {
133 $data['langconversion'] = '';
136 if ( !$wgDisableTitleConversion ) {
137 $data['titleconversion'] = '';
140 if ( $wgContLang->linkPrefixExtension() ) {
141 $data['linkprefix'] = wfMessage( 'linkprefix' )->inContentLanguage()->text();
144 $linktrail = $wgContLang->linkTrail();
146 $data['linktrail'] = $linktrail;
149 $git = SpecialVersion
::getGitHeadSha1( $GLOBALS['IP'] );
151 $data['git-hash'] = $git;
153 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
159 // 'case-insensitive' option is reserved for future
160 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
162 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
163 $data['rightscode'] = $GLOBALS['wgRightsCode'];
165 $data['rights'] = $GLOBALS['wgRightsText'];
166 $data['lang'] = $GLOBALS['wgLanguageCode'];
168 $fallbacks = array();
169 foreach( $wgContLang->getFallbackLanguages() as $code ) {
170 $fallbacks[] = array( 'code' => $code );
172 $data['fallback'] = $fallbacks;
173 $this->getResult()->setIndexedTagName( $data['fallback'], 'lang' );
175 if( $wgContLang->hasVariants() ) {
177 foreach( $wgContLang->getVariants() as $code ) {
178 $variants[] = array( 'code' => $code );
180 $data['variants'] = $variants;
181 $this->getResult()->setIndexedTagName( $data['variants'], 'lang' );
184 if ( $wgContLang->isRTL() ) {
187 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
189 if ( wfReadOnly() ) {
190 $data['readonly'] = '';
191 $data['readonlyreason'] = wfReadOnlyReason();
193 if ( $GLOBALS['wgEnableWriteAPI'] ) {
194 $data['writeapi'] = '';
197 $tz = $GLOBALS['wgLocaltimezone'];
198 $offset = $GLOBALS['wgLocalTZoffset'];
199 if ( is_null( $tz ) ) {
202 } elseif ( is_null( $offset ) ) {
205 $data['timezone'] = $tz;
206 $data['timeoffset'] = intval( $offset );
207 $data['articlepath'] = $GLOBALS['wgArticlePath'];
208 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
209 $data['script'] = $GLOBALS['wgScript'];
210 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
211 $data['server'] = $GLOBALS['wgServer'];
212 $data['wikiid'] = wfWikiID();
213 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
215 if ( $GLOBALS['wgMiserMode'] ) {
216 $data['misermode'] = '';
219 $data['maxuploadsize'] = UploadBase
::getMaxUploadSize();
221 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
223 return $this->getResult()->addValue( 'query', $property, $data );
226 protected function appendNamespaces( $property ) {
229 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
231 'id' => intval( $ns ),
232 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
234 ApiResult
::setContent( $data[$ns], $title );
235 $canonical = MWNamespace
::getCanonicalName( $ns );
237 if ( MWNamespace
::hasSubpages( $ns ) ) {
238 $data[$ns]['subpages'] = '';
242 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
245 if ( MWNamespace
::isContent( $ns ) ) {
246 $data[$ns]['content'] = '';
249 if ( MWNamespace
::isNonincludable( $ns ) ) {
250 $data[$ns]['nonincludable'] = '';
253 $contentmodel = MWNamespace
::getNamespaceContentModel( $ns );
254 if ( $contentmodel ) {
255 $data[$ns]['defaultcontentmodel'] = $contentmodel;
259 $this->getResult()->setIndexedTagName( $data, 'ns' );
260 return $this->getResult()->addValue( 'query', $property, $data );
263 protected function appendNamespaceAliases( $property ) {
264 global $wgNamespaceAliases, $wgContLang;
265 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
266 $namespaces = $wgContLang->getNamespaces();
268 foreach ( $aliases as $title => $ns ) {
269 if ( $namespaces[$ns] == $title ) {
270 // Don't list duplicates
274 'id' => intval( $ns )
276 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
280 $this->getResult()->setIndexedTagName( $data, 'ns' );
281 return $this->getResult()->addValue( 'query', $property, $data );
284 protected function appendSpecialPageAliases( $property ) {
287 $aliases = $wgContLang->getSpecialPageAliases();
288 foreach ( SpecialPageFactory
::getList() as $specialpage => $stuff ) {
289 if ( isset( $aliases[$specialpage] ) ) {
290 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases[$specialpage] );
291 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
295 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
296 return $this->getResult()->addValue( 'query', $property, $data );
299 protected function appendMagicWords( $property ) {
302 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
303 $caseSensitive = array_shift( $aliases );
304 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
305 if ( $caseSensitive ) {
306 $arr['case-sensitive'] = '';
308 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
311 $this->getResult()->setIndexedTagName( $data, 'magicword' );
312 return $this->getResult()->addValue( 'query', $property, $data );
315 protected function appendInterwikiMap( $property, $filter ) {
317 if ( $filter === 'local' ) {
319 } elseif ( $filter === '!local' ) {
321 } elseif ( $filter ) {
322 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
325 $params = $this->extractRequestParams();
326 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
327 $langNames = Language
::fetchLanguageNames( $langCode );
329 $getPrefixes = Interwiki
::getAllPrefixes( $local );
332 foreach ( $getPrefixes as $row ) {
333 $prefix = $row['iw_prefix'];
335 $val['prefix'] = $prefix;
336 if ( $row['iw_local'] == '1' ) {
339 // $val['trans'] = intval( $row['iw_trans'] ); // should this be exposed?
340 if ( isset( $langNames[$prefix] ) ) {
341 $val['language'] = $langNames[$prefix];
343 $val['url'] = wfExpandUrl( $row['iw_url'], PROTO_CURRENT
);
344 if( isset( $row['iw_wikiid'] ) ) {
345 $val['wikiid'] = $row['iw_wikiid'];
347 if( isset( $row['iw_api'] ) ) {
348 $val['api'] = $row['iw_api'];
354 $this->getResult()->setIndexedTagName( $data, 'iw' );
355 return $this->getResult()->addValue( 'query', $property, $data );
358 protected function appendDbReplLagInfo( $property, $includeAll ) {
359 global $wgShowHostnames;
363 if ( !$wgShowHostnames ) {
364 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
367 $lags = $lb->getLagTimes();
368 foreach ( $lags as $i => $lag ) {
370 'host' => $lb->getServerName( $i ),
375 list( $host, $lag, $index ) = $lb->getMaxLag();
377 'host' => $wgShowHostnames
378 ?
$lb->getServerName( $index )
380 'lag' => intval( $lag )
384 $result = $this->getResult();
385 $result->setIndexedTagName( $data, 'db' );
386 return $this->getResult()->addValue( 'query', $property, $data );
389 protected function appendStatistics( $property ) {
390 global $wgDisableCounters;
392 $data['pages'] = intval( SiteStats
::pages() );
393 $data['articles'] = intval( SiteStats
::articles() );
394 if ( !$wgDisableCounters ) {
395 $data['views'] = intval( SiteStats
::views() );
397 $data['edits'] = intval( SiteStats
::edits() );
398 $data['images'] = intval( SiteStats
::images() );
399 $data['users'] = intval( SiteStats
::users() );
400 $data['activeusers'] = intval( SiteStats
::activeUsers() );
401 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
402 $data['jobs'] = intval( SiteStats
::jobs() );
403 return $this->getResult()->addValue( 'query', $property, $data );
406 protected function appendUserGroups( $property, $numberInGroup ) {
407 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
410 $result = $this->getResult();
411 foreach ( $wgGroupPermissions as $group => $permissions ) {
414 'rights' => array_keys( $permissions, true ),
417 if ( $numberInGroup ) {
418 global $wgAutopromote;
420 if ( $group == 'user' ) {
421 $arr['number'] = SiteStats
::users();
423 // '*' and autopromote groups have no size
424 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
425 $arr['number'] = SiteStats
::numberInGroup( $group );
430 'add' => $wgAddGroups,
431 'remove' => $wgRemoveGroups,
432 'add-self' => $wgGroupsAddToSelf,
433 'remove-self' => $wgGroupsRemoveFromSelf
436 foreach ( $groupArr as $type => $rights ) {
437 if ( isset( $rights[$group] ) ) {
438 $arr[$type] = $rights[$group];
439 $result->setIndexedTagName( $arr[$type], 'group' );
443 $result->setIndexedTagName( $arr['rights'], 'permission' );
447 $result->setIndexedTagName( $data, 'group' );
448 return $result->addValue( 'query', $property, $data );
451 protected function appendFileExtensions( $property ) {
452 global $wgFileExtensions;
455 foreach ( $wgFileExtensions as $ext ) {
456 $data[] = array( 'ext' => $ext );
458 $this->getResult()->setIndexedTagName( $data, 'fe' );
459 return $this->getResult()->addValue( 'query', $property, $data );
462 protected function appendExtensions( $property ) {
463 global $wgExtensionCredits;
465 foreach ( $wgExtensionCredits as $type => $extensions ) {
466 foreach ( $extensions as $ext ) {
468 $ret['type'] = $type;
469 if ( isset( $ext['name'] ) ) {
470 $ret['name'] = $ext['name'];
472 if ( isset( $ext['description'] ) ) {
473 $ret['description'] = $ext['description'];
475 if ( isset( $ext['descriptionmsg'] ) ) {
476 // Can be a string or array( key, param1, param2, ... )
477 if ( is_array( $ext['descriptionmsg'] ) ) {
478 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
479 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
480 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
482 $ret['descriptionmsg'] = $ext['descriptionmsg'];
485 if ( isset( $ext['author'] ) ) {
486 $ret['author'] = is_array( $ext['author'] ) ?
487 implode( ', ', $ext['author'] ) : $ext['author'];
489 if ( isset( $ext['url'] ) ) {
490 $ret['url'] = $ext['url'];
492 if ( isset( $ext['version'] ) ) {
493 $ret['version'] = $ext['version'];
494 } elseif ( isset( $ext['svn-revision'] ) &&
495 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
496 $ext['svn-revision'], $m ) )
498 $ret['version'] = 'r' . $m[1];
504 $this->getResult()->setIndexedTagName( $data, 'ext' );
505 return $this->getResult()->addValue( 'query', $property, $data );
508 protected function appendRightsInfo( $property ) {
509 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
510 $title = Title
::newFromText( $wgRightsPage );
511 $url = $title ?
wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
) : $wgRightsUrl;
512 $text = $wgRightsText;
513 if ( !$text && $title ) {
514 $text = $title->getPrefixedText();
518 'url' => $url ?
$url : '',
519 'text' => $text ?
$text : ''
522 return $this->getResult()->addValue( 'query', $property, $data );
525 public function appendLanguages( $property ) {
526 $params = $this->extractRequestParams();
527 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
528 $langNames = Language
::fetchLanguageNames( $langCode );
532 foreach ( $langNames as $code => $name ) {
533 $lang = array( 'code' => $code );
534 ApiResult
::setContent( $lang, $name );
537 $this->getResult()->setIndexedTagName( $data, 'lang' );
538 return $this->getResult()->addValue( 'query', $property, $data );
541 public function appendSkins( $property ) {
543 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
544 $skin = array( 'code' => $name );
545 ApiResult
::setContent( $skin, $displayName );
548 $this->getResult()->setIndexedTagName( $data, 'skin' );
549 return $this->getResult()->addValue( 'query', $property, $data );
552 public function appendExtensionTags( $property ) {
554 $wgParser->firstCallInit();
555 $tags = array_map( array( $this, 'formatParserTags' ), $wgParser->getTags() );
556 $this->getResult()->setIndexedTagName( $tags, 't' );
557 return $this->getResult()->addValue( 'query', $property, $tags );
560 public function appendFunctionHooks( $property ) {
562 $wgParser->firstCallInit();
563 $hooks = $wgParser->getFunctionHooks();
564 $this->getResult()->setIndexedTagName( $hooks, 'h' );
565 return $this->getResult()->addValue( 'query', $property, $hooks );
568 public function appendVariables( $property ) {
569 $variables = MagicWord
::getVariableIDs();
570 $this->getResult()->setIndexedTagName( $variables, 'v' );
571 return $this->getResult()->addValue( 'query', $property, $variables );
574 public function appendProtocols( $property ) {
575 global $wgUrlProtocols;
576 // Make a copy of the global so we don't try to set the _element key of it - bug 45130
577 $protocols = array_values( $wgUrlProtocols );
578 $this->getResult()->setIndexedTagName( $protocols, 'p' );
579 return $this->getResult()->addValue( 'query', $property, $protocols );
582 private function formatParserTags( $item ) {
586 public function appendSubscribedHooks( $property ) {
588 $myWgHooks = $wgHooks;
592 foreach ( $myWgHooks as $hook => $hooks ) {
595 'subscribers' => array_map( array( 'SpecialVersion', 'arrayToString' ), $hooks ),
598 $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
602 $this->getResult()->setIndexedTagName( $data, 'hook' );
603 return $this->getResult()->addValue( 'query', $property, $data );
606 public function getCacheMode( $params ) {
610 public function getAllowedParams() {
613 ApiBase
::PARAM_DFLT
=> 'general',
614 ApiBase
::PARAM_ISMULTI
=> true,
615 ApiBase
::PARAM_TYPE
=> array(
619 'specialpagealiases',
638 ApiBase
::PARAM_TYPE
=> array(
643 'showalldb' => false,
644 'numberingroup' => false,
645 'inlanguagecode' => null,
649 public function getParamDescription() {
650 $p = $this->getModulePrefix();
653 'Which sysinfo properties to get:',
654 ' general - Overall system information',
655 ' namespaces - List of registered namespaces and their canonical names',
656 ' namespacealiases - List of registered namespace aliases',
657 ' specialpagealiases - List of special page aliases',
658 ' magicwords - List of magic words and their aliases',
659 ' statistics - Returns site statistics',
660 " interwikimap - Returns interwiki map (optionally filtered, (optionally localised by using {$p}inlanguagecode))",
661 ' dbrepllag - Returns database server with the highest replication lag',
662 ' usergroups - Returns user groups and the associated permissions',
663 ' extensions - Returns extensions installed on the wiki',
664 ' fileextensions - Returns list of file extensions allowed to be uploaded',
665 ' rightsinfo - Returns wiki rights (license) information if available',
666 " languages - Returns a list of languages MediaWiki supports (optionally localised by using {$p}inlanguagecode)",
667 ' skins - Returns a list of all enabled skins',
668 ' extensiontags - Returns a list of parser extension tags',
669 ' functionhooks - Returns a list of parser function hooks',
670 ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)',
671 ' variables - Returns a list of variable IDs',
672 ' protocols - Returns a list of protocols that are allowed in external links.',
674 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
675 'showalldb' => 'List all database servers, not just the one lagging the most',
676 'numberingroup' => 'Lists the number of users in user groups',
677 'inlanguagecode' => 'Language code for localised language names (best effort, use CLDR extension)',
681 public function getDescription() {
682 return 'Return general information about the site';
685 public function getPossibleErrors() {
686 return array_merge( parent
::getPossibleErrors(), array(
687 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
691 public function getExamples() {
693 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
694 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
695 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
699 public function getHelpUrls() {
700 return 'https://www.mediawiki.org/wiki/API:Meta#siteinfo_.2F_si';