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 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
165 return $this->getResult()->addValue( 'query', $property, $data );
168 protected function appendNamespaces( $property ) {
171 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
173 'id' => intval( $ns ),
174 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
176 ApiResult
::setContent( $data[$ns], $title );
177 $canonical = MWNamespace
::getCanonicalName( $ns );
179 if ( MWNamespace
::hasSubpages( $ns ) ) {
180 $data[$ns]['subpages'] = '';
184 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
187 if ( MWNamespace
::isContent( $ns ) ) {
188 $data[$ns]['content'] = '';
192 $this->getResult()->setIndexedTagName( $data, 'ns' );
193 return $this->getResult()->addValue( 'query', $property, $data );
196 protected function appendNamespaceAliases( $property ) {
197 global $wgNamespaceAliases, $wgContLang;
198 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
199 $namespaces = $wgContLang->getNamespaces();
201 foreach ( $aliases as $title => $ns ) {
202 if ( $namespaces[$ns] == $title ) {
203 // Don't list duplicates
207 'id' => intval( $ns )
209 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
213 $this->getResult()->setIndexedTagName( $data, 'ns' );
214 return $this->getResult()->addValue( 'query', $property, $data );
217 protected function appendSpecialPageAliases( $property ) {
220 foreach ( $wgContLang->getSpecialPageAliases() as $specialpage => $aliases )
222 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
223 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
226 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
227 return $this->getResult()->addValue( 'query', $property, $data );
230 protected function appendMagicWords( $property ) {
233 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
234 $caseSensitive = array_shift( $aliases );
235 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
236 if ( $caseSensitive ) {
237 $arr['case-sensitive'] = '';
239 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
242 $this->getResult()->setIndexedTagName( $data, 'magicword' );
243 return $this->getResult()->addValue( 'query', $property, $data );
246 protected function appendInterwikiMap( $property, $filter ) {
247 $this->resetQueryParams();
248 $this->addTables( 'interwiki' );
249 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
251 if ( $filter === 'local' ) {
252 $this->addWhere( 'iw_local = 1' );
253 } elseif ( $filter === '!local' ) {
254 $this->addWhere( 'iw_local = 0' );
255 } elseif ( $filter ) {
256 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
259 $this->addOption( 'ORDER BY', 'iw_prefix' );
261 $res = $this->select( __METHOD__
);
264 $langNames = Language
::getLanguageNames();
265 foreach ( $res as $row ) {
267 $val['prefix'] = $row->iw_prefix
;
268 if ( $row->iw_local
== '1' ) {
271 // $val['trans'] = intval( $row->iw_trans ); // should this be exposed?
272 if ( isset( $langNames[$row->iw_prefix
] ) ) {
273 $val['language'] = $langNames[$row->iw_prefix
];
275 $val['url'] = $row->iw_url
;
280 $this->getResult()->setIndexedTagName( $data, 'iw' );
281 return $this->getResult()->addValue( 'query', $property, $data );
284 protected function appendDbReplLagInfo( $property, $includeAll ) {
285 global $wgShowHostnames;
288 if ( !$wgShowHostnames ) {
289 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
293 $lags = $lb->getLagTimes();
294 foreach ( $lags as $i => $lag ) {
296 'host' => $lb->getServerName( $i ),
301 list( $host, $lag ) = wfGetLB()->getMaxLag();
303 'host' => $wgShowHostnames ?
$host : '',
304 'lag' => intval( $lag )
308 $result = $this->getResult();
309 $result->setIndexedTagName( $data, 'db' );
310 return $this->getResult()->addValue( 'query', $property, $data );
313 protected function appendStatistics( $property ) {
314 global $wgDisableCounters;
316 $data['pages'] = intval( SiteStats
::pages() );
317 $data['articles'] = intval( SiteStats
::articles() );
318 if ( !$wgDisableCounters ) {
319 $data['views'] = intval( SiteStats
::views() );
321 $data['edits'] = intval( SiteStats
::edits() );
322 $data['images'] = intval( SiteStats
::images() );
323 $data['users'] = intval( SiteStats
::users() );
324 $data['activeusers'] = intval( SiteStats
::activeUsers() );
325 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
326 $data['jobs'] = intval( SiteStats
::jobs() );
327 return $this->getResult()->addValue( 'query', $property, $data );
330 protected function appendUserGroups( $property, $numberInGroup ) {
331 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
334 foreach ( $wgGroupPermissions as $group => $permissions ) {
337 'rights' => array_keys( $permissions, true ),
340 if ( $numberInGroup ) {
341 global $wgAutopromote;
343 if ( $group == 'user' ) {
344 $arr['number'] = SiteStats
::users();
346 // '*' and autopromote groups have no size
347 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
348 $arr['number'] = SiteStats
::numberInGroup( $group );
353 'add' => $wgAddGroups,
354 'remove' => $wgRemoveGroups,
355 'add-self' => $wgGroupsAddToSelf,
356 'remove-self' => $wgGroupsRemoveFromSelf
359 foreach ( $groupArr as $type => $rights ) {
360 if ( isset( $rights[$group] ) ) {
361 $arr[$type] = $rights[$group];
362 $this->getResult()->setIndexedTagName( $arr[$type], 'group' );
366 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
370 $this->getResult()->setIndexedTagName( $data, 'group' );
371 return $this->getResult()->addValue( 'query', $property, $data );
374 protected function appendFileExtensions( $property ) {
375 global $wgFileExtensions;
378 foreach ( $wgFileExtensions as $ext ) {
379 $data[] = array( 'ext' => $ext );
381 $this->getResult()->setIndexedTagName( $data, 'fe' );
382 return $this->getResult()->addValue( 'query', $property, $data );
385 protected function appendExtensions( $property ) {
386 global $wgExtensionCredits;
388 foreach ( $wgExtensionCredits as $type => $extensions ) {
389 foreach ( $extensions as $ext ) {
391 $ret['type'] = $type;
392 if ( isset( $ext['name'] ) ) {
393 $ret['name'] = $ext['name'];
395 if ( isset( $ext['description'] ) ) {
396 $ret['description'] = $ext['description'];
398 if ( isset( $ext['descriptionmsg'] ) ) {
399 // Can be a string or array( key, param1, param2, ... )
400 if ( is_array( $ext['descriptionmsg'] ) ) {
401 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
402 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
403 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
405 $ret['descriptionmsg'] = $ext['descriptionmsg'];
408 if ( isset( $ext['author'] ) ) {
409 $ret['author'] = is_array( $ext['author'] ) ?
410 implode( ', ', $ext['author' ] ) : $ext['author'];
412 if ( isset( $ext['url'] ) ) {
413 $ret['url'] = $ext['url'];
415 if ( isset( $ext['version'] ) ) {
416 $ret['version'] = $ext['version'];
417 } elseif ( isset( $ext['svn-revision'] ) &&
418 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
419 $ext['svn-revision'], $m ) )
421 $ret['version'] = 'r' . $m[1];
427 $this->getResult()->setIndexedTagName( $data, 'ext' );
428 return $this->getResult()->addValue( 'query', $property, $data );
432 protected function appendRightsInfo( $property ) {
433 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
434 $title = Title
::newFromText( $wgRightsPage );
435 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
436 $text = $wgRightsText;
437 if ( !$text && $title ) {
438 $text = $title->getPrefixedText();
442 'url' => $url ?
$url : '',
443 'text' => $text ?
$text : ''
446 return $this->getResult()->addValue( 'query', $property, $data );
449 public function appendLanguages( $property ) {
451 foreach ( Language
::getLanguageNames() as $code => $name ) {
452 $lang = array( 'code' => $code );
453 ApiResult
::setContent( $lang, $name );
456 $this->getResult()->setIndexedTagName( $data, 'lang' );
457 return $this->getResult()->addValue( 'query', $property, $data );
460 public function appendSkins( $property ) {
462 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
463 $skin = array( 'code' => $name );
464 ApiResult
::setContent( $skin, $displayName );
467 $this->getResult()->setIndexedTagName( $data, 'skin' );
468 return $this->getResult()->addValue( 'query', $property, $data );
471 public function getCacheMode( $params ) {
475 public function getAllowedParams() {
478 ApiBase
::PARAM_DFLT
=> 'general',
479 ApiBase
::PARAM_ISMULTI
=> true,
480 ApiBase
::PARAM_TYPE
=> array(
484 'specialpagealiases',
498 ApiBase
::PARAM_TYPE
=> array(
503 'showalldb' => false,
504 'numberingroup' => false,
508 public function getParamDescription() {
511 'Which sysinfo properties to get:',
512 ' general - Overall system information',
513 ' namespaces - List of registered namespaces and their canonical names',
514 ' namespacealiases - List of registered namespace aliases',
515 ' specialpagealiases - List of special page aliases',
516 ' magicwords - List of magic words and their aliases',
517 ' statistics - Returns site statistics',
518 ' interwikimap - Returns interwiki map (optionally filtered)',
519 ' dbrepllag - Returns database server with the highest replication lag',
520 ' usergroups - Returns user groups and the associated permissions',
521 ' extensions - Returns extensions installed on the wiki',
522 ' fileextensions - Returns list of file extensions allowed to be uploaded',
523 ' rightsinfo - Returns wiki rights (license) information if available',
524 ' languages - Returns a list of languages MediaWiki supports',
525 ' skins - Returns a list of all enabled skins',
527 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
528 'showalldb' => 'List all database servers, not just the one lagging the most',
529 'numberingroup' => 'Lists the number of users in user groups',
533 public function getDescription() {
534 return 'Return general information about the site';
537 public function getPossibleErrors() {
538 return array_merge( parent
::getPossibleErrors(), array(
539 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
543 protected function getExamples() {
545 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
546 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
547 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
551 public function getVersion() {
552 return __CLASS__
. ': $Id$';