4 * Created on Sep 25, 2006
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
32 * A query action to return meta information about the wiki site.
36 class ApiQuerySiteinfo
extends ApiQueryBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
::__construct( $query, $moduleName, 'si' );
42 public function execute() {
43 $params = $this->extractRequestParams();
45 foreach ( $params['prop'] as $p ) {
48 $fit = $this->appendGeneralInfo( $p );
51 $fit = $this->appendNamespaces( $p );
53 case 'namespacealiases':
54 $fit = $this->appendNamespaceAliases( $p );
56 case 'specialpagealiases':
57 $fit = $this->appendSpecialPageAliases( $p );
60 $fit = $this->appendMagicWords( $p );
63 $filteriw = isset( $params['filteriw'] ) ?
$params['filteriw'] : false;
64 $fit = $this->appendInterwikiMap( $p, $filteriw );
67 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
70 $fit = $this->appendStatistics( $p );
73 $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
76 $fit = $this->appendExtensions( $p );
78 case 'fileextensions':
79 $fit = $this->appendFileExtensions( $p );
82 $fit = $this->appendRightsInfo( $p );
85 $fit = $this->appendLanguages( $p );
88 ApiBase
::dieDebug( __METHOD__
, "Unknown prop=$p" );
91 // Abuse siprop as a query-continue parameter
92 // and set it to all unprocessed props
93 $this->setContinueEnumParameter( 'prop', implode( '|',
94 array_diff( $params['prop'], $done ) ) );
101 protected function appendGeneralInfo( $property ) {
102 global $wgContLang, $wgLang;
105 $mainPage = Title
::newFromText( wfMsgForContent( 'mainpage' ) );
106 $data['mainpage'] = $mainPage->getPrefixedText();
107 $data['base'] = $mainPage->getFullUrl();
108 $data['sitename'] = $GLOBALS['wgSitename'];
109 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
110 $data['phpversion'] = phpversion();
111 $data['phpsapi'] = php_sapi_name();
112 $data['dbtype'] = $GLOBALS['wgDBtype'];
113 $data['dbversion'] = $this->getDB()->getServerVersion();
115 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
120 // 'case-insensitive' option is reserved for future
121 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
123 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
124 $data['rightscode'] = $GLOBALS['wgRightsCode'];
126 $data['rights'] = $GLOBALS['wgRightsText'];
127 $data['lang'] = $GLOBALS['wgLanguageCode'];
128 if ( $wgContLang->isRTL() ) {
131 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
133 if ( wfReadOnly() ) {
134 $data['readonly'] = '';
135 $data['readonlyreason'] = wfReadOnlyReason();
137 if ( $GLOBALS['wgEnableWriteAPI'] ) {
138 $data['writeapi'] = '';
141 $tz = $GLOBALS['wgLocaltimezone'];
142 $offset = $GLOBALS['wgLocalTZoffset'];
143 if ( is_null( $tz ) ) {
146 } elseif ( is_null( $offset ) ) {
149 $data['timezone'] = $tz;
150 $data['timeoffset'] = intval( $offset );
151 $data['articlepath'] = $GLOBALS['wgArticlePath'];
152 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
153 $data['script'] = $GLOBALS['wgScript'];
154 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
155 $data['server'] = $GLOBALS['wgServer'];
156 $data['wikiid'] = wfWikiID();
157 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
159 return $this->getResult()->addValue( 'query', $property, $data );
162 protected function appendNamespaces( $property ) {
165 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
167 'id' => intval( $ns ),
168 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
170 ApiResult
::setContent( $data[$ns], $title );
171 $canonical = MWNamespace
::getCanonicalName( $ns );
173 if ( MWNamespace
::hasSubpages( $ns ) ) {
174 $data[$ns]['subpages'] = '';
178 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
181 if ( MWNamespace
::isContent( $ns ) ) {
182 $data[$ns]['content'] = '';
186 $this->getResult()->setIndexedTagName( $data, 'ns' );
187 return $this->getResult()->addValue( 'query', $property, $data );
190 protected function appendNamespaceAliases( $property ) {
191 global $wgNamespaceAliases, $wgContLang;
192 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
193 $namespaces = $wgContLang->getNamespaces();
195 foreach ( $aliases as $title => $ns ) {
196 if ( $namespaces[$ns] == $title ) {
197 // Don't list duplicates
201 'id' => intval( $ns )
203 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
207 $this->getResult()->setIndexedTagName( $data, 'ns' );
208 return $this->getResult()->addValue( 'query', $property, $data );
211 protected function appendSpecialPageAliases( $property ) {
214 foreach ( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
216 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
217 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
220 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
221 return $this->getResult()->addValue( 'query', $property, $data );
224 protected function appendMagicWords( $property ) {
227 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
228 $caseSensitive = array_shift( $aliases );
229 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
230 if ( $caseSensitive ) {
231 $arr['case-sensitive'] = '';
233 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
236 $this->getResult()->setIndexedTagName( $data, 'magicword' );
237 return $this->getResult()->addValue( 'query', $property, $data );
240 protected function appendInterwikiMap( $property, $filter ) {
241 $this->resetQueryParams();
242 $this->addTables( 'interwiki' );
243 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
245 if ( $filter === 'local' ) {
246 $this->addWhere( 'iw_local = 1' );
247 } elseif ( $filter === '!local' ) {
248 $this->addWhere( 'iw_local = 0' );
249 } elseif ( $filter ) {
250 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
253 $this->addOption( 'ORDER BY', 'iw_prefix' );
255 $db = $this->getDB();
256 $res = $this->select( __METHOD__
);
259 $langNames = Language
::getLanguageNames();
260 while ( $row = $db->fetchObject( $res ) ) {
262 $val['prefix'] = $row->iw_prefix
;
263 if ( $row->iw_local
== '1' ) {
266 // $val['trans'] = intval( $row->iw_trans ); // should this be exposed?
267 if ( isset( $langNames[$row->iw_prefix
] ) ) {
268 $val['language'] = $langNames[$row->iw_prefix
];
270 $val['url'] = $row->iw_url
;
274 $db->freeResult( $res );
276 $this->getResult()->setIndexedTagName( $data, 'iw' );
277 return $this->getResult()->addValue( 'query', $property, $data );
280 protected function appendDbReplLagInfo( $property, $includeAll ) {
281 global $wgShowHostnames;
284 if ( !$wgShowHostnames ) {
285 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
289 $lags = $lb->getLagTimes();
290 foreach ( $lags as $i => $lag ) {
292 'host' => $lb->getServerName( $i ),
297 list( $host, $lag ) = wfGetLB()->getMaxLag();
299 'host' => $wgShowHostnames ?
$host : '',
300 'lag' => intval( $lag )
304 $result = $this->getResult();
305 $result->setIndexedTagName( $data, 'db' );
306 return $this->getResult()->addValue( 'query', $property, $data );
309 protected function appendStatistics( $property ) {
310 global $wgDisableCounters;
312 $data['pages'] = intval( SiteStats
::pages() );
313 $data['articles'] = intval( SiteStats
::articles() );
314 if ( !$wgDisableCounters ) {
315 $data['views'] = intval( SiteStats
::views() );
317 $data['edits'] = intval( SiteStats
::edits() );
318 $data['images'] = intval( SiteStats
::images() );
319 $data['users'] = intval( SiteStats
::users() );
320 $data['activeusers'] = intval( SiteStats
::activeUsers() );
321 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
322 $data['jobs'] = intval( SiteStats
::jobs() );
323 return $this->getResult()->addValue( 'query', $property, $data );
326 protected function appendUserGroups( $property, $numberInGroup ) {
327 global $wgGroupPermissions;
329 foreach ( $wgGroupPermissions as $group => $permissions ) {
332 'rights' => array_keys( $permissions, true ),
334 if ( $numberInGroup ) {
335 $arr['number'] = SiteStats
::numberInGroup( $group );
338 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
342 $this->getResult()->setIndexedTagName( $data, 'group' );
343 return $this->getResult()->addValue( 'query', $property, $data );
346 protected function appendFileExtensions( $property ) {
347 global $wgFileExtensions;
350 foreach ( $wgFileExtensions as $ext ) {
351 $data[] = array( 'ext' => $ext );
353 $this->getResult()->setIndexedTagName( $data, 'fe' );
354 return $this->getResult()->addValue( 'query', $property, $data );
357 protected function appendExtensions( $property ) {
358 global $wgExtensionCredits;
360 foreach ( $wgExtensionCredits as $type => $extensions ) {
361 foreach ( $extensions as $ext ) {
363 $ret['type'] = $type;
364 if ( isset( $ext['name'] ) ) {
365 $ret['name'] = $ext['name'];
367 if ( isset( $ext['description'] ) ) {
368 $ret['description'] = $ext['description'];
370 if ( isset( $ext['descriptionmsg'] ) ) {
371 // Can be a string or array( key, param1, param2, ... )
372 if ( is_array( $ext['descriptionmsg'] ) ) {
373 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
374 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
375 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
377 $ret['descriptionmsg'] = $ext['descriptionmsg'];
380 if ( isset( $ext['author'] ) ) {
381 $ret['author'] = is_array( $ext['author'] ) ?
382 implode( ', ', $ext['author' ] ) : $ext['author'];
384 if ( isset( $ext['url'] ) ) {
385 $ret['url'] = $ext['url'];
387 if ( isset( $ext['version'] ) ) {
388 $ret['version'] = $ext['version'];
389 } elseif ( isset( $ext['svn-revision'] ) &&
390 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
391 $ext['svn-revision'], $m ) )
393 $ret['version'] = 'r' . $m[1];
399 $this->getResult()->setIndexedTagName( $data, 'ext' );
400 return $this->getResult()->addValue( 'query', $property, $data );
404 protected function appendRightsInfo( $property ) {
405 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
406 $title = Title
::newFromText( $wgRightsPage );
407 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
408 $text = $wgRightsText;
409 if ( !$text && $title ) {
410 $text = $title->getPrefixedText();
414 'url' => $url ?
$url : '',
415 'text' => $text ?
$text : ''
418 return $this->getResult()->addValue( 'query', $property, $data );
421 public function appendLanguages( $property ) {
423 foreach ( Language
::getLanguageNames() as $code => $name ) {
424 $lang = array( 'code' => $code );
425 ApiResult
::setContent( $lang, $name );
428 $this->getResult()->setIndexedTagName( $data, 'lang' );
429 return $this->getResult()->addValue( 'query', $property, $data );
432 public function getAllowedParams() {
435 ApiBase
::PARAM_DFLT
=> 'general',
436 ApiBase
::PARAM_ISMULTI
=> true,
437 ApiBase
::PARAM_TYPE
=> array(
441 'specialpagealiases',
454 ApiBase
::PARAM_TYPE
=> array(
459 'showalldb' => false,
460 'numberingroup' => false,
464 public function getParamDescription() {
467 'Which sysinfo properties to get:',
468 ' general - Overall system information',
469 ' namespaces - List of registered namespaces and their canonical names',
470 ' namespacealiases - List of registered namespace aliases',
471 ' specialpagealiases - List of special page aliases',
472 ' magicwords - List of magic words and their aliases',
473 ' statistics - Returns site statistics',
474 ' interwikimap - Returns interwiki map (optionally filtered)',
475 ' dbrepllag - Returns database server with the highest replication lag',
476 ' usergroups - Returns user groups and the associated permissions',
477 ' extensions - Returns extensions installed on the wiki',
478 ' fileextensions - Returns list of file extensions allowed to be uploaded',
479 ' rightsinfo - Returns wiki rights (license) information if available',
480 ' languages - Returns a list of languages MediaWiki supports',
482 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
483 'showalldb' => 'List all database servers, not just the one lagging the most',
484 'numberingroup' => 'Lists the number of users in user groups',
488 public function getDescription() {
489 return 'Return general information about the site.';
492 public function getPossibleErrors() {
493 return array_merge( parent
::getPossibleErrors(), array(
494 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
498 protected function getExamples() {
500 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
501 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
502 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
506 public function getVersion() {
507 return __CLASS__
. ': $Id$';