d8ef4b3ef20d38830378ede30c304966b459d187
4 * Created on Sep 25, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 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 )
50 $fit = $this->appendGeneralInfo( $p );
53 $fit = $this->appendNamespaces( $p );
55 case 'namespacealiases':
56 $fit = $this->appendNamespaceAliases( $p );
58 case 'specialpagealiases':
59 $fit = $this->appendSpecialPageAliases( $p );
62 $fit = $this->appendMagicWords( $p );
65 $filteriw = isset( $params['filteriw'] ) ?
$params['filteriw'] : false;
66 $fit = $this->appendInterwikiMap( $p, $filteriw );
69 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
72 $fit = $this->appendStatistics( $p );
75 $fit = $this->appendUserGroups( $p );
78 $fit = $this->appendExtensions( $p );
80 case 'fileextensions':
81 $fit = $this->appendFileExtensions( $p );
84 $fit = $this->appendRightsInfo( $p );
87 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 ) {
106 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
107 $data['mainpage'] = $mainPage->getPrefixedText();
108 $data['base'] = $mainPage->getFullUrl();
109 $data['sitename'] = $GLOBALS['wgSitename'];
110 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
111 $data['phpversion'] = phpversion();
112 $data['phpsapi'] = php_sapi_name();
113 $data['dbtype'] = $GLOBALS['wgDBtype'];
114 $data['dbversion'] = $this->getDB()->getServerVersion();
116 $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'];
125 $data['rights'] = $GLOBALS['wgRightsText'];
126 $data['lang'] = $GLOBALS['wgLanguageCode'];
127 if( $wgContLang->isRTL() )
129 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
132 $data['readonly'] = '';
133 $data['readonlyreason'] = wfReadOnlyReason();
135 if( $GLOBALS['wgEnableWriteAPI'] )
136 $data['writeapi'] = '';
138 $tz = $GLOBALS['wgLocaltimezone'];
139 $offset = $GLOBALS['wgLocalTZoffset'];
140 if( is_null( $tz ) ) {
143 } elseif( is_null( $offset ) ) {
146 $data['timezone'] = $tz;
147 $data['timeoffset'] = intval($offset);
148 $data['articlepath'] = $GLOBALS['wgArticlePath'];
149 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
150 $data['script'] = $GLOBALS['wgScript'];
151 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
152 $data['server'] = $GLOBALS['wgServer'];
153 $data['wikiid'] = wfWikiID();
155 return $this->getResult()->addValue( 'query', $property, $data );
158 protected function appendNamespaces( $property ) {
161 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
166 ApiResult
:: setContent( $data[$ns], $title );
167 $canonical = MWNamespace
::getCanonicalName( $ns );
169 if( MWNamespace
::hasSubpages( $ns ) )
170 $data[$ns]['subpages'] = '';
173 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
175 if( MWNamespace
::isContent( $ns ) )
176 $data[$ns]['content'] = '';
179 $this->getResult()->setIndexedTagName( $data, 'ns' );
180 return $this->getResult()->addValue( 'query', $property, $data );
183 protected function appendNamespaceAliases( $property ) {
184 global $wgNamespaceAliases, $wgContLang;
186 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->namespaceAliases
);
187 $namespaces = $wgContLang->getNamespaces();
189 foreach( $aliases as $title => $ns ) {
190 if( $namespaces[$ns] == $title ) {
191 // Don't list duplicates
197 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
201 $this->getResult()->setIndexedTagName( $data, 'ns' );
202 return $this->getResult()->addValue( 'query', $property, $data );
205 protected function appendSpecialPageAliases( $property ) {
208 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
210 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
211 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
214 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
215 return $this->getResult()->addValue( 'query', $property, $data );
218 protected function appendMagicWords( $property ) {
221 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
223 $caseSensitive = array_shift($aliases);
224 $arr = array('name' => $magicword, 'aliases' => $aliases);
226 $arr['case-sensitive'] = '';
227 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
230 $this->getResult()->setIndexedTagName($data, 'magicword');
231 return $this->getResult()->addValue( 'query', $property, $data );
234 protected function appendInterwikiMap( $property, $filter ) {
235 $this->resetQueryParams();
236 $this->addTables( 'interwiki' );
237 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
239 if( $filter === 'local' )
240 $this->addWhere( 'iw_local = 1' );
241 elseif( $filter === '!local' )
242 $this->addWhere( 'iw_local = 0' );
244 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
246 $this->addOption( 'ORDER BY', 'iw_prefix' );
248 $db = $this->getDB();
249 $res = $this->select( __METHOD__
);
252 $langNames = Language
::getLanguageNames();
253 while( $row = $db->fetchObject($res) )
256 $val['prefix'] = $row->iw_prefix
;
257 if( $row->iw_local
== '1' )
259 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
260 if( isset( $langNames[$row->iw_prefix
] ) )
261 $val['language'] = $langNames[$row->iw_prefix
];
262 $val['url'] = $row->iw_url
;
266 $db->freeResult( $res );
268 $this->getResult()->setIndexedTagName( $data, 'iw' );
269 return $this->getResult()->addValue( 'query', $property, $data );
272 protected function appendDbReplLagInfo( $property, $includeAll ) {
273 global $wgShowHostnames;
276 if ( !$wgShowHostnames )
277 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
280 $lags = $lb->getLagTimes();
281 foreach( $lags as $i => $lag ) {
283 'host' => $lb->getServerName( $i ),
288 list( $host, $lag ) = wfGetLB()->getMaxLag();
290 'host' => $wgShowHostnames ?
$host : '',
291 'lag' => intval( $lag )
295 $result = $this->getResult();
296 $result->setIndexedTagName( $data, 'db' );
297 return $this->getResult()->addValue( 'query', $property, $data );
300 protected function appendStatistics( $property ) {
301 global $wgDisableCounters;
303 $data['pages'] = intval( SiteStats
::pages() );
304 $data['articles'] = intval( SiteStats
::articles() );
305 if ( !$wgDisableCounters ) {
306 $data['views'] = intval( SiteStats
::views() );
308 $data['edits'] = intval( SiteStats
::edits() );
309 $data['images'] = intval( SiteStats
::images() );
310 $data['users'] = intval( SiteStats
::users() );
311 $data['activeusers'] = intval( SiteStats
::activeUsers() );
312 $data['admins'] = intval( SiteStats
::numberingroup('sysop') );
313 $data['jobs'] = intval( SiteStats
::jobs() );
314 return $this->getResult()->addValue( 'query', $property, $data );
317 protected function appendUserGroups( $property ) {
318 global $wgGroupPermissions;
320 foreach( $wgGroupPermissions as $group => $permissions ) {
321 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
322 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
326 $this->getResult()->setIndexedTagName( $data, 'group' );
327 return $this->getResult()->addValue( 'query', $property, $data );
330 protected function appendFileExtensions( $property ) {
331 global $wgFileExtensions;
334 foreach( $wgFileExtensions as $ext ) {
335 $data[] = array( 'ext' => $ext );
337 $this->getResult()->setIndexedTagName( $data, 'fe' );
338 return $this->getResult()->addValue( 'query', $property, $data );
341 protected function appendExtensions( $property ) {
342 global $wgExtensionCredits;
344 foreach ( $wgExtensionCredits as $type => $extensions ) {
345 foreach ( $extensions as $ext ) {
347 $ret['type'] = $type;
348 if ( isset( $ext['name'] ) )
349 $ret['name'] = $ext['name'];
350 if ( isset( $ext['description'] ) )
351 $ret['description'] = $ext['description'];
352 if ( isset( $ext['descriptionmsg'] ) )
353 $ret['descriptionmsg'] = $ext['descriptionmsg'];
354 if ( isset( $ext['author'] ) ) {
355 $ret['author'] = is_array( $ext['author'] ) ?
356 implode( ', ', $ext['author' ] ) : $ext['author'];
358 if ( isset( $ext['version'] ) ) {
359 $ret['version'] = $ext['version'];
360 } elseif ( isset( $ext['svn-revision'] ) &&
361 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
362 $ext['svn-revision'], $m ) )
364 $ret['version'] = 'r' . $m[1];
370 $this->getResult()->setIndexedTagName( $data, 'ext' );
371 return $this->getResult()->addValue( 'query', $property, $data );
375 protected function appendRightsInfo( $property ) {
376 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
377 $title = Title
::newFromText( $wgRightsPage );
378 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
379 $text = $wgRightsText;
380 if( !$text && $title ) {
381 $text = $title->getPrefixedText();
385 'url' => $url ?
$url : '',
386 'text' => $text ?
$text : ''
389 return $this->getResult()->addValue( 'query', $property, $data );
393 public function getAllowedParams() {
396 ApiBase
:: PARAM_DFLT
=> 'general',
397 ApiBase
:: PARAM_ISMULTI
=> true,
398 ApiBase
:: PARAM_TYPE
=> array(
402 'specialpagealiases',
414 ApiBase
:: PARAM_TYPE
=> array(
419 'showalldb' => false,
423 public function getParamDescription() {
426 'Which sysinfo properties to get:',
427 ' general - Overall system information',
428 ' namespaces - List of registered namespaces and their canonical names',
429 ' namespacealiases - List of registered namespace aliases',
430 ' specialpagealiases - List of special page aliases',
431 ' magicwords - List of magic words and their aliases',
432 ' statistics - Returns site statistics',
433 ' interwikimap - Returns interwiki map (optionally filtered)',
434 ' dbrepllag - Returns database server with the highest replication lag',
435 ' usergroups - Returns user groups and the associated permissions',
436 ' extensions - Returns extensions installed on the wiki',
437 ' fileextensions - Returns list of file extensions allowed to be uploaded',
438 ' rightsinfo - Returns wiki rights (license) information if available',
440 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
441 'showalldb' => 'List all database servers, not just the one lagging the most',
445 public function getDescription() {
446 return 'Return general information about the site.';
449 protected function getExamples() {
451 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
452 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
453 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
457 public function getVersion() {
458 return __CLASS__
. ': $Id$';