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
::newMainPage();
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
;
275 $this->getResult()->setIndexedTagName( $data, 'iw' );
276 return $this->getResult()->addValue( 'query', $property, $data );
279 protected function appendDbReplLagInfo( $property, $includeAll ) {
280 global $wgShowHostnames;
283 if ( !$wgShowHostnames ) {
284 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
288 $lags = $lb->getLagTimes();
289 foreach ( $lags as $i => $lag ) {
291 'host' => $lb->getServerName( $i ),
296 list( $host, $lag ) = wfGetLB()->getMaxLag();
298 'host' => $wgShowHostnames ?
$host : '',
299 'lag' => intval( $lag )
303 $result = $this->getResult();
304 $result->setIndexedTagName( $data, 'db' );
305 return $this->getResult()->addValue( 'query', $property, $data );
308 protected function appendStatistics( $property ) {
309 global $wgDisableCounters;
311 $data['pages'] = intval( SiteStats
::pages() );
312 $data['articles'] = intval( SiteStats
::articles() );
313 if ( !$wgDisableCounters ) {
314 $data['views'] = intval( SiteStats
::views() );
316 $data['edits'] = intval( SiteStats
::edits() );
317 $data['images'] = intval( SiteStats
::images() );
318 $data['users'] = intval( SiteStats
::users() );
319 $data['activeusers'] = intval( SiteStats
::activeUsers() );
320 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
321 $data['jobs'] = intval( SiteStats
::jobs() );
322 return $this->getResult()->addValue( 'query', $property, $data );
325 protected function appendUserGroups( $property, $numberInGroup ) {
326 global $wgGroupPermissions;
328 foreach ( $wgGroupPermissions as $group => $permissions ) {
331 'rights' => array_keys( $permissions, true ),
333 if ( $numberInGroup ) {
334 $arr['number'] = SiteStats
::numberInGroup( $group );
337 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
341 $this->getResult()->setIndexedTagName( $data, 'group' );
342 return $this->getResult()->addValue( 'query', $property, $data );
345 protected function appendFileExtensions( $property ) {
346 global $wgFileExtensions;
349 foreach ( $wgFileExtensions as $ext ) {
350 $data[] = array( 'ext' => $ext );
352 $this->getResult()->setIndexedTagName( $data, 'fe' );
353 return $this->getResult()->addValue( 'query', $property, $data );
356 protected function appendExtensions( $property ) {
357 global $wgExtensionCredits;
359 foreach ( $wgExtensionCredits as $type => $extensions ) {
360 foreach ( $extensions as $ext ) {
362 $ret['type'] = $type;
363 if ( isset( $ext['name'] ) ) {
364 $ret['name'] = $ext['name'];
366 if ( isset( $ext['description'] ) ) {
367 $ret['description'] = $ext['description'];
369 if ( isset( $ext['descriptionmsg'] ) ) {
370 // Can be a string or array( key, param1, param2, ... )
371 if ( is_array( $ext['descriptionmsg'] ) ) {
372 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
373 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
374 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
376 $ret['descriptionmsg'] = $ext['descriptionmsg'];
379 if ( isset( $ext['author'] ) ) {
380 $ret['author'] = is_array( $ext['author'] ) ?
381 implode( ', ', $ext['author' ] ) : $ext['author'];
383 if ( isset( $ext['url'] ) ) {
384 $ret['url'] = $ext['url'];
386 if ( isset( $ext['version'] ) ) {
387 $ret['version'] = $ext['version'];
388 } elseif ( isset( $ext['svn-revision'] ) &&
389 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
390 $ext['svn-revision'], $m ) )
392 $ret['version'] = 'r' . $m[1];
398 $this->getResult()->setIndexedTagName( $data, 'ext' );
399 return $this->getResult()->addValue( 'query', $property, $data );
403 protected function appendRightsInfo( $property ) {
404 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
405 $title = Title
::newFromText( $wgRightsPage );
406 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
407 $text = $wgRightsText;
408 if ( !$text && $title ) {
409 $text = $title->getPrefixedText();
413 'url' => $url ?
$url : '',
414 'text' => $text ?
$text : ''
417 return $this->getResult()->addValue( 'query', $property, $data );
420 public function appendLanguages( $property ) {
422 foreach ( Language
::getLanguageNames() as $code => $name ) {
423 $lang = array( 'code' => $code );
424 ApiResult
::setContent( $lang, $name );
427 $this->getResult()->setIndexedTagName( $data, 'lang' );
428 return $this->getResult()->addValue( 'query', $property, $data );
431 public function getAllowedParams() {
434 ApiBase
::PARAM_DFLT
=> 'general',
435 ApiBase
::PARAM_ISMULTI
=> true,
436 ApiBase
::PARAM_TYPE
=> array(
440 'specialpagealiases',
453 ApiBase
::PARAM_TYPE
=> array(
458 'showalldb' => false,
459 'numberingroup' => false,
463 public function getParamDescription() {
466 'Which sysinfo properties to get:',
467 ' general - Overall system information',
468 ' namespaces - List of registered namespaces and their canonical names',
469 ' namespacealiases - List of registered namespace aliases',
470 ' specialpagealiases - List of special page aliases',
471 ' magicwords - List of magic words and their aliases',
472 ' statistics - Returns site statistics',
473 ' interwikimap - Returns interwiki map (optionally filtered)',
474 ' dbrepllag - Returns database server with the highest replication lag',
475 ' usergroups - Returns user groups and the associated permissions',
476 ' extensions - Returns extensions installed on the wiki',
477 ' fileextensions - Returns list of file extensions allowed to be uploaded',
478 ' rightsinfo - Returns wiki rights (license) information if available',
479 ' languages - Returns a list of languages MediaWiki supports',
481 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
482 'showalldb' => 'List all database servers, not just the one lagging the most',
483 'numberingroup' => 'Lists the number of users in user groups',
487 public function getDescription() {
488 return 'Return general information about the site';
491 public function getPossibleErrors() {
492 return array_merge( parent
::getPossibleErrors(), array(
493 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
497 protected function getExamples() {
499 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
500 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
501 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
505 public function getVersion() {
506 return __CLASS__
. ': $Id$';