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 ) {
120 $mainPage = Title
::newMainPage();
121 $data['mainpage'] = $mainPage->getPrefixedText();
122 $data['base'] = wfExpandUrl( $mainPage->getFullUrl(), PROTO_CURRENT
);
123 $data['sitename'] = $GLOBALS['wgSitename'];
124 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
125 $data['phpversion'] = phpversion();
126 $data['phpsapi'] = PHP_SAPI
;
127 $data['dbtype'] = $GLOBALS['wgDBtype'];
128 $data['dbversion'] = $this->getDB()->getServerVersion();
130 $git = SpecialVersion
::getGitHeadSha1( $GLOBALS['IP'] );
132 $data['git-hash'] = $git;
134 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
140 // 'case-insensitive' option is reserved for future
141 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
143 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
144 $data['rightscode'] = $GLOBALS['wgRightsCode'];
146 $data['rights'] = $GLOBALS['wgRightsText'];
147 $data['lang'] = $GLOBALS['wgLanguageCode'];
149 $fallbacks = array();
150 foreach( $wgContLang->getFallbackLanguages() as $code ) {
151 $fallbacks[] = array( 'code' => $code );
153 $data['fallback'] = $fallbacks;
154 $this->getResult()->setIndexedTagName( $data['fallback'], 'lang' );
156 if( $wgContLang->hasVariants() ) {
158 foreach( $wgContLang->getVariants() as $code ) {
159 $variants[] = array( 'code' => $code );
161 $data['variants'] = $variants;
162 $this->getResult()->setIndexedTagName( $data['variants'], 'lang' );
165 if ( $wgContLang->isRTL() ) {
168 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
170 if ( wfReadOnly() ) {
171 $data['readonly'] = '';
172 $data['readonlyreason'] = wfReadOnlyReason();
174 if ( $GLOBALS['wgEnableWriteAPI'] ) {
175 $data['writeapi'] = '';
178 $tz = $GLOBALS['wgLocaltimezone'];
179 $offset = $GLOBALS['wgLocalTZoffset'];
180 if ( is_null( $tz ) ) {
183 } elseif ( is_null( $offset ) ) {
186 $data['timezone'] = $tz;
187 $data['timeoffset'] = intval( $offset );
188 $data['articlepath'] = $GLOBALS['wgArticlePath'];
189 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
190 $data['script'] = $GLOBALS['wgScript'];
191 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
192 $data['server'] = $GLOBALS['wgServer'];
193 $data['wikiid'] = wfWikiID();
194 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
196 if ( $GLOBALS['wgMiserMode'] ) {
197 $data['misermode'] = '';
200 $data['maxuploadsize'] = UploadBase
::getMaxUploadSize();
202 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
204 return $this->getResult()->addValue( 'query', $property, $data );
207 protected function appendNamespaces( $property ) {
210 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
212 'id' => intval( $ns ),
213 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
215 ApiResult
::setContent( $data[$ns], $title );
216 $canonical = MWNamespace
::getCanonicalName( $ns );
218 if ( MWNamespace
::hasSubpages( $ns ) ) {
219 $data[$ns]['subpages'] = '';
223 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
226 if ( MWNamespace
::isContent( $ns ) ) {
227 $data[$ns]['content'] = '';
230 if ( MWNamespace
::isNonincludable( $ns ) ) {
231 $data[$ns]['nonincludable'] = '';
234 $contentmodel = MWNamespace
::getNamespaceContentModel( $ns );
235 if ( $contentmodel ) {
236 $data[$ns]['defaultcontentmodel'] = $contentmodel;
240 $this->getResult()->setIndexedTagName( $data, 'ns' );
241 return $this->getResult()->addValue( 'query', $property, $data );
244 protected function appendNamespaceAliases( $property ) {
245 global $wgNamespaceAliases, $wgContLang;
246 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
247 $namespaces = $wgContLang->getNamespaces();
249 foreach ( $aliases as $title => $ns ) {
250 if ( $namespaces[$ns] == $title ) {
251 // Don't list duplicates
255 'id' => intval( $ns )
257 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
261 $this->getResult()->setIndexedTagName( $data, 'ns' );
262 return $this->getResult()->addValue( 'query', $property, $data );
265 protected function appendSpecialPageAliases( $property ) {
268 $aliases = $wgContLang->getSpecialPageAliases();
269 foreach ( SpecialPageFactory
::getList() as $specialpage => $stuff ) {
270 if ( isset( $aliases[$specialpage] ) ) {
271 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases[$specialpage] );
272 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
276 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
277 return $this->getResult()->addValue( 'query', $property, $data );
280 protected function appendMagicWords( $property ) {
283 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
284 $caseSensitive = array_shift( $aliases );
285 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
286 if ( $caseSensitive ) {
287 $arr['case-sensitive'] = '';
289 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
292 $this->getResult()->setIndexedTagName( $data, 'magicword' );
293 return $this->getResult()->addValue( 'query', $property, $data );
296 protected function appendInterwikiMap( $property, $filter ) {
298 if ( $filter === 'local' ) {
300 } elseif ( $filter === '!local' ) {
302 } elseif ( $filter ) {
303 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
306 $params = $this->extractRequestParams();
307 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
308 $langNames = Language
::fetchLanguageNames( $langCode );
310 $getPrefixes = Interwiki
::getAllPrefixes( $local );
313 foreach ( $getPrefixes as $row ) {
314 $prefix = $row['iw_prefix'];
316 $val['prefix'] = $prefix;
317 if ( $row['iw_local'] == '1' ) {
320 // $val['trans'] = intval( $row['iw_trans'] ); // should this be exposed?
321 if ( isset( $langNames[$prefix] ) ) {
322 $val['language'] = $langNames[$prefix];
324 $val['url'] = wfExpandUrl( $row['iw_url'], PROTO_CURRENT
);
325 if( isset( $row['iw_wikiid'] ) ) {
326 $val['wikiid'] = $row['iw_wikiid'];
328 if( isset( $row['iw_api'] ) ) {
329 $val['api'] = $row['iw_api'];
335 $this->getResult()->setIndexedTagName( $data, 'iw' );
336 return $this->getResult()->addValue( 'query', $property, $data );
339 protected function appendDbReplLagInfo( $property, $includeAll ) {
340 global $wgShowHostnames;
344 if ( !$wgShowHostnames ) {
345 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
348 $lags = $lb->getLagTimes();
349 foreach ( $lags as $i => $lag ) {
351 'host' => $lb->getServerName( $i ),
356 list( $host, $lag, $index ) = $lb->getMaxLag();
358 'host' => $wgShowHostnames
359 ?
$lb->getServerName( $index )
361 'lag' => intval( $lag )
365 $result = $this->getResult();
366 $result->setIndexedTagName( $data, 'db' );
367 return $this->getResult()->addValue( 'query', $property, $data );
370 protected function appendStatistics( $property ) {
371 global $wgDisableCounters;
373 $data['pages'] = intval( SiteStats
::pages() );
374 $data['articles'] = intval( SiteStats
::articles() );
375 if ( !$wgDisableCounters ) {
376 $data['views'] = intval( SiteStats
::views() );
378 $data['edits'] = intval( SiteStats
::edits() );
379 $data['images'] = intval( SiteStats
::images() );
380 $data['users'] = intval( SiteStats
::users() );
381 $data['activeusers'] = intval( SiteStats
::activeUsers() );
382 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
383 $data['jobs'] = intval( SiteStats
::jobs() );
384 return $this->getResult()->addValue( 'query', $property, $data );
387 protected function appendUserGroups( $property, $numberInGroup ) {
388 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
391 $result = $this->getResult();
392 foreach ( $wgGroupPermissions as $group => $permissions ) {
395 'rights' => array_keys( $permissions, true ),
398 if ( $numberInGroup ) {
399 global $wgAutopromote;
401 if ( $group == 'user' ) {
402 $arr['number'] = SiteStats
::users();
404 // '*' and autopromote groups have no size
405 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
406 $arr['number'] = SiteStats
::numberInGroup( $group );
411 'add' => $wgAddGroups,
412 'remove' => $wgRemoveGroups,
413 'add-self' => $wgGroupsAddToSelf,
414 'remove-self' => $wgGroupsRemoveFromSelf
417 foreach ( $groupArr as $type => $rights ) {
418 if ( isset( $rights[$group] ) ) {
419 $arr[$type] = $rights[$group];
420 $result->setIndexedTagName( $arr[$type], 'group' );
424 $result->setIndexedTagName( $arr['rights'], 'permission' );
428 $result->setIndexedTagName( $data, 'group' );
429 return $result->addValue( 'query', $property, $data );
432 protected function appendFileExtensions( $property ) {
433 global $wgFileExtensions;
436 foreach ( $wgFileExtensions as $ext ) {
437 $data[] = array( 'ext' => $ext );
439 $this->getResult()->setIndexedTagName( $data, 'fe' );
440 return $this->getResult()->addValue( 'query', $property, $data );
443 protected function appendExtensions( $property ) {
444 global $wgExtensionCredits;
446 foreach ( $wgExtensionCredits as $type => $extensions ) {
447 foreach ( $extensions as $ext ) {
449 $ret['type'] = $type;
450 if ( isset( $ext['name'] ) ) {
451 $ret['name'] = $ext['name'];
453 if ( isset( $ext['description'] ) ) {
454 $ret['description'] = $ext['description'];
456 if ( isset( $ext['descriptionmsg'] ) ) {
457 // Can be a string or array( key, param1, param2, ... )
458 if ( is_array( $ext['descriptionmsg'] ) ) {
459 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
460 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
461 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
463 $ret['descriptionmsg'] = $ext['descriptionmsg'];
466 if ( isset( $ext['author'] ) ) {
467 $ret['author'] = is_array( $ext['author'] ) ?
468 implode( ', ', $ext['author'] ) : $ext['author'];
470 if ( isset( $ext['url'] ) ) {
471 $ret['url'] = $ext['url'];
473 if ( isset( $ext['version'] ) ) {
474 $ret['version'] = $ext['version'];
475 } elseif ( isset( $ext['svn-revision'] ) &&
476 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
477 $ext['svn-revision'], $m ) )
479 $ret['version'] = 'r' . $m[1];
485 $this->getResult()->setIndexedTagName( $data, 'ext' );
486 return $this->getResult()->addValue( 'query', $property, $data );
489 protected function appendRightsInfo( $property ) {
490 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
491 $title = Title
::newFromText( $wgRightsPage );
492 $url = $title ?
wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
) : $wgRightsUrl;
493 $text = $wgRightsText;
494 if ( !$text && $title ) {
495 $text = $title->getPrefixedText();
499 'url' => $url ?
$url : '',
500 'text' => $text ?
$text : ''
503 return $this->getResult()->addValue( 'query', $property, $data );
506 public function appendLanguages( $property ) {
507 $params = $this->extractRequestParams();
508 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
509 $langNames = Language
::fetchLanguageNames( $langCode );
513 foreach ( $langNames as $code => $name ) {
514 $lang = array( 'code' => $code );
515 ApiResult
::setContent( $lang, $name );
518 $this->getResult()->setIndexedTagName( $data, 'lang' );
519 return $this->getResult()->addValue( 'query', $property, $data );
522 public function appendSkins( $property ) {
524 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
525 $skin = array( 'code' => $name );
526 ApiResult
::setContent( $skin, $displayName );
529 $this->getResult()->setIndexedTagName( $data, 'skin' );
530 return $this->getResult()->addValue( 'query', $property, $data );
533 public function appendExtensionTags( $property ) {
535 $wgParser->firstCallInit();
536 $tags = array_map( array( $this, 'formatParserTags' ), $wgParser->getTags() );
537 $this->getResult()->setIndexedTagName( $tags, 't' );
538 return $this->getResult()->addValue( 'query', $property, $tags );
541 public function appendFunctionHooks( $property ) {
543 $wgParser->firstCallInit();
544 $hooks = $wgParser->getFunctionHooks();
545 $this->getResult()->setIndexedTagName( $hooks, 'h' );
546 return $this->getResult()->addValue( 'query', $property, $hooks );
549 public function appendVariables( $property ) {
550 $variables = MagicWord
::getVariableIDs();
551 $this->getResult()->setIndexedTagName( $variables, 'v' );
552 return $this->getResult()->addValue( 'query', $property, $variables );
555 public function appendProtocols( $property ) {
556 global $wgUrlProtocols;
557 // Make a copy of the global so we don't try to set the _element key of it - bug 45130
558 $protocols = array_values( $wgUrlProtocols );
559 $this->getResult()->setIndexedTagName( $protocols, 'p' );
560 return $this->getResult()->addValue( 'query', $property, $protocols );
563 private function formatParserTags( $item ) {
567 public function appendSubscribedHooks( $property ) {
569 $myWgHooks = $wgHooks;
573 foreach ( $myWgHooks as $hook => $hooks ) {
576 'subscribers' => array_map( array( 'SpecialVersion', 'arrayToString' ), $hooks ),
579 $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
583 $this->getResult()->setIndexedTagName( $data, 'hook' );
584 return $this->getResult()->addValue( 'query', $property, $data );
587 public function getCacheMode( $params ) {
591 public function getAllowedParams() {
594 ApiBase
::PARAM_DFLT
=> 'general',
595 ApiBase
::PARAM_ISMULTI
=> true,
596 ApiBase
::PARAM_TYPE
=> array(
600 'specialpagealiases',
619 ApiBase
::PARAM_TYPE
=> array(
624 'showalldb' => false,
625 'numberingroup' => false,
626 'inlanguagecode' => null,
630 public function getParamDescription() {
631 $p = $this->getModulePrefix();
634 'Which sysinfo properties to get:',
635 ' general - Overall system information',
636 ' namespaces - List of registered namespaces and their canonical names',
637 ' namespacealiases - List of registered namespace aliases',
638 ' specialpagealiases - List of special page aliases',
639 ' magicwords - List of magic words and their aliases',
640 ' statistics - Returns site statistics',
641 " interwikimap - Returns interwiki map (optionally filtered, (optionally localised by using {$p}inlanguagecode))",
642 ' dbrepllag - Returns database server with the highest replication lag',
643 ' usergroups - Returns user groups and the associated permissions',
644 ' extensions - Returns extensions installed on the wiki',
645 ' fileextensions - Returns list of file extensions allowed to be uploaded',
646 ' rightsinfo - Returns wiki rights (license) information if available',
647 " languages - Returns a list of languages MediaWiki supports (optionally localised by using {$p}inlanguagecode)",
648 ' skins - Returns a list of all enabled skins',
649 ' extensiontags - Returns a list of parser extension tags',
650 ' functionhooks - Returns a list of parser function hooks',
651 ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)',
652 ' variables - Returns a list of variable IDs',
653 ' protocols - Returns a list of protocols that are allowed in external links.',
655 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
656 'showalldb' => 'List all database servers, not just the one lagging the most',
657 'numberingroup' => 'Lists the number of users in user groups',
658 'inlanguagecode' => 'Language code for localised language names (best effort, use CLDR extension)',
662 public function getDescription() {
663 return 'Return general information about the site';
666 public function getPossibleErrors() {
667 return array_merge( parent
::getPossibleErrors(), array(
668 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
672 public function getExamples() {
674 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
675 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
676 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
680 public function getHelpUrls() {
681 return 'https://www.mediawiki.org/wiki/API:Meta#siteinfo_.2F_si';