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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * A query action to return meta information about the wiki site.
37 class ApiQuerySiteinfo
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'si' );
43 public function execute() {
44 $params = $this->extractRequestParams();
46 foreach ( $params['prop'] as $p ) {
49 $fit = $this->appendGeneralInfo( $p );
52 $fit = $this->appendNamespaces( $p );
54 case 'namespacealiases':
55 $fit = $this->appendNamespaceAliases( $p );
57 case 'specialpagealiases':
58 $fit = $this->appendSpecialPageAliases( $p );
61 $fit = $this->appendMagicWords( $p );
64 $filteriw = isset( $params['filteriw'] ) ?
$params['filteriw'] : false;
65 $fit = $this->appendInterwikiMap( $p, $filteriw );
68 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
71 $fit = $this->appendStatistics( $p );
74 $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
77 $fit = $this->appendExtensions( $p );
79 case 'fileextensions':
80 $fit = $this->appendFileExtensions( $p );
83 $fit = $this->appendRightsInfo( $p );
86 $fit = $this->appendLanguages( $p );
89 $fit = $this->appendSkins( $p );
92 ApiBase
::dieDebug( __METHOD__
, "Unknown prop=$p" );
95 // Abuse siprop as a query-continue parameter
96 // and set it to all unprocessed props
97 $this->setContinueEnumParameter( 'prop', implode( '|',
98 array_diff( $params['prop'], $done ) ) );
105 protected function appendGeneralInfo( $property ) {
109 $mainPage = Title
::newMainPage();
110 $data['mainpage'] = $mainPage->getPrefixedText();
111 $data['base'] = $mainPage->getFullUrl();
112 $data['sitename'] = $GLOBALS['wgSitename'];
113 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
114 $data['phpversion'] = phpversion();
115 $data['phpsapi'] = php_sapi_name();
116 $data['dbtype'] = $GLOBALS['wgDBtype'];
117 $data['dbversion'] = $this->getDB()->getServerVersion();
119 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
124 // 'case-insensitive' option is reserved for future
125 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
127 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
128 $data['rightscode'] = $GLOBALS['wgRightsCode'];
130 $data['rights'] = $GLOBALS['wgRightsText'];
131 $data['lang'] = $GLOBALS['wgLanguageCode'];
132 if ( $wgContLang->isRTL() ) {
135 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
137 if ( wfReadOnly() ) {
138 $data['readonly'] = '';
139 $data['readonlyreason'] = wfReadOnlyReason();
141 if ( $GLOBALS['wgEnableWriteAPI'] ) {
142 $data['writeapi'] = '';
145 $tz = $GLOBALS['wgLocaltimezone'];
146 $offset = $GLOBALS['wgLocalTZoffset'];
147 if ( is_null( $tz ) ) {
150 } elseif ( is_null( $offset ) ) {
153 $data['timezone'] = $tz;
154 $data['timeoffset'] = intval( $offset );
155 $data['articlepath'] = $GLOBALS['wgArticlePath'];
156 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
157 $data['script'] = $GLOBALS['wgScript'];
158 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
159 $data['server'] = $GLOBALS['wgServer'];
160 $data['wikiid'] = wfWikiID();
161 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
163 return $this->getResult()->addValue( 'query', $property, $data );
166 protected function appendNamespaces( $property ) {
169 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
171 'id' => intval( $ns ),
172 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
174 ApiResult
::setContent( $data[$ns], $title );
175 $canonical = MWNamespace
::getCanonicalName( $ns );
177 if ( MWNamespace
::hasSubpages( $ns ) ) {
178 $data[$ns]['subpages'] = '';
182 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
185 if ( MWNamespace
::isContent( $ns ) ) {
186 $data[$ns]['content'] = '';
190 $this->getResult()->setIndexedTagName( $data, 'ns' );
191 return $this->getResult()->addValue( 'query', $property, $data );
194 protected function appendNamespaceAliases( $property ) {
195 global $wgNamespaceAliases, $wgContLang;
196 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
197 $namespaces = $wgContLang->getNamespaces();
199 foreach ( $aliases as $title => $ns ) {
200 if ( $namespaces[$ns] == $title ) {
201 // Don't list duplicates
205 'id' => intval( $ns )
207 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
211 $this->getResult()->setIndexedTagName( $data, 'ns' );
212 return $this->getResult()->addValue( 'query', $property, $data );
215 protected function appendSpecialPageAliases( $property ) {
218 foreach ( $wgContLang->getSpecialPageAliases() as $specialpage => $aliases )
220 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
221 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
224 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
225 return $this->getResult()->addValue( 'query', $property, $data );
228 protected function appendMagicWords( $property ) {
231 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
232 $caseSensitive = array_shift( $aliases );
233 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
234 if ( $caseSensitive ) {
235 $arr['case-sensitive'] = '';
237 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
240 $this->getResult()->setIndexedTagName( $data, 'magicword' );
241 return $this->getResult()->addValue( 'query', $property, $data );
244 protected function appendInterwikiMap( $property, $filter ) {
245 $this->resetQueryParams();
246 $this->addTables( 'interwiki' );
247 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
249 if ( $filter === 'local' ) {
250 $this->addWhere( 'iw_local = 1' );
251 } elseif ( $filter === '!local' ) {
252 $this->addWhere( 'iw_local = 0' );
253 } elseif ( $filter ) {
254 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
257 $this->addOption( 'ORDER BY', 'iw_prefix' );
259 $res = $this->select( __METHOD__
);
262 $langNames = Language
::getLanguageNames();
263 foreach ( $res as $row ) {
265 $val['prefix'] = $row->iw_prefix
;
266 if ( $row->iw_local
== '1' ) {
269 // $val['trans'] = intval( $row->iw_trans ); // should this be exposed?
270 if ( isset( $langNames[$row->iw_prefix
] ) ) {
271 $val['language'] = $langNames[$row->iw_prefix
];
273 $val['url'] = $row->iw_url
;
278 $this->getResult()->setIndexedTagName( $data, 'iw' );
279 return $this->getResult()->addValue( 'query', $property, $data );
282 protected function appendDbReplLagInfo( $property, $includeAll ) {
283 global $wgShowHostnames;
286 if ( !$wgShowHostnames ) {
287 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
291 $lags = $lb->getLagTimes();
292 foreach ( $lags as $i => $lag ) {
294 'host' => $lb->getServerName( $i ),
299 list( $host, $lag ) = wfGetLB()->getMaxLag();
301 'host' => $wgShowHostnames ?
$host : '',
302 'lag' => intval( $lag )
306 $result = $this->getResult();
307 $result->setIndexedTagName( $data, 'db' );
308 return $this->getResult()->addValue( 'query', $property, $data );
311 protected function appendStatistics( $property ) {
312 global $wgDisableCounters;
314 $data['pages'] = intval( SiteStats
::pages() );
315 $data['articles'] = intval( SiteStats
::articles() );
316 if ( !$wgDisableCounters ) {
317 $data['views'] = intval( SiteStats
::views() );
319 $data['edits'] = intval( SiteStats
::edits() );
320 $data['images'] = intval( SiteStats
::images() );
321 $data['users'] = intval( SiteStats
::users() );
322 $data['activeusers'] = intval( SiteStats
::activeUsers() );
323 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
324 $data['jobs'] = intval( SiteStats
::jobs() );
325 return $this->getResult()->addValue( 'query', $property, $data );
328 protected function appendUserGroups( $property, $numberInGroup ) {
329 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
332 foreach ( $wgGroupPermissions as $group => $permissions ) {
335 'rights' => array_keys( $permissions, true ),
338 if ( $numberInGroup ) {
339 global $wgAutopromote;
341 if ( $group == 'user' ) {
342 $arr['number'] = SiteStats
::users();
344 // '*' and autopromote groups have no size
345 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
346 $arr['number'] = SiteStats
::numberInGroup( $group );
351 'add' => $wgAddGroups,
352 'remove' => $wgRemoveGroups,
353 'add-self' => $wgGroupsAddToSelf,
354 'remove-self' => $wgGroupsRemoveFromSelf
357 foreach ( $groupArr as $type => $rights ) {
358 if ( isset( $rights[$group] ) ) {
359 $arr[$type] = $rights[$group];
360 $this->getResult()->setIndexedTagName( $arr[$type], 'group' );
364 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
368 $this->getResult()->setIndexedTagName( $data, 'group' );
369 return $this->getResult()->addValue( 'query', $property, $data );
372 protected function appendFileExtensions( $property ) {
373 global $wgFileExtensions;
376 foreach ( $wgFileExtensions as $ext ) {
377 $data[] = array( 'ext' => $ext );
379 $this->getResult()->setIndexedTagName( $data, 'fe' );
380 return $this->getResult()->addValue( 'query', $property, $data );
383 protected function appendExtensions( $property ) {
384 global $wgExtensionCredits;
386 foreach ( $wgExtensionCredits as $type => $extensions ) {
387 foreach ( $extensions as $ext ) {
389 $ret['type'] = $type;
390 if ( isset( $ext['name'] ) ) {
391 $ret['name'] = $ext['name'];
393 if ( isset( $ext['description'] ) ) {
394 $ret['description'] = $ext['description'];
396 if ( isset( $ext['descriptionmsg'] ) ) {
397 // Can be a string or array( key, param1, param2, ... )
398 if ( is_array( $ext['descriptionmsg'] ) ) {
399 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
400 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
401 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
403 $ret['descriptionmsg'] = $ext['descriptionmsg'];
406 if ( isset( $ext['author'] ) ) {
407 $ret['author'] = is_array( $ext['author'] ) ?
408 implode( ', ', $ext['author' ] ) : $ext['author'];
410 if ( isset( $ext['url'] ) ) {
411 $ret['url'] = $ext['url'];
413 if ( isset( $ext['version'] ) ) {
414 $ret['version'] = $ext['version'];
415 } elseif ( isset( $ext['svn-revision'] ) &&
416 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
417 $ext['svn-revision'], $m ) )
419 $ret['version'] = 'r' . $m[1];
425 $this->getResult()->setIndexedTagName( $data, 'ext' );
426 return $this->getResult()->addValue( 'query', $property, $data );
430 protected function appendRightsInfo( $property ) {
431 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
432 $title = Title
::newFromText( $wgRightsPage );
433 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
434 $text = $wgRightsText;
435 if ( !$text && $title ) {
436 $text = $title->getPrefixedText();
440 'url' => $url ?
$url : '',
441 'text' => $text ?
$text : ''
444 return $this->getResult()->addValue( 'query', $property, $data );
447 public function appendLanguages( $property ) {
449 foreach ( Language
::getLanguageNames() as $code => $name ) {
450 $lang = array( 'code' => $code );
451 ApiResult
::setContent( $lang, $name );
454 $this->getResult()->setIndexedTagName( $data, 'lang' );
455 return $this->getResult()->addValue( 'query', $property, $data );
458 public function appendSkins( $property ) {
460 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
461 $skin = array( 'code' => $name );
462 ApiResult
::setContent( $skin, $displayName );
465 $this->getResult()->setIndexedTagName( $data, 'skin' );
466 return $this->getResult()->addValue( 'query', $property, $data );
469 public function getCacheMode( $params ) {
473 public function getAllowedParams() {
476 ApiBase
::PARAM_DFLT
=> 'general',
477 ApiBase
::PARAM_ISMULTI
=> true,
478 ApiBase
::PARAM_TYPE
=> array(
482 'specialpagealiases',
496 ApiBase
::PARAM_TYPE
=> array(
501 'showalldb' => false,
502 'numberingroup' => false,
506 public function getParamDescription() {
509 'Which sysinfo properties to get:',
510 ' general - Overall system information',
511 ' namespaces - List of registered namespaces and their canonical names',
512 ' namespacealiases - List of registered namespace aliases',
513 ' specialpagealiases - List of special page aliases',
514 ' magicwords - List of magic words and their aliases',
515 ' statistics - Returns site statistics',
516 ' interwikimap - Returns interwiki map (optionally filtered)',
517 ' dbrepllag - Returns database server with the highest replication lag',
518 ' usergroups - Returns user groups and the associated permissions',
519 ' extensions - Returns extensions installed on the wiki',
520 ' fileextensions - Returns list of file extensions allowed to be uploaded',
521 ' rightsinfo - Returns wiki rights (license) information if available',
522 ' languages - Returns a list of languages MediaWiki supports',
523 ' skins - Returns a list of all enabled skins',
525 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
526 'showalldb' => 'List all database servers, not just the one lagging the most',
527 'numberingroup' => 'Lists the number of users in user groups',
531 public function getDescription() {
532 return 'Return general information about the site';
535 public function getPossibleErrors() {
536 return array_merge( parent
::getPossibleErrors(), array(
537 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
541 protected function getExamples() {
543 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
544 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
545 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
549 public function getVersion() {
550 return __CLASS__
. ': $Id$';