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 ApiBase
::dieDebug( __METHOD__
, "Unknown prop=$p" );
100 // Abuse siprop as a query-continue parameter
101 // and set it to all unprocessed props
102 $this->setContinueEnumParameter( 'prop', implode( '|',
103 array_diff( $params['prop'], $done ) ) );
110 protected function appendGeneralInfo( $property ) {
114 $mainPage = Title
::newMainPage();
115 $data['mainpage'] = $mainPage->getPrefixedText();
116 $data['base'] = wfExpandUrl( $mainPage->getFullUrl(), PROTO_CURRENT
);
117 $data['sitename'] = $GLOBALS['wgSitename'];
118 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
119 $data['phpversion'] = phpversion();
120 $data['phpsapi'] = php_sapi_name();
121 $data['dbtype'] = $GLOBALS['wgDBtype'];
122 $data['dbversion'] = $this->getDB()->getServerVersion();
124 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
129 // 'case-insensitive' option is reserved for future
130 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
132 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
133 $data['rightscode'] = $GLOBALS['wgRightsCode'];
135 $data['rights'] = $GLOBALS['wgRightsText'];
136 $data['lang'] = $GLOBALS['wgLanguageCode'];
138 $fallbacks = array();
139 foreach( $wgContLang->getFallbackLanguages() as $code ) {
140 $fallbacks[] = array( 'code' => $code );
142 $data['fallback'] = $fallbacks;
143 $this->getResult()->setIndexedTagName( $data['fallback'], 'lang' );
145 if ( $wgContLang->isRTL() ) {
148 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
150 if ( wfReadOnly() ) {
151 $data['readonly'] = '';
152 $data['readonlyreason'] = wfReadOnlyReason();
154 if ( $GLOBALS['wgEnableWriteAPI'] ) {
155 $data['writeapi'] = '';
158 $tz = $GLOBALS['wgLocaltimezone'];
159 $offset = $GLOBALS['wgLocalTZoffset'];
160 if ( is_null( $tz ) ) {
163 } elseif ( is_null( $offset ) ) {
166 $data['timezone'] = $tz;
167 $data['timeoffset'] = intval( $offset );
168 $data['articlepath'] = $GLOBALS['wgArticlePath'];
169 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
170 $data['script'] = $GLOBALS['wgScript'];
171 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
172 $data['server'] = $GLOBALS['wgServer'];
173 $data['wikiid'] = wfWikiID();
174 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
176 if ( $GLOBALS['wgMiserMode'] ) {
177 $data['misermode'] = '';
180 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
182 return $this->getResult()->addValue( 'query', $property, $data );
185 protected function appendNamespaces( $property ) {
188 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
190 'id' => intval( $ns ),
191 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
193 ApiResult
::setContent( $data[$ns], $title );
194 $canonical = MWNamespace
::getCanonicalName( $ns );
196 if ( MWNamespace
::hasSubpages( $ns ) ) {
197 $data[$ns]['subpages'] = '';
201 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
204 if ( MWNamespace
::isContent( $ns ) ) {
205 $data[$ns]['content'] = '';
209 $this->getResult()->setIndexedTagName( $data, 'ns' );
210 return $this->getResult()->addValue( 'query', $property, $data );
213 protected function appendNamespaceAliases( $property ) {
214 global $wgNamespaceAliases, $wgContLang;
215 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
216 $namespaces = $wgContLang->getNamespaces();
218 foreach ( $aliases as $title => $ns ) {
219 if ( $namespaces[$ns] == $title ) {
220 // Don't list duplicates
224 'id' => intval( $ns )
226 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
230 $this->getResult()->setIndexedTagName( $data, 'ns' );
231 return $this->getResult()->addValue( 'query', $property, $data );
234 protected function appendSpecialPageAliases( $property ) {
237 foreach ( $wgContLang->getSpecialPageAliases() as $specialpage => $aliases ) {
238 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
239 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
242 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
243 return $this->getResult()->addValue( 'query', $property, $data );
246 protected function appendMagicWords( $property ) {
249 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
250 $caseSensitive = array_shift( $aliases );
251 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
252 if ( $caseSensitive ) {
253 $arr['case-sensitive'] = '';
255 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
258 $this->getResult()->setIndexedTagName( $data, 'magicword' );
259 return $this->getResult()->addValue( 'query', $property, $data );
262 protected function appendInterwikiMap( $property, $filter ) {
264 if ( $filter === 'local' ) {
266 } elseif ( $filter === '!local' ) {
268 } elseif ( $filter ) {
269 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
272 $params = $this->extractRequestParams();
273 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
276 $langNames = Language
::getTranslatedLanguageNames( $langCode );
278 $langNames = Language
::getLanguageNames();
281 $getPrefixes = Interwiki
::getAllPrefixes( $local );
284 foreach ( $getPrefixes as $row ) {
285 $prefix = $row['iw_prefix'];
287 $val['prefix'] = $prefix;
288 if ( $row['iw_local'] == '1' ) {
291 // $val['trans'] = intval( $row['iw_trans'] ); // should this be exposed?
292 if ( isset( $langNames[$prefix] ) ) {
293 $val['language'] = $langNames[$prefix];
295 $val['url'] = wfExpandUrl( $row['iw_url'], PROTO_CURRENT
);
296 if( isset( $row['iw_wikiid'] ) ) {
297 $val['wikiid'] = $row['iw_wikiid'];
299 if( isset( $row['iw_api'] ) ) {
300 $val['api'] = $row['iw_api'];
306 $this->getResult()->setIndexedTagName( $data, 'iw' );
307 return $this->getResult()->addValue( 'query', $property, $data );
310 protected function appendDbReplLagInfo( $property, $includeAll ) {
311 global $wgShowHostnames;
315 if ( !$wgShowHostnames ) {
316 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
319 $lags = $lb->getLagTimes();
320 foreach ( $lags as $i => $lag ) {
322 'host' => $lb->getServerName( $i ),
327 list( $host, $lag, $index ) = $lb->getMaxLag();
329 'host' => $wgShowHostnames
330 ?
$lb->getServerName( $index )
332 'lag' => intval( $lag )
336 $result = $this->getResult();
337 $result->setIndexedTagName( $data, 'db' );
338 return $this->getResult()->addValue( 'query', $property, $data );
341 protected function appendStatistics( $property ) {
342 global $wgDisableCounters;
344 $data['pages'] = intval( SiteStats
::pages() );
345 $data['articles'] = intval( SiteStats
::articles() );
346 if ( !$wgDisableCounters ) {
347 $data['views'] = intval( SiteStats
::views() );
349 $data['edits'] = intval( SiteStats
::edits() );
350 $data['images'] = intval( SiteStats
::images() );
351 $data['users'] = intval( SiteStats
::users() );
352 $data['activeusers'] = intval( SiteStats
::activeUsers() );
353 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
354 $data['jobs'] = intval( SiteStats
::jobs() );
355 return $this->getResult()->addValue( 'query', $property, $data );
358 protected function appendUserGroups( $property, $numberInGroup ) {
359 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
362 $result = $this->getResult();
363 foreach ( $wgGroupPermissions as $group => $permissions ) {
366 'rights' => array_keys( $permissions, true ),
369 if ( $numberInGroup ) {
370 global $wgAutopromote;
372 if ( $group == 'user' ) {
373 $arr['number'] = SiteStats
::users();
375 // '*' and autopromote groups have no size
376 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
377 $arr['number'] = SiteStats
::numberInGroup( $group );
382 'add' => $wgAddGroups,
383 'remove' => $wgRemoveGroups,
384 'add-self' => $wgGroupsAddToSelf,
385 'remove-self' => $wgGroupsRemoveFromSelf
388 foreach ( $groupArr as $type => $rights ) {
389 if ( isset( $rights[$group] ) ) {
390 $arr[$type] = $rights[$group];
391 $result->setIndexedTagName( $arr[$type], 'group' );
395 $result->setIndexedTagName( $arr['rights'], 'permission' );
399 $result->setIndexedTagName( $data, 'group' );
400 return $result->addValue( 'query', $property, $data );
403 protected function appendFileExtensions( $property ) {
404 global $wgFileExtensions;
407 foreach ( $wgFileExtensions as $ext ) {
408 $data[] = array( 'ext' => $ext );
410 $this->getResult()->setIndexedTagName( $data, 'fe' );
411 return $this->getResult()->addValue( 'query', $property, $data );
414 protected function appendExtensions( $property ) {
415 global $wgExtensionCredits;
417 foreach ( $wgExtensionCredits as $type => $extensions ) {
418 foreach ( $extensions as $ext ) {
420 $ret['type'] = $type;
421 if ( isset( $ext['name'] ) ) {
422 $ret['name'] = $ext['name'];
424 if ( isset( $ext['description'] ) ) {
425 $ret['description'] = $ext['description'];
427 if ( isset( $ext['descriptionmsg'] ) ) {
428 // Can be a string or array( key, param1, param2, ... )
429 if ( is_array( $ext['descriptionmsg'] ) ) {
430 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
431 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
432 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
434 $ret['descriptionmsg'] = $ext['descriptionmsg'];
437 if ( isset( $ext['author'] ) ) {
438 $ret['author'] = is_array( $ext['author'] ) ?
439 implode( ', ', $ext['author' ] ) : $ext['author'];
441 if ( isset( $ext['url'] ) ) {
442 $ret['url'] = $ext['url'];
444 if ( isset( $ext['version'] ) ) {
445 $ret['version'] = $ext['version'];
446 } elseif ( isset( $ext['svn-revision'] ) &&
447 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
448 $ext['svn-revision'], $m ) )
450 $ret['version'] = 'r' . $m[1];
456 $this->getResult()->setIndexedTagName( $data, 'ext' );
457 return $this->getResult()->addValue( 'query', $property, $data );
460 protected function appendRightsInfo( $property ) {
461 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
462 $title = Title
::newFromText( $wgRightsPage );
463 $url = $title ?
wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
) : $wgRightsUrl;
464 $text = $wgRightsText;
465 if ( !$text && $title ) {
466 $text = $title->getPrefixedText();
470 'url' => $url ?
$url : '',
471 'text' => $text ?
$text : ''
474 return $this->getResult()->addValue( 'query', $property, $data );
477 public function appendLanguages( $property ) {
478 $params = $this->extractRequestParams();
479 $langCode = isset( $params['inlanguagecode'] ) ?
$params['inlanguagecode'] : '';
482 $langNames = Language
::getTranslatedLanguageNames( $langCode );
484 $langNames = Language
::getLanguageNames();
489 foreach ( $langNames as $code => $name ) {
490 $lang = array( 'code' => $code );
491 ApiResult
::setContent( $lang, $name );
494 $this->getResult()->setIndexedTagName( $data, 'lang' );
495 return $this->getResult()->addValue( 'query', $property, $data );
498 public function appendSkins( $property ) {
500 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
501 $skin = array( 'code' => $name );
502 ApiResult
::setContent( $skin, $displayName );
505 $this->getResult()->setIndexedTagName( $data, 'skin' );
506 return $this->getResult()->addValue( 'query', $property, $data );
509 public function appendExtensionTags( $property ) {
511 $wgParser->firstCallInit();
512 $tags = array_map( array( $this, 'formatParserTags'), $wgParser->getTags() );
513 $this->getResult()->setIndexedTagName( $tags, 't' );
514 return $this->getResult()->addValue( 'query', $property, $tags );
517 public function appendFunctionHooks( $property ) {
519 $wgParser->firstCallInit();
520 $hooks = $wgParser->getFunctionHooks();
521 $this->getResult()->setIndexedTagName( $hooks, 'h' );
522 return $this->getResult()->addValue( 'query', $property, $hooks );
525 private function formatParserTags( $item ) {
529 public function appendSubscribedHooks( $property ) {
531 $myWgHooks = $wgHooks;
535 foreach ( $myWgHooks as $hook => $hooks ) {
538 'subscribers' => array_map( array( 'SpecialVersion', 'arrayToString' ), $hooks ),
541 $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
545 $this->getResult()->setIndexedTagName( $data, 'hook' );
546 return $this->getResult()->addValue( 'query', $property, $data );
549 public function getCacheMode( $params ) {
553 public function getAllowedParams() {
556 ApiBase
::PARAM_DFLT
=> 'general',
557 ApiBase
::PARAM_ISMULTI
=> true,
558 ApiBase
::PARAM_TYPE
=> array(
562 'specialpagealiases',
579 ApiBase
::PARAM_TYPE
=> array(
584 'showalldb' => false,
585 'numberingroup' => false,
586 'inlanguagecode' => null,
590 public function getParamDescription() {
591 $p = $this->getModulePrefix();
594 'Which sysinfo properties to get:',
595 ' general - Overall system information',
596 ' namespaces - List of registered namespaces and their canonical names',
597 ' namespacealiases - List of registered namespace aliases',
598 ' specialpagealiases - List of special page aliases',
599 ' magicwords - List of magic words and their aliases',
600 ' statistics - Returns site statistics',
601 " interwikimap - Returns interwiki map (optionally filtered, (optionally localised by using {$p}inlanguagecode))",
602 ' dbrepllag - Returns database server with the highest replication lag',
603 ' usergroups - Returns user groups and the associated permissions',
604 ' extensions - Returns extensions installed on the wiki',
605 ' fileextensions - Returns list of file extensions allowed to be uploaded',
606 ' rightsinfo - Returns wiki rights (license) information if available',
607 " languages - Returns a list of languages MediaWiki supports (optionally localised by using {$p}inlanguagecode)",
608 ' skins - Returns a list of all enabled skins',
609 ' extensiontags - Returns a list of parser extension tags',
610 ' functionhooks - Returns a list of parser function hooks',
611 ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)'
613 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
614 'showalldb' => 'List all database servers, not just the one lagging the most',
615 'numberingroup' => 'Lists the number of users in user groups',
616 'inlanguagecode' => 'Language code for localised language names (best effort, use CLDR extension)',
620 public function getDescription() {
621 return 'Return general information about the site';
624 public function getPossibleErrors() {
625 return array_merge( parent
::getPossibleErrors(), array(
626 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
630 public function getExamples() {
632 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
633 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
634 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
638 public function getHelpUrls() {
639 return 'https://www.mediawiki.org/wiki/API:Meta#siteinfo_.2F_si';
642 public function getVersion() {
643 return __CLASS__
. ': $Id$';