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 $allowFrom = array( '' );
133 $allowException = true;
134 if ( !$GLOBALS['wgAllowExternalImages'] ) {
135 if ( $GLOBALS['wgEnableImageWhitelist'] ) {
136 $data['imagewhitelistenabled'] = '';
138 $allowFrom = $GLOBALS['wgAllowExternalImagesFrom'];
139 $allowException = !empty( $allowFrom );
141 if ( $allowException ) {
142 $data['externalimages'] = (array) $allowFrom;
143 $this->getResult()->setIndexedTagName( $data['externalimages'], 'prefix' );
146 if ( !$wgDisableLangConversion ) {
147 $data['langconversion'] = '';
150 if ( !$wgDisableTitleConversion ) {
151 $data['titleconversion'] = '';
154 if ( $wgContLang->linkPrefixExtension() ) {
155 $data['linkprefix'] = wfMessage( 'linkprefix' )->inContentLanguage()->text();
157 $data['linkprefix'] = '';
160 $linktrail = $wgContLang->linkTrail();
162 $data['linktrail'] = $linktrail;
164 $data['linktrail'] = '';
167 $git = SpecialVersion
::getGitHeadSha1( $GLOBALS['IP'] );
169 $data['git-hash'] = $git;
171 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
177 // 'case-insensitive' option is reserved for future
178 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
180 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
181 $data['rightscode'] = $GLOBALS['wgRightsCode'];
183 $data['rights'] = $GLOBALS['wgRightsText'];
184 $data['lang'] = $GLOBALS['wgLanguageCode'];
186 $fallbacks = array();
187 foreach ( $wgContLang->getFallbackLanguages() as $code ) {
188 $fallbacks[] = array( 'code' => $code );
190 $data['fallback'] = $fallbacks;
191 $this->getResult()->setIndexedTagName( $data['fallback'], 'lang' );
193 if ( $wgContLang->hasVariants() ) {
195 foreach ( $wgContLang->getVariants() as $code ) {
196 $variants[] = array( 'code' => $code );
198 $data['variants'] = $variants;
199 $this->getResult()->setIndexedTagName( $data['variants'], 'lang' );
202 if ( $wgContLang->isRTL() ) {
205 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
207 if ( wfReadOnly() ) {
208 $data['readonly'] = '';
209 $data['readonlyreason'] = wfReadOnlyReason();
211 if ( $GLOBALS['wgEnableWriteAPI'] ) {
212 $data['writeapi'] = '';
215 $tz = $GLOBALS['wgLocaltimezone'];
216 $offset = $GLOBALS['wgLocalTZoffset'];
217 if ( is_null( $tz ) ) {
220 } elseif ( is_null( $offset ) ) {
223 $data['timezone'] = $tz;
224 $data['timeoffset'] = intval( $offset );
225 $data['articlepath'] = $GLOBALS['wgArticlePath'];
226 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
227 $data['script'] = $GLOBALS['wgScript'];
228 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
229 $data['server'] = $GLOBALS['wgServer'];
230 $data['wikiid'] = wfWikiID();
231 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
233 if ( $GLOBALS['wgMiserMode'] ) {
234 $data['misermode'] = '';
237 $data['maxuploadsize'] = UploadBase
::getMaxUploadSize();
239 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
241 return $this->getResult()->addValue( 'query', $property, $data );
244 protected function appendNamespaces( $property ) {
247 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
249 'id' => intval( $ns ),
250 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
252 ApiResult
::setContent( $data[$ns], $title );
253 $canonical = MWNamespace
::getCanonicalName( $ns );
255 if ( MWNamespace
::hasSubpages( $ns ) ) {
256 $data[$ns]['subpages'] = '';
260 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
263 if ( MWNamespace
::isContent( $ns ) ) {
264 $data[$ns]['content'] = '';
267 if ( MWNamespace
::isNonincludable( $ns ) ) {
268 $data[$ns]['nonincludable'] = '';
271 $contentmodel = MWNamespace
::getNamespaceContentModel( $ns );
272 if ( $contentmodel ) {
273 $data[$ns]['defaultcontentmodel'] = $contentmodel;
277 $this->getResult()->setIndexedTagName( $data, 'ns' );
278 return $this->getResult()->addValue( 'query', $property, $data );
281 protected function appendNamespaceAliases( $property ) {
282 global $wgNamespaceAliases, $wgContLang;
283 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
284 $namespaces = $wgContLang->getNamespaces();
286 foreach ( $aliases as $title => $ns ) {
287 if ( $namespaces[$ns] == $title ) {
288 // Don't list duplicates
292 'id' => intval( $ns )
294 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
298 $this->getResult()->setIndexedTagName( $data, 'ns' );
299 return $this->getResult()->addValue( 'query', $property, $data );
302 protected function appendSpecialPageAliases( $property ) {
305 $aliases = $wgContLang->getSpecialPageAliases();
306 foreach ( SpecialPageFactory
::getList() as $specialpage => $stuff ) {
307 if ( isset( $aliases[$specialpage] ) ) {
308 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases[$specialpage] );
309 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
313 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
314 return $this->getResult()->addValue( 'query', $property, $data );
317 protected function appendMagicWords( $property ) {
320 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
321 $caseSensitive = array_shift( $aliases );
322 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
323 if ( $caseSensitive ) {
324 $arr['case-sensitive'] = '';
326 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
329 $this->getResult()->setIndexedTagName( $data, 'magicword' );
330 return $this->getResult()->addValue( 'query', $property, $data );
333 protected function appendInterwikiMap( $property, $filter ) {
335 if ( $filter === 'local' ) {
337 } elseif ( $filter === '!local' ) {
339 } elseif ( $filter ) {
340 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
343 $params = $this->extractRequestParams();
344 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
345 $langNames = Language
::fetchLanguageNames( $langCode );
347 $getPrefixes = Interwiki
::getAllPrefixes( $local );
350 foreach ( $getPrefixes as $row ) {
351 $prefix = $row['iw_prefix'];
353 $val['prefix'] = $prefix;
354 if ( $row['iw_local'] == '1' ) {
357 // $val['trans'] = intval( $row['iw_trans'] ); // should this be exposed?
358 if ( isset( $langNames[$prefix] ) ) {
359 $val['language'] = $langNames[$prefix];
361 $val['url'] = wfExpandUrl( $row['iw_url'], PROTO_CURRENT
);
362 if ( isset( $row['iw_wikiid'] ) ) {
363 $val['wikiid'] = $row['iw_wikiid'];
365 if ( isset( $row['iw_api'] ) ) {
366 $val['api'] = $row['iw_api'];
372 $this->getResult()->setIndexedTagName( $data, 'iw' );
373 return $this->getResult()->addValue( 'query', $property, $data );
376 protected function appendDbReplLagInfo( $property, $includeAll ) {
377 global $wgShowHostnames;
381 if ( !$wgShowHostnames ) {
382 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
385 $lags = $lb->getLagTimes();
386 foreach ( $lags as $i => $lag ) {
388 'host' => $lb->getServerName( $i ),
393 list( , $lag, $index ) = $lb->getMaxLag();
395 'host' => $wgShowHostnames
396 ?
$lb->getServerName( $index )
398 'lag' => intval( $lag )
402 $result = $this->getResult();
403 $result->setIndexedTagName( $data, 'db' );
404 return $this->getResult()->addValue( 'query', $property, $data );
407 protected function appendStatistics( $property ) {
408 global $wgDisableCounters;
410 $data['pages'] = intval( SiteStats
::pages() );
411 $data['articles'] = intval( SiteStats
::articles() );
412 if ( !$wgDisableCounters ) {
413 $data['views'] = intval( SiteStats
::views() );
415 $data['edits'] = intval( SiteStats
::edits() );
416 $data['images'] = intval( SiteStats
::images() );
417 $data['users'] = intval( SiteStats
::users() );
418 $data['activeusers'] = intval( SiteStats
::activeUsers() );
419 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
420 $data['jobs'] = intval( SiteStats
::jobs() );
421 return $this->getResult()->addValue( 'query', $property, $data );
424 protected function appendUserGroups( $property, $numberInGroup ) {
425 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups;
426 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
429 $result = $this->getResult();
430 foreach ( $wgGroupPermissions as $group => $permissions ) {
433 'rights' => array_keys( $permissions, true ),
436 if ( $numberInGroup ) {
437 global $wgAutopromote;
439 if ( $group == 'user' ) {
440 $arr['number'] = SiteStats
::users();
442 // '*' and autopromote groups have no size
443 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
444 $arr['number'] = SiteStats
::numberInGroup( $group );
449 'add' => $wgAddGroups,
450 'remove' => $wgRemoveGroups,
451 'add-self' => $wgGroupsAddToSelf,
452 'remove-self' => $wgGroupsRemoveFromSelf
455 foreach ( $groupArr as $type => $rights ) {
456 if ( isset( $rights[$group] ) ) {
457 $arr[$type] = $rights[$group];
458 $result->setIndexedTagName( $arr[$type], 'group' );
462 $result->setIndexedTagName( $arr['rights'], 'permission' );
466 $result->setIndexedTagName( $data, 'group' );
467 return $result->addValue( 'query', $property, $data );
470 protected function appendFileExtensions( $property ) {
471 global $wgFileExtensions;
474 foreach ( $wgFileExtensions as $ext ) {
475 $data[] = array( 'ext' => $ext );
477 $this->getResult()->setIndexedTagName( $data, 'fe' );
478 return $this->getResult()->addValue( 'query', $property, $data );
481 protected function appendExtensions( $property ) {
482 global $wgExtensionCredits;
484 foreach ( $wgExtensionCredits as $type => $extensions ) {
485 foreach ( $extensions as $ext ) {
487 $ret['type'] = $type;
488 if ( isset( $ext['name'] ) ) {
489 $ret['name'] = $ext['name'];
491 if ( isset( $ext['description'] ) ) {
492 $ret['description'] = $ext['description'];
494 if ( isset( $ext['descriptionmsg'] ) ) {
495 // Can be a string or array( key, param1, param2, ... )
496 if ( is_array( $ext['descriptionmsg'] ) ) {
497 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
498 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
499 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
501 $ret['descriptionmsg'] = $ext['descriptionmsg'];
504 if ( isset( $ext['author'] ) ) {
505 $ret['author'] = is_array( $ext['author'] ) ?
506 implode( ', ', $ext['author'] ) : $ext['author'];
508 if ( isset( $ext['url'] ) ) {
509 $ret['url'] = $ext['url'];
511 if ( isset( $ext['version'] ) ) {
512 $ret['version'] = $ext['version'];
513 } elseif ( isset( $ext['svn-revision'] ) &&
514 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
515 $ext['svn-revision'], $m ) )
517 $ret['version'] = 'r' . $m[1];
523 $this->getResult()->setIndexedTagName( $data, 'ext' );
524 return $this->getResult()->addValue( 'query', $property, $data );
527 protected function appendRightsInfo( $property ) {
528 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
529 $title = Title
::newFromText( $wgRightsPage );
530 $url = $title ?
wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
) : $wgRightsUrl;
531 $text = $wgRightsText;
532 if ( !$text && $title ) {
533 $text = $title->getPrefixedText();
537 'url' => $url ?
$url : '',
538 'text' => $text ?
$text : ''
541 return $this->getResult()->addValue( 'query', $property, $data );
544 public function appendLanguages( $property ) {
545 $params = $this->extractRequestParams();
546 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
547 $langNames = Language
::fetchLanguageNames( $langCode );
551 foreach ( $langNames as $code => $name ) {
552 $lang = array( 'code' => $code );
553 ApiResult
::setContent( $lang, $name );
556 $this->getResult()->setIndexedTagName( $data, 'lang' );
557 return $this->getResult()->addValue( 'query', $property, $data );
560 public function appendSkins( $property ) {
562 $usable = Skin
::getUsableSkins();
563 $default = Skin
::normalizeKey( 'default' );
564 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
565 $skin = array( 'code' => $name );
566 ApiResult
::setContent( $skin, $displayName );
567 if ( !isset( $usable[$name] ) ) {
568 $skin['unusable'] = '';
570 if ( $name === $default ) {
571 $skin['default'] = '';
575 $this->getResult()->setIndexedTagName( $data, 'skin' );
576 return $this->getResult()->addValue( 'query', $property, $data );
579 public function appendExtensionTags( $property ) {
581 $wgParser->firstCallInit();
582 $tags = array_map( array( $this, 'formatParserTags' ), $wgParser->getTags() );
583 $this->getResult()->setIndexedTagName( $tags, 't' );
584 return $this->getResult()->addValue( 'query', $property, $tags );
587 public function appendFunctionHooks( $property ) {
589 $wgParser->firstCallInit();
590 $hooks = $wgParser->getFunctionHooks();
591 $this->getResult()->setIndexedTagName( $hooks, 'h' );
592 return $this->getResult()->addValue( 'query', $property, $hooks );
595 public function appendVariables( $property ) {
596 $variables = MagicWord
::getVariableIDs();
597 $this->getResult()->setIndexedTagName( $variables, 'v' );
598 return $this->getResult()->addValue( 'query', $property, $variables );
601 public function appendProtocols( $property ) {
602 global $wgUrlProtocols;
603 // Make a copy of the global so we don't try to set the _element key of it - bug 45130
604 $protocols = array_values( $wgUrlProtocols );
605 $this->getResult()->setIndexedTagName( $protocols, 'p' );
606 return $this->getResult()->addValue( 'query', $property, $protocols );
609 private function formatParserTags( $item ) {
613 public function appendSubscribedHooks( $property ) {
615 $myWgHooks = $wgHooks;
619 foreach ( $myWgHooks as $hook => $hooks ) {
622 'subscribers' => array_map( array( 'SpecialVersion', 'arrayToString' ), $hooks ),
625 $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
629 $this->getResult()->setIndexedTagName( $data, 'hook' );
630 return $this->getResult()->addValue( 'query', $property, $data );
633 public function getCacheMode( $params ) {
637 public function getAllowedParams() {
640 ApiBase
::PARAM_DFLT
=> 'general',
641 ApiBase
::PARAM_ISMULTI
=> true,
642 ApiBase
::PARAM_TYPE
=> array(
646 'specialpagealiases',
665 ApiBase
::PARAM_TYPE
=> array(
670 'showalldb' => false,
671 'numberingroup' => false,
672 'inlanguagecode' => null,
676 public function getParamDescription() {
677 $p = $this->getModulePrefix();
680 'Which sysinfo properties to get:',
681 ' general - Overall system information',
682 ' namespaces - List of registered namespaces and their canonical names',
683 ' namespacealiases - List of registered namespace aliases',
684 ' specialpagealiases - List of special page aliases',
685 ' magicwords - List of magic words and their aliases',
686 ' statistics - Returns site statistics',
687 " interwikimap - Returns interwiki map " .
688 "(optionally filtered, (optionally localised by using {$p}inlanguagecode))",
689 ' dbrepllag - Returns database server with the highest replication lag',
690 ' usergroups - Returns user groups and the associated permissions',
691 ' extensions - Returns extensions installed on the wiki',
692 ' fileextensions - Returns list of file extensions allowed to be uploaded',
693 ' rightsinfo - Returns wiki rights (license) information if available',
694 " languages - Returns a list of languages MediaWiki supports".
695 "(optionally localised by using {$p}inlanguagecode)",
696 ' skins - Returns a list of all enabled skins',
697 ' extensiontags - Returns a list of parser extension tags',
698 ' functionhooks - Returns a list of parser function hooks',
699 ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)',
700 ' variables - Returns a list of variable IDs',
701 ' protocols - Returns a list of protocols that are allowed in external links.',
703 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
704 'showalldb' => 'List all database servers, not just the one lagging the most',
705 'numberingroup' => 'Lists the number of users in user groups',
706 'inlanguagecode' => 'Language code for localised language names (best effort, use CLDR extension)',
710 public function getDescription() {
711 return 'Return general information about the site';
714 public function getPossibleErrors() {
715 return array_merge( parent
::getPossibleErrors(), array( array(
716 'code' => 'includeAllDenied',
717 'info' => 'Cannot view all servers info unless $wgShowHostnames is true'
721 public function getExamples() {
723 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
724 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
725 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
729 public function getHelpUrls() {
730 return 'https://www.mediawiki.org/wiki/API:Meta#siteinfo_.2F_si';