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 ) {
117 global $wgContLang, $wgDisableLangConversion, $wgDisableTitleConversion;
120 $mainPage = Title
::newMainPage();
121 $data['mainpage'] = $mainPage->getPrefixedText();
122 $data['base'] = wfExpandUrl( $mainPage->getFullURL(), PROTO_CURRENT
);
123 $data['sitename'] = $GLOBALS['wgSitename'];
124 $data['logo'] = $GLOBALS['wgLogo'];
125 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
126 $data['phpversion'] = phpversion();
127 $data['phpsapi'] = PHP_SAPI
;
128 $data['dbtype'] = $GLOBALS['wgDBtype'];
129 $data['dbversion'] = $this->getDB()->getServerVersion();
131 $allowFrom = array( '' );
132 $allowException = true;
133 if ( !$GLOBALS['wgAllowExternalImages'] ) {
134 if ( $GLOBALS['wgEnableImageWhitelist'] ) {
135 $data['imagewhitelistenabled'] = '';
137 $allowFrom = $GLOBALS['wgAllowExternalImagesFrom'];
138 $allowException = !empty( $allowFrom );
140 if ( $allowException ) {
141 $data['externalimages'] = (array)$allowFrom;
142 $this->getResult()->setIndexedTagName( $data['externalimages'], 'prefix' );
145 if ( !$wgDisableLangConversion ) {
146 $data['langconversion'] = '';
149 if ( !$wgDisableTitleConversion ) {
150 $data['titleconversion'] = '';
153 if ( $wgContLang->linkPrefixExtension() ) {
154 $linkPrefixCharset = $wgContLang->linkPrefixCharset();
155 $data['linkprefixcharset'] = $linkPrefixCharset;
156 // For backwards compatability
157 $data['linkprefix'] = "/^((?>.*[^$linkPrefixCharset]|))(.+)$/sDu";
159 $data['linkprefixcharset'] = '';
160 $data['linkprefix'] = '';
163 $linktrail = $wgContLang->linkTrail();
165 $data['linktrail'] = $linktrail;
167 $data['linktrail'] = '';
170 $git = SpecialVersion
::getGitHeadSha1( $GLOBALS['IP'] );
172 $data['git-hash'] = $git;
174 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
180 // 'case-insensitive' option is reserved for future
181 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
183 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
184 $data['rightscode'] = $GLOBALS['wgRightsCode'];
186 $data['rights'] = $GLOBALS['wgRightsText'];
187 $data['lang'] = $GLOBALS['wgLanguageCode'];
189 $fallbacks = array();
190 foreach ( $wgContLang->getFallbackLanguages() as $code ) {
191 $fallbacks[] = array( 'code' => $code );
193 $data['fallback'] = $fallbacks;
194 $this->getResult()->setIndexedTagName( $data['fallback'], 'lang' );
196 if ( $wgContLang->hasVariants() ) {
198 foreach ( $wgContLang->getVariants() as $code ) {
199 $variants[] = array( 'code' => $code );
201 $data['variants'] = $variants;
202 $this->getResult()->setIndexedTagName( $data['variants'], 'lang' );
205 if ( $wgContLang->isRTL() ) {
208 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
210 if ( wfReadOnly() ) {
211 $data['readonly'] = '';
212 $data['readonlyreason'] = wfReadOnlyReason();
214 if ( $GLOBALS['wgEnableWriteAPI'] ) {
215 $data['writeapi'] = '';
218 $tz = $GLOBALS['wgLocaltimezone'];
219 $offset = $GLOBALS['wgLocalTZoffset'];
220 if ( is_null( $tz ) ) {
223 } elseif ( is_null( $offset ) ) {
226 $data['timezone'] = $tz;
227 $data['timeoffset'] = intval( $offset );
228 $data['articlepath'] = $GLOBALS['wgArticlePath'];
229 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
230 $data['script'] = $GLOBALS['wgScript'];
231 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
232 $data['server'] = $GLOBALS['wgServer'];
233 $data['wikiid'] = wfWikiID();
234 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
236 if ( $GLOBALS['wgMiserMode'] ) {
237 $data['misermode'] = '';
240 $data['maxuploadsize'] = UploadBase
::getMaxUploadSize();
242 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
244 return $this->getResult()->addValue( 'query', $property, $data );
247 protected function appendNamespaces( $property ) {
250 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
252 'id' => intval( $ns ),
253 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
255 ApiResult
::setContent( $data[$ns], $title );
256 $canonical = MWNamespace
::getCanonicalName( $ns );
258 if ( MWNamespace
::hasSubpages( $ns ) ) {
259 $data[$ns]['subpages'] = '';
263 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
266 if ( MWNamespace
::isContent( $ns ) ) {
267 $data[$ns]['content'] = '';
270 if ( MWNamespace
::isNonincludable( $ns ) ) {
271 $data[$ns]['nonincludable'] = '';
274 $contentmodel = MWNamespace
::getNamespaceContentModel( $ns );
275 if ( $contentmodel ) {
276 $data[$ns]['defaultcontentmodel'] = $contentmodel;
280 $this->getResult()->setIndexedTagName( $data, 'ns' );
282 return $this->getResult()->addValue( 'query', $property, $data );
285 protected function appendNamespaceAliases( $property ) {
286 global $wgNamespaceAliases, $wgContLang;
287 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
288 $namespaces = $wgContLang->getNamespaces();
290 foreach ( $aliases as $title => $ns ) {
291 if ( $namespaces[$ns] == $title ) {
292 // Don't list duplicates
296 'id' => intval( $ns )
298 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
304 $this->getResult()->setIndexedTagName( $data, 'ns' );
306 return $this->getResult()->addValue( 'query', $property, $data );
309 protected function appendSpecialPageAliases( $property ) {
312 $aliases = $wgContLang->getSpecialPageAliases();
313 foreach ( SpecialPageFactory
::getList() as $specialpage => $stuff ) {
314 if ( isset( $aliases[$specialpage] ) ) {
315 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases[$specialpage] );
316 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
320 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
322 return $this->getResult()->addValue( 'query', $property, $data );
325 protected function appendMagicWords( $property ) {
328 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
329 $caseSensitive = array_shift( $aliases );
330 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
331 if ( $caseSensitive ) {
332 $arr['case-sensitive'] = '';
334 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
337 $this->getResult()->setIndexedTagName( $data, 'magicword' );
339 return $this->getResult()->addValue( 'query', $property, $data );
342 protected function appendInterwikiMap( $property, $filter ) {
344 if ( $filter === 'local' ) {
346 } elseif ( $filter === '!local' ) {
348 } elseif ( $filter ) {
349 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
352 $params = $this->extractRequestParams();
353 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
354 $langNames = Language
::fetchLanguageNames( $langCode );
356 $getPrefixes = Interwiki
::getAllPrefixes( $local );
359 foreach ( $getPrefixes as $row ) {
360 $prefix = $row['iw_prefix'];
362 $val['prefix'] = $prefix;
363 if ( $row['iw_local'] == '1' ) {
366 if ( $row['iw_trans'] == '1' ) {
369 if ( isset( $langNames[$prefix] ) ) {
370 $val['language'] = $langNames[$prefix];
372 $val['url'] = wfExpandUrl( $row['iw_url'], PROTO_CURRENT
);
373 if ( isset( $row['iw_wikiid'] ) ) {
374 $val['wikiid'] = $row['iw_wikiid'];
376 if ( isset( $row['iw_api'] ) ) {
377 $val['api'] = $row['iw_api'];
383 $this->getResult()->setIndexedTagName( $data, 'iw' );
385 return $this->getResult()->addValue( 'query', $property, $data );
388 protected function appendDbReplLagInfo( $property, $includeAll ) {
389 global $wgShowHostnames;
393 if ( !$wgShowHostnames ) {
395 'Cannot view all servers info unless $wgShowHostnames is true',
400 $lags = $lb->getLagTimes();
401 foreach ( $lags as $i => $lag ) {
403 'host' => $lb->getServerName( $i ),
408 list( , $lag, $index ) = $lb->getMaxLag();
410 'host' => $wgShowHostnames
411 ?
$lb->getServerName( $index )
413 'lag' => intval( $lag )
417 $result = $this->getResult();
418 $result->setIndexedTagName( $data, 'db' );
420 return $this->getResult()->addValue( 'query', $property, $data );
423 protected function appendStatistics( $property ) {
424 global $wgDisableCounters;
426 $data['pages'] = intval( SiteStats
::pages() );
427 $data['articles'] = intval( SiteStats
::articles() );
428 if ( !$wgDisableCounters ) {
429 $data['views'] = intval( SiteStats
::views() );
431 $data['edits'] = intval( SiteStats
::edits() );
432 $data['images'] = intval( SiteStats
::images() );
433 $data['users'] = intval( SiteStats
::users() );
434 $data['activeusers'] = intval( SiteStats
::activeUsers() );
435 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
436 $data['jobs'] = intval( SiteStats
::jobs() );
438 wfRunHooks( 'APIQuerySiteInfoStatisticsInfo', array( &$data ) );
440 return $this->getResult()->addValue( 'query', $property, $data );
443 protected function appendUserGroups( $property, $numberInGroup ) {
444 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups;
445 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
448 $result = $this->getResult();
449 foreach ( $wgGroupPermissions as $group => $permissions ) {
452 'rights' => array_keys( $permissions, true ),
455 if ( $numberInGroup ) {
456 global $wgAutopromote;
458 if ( $group == 'user' ) {
459 $arr['number'] = SiteStats
::users();
460 // '*' and autopromote groups have no size
461 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
462 $arr['number'] = SiteStats
::numberInGroup( $group );
467 'add' => $wgAddGroups,
468 'remove' => $wgRemoveGroups,
469 'add-self' => $wgGroupsAddToSelf,
470 'remove-self' => $wgGroupsRemoveFromSelf
473 foreach ( $groupArr as $type => $rights ) {
474 if ( isset( $rights[$group] ) ) {
475 $arr[$type] = $rights[$group];
476 $result->setIndexedTagName( $arr[$type], 'group' );
480 $result->setIndexedTagName( $arr['rights'], 'permission' );
484 $result->setIndexedTagName( $data, 'group' );
486 return $result->addValue( 'query', $property, $data );
489 protected function appendFileExtensions( $property ) {
490 global $wgFileExtensions;
493 foreach ( array_unique( $wgFileExtensions ) as $ext ) {
494 $data[] = array( 'ext' => $ext );
496 $this->getResult()->setIndexedTagName( $data, 'fe' );
498 return $this->getResult()->addValue( 'query', $property, $data );
501 protected function appendExtensions( $property ) {
502 global $wgExtensionCredits;
504 foreach ( $wgExtensionCredits as $type => $extensions ) {
505 foreach ( $extensions as $ext ) {
507 $ret['type'] = $type;
508 if ( isset( $ext['name'] ) ) {
509 $ret['name'] = $ext['name'];
511 if ( isset( $ext['description'] ) ) {
512 $ret['description'] = $ext['description'];
514 if ( isset( $ext['descriptionmsg'] ) ) {
515 // Can be a string or array( key, param1, param2, ... )
516 if ( is_array( $ext['descriptionmsg'] ) ) {
517 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
518 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
519 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
521 $ret['descriptionmsg'] = $ext['descriptionmsg'];
524 if ( isset( $ext['author'] ) ) {
525 $ret['author'] = is_array( $ext['author'] ) ?
526 implode( ', ', $ext['author'] ) : $ext['author'];
528 if ( isset( $ext['url'] ) ) {
529 $ret['url'] = $ext['url'];
531 if ( isset( $ext['version'] ) ) {
532 $ret['version'] = $ext['version'];
533 } elseif ( isset( $ext['svn-revision'] ) &&
534 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
535 $ext['svn-revision'], $m )
537 $ret['version'] = 'r' . $m[1];
543 $this->getResult()->setIndexedTagName( $data, 'ext' );
545 return $this->getResult()->addValue( 'query', $property, $data );
548 protected function appendRightsInfo( $property ) {
549 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
550 $title = Title
::newFromText( $wgRightsPage );
551 $url = $title ?
wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
) : $wgRightsUrl;
552 $text = $wgRightsText;
553 if ( !$text && $title ) {
554 $text = $title->getPrefixedText();
558 'url' => $url ?
$url : '',
559 'text' => $text ?
$text : ''
562 return $this->getResult()->addValue( 'query', $property, $data );
565 public function appendLanguages( $property ) {
566 $params = $this->extractRequestParams();
567 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
568 $langNames = Language
::fetchLanguageNames( $langCode );
572 foreach ( $langNames as $code => $name ) {
573 $lang = array( 'code' => $code );
574 ApiResult
::setContent( $lang, $name );
577 $this->getResult()->setIndexedTagName( $data, 'lang' );
579 return $this->getResult()->addValue( 'query', $property, $data );
582 public function appendSkins( $property ) {
584 $usable = Skin
::getUsableSkins();
585 $default = Skin
::normalizeKey( 'default' );
586 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
587 $skin = array( 'code' => $name );
588 ApiResult
::setContent( $skin, $displayName );
589 if ( !isset( $usable[$name] ) ) {
590 $skin['unusable'] = '';
592 if ( $name === $default ) {
593 $skin['default'] = '';
597 $this->getResult()->setIndexedTagName( $data, 'skin' );
599 return $this->getResult()->addValue( 'query', $property, $data );
602 public function appendExtensionTags( $property ) {
604 $wgParser->firstCallInit();
605 $tags = array_map( array( $this, 'formatParserTags' ), $wgParser->getTags() );
606 $this->getResult()->setIndexedTagName( $tags, 't' );
608 return $this->getResult()->addValue( 'query', $property, $tags );
611 public function appendFunctionHooks( $property ) {
613 $wgParser->firstCallInit();
614 $hooks = $wgParser->getFunctionHooks();
615 $this->getResult()->setIndexedTagName( $hooks, 'h' );
617 return $this->getResult()->addValue( 'query', $property, $hooks );
620 public function appendVariables( $property ) {
621 $variables = MagicWord
::getVariableIDs();
622 $this->getResult()->setIndexedTagName( $variables, 'v' );
624 return $this->getResult()->addValue( 'query', $property, $variables );
627 public function appendProtocols( $property ) {
628 global $wgUrlProtocols;
629 // Make a copy of the global so we don't try to set the _element key of it - bug 45130
630 $protocols = array_values( $wgUrlProtocols );
631 $this->getResult()->setIndexedTagName( $protocols, 'p' );
633 return $this->getResult()->addValue( 'query', $property, $protocols );
636 private function formatParserTags( $item ) {
640 public function appendSubscribedHooks( $property ) {
642 $myWgHooks = $wgHooks;
646 foreach ( $myWgHooks as $hook => $hooks ) {
649 'subscribers' => array_map( array( 'SpecialVersion', 'arrayToString' ), $hooks ),
652 $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
656 $this->getResult()->setIndexedTagName( $data, 'hook' );
658 return $this->getResult()->addValue( 'query', $property, $data );
661 public function getCacheMode( $params ) {
665 public function getAllowedParams() {
668 ApiBase
::PARAM_DFLT
=> 'general',
669 ApiBase
::PARAM_ISMULTI
=> true,
670 ApiBase
::PARAM_TYPE
=> array(
674 'specialpagealiases',
693 ApiBase
::PARAM_TYPE
=> array(
698 'showalldb' => false,
699 'numberingroup' => false,
700 'inlanguagecode' => null,
704 public function getParamDescription() {
705 $p = $this->getModulePrefix();
709 'Which sysinfo properties to get:',
710 ' general - Overall system information',
711 ' namespaces - List of registered namespaces and their canonical names',
712 ' namespacealiases - List of registered namespace aliases',
713 ' specialpagealiases - List of special page aliases',
714 ' magicwords - List of magic words and their aliases',
715 ' statistics - Returns site statistics',
716 ' interwikimap - Returns interwiki map ' .
717 "(optionally filtered, (optionally localised by using {$p}inlanguagecode))",
718 ' dbrepllag - Returns database server with the highest replication lag',
719 ' usergroups - Returns user groups and the associated permissions',
720 ' extensions - Returns extensions installed on the wiki',
721 ' fileextensions - Returns list of file extensions allowed to be uploaded',
722 ' rightsinfo - Returns wiki rights (license) information if available',
723 ' languages - Returns a list of languages MediaWiki supports' .
724 "(optionally localised by using {$p}inlanguagecode)",
725 ' skins - Returns a list of all enabled skins',
726 ' extensiontags - Returns a list of parser extension tags',
727 ' functionhooks - Returns a list of parser function hooks',
728 ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)',
729 ' variables - Returns a list of variable IDs',
730 ' protocols - Returns a list of protocols that are allowed in external links.',
732 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
733 'showalldb' => 'List all database servers, not just the one lagging the most',
734 'numberingroup' => 'Lists the number of users in user groups',
735 'inlanguagecode' => 'Language code for localised language names ' .
736 '(best effort, use CLDR extension)',
740 public function getDescription() {
741 return 'Return general information about the site';
744 public function getPossibleErrors() {
745 return array_merge( parent
::getPossibleErrors(), array( array(
746 'code' => 'includeAllDenied',
747 'info' => 'Cannot view all servers info unless $wgShowHostnames is true'
751 public function getExamples() {
753 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
754 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
755 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
759 public function getHelpUrls() {
760 return 'https://www.mediawiki.org/wiki/API:Meta#siteinfo_.2F_si';