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 $git = SpecialVersion
::getGitHeadSha1( $GLOBALS['IP'] );
142 $data['git-hash'] = $git;
144 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
150 // 'case-insensitive' option is reserved for future
151 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
153 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
154 $data['rightscode'] = $GLOBALS['wgRightsCode'];
156 $data['rights'] = $GLOBALS['wgRightsText'];
157 $data['lang'] = $GLOBALS['wgLanguageCode'];
159 $fallbacks = array();
160 foreach( $wgContLang->getFallbackLanguages() as $code ) {
161 $fallbacks[] = array( 'code' => $code );
163 $data['fallback'] = $fallbacks;
164 $this->getResult()->setIndexedTagName( $data['fallback'], 'lang' );
166 if( $wgContLang->hasVariants() ) {
168 foreach( $wgContLang->getVariants() as $code ) {
169 $variants[] = array( 'code' => $code );
171 $data['variants'] = $variants;
172 $this->getResult()->setIndexedTagName( $data['variants'], 'lang' );
175 if ( $wgContLang->isRTL() ) {
178 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
180 if ( wfReadOnly() ) {
181 $data['readonly'] = '';
182 $data['readonlyreason'] = wfReadOnlyReason();
184 if ( $GLOBALS['wgEnableWriteAPI'] ) {
185 $data['writeapi'] = '';
188 $tz = $GLOBALS['wgLocaltimezone'];
189 $offset = $GLOBALS['wgLocalTZoffset'];
190 if ( is_null( $tz ) ) {
193 } elseif ( is_null( $offset ) ) {
196 $data['timezone'] = $tz;
197 $data['timeoffset'] = intval( $offset );
198 $data['articlepath'] = $GLOBALS['wgArticlePath'];
199 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
200 $data['script'] = $GLOBALS['wgScript'];
201 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
202 $data['server'] = $GLOBALS['wgServer'];
203 $data['wikiid'] = wfWikiID();
204 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
206 if ( $GLOBALS['wgMiserMode'] ) {
207 $data['misermode'] = '';
210 $data['maxuploadsize'] = UploadBase
::getMaxUploadSize();
212 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
214 return $this->getResult()->addValue( 'query', $property, $data );
217 protected function appendNamespaces( $property ) {
220 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
222 'id' => intval( $ns ),
223 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
225 ApiResult
::setContent( $data[$ns], $title );
226 $canonical = MWNamespace
::getCanonicalName( $ns );
228 if ( MWNamespace
::hasSubpages( $ns ) ) {
229 $data[$ns]['subpages'] = '';
233 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
236 if ( MWNamespace
::isContent( $ns ) ) {
237 $data[$ns]['content'] = '';
240 if ( MWNamespace
::isNonincludable( $ns ) ) {
241 $data[$ns]['nonincludable'] = '';
244 $contentmodel = MWNamespace
::getNamespaceContentModel( $ns );
245 if ( $contentmodel ) {
246 $data[$ns]['defaultcontentmodel'] = $contentmodel;
250 $this->getResult()->setIndexedTagName( $data, 'ns' );
251 return $this->getResult()->addValue( 'query', $property, $data );
254 protected function appendNamespaceAliases( $property ) {
255 global $wgNamespaceAliases, $wgContLang;
256 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
257 $namespaces = $wgContLang->getNamespaces();
259 foreach ( $aliases as $title => $ns ) {
260 if ( $namespaces[$ns] == $title ) {
261 // Don't list duplicates
265 'id' => intval( $ns )
267 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
271 $this->getResult()->setIndexedTagName( $data, 'ns' );
272 return $this->getResult()->addValue( 'query', $property, $data );
275 protected function appendSpecialPageAliases( $property ) {
278 $aliases = $wgContLang->getSpecialPageAliases();
279 foreach ( SpecialPageFactory
::getList() as $specialpage => $stuff ) {
280 if ( isset( $aliases[$specialpage] ) ) {
281 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases[$specialpage] );
282 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
286 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
287 return $this->getResult()->addValue( 'query', $property, $data );
290 protected function appendMagicWords( $property ) {
293 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
294 $caseSensitive = array_shift( $aliases );
295 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
296 if ( $caseSensitive ) {
297 $arr['case-sensitive'] = '';
299 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
302 $this->getResult()->setIndexedTagName( $data, 'magicword' );
303 return $this->getResult()->addValue( 'query', $property, $data );
306 protected function appendInterwikiMap( $property, $filter ) {
308 if ( $filter === 'local' ) {
310 } elseif ( $filter === '!local' ) {
312 } elseif ( $filter ) {
313 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
316 $params = $this->extractRequestParams();
317 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
318 $langNames = Language
::fetchLanguageNames( $langCode );
320 $getPrefixes = Interwiki
::getAllPrefixes( $local );
323 foreach ( $getPrefixes as $row ) {
324 $prefix = $row['iw_prefix'];
326 $val['prefix'] = $prefix;
327 if ( $row['iw_local'] == '1' ) {
330 // $val['trans'] = intval( $row['iw_trans'] ); // should this be exposed?
331 if ( isset( $langNames[$prefix] ) ) {
332 $val['language'] = $langNames[$prefix];
334 $val['url'] = wfExpandUrl( $row['iw_url'], PROTO_CURRENT
);
335 if( isset( $row['iw_wikiid'] ) ) {
336 $val['wikiid'] = $row['iw_wikiid'];
338 if( isset( $row['iw_api'] ) ) {
339 $val['api'] = $row['iw_api'];
345 $this->getResult()->setIndexedTagName( $data, 'iw' );
346 return $this->getResult()->addValue( 'query', $property, $data );
349 protected function appendDbReplLagInfo( $property, $includeAll ) {
350 global $wgShowHostnames;
354 if ( !$wgShowHostnames ) {
355 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
358 $lags = $lb->getLagTimes();
359 foreach ( $lags as $i => $lag ) {
361 'host' => $lb->getServerName( $i ),
366 list( $host, $lag, $index ) = $lb->getMaxLag();
368 'host' => $wgShowHostnames
369 ?
$lb->getServerName( $index )
371 'lag' => intval( $lag )
375 $result = $this->getResult();
376 $result->setIndexedTagName( $data, 'db' );
377 return $this->getResult()->addValue( 'query', $property, $data );
380 protected function appendStatistics( $property ) {
381 global $wgDisableCounters;
383 $data['pages'] = intval( SiteStats
::pages() );
384 $data['articles'] = intval( SiteStats
::articles() );
385 if ( !$wgDisableCounters ) {
386 $data['views'] = intval( SiteStats
::views() );
388 $data['edits'] = intval( SiteStats
::edits() );
389 $data['images'] = intval( SiteStats
::images() );
390 $data['users'] = intval( SiteStats
::users() );
391 $data['activeusers'] = intval( SiteStats
::activeUsers() );
392 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
393 $data['jobs'] = intval( SiteStats
::jobs() );
394 return $this->getResult()->addValue( 'query', $property, $data );
397 protected function appendUserGroups( $property, $numberInGroup ) {
398 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
401 $result = $this->getResult();
402 foreach ( $wgGroupPermissions as $group => $permissions ) {
405 'rights' => array_keys( $permissions, true ),
408 if ( $numberInGroup ) {
409 global $wgAutopromote;
411 if ( $group == 'user' ) {
412 $arr['number'] = SiteStats
::users();
414 // '*' and autopromote groups have no size
415 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
416 $arr['number'] = SiteStats
::numberInGroup( $group );
421 'add' => $wgAddGroups,
422 'remove' => $wgRemoveGroups,
423 'add-self' => $wgGroupsAddToSelf,
424 'remove-self' => $wgGroupsRemoveFromSelf
427 foreach ( $groupArr as $type => $rights ) {
428 if ( isset( $rights[$group] ) ) {
429 $arr[$type] = $rights[$group];
430 $result->setIndexedTagName( $arr[$type], 'group' );
434 $result->setIndexedTagName( $arr['rights'], 'permission' );
438 $result->setIndexedTagName( $data, 'group' );
439 return $result->addValue( 'query', $property, $data );
442 protected function appendFileExtensions( $property ) {
443 global $wgFileExtensions;
446 foreach ( $wgFileExtensions as $ext ) {
447 $data[] = array( 'ext' => $ext );
449 $this->getResult()->setIndexedTagName( $data, 'fe' );
450 return $this->getResult()->addValue( 'query', $property, $data );
453 protected function appendExtensions( $property ) {
454 global $wgExtensionCredits;
456 foreach ( $wgExtensionCredits as $type => $extensions ) {
457 foreach ( $extensions as $ext ) {
459 $ret['type'] = $type;
460 if ( isset( $ext['name'] ) ) {
461 $ret['name'] = $ext['name'];
463 if ( isset( $ext['description'] ) ) {
464 $ret['description'] = $ext['description'];
466 if ( isset( $ext['descriptionmsg'] ) ) {
467 // Can be a string or array( key, param1, param2, ... )
468 if ( is_array( $ext['descriptionmsg'] ) ) {
469 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
470 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
471 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
473 $ret['descriptionmsg'] = $ext['descriptionmsg'];
476 if ( isset( $ext['author'] ) ) {
477 $ret['author'] = is_array( $ext['author'] ) ?
478 implode( ', ', $ext['author'] ) : $ext['author'];
480 if ( isset( $ext['url'] ) ) {
481 $ret['url'] = $ext['url'];
483 if ( isset( $ext['version'] ) ) {
484 $ret['version'] = $ext['version'];
485 } elseif ( isset( $ext['svn-revision'] ) &&
486 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
487 $ext['svn-revision'], $m ) )
489 $ret['version'] = 'r' . $m[1];
495 $this->getResult()->setIndexedTagName( $data, 'ext' );
496 return $this->getResult()->addValue( 'query', $property, $data );
499 protected function appendRightsInfo( $property ) {
500 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
501 $title = Title
::newFromText( $wgRightsPage );
502 $url = $title ?
wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
) : $wgRightsUrl;
503 $text = $wgRightsText;
504 if ( !$text && $title ) {
505 $text = $title->getPrefixedText();
509 'url' => $url ?
$url : '',
510 'text' => $text ?
$text : ''
513 return $this->getResult()->addValue( 'query', $property, $data );
516 public function appendLanguages( $property ) {
517 $params = $this->extractRequestParams();
518 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
519 $langNames = Language
::fetchLanguageNames( $langCode );
523 foreach ( $langNames as $code => $name ) {
524 $lang = array( 'code' => $code );
525 ApiResult
::setContent( $lang, $name );
528 $this->getResult()->setIndexedTagName( $data, 'lang' );
529 return $this->getResult()->addValue( 'query', $property, $data );
532 public function appendSkins( $property ) {
534 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
535 $skin = array( 'code' => $name );
536 ApiResult
::setContent( $skin, $displayName );
539 $this->getResult()->setIndexedTagName( $data, 'skin' );
540 return $this->getResult()->addValue( 'query', $property, $data );
543 public function appendExtensionTags( $property ) {
545 $wgParser->firstCallInit();
546 $tags = array_map( array( $this, 'formatParserTags' ), $wgParser->getTags() );
547 $this->getResult()->setIndexedTagName( $tags, 't' );
548 return $this->getResult()->addValue( 'query', $property, $tags );
551 public function appendFunctionHooks( $property ) {
553 $wgParser->firstCallInit();
554 $hooks = $wgParser->getFunctionHooks();
555 $this->getResult()->setIndexedTagName( $hooks, 'h' );
556 return $this->getResult()->addValue( 'query', $property, $hooks );
559 public function appendVariables( $property ) {
560 $variables = MagicWord
::getVariableIDs();
561 $this->getResult()->setIndexedTagName( $variables, 'v' );
562 return $this->getResult()->addValue( 'query', $property, $variables );
565 public function appendProtocols( $property ) {
566 global $wgUrlProtocols;
567 // Make a copy of the global so we don't try to set the _element key of it - bug 45130
568 $protocols = array_values( $wgUrlProtocols );
569 $this->getResult()->setIndexedTagName( $protocols, 'p' );
570 return $this->getResult()->addValue( 'query', $property, $protocols );
573 private function formatParserTags( $item ) {
577 public function appendSubscribedHooks( $property ) {
579 $myWgHooks = $wgHooks;
583 foreach ( $myWgHooks as $hook => $hooks ) {
586 'subscribers' => array_map( array( 'SpecialVersion', 'arrayToString' ), $hooks ),
589 $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
593 $this->getResult()->setIndexedTagName( $data, 'hook' );
594 return $this->getResult()->addValue( 'query', $property, $data );
597 public function getCacheMode( $params ) {
601 public function getAllowedParams() {
604 ApiBase
::PARAM_DFLT
=> 'general',
605 ApiBase
::PARAM_ISMULTI
=> true,
606 ApiBase
::PARAM_TYPE
=> array(
610 'specialpagealiases',
629 ApiBase
::PARAM_TYPE
=> array(
634 'showalldb' => false,
635 'numberingroup' => false,
636 'inlanguagecode' => null,
640 public function getParamDescription() {
641 $p = $this->getModulePrefix();
644 'Which sysinfo properties to get:',
645 ' general - Overall system information',
646 ' namespaces - List of registered namespaces and their canonical names',
647 ' namespacealiases - List of registered namespace aliases',
648 ' specialpagealiases - List of special page aliases',
649 ' magicwords - List of magic words and their aliases',
650 ' statistics - Returns site statistics',
651 " interwikimap - Returns interwiki map (optionally filtered, (optionally localised by using {$p}inlanguagecode))",
652 ' dbrepllag - Returns database server with the highest replication lag',
653 ' usergroups - Returns user groups and the associated permissions',
654 ' extensions - Returns extensions installed on the wiki',
655 ' fileextensions - Returns list of file extensions allowed to be uploaded',
656 ' rightsinfo - Returns wiki rights (license) information if available',
657 " languages - Returns a list of languages MediaWiki supports (optionally localised by using {$p}inlanguagecode)",
658 ' skins - Returns a list of all enabled skins',
659 ' extensiontags - Returns a list of parser extension tags',
660 ' functionhooks - Returns a list of parser function hooks',
661 ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)',
662 ' variables - Returns a list of variable IDs',
663 ' protocols - Returns a list of protocols that are allowed in external links.',
665 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
666 'showalldb' => 'List all database servers, not just the one lagging the most',
667 'numberingroup' => 'Lists the number of users in user groups',
668 'inlanguagecode' => 'Language code for localised language names (best effort, use CLDR extension)',
672 public function getDescription() {
673 return 'Return general information about the site';
676 public function getPossibleErrors() {
677 return array_merge( parent
::getPossibleErrors(), array(
678 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
682 public function getExamples() {
684 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
685 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
686 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
690 public function getHelpUrls() {
691 return 'https://www.mediawiki.org/wiki/API:Meta#siteinfo_.2F_si';