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 if ( $GLOBALS['wgMiserMode'] ) {
164 $data['misermode'] = '';
167 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
169 return $this->getResult()->addValue( 'query', $property, $data );
172 protected function appendNamespaces( $property ) {
175 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
177 'id' => intval( $ns ),
178 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
180 ApiResult
::setContent( $data[$ns], $title );
181 $canonical = MWNamespace
::getCanonicalName( $ns );
183 if ( MWNamespace
::hasSubpages( $ns ) ) {
184 $data[$ns]['subpages'] = '';
188 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
191 if ( MWNamespace
::isContent( $ns ) ) {
192 $data[$ns]['content'] = '';
196 $this->getResult()->setIndexedTagName( $data, 'ns' );
197 return $this->getResult()->addValue( 'query', $property, $data );
200 protected function appendNamespaceAliases( $property ) {
201 global $wgNamespaceAliases, $wgContLang;
202 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
203 $namespaces = $wgContLang->getNamespaces();
205 foreach ( $aliases as $title => $ns ) {
206 if ( $namespaces[$ns] == $title ) {
207 // Don't list duplicates
211 'id' => intval( $ns )
213 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
217 $this->getResult()->setIndexedTagName( $data, 'ns' );
218 return $this->getResult()->addValue( 'query', $property, $data );
221 protected function appendSpecialPageAliases( $property ) {
224 foreach ( $wgContLang->getSpecialPageAliases() as $specialpage => $aliases )
226 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
227 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
230 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
231 return $this->getResult()->addValue( 'query', $property, $data );
234 protected function appendMagicWords( $property ) {
237 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
238 $caseSensitive = array_shift( $aliases );
239 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
240 if ( $caseSensitive ) {
241 $arr['case-sensitive'] = '';
243 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
246 $this->getResult()->setIndexedTagName( $data, 'magicword' );
247 return $this->getResult()->addValue( 'query', $property, $data );
250 protected function appendInterwikiMap( $property, $filter ) {
251 $this->resetQueryParams();
252 $this->addTables( 'interwiki' );
253 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
255 if ( $filter === 'local' ) {
256 $this->addWhere( 'iw_local = 1' );
257 } elseif ( $filter === '!local' ) {
258 $this->addWhere( 'iw_local = 0' );
259 } elseif ( $filter ) {
260 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
263 $this->addOption( 'ORDER BY', 'iw_prefix' );
265 $res = $this->select( __METHOD__
);
268 $langNames = Language
::getLanguageNames();
269 foreach ( $res as $row ) {
271 $val['prefix'] = $row->iw_prefix
;
272 if ( $row->iw_local
== '1' ) {
275 // $val['trans'] = intval( $row->iw_trans ); // should this be exposed?
276 if ( isset( $langNames[$row->iw_prefix
] ) ) {
277 $val['language'] = $langNames[$row->iw_prefix
];
279 $val['url'] = $row->iw_url
;
284 $this->getResult()->setIndexedTagName( $data, 'iw' );
285 return $this->getResult()->addValue( 'query', $property, $data );
288 protected function appendDbReplLagInfo( $property, $includeAll ) {
289 global $wgShowHostnames;
292 if ( !$wgShowHostnames ) {
293 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
297 $lags = $lb->getLagTimes();
298 foreach ( $lags as $i => $lag ) {
300 'host' => $lb->getServerName( $i ),
305 list( $host, $lag ) = wfGetLB()->getMaxLag();
307 'host' => $wgShowHostnames ?
$host : '',
308 'lag' => intval( $lag )
312 $result = $this->getResult();
313 $result->setIndexedTagName( $data, 'db' );
314 return $this->getResult()->addValue( 'query', $property, $data );
317 protected function appendStatistics( $property ) {
318 global $wgDisableCounters;
320 $data['pages'] = intval( SiteStats
::pages() );
321 $data['articles'] = intval( SiteStats
::articles() );
322 if ( !$wgDisableCounters ) {
323 $data['views'] = intval( SiteStats
::views() );
325 $data['edits'] = intval( SiteStats
::edits() );
326 $data['images'] = intval( SiteStats
::images() );
327 $data['users'] = intval( SiteStats
::users() );
328 $data['activeusers'] = intval( SiteStats
::activeUsers() );
329 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
330 $data['jobs'] = intval( SiteStats
::jobs() );
331 return $this->getResult()->addValue( 'query', $property, $data );
334 protected function appendUserGroups( $property, $numberInGroup ) {
335 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
338 foreach ( $wgGroupPermissions as $group => $permissions ) {
341 'rights' => array_keys( $permissions, true ),
344 if ( $numberInGroup ) {
345 global $wgAutopromote;
347 if ( $group == 'user' ) {
348 $arr['number'] = SiteStats
::users();
350 // '*' and autopromote groups have no size
351 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
352 $arr['number'] = SiteStats
::numberInGroup( $group );
357 'add' => $wgAddGroups,
358 'remove' => $wgRemoveGroups,
359 'add-self' => $wgGroupsAddToSelf,
360 'remove-self' => $wgGroupsRemoveFromSelf
363 foreach ( $groupArr as $type => $rights ) {
364 if ( isset( $rights[$group] ) ) {
365 $arr[$type] = $rights[$group];
366 $this->getResult()->setIndexedTagName( $arr[$type], 'group' );
370 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
374 $this->getResult()->setIndexedTagName( $data, 'group' );
375 return $this->getResult()->addValue( 'query', $property, $data );
378 protected function appendFileExtensions( $property ) {
379 global $wgFileExtensions;
382 foreach ( $wgFileExtensions as $ext ) {
383 $data[] = array( 'ext' => $ext );
385 $this->getResult()->setIndexedTagName( $data, 'fe' );
386 return $this->getResult()->addValue( 'query', $property, $data );
389 protected function appendExtensions( $property ) {
390 global $wgExtensionCredits;
392 foreach ( $wgExtensionCredits as $type => $extensions ) {
393 foreach ( $extensions as $ext ) {
395 $ret['type'] = $type;
396 if ( isset( $ext['name'] ) ) {
397 $ret['name'] = $ext['name'];
399 if ( isset( $ext['description'] ) ) {
400 $ret['description'] = $ext['description'];
402 if ( isset( $ext['descriptionmsg'] ) ) {
403 // Can be a string or array( key, param1, param2, ... )
404 if ( is_array( $ext['descriptionmsg'] ) ) {
405 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
406 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
407 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
409 $ret['descriptionmsg'] = $ext['descriptionmsg'];
412 if ( isset( $ext['author'] ) ) {
413 $ret['author'] = is_array( $ext['author'] ) ?
414 implode( ', ', $ext['author' ] ) : $ext['author'];
416 if ( isset( $ext['url'] ) ) {
417 $ret['url'] = $ext['url'];
419 if ( isset( $ext['version'] ) ) {
420 $ret['version'] = $ext['version'];
421 } elseif ( isset( $ext['svn-revision'] ) &&
422 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
423 $ext['svn-revision'], $m ) )
425 $ret['version'] = 'r' . $m[1];
431 $this->getResult()->setIndexedTagName( $data, 'ext' );
432 return $this->getResult()->addValue( 'query', $property, $data );
436 protected function appendRightsInfo( $property ) {
437 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
438 $title = Title
::newFromText( $wgRightsPage );
439 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
440 $text = $wgRightsText;
441 if ( !$text && $title ) {
442 $text = $title->getPrefixedText();
446 'url' => $url ?
$url : '',
447 'text' => $text ?
$text : ''
450 return $this->getResult()->addValue( 'query', $property, $data );
453 public function appendLanguages( $property ) {
455 foreach ( Language
::getLanguageNames() as $code => $name ) {
456 $lang = array( 'code' => $code );
457 ApiResult
::setContent( $lang, $name );
460 $this->getResult()->setIndexedTagName( $data, 'lang' );
461 return $this->getResult()->addValue( 'query', $property, $data );
464 public function appendSkins( $property ) {
466 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
467 $skin = array( 'code' => $name );
468 ApiResult
::setContent( $skin, $displayName );
471 $this->getResult()->setIndexedTagName( $data, 'skin' );
472 return $this->getResult()->addValue( 'query', $property, $data );
475 public function getCacheMode( $params ) {
479 public function getAllowedParams() {
482 ApiBase
::PARAM_DFLT
=> 'general',
483 ApiBase
::PARAM_ISMULTI
=> true,
484 ApiBase
::PARAM_TYPE
=> array(
488 'specialpagealiases',
502 ApiBase
::PARAM_TYPE
=> array(
507 'showalldb' => false,
508 'numberingroup' => false,
512 public function getParamDescription() {
515 'Which sysinfo properties to get:',
516 ' general - Overall system information',
517 ' namespaces - List of registered namespaces and their canonical names',
518 ' namespacealiases - List of registered namespace aliases',
519 ' specialpagealiases - List of special page aliases',
520 ' magicwords - List of magic words and their aliases',
521 ' statistics - Returns site statistics',
522 ' interwikimap - Returns interwiki map (optionally filtered)',
523 ' dbrepllag - Returns database server with the highest replication lag',
524 ' usergroups - Returns user groups and the associated permissions',
525 ' extensions - Returns extensions installed on the wiki',
526 ' fileextensions - Returns list of file extensions allowed to be uploaded',
527 ' rightsinfo - Returns wiki rights (license) information if available',
528 ' languages - Returns a list of languages MediaWiki supports',
529 ' skins - Returns a list of all enabled skins',
531 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
532 'showalldb' => 'List all database servers, not just the one lagging the most',
533 'numberingroup' => 'Lists the number of users in user groups',
537 public function getDescription() {
538 return 'Return general information about the site';
541 public function getPossibleErrors() {
542 return array_merge( parent
::getPossibleErrors(), array(
543 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
547 protected function getExamples() {
549 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
550 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
551 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
555 public function getVersion() {
556 return __CLASS__
. ': $Id$';