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 ) {
102 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgContLang;
103 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
106 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
107 $data['mainpage'] = $mainPage->getPrefixedText();
108 $data['base'] = $mainPage->getFullUrl();
109 $data['sitename'] = $wgSitename;
110 $data['generator'] = "MediaWiki $wgVersion";
112 $svn = SpecialVersion
::getSvnRevision( $IP );
116 $data['case'] = $wgCapitalLinks ?
'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
118 if( isset( $wgRightsCode ) )
119 $data['rightscode'] = $wgRightsCode;
120 $data['rights'] = $wgRightsText;
121 $data['lang'] = $wgLanguageCode;
122 if( $wgContLang->isRTL() )
124 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
127 $data['readonly'] = '';
128 if( $wgEnableWriteAPI )
129 $data['writeapi'] = '';
131 $tz = $wgLocaltimezone;
132 $offset = $wgLocalTZoffset;
133 if( is_null( $tz ) ) {
136 } elseif( is_null( $offset ) ) {
139 $data['timezone'] = $tz;
140 $data['timeoffset'] = $offset;
142 return $this->getResult()->addValue( 'query', $property, $data );
145 protected function appendNamespaces( $property ) {
148 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
153 ApiResult
:: setContent( $data[$ns], $title );
154 $canonical = MWNamespace
::getCanonicalName( $ns );
156 if( MWNamespace
::hasSubpages( $ns ) )
157 $data[$ns]['subpages'] = '';
160 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
163 $this->getResult()->setIndexedTagName( $data, 'ns' );
164 return $this->getResult()->addValue( 'query', $property, $data );
167 protected function appendNamespaceAliases( $property ) {
168 global $wgNamespaceAliases, $wgContLang;
170 $aliases = array_merge($wgNamespaceAliases, $wgContLang->namespaceAliases
);
172 foreach( $aliases as $title => $ns ) {
176 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
180 $this->getResult()->setIndexedTagName( $data, 'ns' );
181 return $this->getResult()->addValue( 'query', $property, $data );
184 protected function appendSpecialPageAliases( $property ) {
187 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
189 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
190 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
193 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
194 return $this->getResult()->addValue( 'query', $property, $data );
197 protected function appendMagicWords( $property ) {
200 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
202 $caseSensitive = array_shift($aliases);
203 $arr = array('name' => $magicword, 'aliases' => $aliases);
205 $arr['case-sensitive'] = '';
206 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
209 $this->getResult()->setIndexedTagName($data, 'magicword');
210 return $this->getResult()->addValue( 'query', $property, $data );
213 protected function appendInterwikiMap( $property, $filter ) {
214 $this->resetQueryParams();
215 $this->addTables( 'interwiki' );
216 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
218 if( $filter === 'local' )
219 $this->addWhere( 'iw_local = 1' );
220 elseif( $filter === '!local' )
221 $this->addWhere( 'iw_local = 0' );
223 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
225 $this->addOption( 'ORDER BY', 'iw_prefix' );
227 $db = $this->getDB();
228 $res = $this->select( __METHOD__
);
231 $langNames = Language
::getLanguageNames();
232 while( $row = $db->fetchObject($res) )
235 $val['prefix'] = $row->iw_prefix
;
236 if( $row->iw_local
== '1' )
238 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
239 if( isset( $langNames[$row->iw_prefix
] ) )
240 $val['language'] = $langNames[$row->iw_prefix
];
241 $val['url'] = $row->iw_url
;
245 $db->freeResult( $res );
247 $this->getResult()->setIndexedTagName( $data, 'iw' );
248 return $this->getResult()->addValue( 'query', $property, $data );
251 protected function appendDbReplLagInfo( $property, $includeAll ) {
252 global $wgShowHostnames;
255 if ( !$wgShowHostnames )
256 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
259 $lags = $lb->getLagTimes();
260 foreach( $lags as $i => $lag ) {
262 'host' => $lb->getServerName( $i ),
267 list( $host, $lag ) = wfGetLB()->getMaxLag();
269 'host' => $wgShowHostnames ?
$host : '',
274 $result = $this->getResult();
275 $result->setIndexedTagName( $data, 'db' );
276 return $this->getResult()->addValue( 'query', $property, $data );
279 protected function appendStatistics( $property ) {
281 $data['pages'] = intval( SiteStats
::pages() );
282 $data['articles'] = intval( SiteStats
::articles() );
283 $data['views'] = intval( SiteStats
::views() );
284 $data['edits'] = intval( SiteStats
::edits() );
285 $data['images'] = intval( SiteStats
::images() );
286 $data['users'] = intval( SiteStats
::users() );
287 $data['activeusers'] = intval( SiteStats
::activeUsers() );
288 $data['admins'] = intval( SiteStats
::numberingroup('sysop') );
289 $data['jobs'] = intval( SiteStats
::jobs() );
290 return $this->getResult()->addValue( 'query', $property, $data );
293 protected function appendUserGroups( $property ) {
294 global $wgGroupPermissions;
296 foreach( $wgGroupPermissions as $group => $permissions ) {
297 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
298 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
302 $this->getResult()->setIndexedTagName( $data, 'group' );
303 return $this->getResult()->addValue( 'query', $property, $data );
306 protected function appendFileExtensions( $property ) {
307 global $wgFileExtensions;
310 foreach( $wgFileExtensions as $ext ) {
311 $data[] = array( 'ext' => $ext );
313 $this->getResult()->setIndexedTagName( $data, 'fe' );
314 return $this->getResult()->addValue( 'query', $property, $data );
317 protected function appendExtensions( $property ) {
318 global $wgExtensionCredits;
320 foreach ( $wgExtensionCredits as $type => $extensions ) {
321 foreach ( $extensions as $ext ) {
323 $ret['type'] = $type;
324 if ( isset( $ext['name'] ) )
325 $ret['name'] = $ext['name'];
326 if ( isset( $ext['description'] ) )
327 $ret['description'] = $ext['description'];
328 if ( isset( $ext['descriptionmsg'] ) )
329 $ret['descriptionmsg'] = $ext['descriptionmsg'];
330 if ( isset( $ext['author'] ) ) {
331 $ret['author'] = is_array( $ext['author'] ) ?
332 implode( ', ', $ext['author' ] ) : $ext['author'];
334 if ( isset( $ext['version'] ) ) {
335 $ret['version'] = $ext['version'];
336 } elseif ( isset( $ext['svn-revision'] ) &&
337 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
338 $ext['svn-revision'], $m ) )
340 $ret['version'] = 'r' . $m[1];
346 $this->getResult()->setIndexedTagName( $data, 'ext' );
347 return $this->getResult()->addValue( 'query', $property, $data );
351 protected function appendRightsInfo( $property ) {
352 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
353 $title = Title
::newFromText( $wgRightsPage );
354 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
355 $text = $wgRightsText;
356 if( !$text && $title ) {
357 $text = $title->getPrefixedText();
361 'url' => $url ?
$url : '',
362 'text' => $text ?
$text : ''
365 return $this->getResult()->addValue( 'query', $property, $data );
369 public function getAllowedParams() {
372 ApiBase
:: PARAM_DFLT
=> 'general',
373 ApiBase
:: PARAM_ISMULTI
=> true,
374 ApiBase
:: PARAM_TYPE
=> array(
378 'specialpagealiases',
390 ApiBase
:: PARAM_TYPE
=> array(
395 'showalldb' => false,
399 public function getParamDescription() {
402 'Which sysinfo properties to get:',
403 ' general - Overall system information',
404 ' namespaces - List of registered namespaces and their canonical names',
405 ' namespacealiases - List of registered namespace aliases',
406 ' specialpagealiases - List of special page aliases',
407 ' magicwords - List of magic words and their aliases',
408 ' statistics - Returns site statistics',
409 ' interwikimap - Returns interwiki map (optionally filtered)',
410 ' dbrepllag - Returns database server with the highest replication lag',
411 ' usergroups - Returns user groups and the associated permissions',
412 ' extensions - Returns extensions installed on the wiki',
413 ' fileextensions - Returns list of file extensions allowed to be uploaded',
414 ' rightsinfo - Returns wiki rights (license) information if available',
416 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
417 'showalldb' => 'List all database servers, not just the one lagging the most',
421 public function getDescription() {
422 return 'Return general information about the site.';
425 protected function getExamples() {
427 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
428 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
429 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
433 public function getVersion() {
434 return __CLASS__
. ': $Id$';