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() );
424 return $this->getResult()->addValue( 'query', $property, $data );
427 protected function appendUserGroups( $property, $numberInGroup ) {
428 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups;
429 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
432 $result = $this->getResult();
433 foreach ( $wgGroupPermissions as $group => $permissions ) {
436 'rights' => array_keys( $permissions, true ),
439 if ( $numberInGroup ) {
440 global $wgAutopromote;
442 if ( $group == 'user' ) {
443 $arr['number'] = SiteStats
::users();
445 // '*' and autopromote groups have no size
446 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
447 $arr['number'] = SiteStats
::numberInGroup( $group );
452 'add' => $wgAddGroups,
453 'remove' => $wgRemoveGroups,
454 'add-self' => $wgGroupsAddToSelf,
455 'remove-self' => $wgGroupsRemoveFromSelf
458 foreach ( $groupArr as $type => $rights ) {
459 if ( isset( $rights[$group] ) ) {
460 $arr[$type] = $rights[$group];
461 $result->setIndexedTagName( $arr[$type], 'group' );
465 $result->setIndexedTagName( $arr['rights'], 'permission' );
469 $result->setIndexedTagName( $data, 'group' );
470 return $result->addValue( 'query', $property, $data );
473 protected function appendFileExtensions( $property ) {
474 global $wgFileExtensions;
477 foreach ( array_unique( $wgFileExtensions ) as $ext ) {
478 $data[] = array( 'ext' => $ext );
480 $this->getResult()->setIndexedTagName( $data, 'fe' );
481 return $this->getResult()->addValue( 'query', $property, $data );
484 protected function appendExtensions( $property ) {
485 global $wgExtensionCredits;
487 foreach ( $wgExtensionCredits as $type => $extensions ) {
488 foreach ( $extensions as $ext ) {
490 $ret['type'] = $type;
491 if ( isset( $ext['name'] ) ) {
492 $ret['name'] = $ext['name'];
494 if ( isset( $ext['description'] ) ) {
495 $ret['description'] = $ext['description'];
497 if ( isset( $ext['descriptionmsg'] ) ) {
498 // Can be a string or array( key, param1, param2, ... )
499 if ( is_array( $ext['descriptionmsg'] ) ) {
500 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
501 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
502 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
504 $ret['descriptionmsg'] = $ext['descriptionmsg'];
507 if ( isset( $ext['author'] ) ) {
508 $ret['author'] = is_array( $ext['author'] ) ?
509 implode( ', ', $ext['author'] ) : $ext['author'];
511 if ( isset( $ext['url'] ) ) {
512 $ret['url'] = $ext['url'];
514 if ( isset( $ext['version'] ) ) {
515 $ret['version'] = $ext['version'];
516 } elseif ( isset( $ext['svn-revision'] ) &&
517 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
518 $ext['svn-revision'], $m ) )
520 $ret['version'] = 'r' . $m[1];
526 $this->getResult()->setIndexedTagName( $data, 'ext' );
527 return $this->getResult()->addValue( 'query', $property, $data );
530 protected function appendRightsInfo( $property ) {
531 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
532 $title = Title
::newFromText( $wgRightsPage );
533 $url = $title ?
wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
) : $wgRightsUrl;
534 $text = $wgRightsText;
535 if ( !$text && $title ) {
536 $text = $title->getPrefixedText();
540 'url' => $url ?
$url : '',
541 'text' => $text ?
$text : ''
544 return $this->getResult()->addValue( 'query', $property, $data );
547 public function appendLanguages( $property ) {
548 $params = $this->extractRequestParams();
549 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
550 $langNames = Language
::fetchLanguageNames( $langCode );
554 foreach ( $langNames as $code => $name ) {
555 $lang = array( 'code' => $code );
556 ApiResult
::setContent( $lang, $name );
559 $this->getResult()->setIndexedTagName( $data, 'lang' );
560 return $this->getResult()->addValue( 'query', $property, $data );
563 public function appendSkins( $property ) {
565 $usable = Skin
::getUsableSkins();
566 $default = Skin
::normalizeKey( 'default' );
567 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
568 $skin = array( 'code' => $name );
569 ApiResult
::setContent( $skin, $displayName );
570 if ( !isset( $usable[$name] ) ) {
571 $skin['unusable'] = '';
573 if ( $name === $default ) {
574 $skin['default'] = '';
578 $this->getResult()->setIndexedTagName( $data, 'skin' );
579 return $this->getResult()->addValue( 'query', $property, $data );
582 public function appendExtensionTags( $property ) {
584 $wgParser->firstCallInit();
585 $tags = array_map( array( $this, 'formatParserTags' ), $wgParser->getTags() );
586 $this->getResult()->setIndexedTagName( $tags, 't' );
587 return $this->getResult()->addValue( 'query', $property, $tags );
590 public function appendFunctionHooks( $property ) {
592 $wgParser->firstCallInit();
593 $hooks = $wgParser->getFunctionHooks();
594 $this->getResult()->setIndexedTagName( $hooks, 'h' );
595 return $this->getResult()->addValue( 'query', $property, $hooks );
598 public function appendVariables( $property ) {
599 $variables = MagicWord
::getVariableIDs();
600 $this->getResult()->setIndexedTagName( $variables, 'v' );
601 return $this->getResult()->addValue( 'query', $property, $variables );
604 public function appendProtocols( $property ) {
605 global $wgUrlProtocols;
606 // Make a copy of the global so we don't try to set the _element key of it - bug 45130
607 $protocols = array_values( $wgUrlProtocols );
608 $this->getResult()->setIndexedTagName( $protocols, 'p' );
609 return $this->getResult()->addValue( 'query', $property, $protocols );
612 private function formatParserTags( $item ) {
616 public function appendSubscribedHooks( $property ) {
618 $myWgHooks = $wgHooks;
622 foreach ( $myWgHooks as $hook => $hooks ) {
625 'subscribers' => array_map( array( 'SpecialVersion', 'arrayToString' ), $hooks ),
628 $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
632 $this->getResult()->setIndexedTagName( $data, 'hook' );
633 return $this->getResult()->addValue( 'query', $property, $data );
636 public function getCacheMode( $params ) {
640 public function getAllowedParams() {
643 ApiBase
::PARAM_DFLT
=> 'general',
644 ApiBase
::PARAM_ISMULTI
=> true,
645 ApiBase
::PARAM_TYPE
=> array(
649 'specialpagealiases',
668 ApiBase
::PARAM_TYPE
=> array(
673 'showalldb' => false,
674 'numberingroup' => false,
675 'inlanguagecode' => null,
679 public function getParamDescription() {
680 $p = $this->getModulePrefix();
683 'Which sysinfo properties to get:',
684 ' general - Overall system information',
685 ' namespaces - List of registered namespaces and their canonical names',
686 ' namespacealiases - List of registered namespace aliases',
687 ' specialpagealiases - List of special page aliases',
688 ' magicwords - List of magic words and their aliases',
689 ' statistics - Returns site statistics',
690 " interwikimap - Returns interwiki map " .
691 "(optionally filtered, (optionally localised by using {$p}inlanguagecode))",
692 ' dbrepllag - Returns database server with the highest replication lag',
693 ' usergroups - Returns user groups and the associated permissions',
694 ' extensions - Returns extensions installed on the wiki',
695 ' fileextensions - Returns list of file extensions allowed to be uploaded',
696 ' rightsinfo - Returns wiki rights (license) information if available',
697 " languages - Returns a list of languages MediaWiki supports" .
698 "(optionally localised by using {$p}inlanguagecode)",
699 ' skins - Returns a list of all enabled skins',
700 ' extensiontags - Returns a list of parser extension tags',
701 ' functionhooks - Returns a list of parser function hooks',
702 ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)',
703 ' variables - Returns a list of variable IDs',
704 ' protocols - Returns a list of protocols that are allowed in external links.',
706 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
707 'showalldb' => 'List all database servers, not just the one lagging the most',
708 'numberingroup' => 'Lists the number of users in user groups',
709 'inlanguagecode' => 'Language code for localised language names (best effort, use CLDR extension)',
713 public function getDescription() {
714 return 'Return general information about the site';
717 public function getPossibleErrors() {
718 return array_merge( parent
::getPossibleErrors(), array( array(
719 'code' => 'includeAllDenied',
720 'info' => 'Cannot view all servers info unless $wgShowHostnames is true'
724 public function getExamples() {
726 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
727 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
728 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
732 public function getHelpUrls() {
733 return 'https://www.mediawiki.org/wiki/API:Meta#siteinfo_.2F_si';