a7767062e8aa908976d718e4c1736ca5512d75f4
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['logo'] = $GLOBALS['wgLogo'];
127 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
128 $data['phpversion'] = phpversion();
129 $data['phpsapi'] = PHP_SAPI
;
130 $data['dbtype'] = $GLOBALS['wgDBtype'];
131 $data['dbversion'] = $this->getDB()->getServerVersion();
133 $allowFrom = array( '' );
134 $allowException = true;
135 if ( !$GLOBALS['wgAllowExternalImages'] ) {
136 if ( $GLOBALS['wgEnableImageWhitelist'] ) {
137 $data['imagewhitelistenabled'] = '';
139 $allowFrom = $GLOBALS['wgAllowExternalImagesFrom'];
140 $allowException = !empty( $allowFrom );
142 if ( $allowException ) {
143 $data['externalimages'] = (array)$allowFrom;
144 $this->getResult()->setIndexedTagName( $data['externalimages'], 'prefix' );
147 if ( !$wgDisableLangConversion ) {
148 $data['langconversion'] = '';
151 if ( !$wgDisableTitleConversion ) {
152 $data['titleconversion'] = '';
155 if ( $wgContLang->linkPrefixExtension() ) {
156 $data['linkprefix'] = wfMessage( 'linkprefix' )->inContentLanguage()->text();
158 $data['linkprefix'] = '';
161 $linktrail = $wgContLang->linkTrail();
163 $data['linktrail'] = $linktrail;
165 $data['linktrail'] = '';
168 $git = SpecialVersion
::getGitHeadSha1( $GLOBALS['IP'] );
170 $data['git-hash'] = $git;
172 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
178 // 'case-insensitive' option is reserved for future
179 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
181 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
182 $data['rightscode'] = $GLOBALS['wgRightsCode'];
184 $data['rights'] = $GLOBALS['wgRightsText'];
185 $data['lang'] = $GLOBALS['wgLanguageCode'];
187 $fallbacks = array();
188 foreach ( $wgContLang->getFallbackLanguages() as $code ) {
189 $fallbacks[] = array( 'code' => $code );
191 $data['fallback'] = $fallbacks;
192 $this->getResult()->setIndexedTagName( $data['fallback'], 'lang' );
194 if ( $wgContLang->hasVariants() ) {
196 foreach ( $wgContLang->getVariants() as $code ) {
197 $variants[] = array( 'code' => $code );
199 $data['variants'] = $variants;
200 $this->getResult()->setIndexedTagName( $data['variants'], 'lang' );
203 if ( $wgContLang->isRTL() ) {
206 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
208 if ( wfReadOnly() ) {
209 $data['readonly'] = '';
210 $data['readonlyreason'] = wfReadOnlyReason();
212 if ( $GLOBALS['wgEnableWriteAPI'] ) {
213 $data['writeapi'] = '';
216 $tz = $GLOBALS['wgLocaltimezone'];
217 $offset = $GLOBALS['wgLocalTZoffset'];
218 if ( is_null( $tz ) ) {
221 } elseif ( is_null( $offset ) ) {
224 $data['timezone'] = $tz;
225 $data['timeoffset'] = intval( $offset );
226 $data['articlepath'] = $GLOBALS['wgArticlePath'];
227 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
228 $data['script'] = $GLOBALS['wgScript'];
229 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
230 $data['server'] = $GLOBALS['wgServer'];
231 $data['wikiid'] = wfWikiID();
232 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
234 if ( $GLOBALS['wgMiserMode'] ) {
235 $data['misermode'] = '';
238 $data['maxuploadsize'] = UploadBase
::getMaxUploadSize();
240 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
242 return $this->getResult()->addValue( 'query', $property, $data );
245 protected function appendNamespaces( $property ) {
248 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
250 'id' => intval( $ns ),
251 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
253 ApiResult
::setContent( $data[$ns], $title );
254 $canonical = MWNamespace
::getCanonicalName( $ns );
256 if ( MWNamespace
::hasSubpages( $ns ) ) {
257 $data[$ns]['subpages'] = '';
261 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
264 if ( MWNamespace
::isContent( $ns ) ) {
265 $data[$ns]['content'] = '';
268 if ( MWNamespace
::isNonincludable( $ns ) ) {
269 $data[$ns]['nonincludable'] = '';
272 $contentmodel = MWNamespace
::getNamespaceContentModel( $ns );
273 if ( $contentmodel ) {
274 $data[$ns]['defaultcontentmodel'] = $contentmodel;
278 $this->getResult()->setIndexedTagName( $data, 'ns' );
279 return $this->getResult()->addValue( 'query', $property, $data );
282 protected function appendNamespaceAliases( $property ) {
283 global $wgNamespaceAliases, $wgContLang;
284 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
285 $namespaces = $wgContLang->getNamespaces();
287 foreach ( $aliases as $title => $ns ) {
288 if ( $namespaces[$ns] == $title ) {
289 // Don't list duplicates
293 'id' => intval( $ns )
295 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
301 $this->getResult()->setIndexedTagName( $data, 'ns' );
302 return $this->getResult()->addValue( 'query', $property, $data );
305 protected function appendSpecialPageAliases( $property ) {
308 $aliases = $wgContLang->getSpecialPageAliases();
309 foreach ( SpecialPageFactory
::getList() as $specialpage => $stuff ) {
310 if ( isset( $aliases[$specialpage] ) ) {
311 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases[$specialpage] );
312 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
316 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
317 return $this->getResult()->addValue( 'query', $property, $data );
320 protected function appendMagicWords( $property ) {
323 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
324 $caseSensitive = array_shift( $aliases );
325 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
326 if ( $caseSensitive ) {
327 $arr['case-sensitive'] = '';
329 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
332 $this->getResult()->setIndexedTagName( $data, 'magicword' );
333 return $this->getResult()->addValue( 'query', $property, $data );
336 protected function appendInterwikiMap( $property, $filter ) {
338 if ( $filter === 'local' ) {
340 } elseif ( $filter === '!local' ) {
342 } elseif ( $filter ) {
343 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
346 $params = $this->extractRequestParams();
347 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
348 $langNames = Language
::fetchLanguageNames( $langCode );
350 $getPrefixes = Interwiki
::getAllPrefixes( $local );
353 foreach ( $getPrefixes as $row ) {
354 $prefix = $row['iw_prefix'];
356 $val['prefix'] = $prefix;
357 if ( $row['iw_local'] == '1' ) {
360 // $val['trans'] = intval( $row['iw_trans'] ); // should this be exposed?
361 if ( isset( $langNames[$prefix] ) ) {
362 $val['language'] = $langNames[$prefix];
364 $val['url'] = wfExpandUrl( $row['iw_url'], PROTO_CURRENT
);
365 if ( isset( $row['iw_wikiid'] ) ) {
366 $val['wikiid'] = $row['iw_wikiid'];
368 if ( isset( $row['iw_api'] ) ) {
369 $val['api'] = $row['iw_api'];
375 $this->getResult()->setIndexedTagName( $data, 'iw' );
376 return $this->getResult()->addValue( 'query', $property, $data );
379 protected function appendDbReplLagInfo( $property, $includeAll ) {
380 global $wgShowHostnames;
384 if ( !$wgShowHostnames ) {
385 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
388 $lags = $lb->getLagTimes();
389 foreach ( $lags as $i => $lag ) {
391 'host' => $lb->getServerName( $i ),
396 list( , $lag, $index ) = $lb->getMaxLag();
398 'host' => $wgShowHostnames
399 ?
$lb->getServerName( $index )
401 'lag' => intval( $lag )
405 $result = $this->getResult();
406 $result->setIndexedTagName( $data, 'db' );
407 return $this->getResult()->addValue( 'query', $property, $data );
410 protected function appendStatistics( $property ) {
411 global $wgDisableCounters;
413 $data['pages'] = intval( SiteStats
::pages() );
414 $data['articles'] = intval( SiteStats
::articles() );
415 if ( !$wgDisableCounters ) {
416 $data['views'] = intval( SiteStats
::views() );
418 $data['edits'] = intval( SiteStats
::edits() );
419 $data['images'] = intval( SiteStats
::images() );
420 $data['users'] = intval( SiteStats
::users() );
421 $data['activeusers'] = intval( SiteStats
::activeUsers() );
422 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
423 $data['jobs'] = intval( SiteStats
::jobs() );
425 wfRunHooks( 'APIQuerySiteInfoStatisticsInfo', array( &$data ) );
427 return $this->getResult()->addValue( 'query', $property, $data );
430 protected function appendUserGroups( $property, $numberInGroup ) {
431 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups;
432 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
435 $result = $this->getResult();
436 foreach ( $wgGroupPermissions as $group => $permissions ) {
439 'rights' => array_keys( $permissions, true ),
442 if ( $numberInGroup ) {
443 global $wgAutopromote;
445 if ( $group == 'user' ) {
446 $arr['number'] = SiteStats
::users();
448 // '*' and autopromote groups have no size
449 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
450 $arr['number'] = SiteStats
::numberInGroup( $group );
455 'add' => $wgAddGroups,
456 'remove' => $wgRemoveGroups,
457 'add-self' => $wgGroupsAddToSelf,
458 'remove-self' => $wgGroupsRemoveFromSelf
461 foreach ( $groupArr as $type => $rights ) {
462 if ( isset( $rights[$group] ) ) {
463 $arr[$type] = $rights[$group];
464 $result->setIndexedTagName( $arr[$type], 'group' );
468 $result->setIndexedTagName( $arr['rights'], 'permission' );
472 $result->setIndexedTagName( $data, 'group' );
473 return $result->addValue( 'query', $property, $data );
476 protected function appendFileExtensions( $property ) {
477 global $wgFileExtensions;
480 foreach ( array_unique( $wgFileExtensions ) as $ext ) {
481 $data[] = array( 'ext' => $ext );
483 $this->getResult()->setIndexedTagName( $data, 'fe' );
484 return $this->getResult()->addValue( 'query', $property, $data );
487 protected function appendExtensions( $property ) {
488 global $wgExtensionCredits;
490 foreach ( $wgExtensionCredits as $type => $extensions ) {
491 foreach ( $extensions as $ext ) {
493 $ret['type'] = $type;
494 if ( isset( $ext['name'] ) ) {
495 $ret['name'] = $ext['name'];
497 if ( isset( $ext['description'] ) ) {
498 $ret['description'] = $ext['description'];
500 if ( isset( $ext['descriptionmsg'] ) ) {
501 // Can be a string or array( key, param1, param2, ... )
502 if ( is_array( $ext['descriptionmsg'] ) ) {
503 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
504 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
505 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
507 $ret['descriptionmsg'] = $ext['descriptionmsg'];
510 if ( isset( $ext['author'] ) ) {
511 $ret['author'] = is_array( $ext['author'] ) ?
512 implode( ', ', $ext['author'] ) : $ext['author'];
514 if ( isset( $ext['url'] ) ) {
515 $ret['url'] = $ext['url'];
517 if ( isset( $ext['version'] ) ) {
518 $ret['version'] = $ext['version'];
519 } elseif ( isset( $ext['svn-revision'] ) &&
520 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
521 $ext['svn-revision'], $m ) )
523 $ret['version'] = 'r' . $m[1];
529 $this->getResult()->setIndexedTagName( $data, 'ext' );
530 return $this->getResult()->addValue( 'query', $property, $data );
533 protected function appendRightsInfo( $property ) {
534 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
535 $title = Title
::newFromText( $wgRightsPage );
536 $url = $title ?
wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
) : $wgRightsUrl;
537 $text = $wgRightsText;
538 if ( !$text && $title ) {
539 $text = $title->getPrefixedText();
543 'url' => $url ?
$url : '',
544 'text' => $text ?
$text : ''
547 return $this->getResult()->addValue( 'query', $property, $data );
550 public function appendLanguages( $property ) {
551 $params = $this->extractRequestParams();
552 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
553 $langNames = Language
::fetchLanguageNames( $langCode );
557 foreach ( $langNames as $code => $name ) {
558 $lang = array( 'code' => $code );
559 ApiResult
::setContent( $lang, $name );
562 $this->getResult()->setIndexedTagName( $data, 'lang' );
563 return $this->getResult()->addValue( 'query', $property, $data );
566 public function appendSkins( $property ) {
568 $usable = Skin
::getUsableSkins();
569 $default = Skin
::normalizeKey( 'default' );
570 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
571 $skin = array( 'code' => $name );
572 ApiResult
::setContent( $skin, $displayName );
573 if ( !isset( $usable[$name] ) ) {
574 $skin['unusable'] = '';
576 if ( $name === $default ) {
577 $skin['default'] = '';
581 $this->getResult()->setIndexedTagName( $data, 'skin' );
582 return $this->getResult()->addValue( 'query', $property, $data );
585 public function appendExtensionTags( $property ) {
587 $wgParser->firstCallInit();
588 $tags = array_map( array( $this, 'formatParserTags' ), $wgParser->getTags() );
589 $this->getResult()->setIndexedTagName( $tags, 't' );
590 return $this->getResult()->addValue( 'query', $property, $tags );
593 public function appendFunctionHooks( $property ) {
595 $wgParser->firstCallInit();
596 $hooks = $wgParser->getFunctionHooks();
597 $this->getResult()->setIndexedTagName( $hooks, 'h' );
598 return $this->getResult()->addValue( 'query', $property, $hooks );
601 public function appendVariables( $property ) {
602 $variables = MagicWord
::getVariableIDs();
603 $this->getResult()->setIndexedTagName( $variables, 'v' );
604 return $this->getResult()->addValue( 'query', $property, $variables );
607 public function appendProtocols( $property ) {
608 global $wgUrlProtocols;
609 // Make a copy of the global so we don't try to set the _element key of it - bug 45130
610 $protocols = array_values( $wgUrlProtocols );
611 $this->getResult()->setIndexedTagName( $protocols, 'p' );
612 return $this->getResult()->addValue( 'query', $property, $protocols );
615 private function formatParserTags( $item ) {
619 public function appendSubscribedHooks( $property ) {
621 $myWgHooks = $wgHooks;
625 foreach ( $myWgHooks as $hook => $hooks ) {
628 'subscribers' => array_map( array( 'SpecialVersion', 'arrayToString' ), $hooks ),
631 $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
635 $this->getResult()->setIndexedTagName( $data, 'hook' );
636 return $this->getResult()->addValue( 'query', $property, $data );
639 public function getCacheMode( $params ) {
643 public function getAllowedParams() {
646 ApiBase
::PARAM_DFLT
=> 'general',
647 ApiBase
::PARAM_ISMULTI
=> true,
648 ApiBase
::PARAM_TYPE
=> array(
652 'specialpagealiases',
671 ApiBase
::PARAM_TYPE
=> array(
676 'showalldb' => false,
677 'numberingroup' => false,
678 'inlanguagecode' => null,
682 public function getParamDescription() {
683 $p = $this->getModulePrefix();
686 'Which sysinfo properties to get:',
687 ' general - Overall system information',
688 ' namespaces - List of registered namespaces and their canonical names',
689 ' namespacealiases - List of registered namespace aliases',
690 ' specialpagealiases - List of special page aliases',
691 ' magicwords - List of magic words and their aliases',
692 ' statistics - Returns site statistics',
693 " interwikimap - Returns interwiki map " .
694 "(optionally filtered, (optionally localised by using {$p}inlanguagecode))",
695 ' dbrepllag - Returns database server with the highest replication lag',
696 ' usergroups - Returns user groups and the associated permissions',
697 ' extensions - Returns extensions installed on the wiki',
698 ' fileextensions - Returns list of file extensions allowed to be uploaded',
699 ' rightsinfo - Returns wiki rights (license) information if available',
700 " languages - Returns a list of languages MediaWiki supports" .
701 "(optionally localised by using {$p}inlanguagecode)",
702 ' skins - Returns a list of all enabled skins',
703 ' extensiontags - Returns a list of parser extension tags',
704 ' functionhooks - Returns a list of parser function hooks',
705 ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)',
706 ' variables - Returns a list of variable IDs',
707 ' protocols - Returns a list of protocols that are allowed in external links.',
709 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
710 'showalldb' => 'List all database servers, not just the one lagging the most',
711 'numberingroup' => 'Lists the number of users in user groups',
712 'inlanguagecode' => 'Language code for localised language names (best effort, use CLDR extension)',
716 public function getDescription() {
717 return 'Return general information about the site';
720 public function getPossibleErrors() {
721 return array_merge( parent
::getPossibleErrors(), array( array(
722 'code' => 'includeAllDenied',
723 'info' => 'Cannot view all servers info unless $wgShowHostnames is true'
727 public function getExamples() {
729 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
730 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
731 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
735 public function getHelpUrls() {
736 return 'https://www.mediawiki.org/wiki/API:Meta#siteinfo_.2F_si';