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();
44 foreach( $params['prop'] as $p )
49 $this->appendGeneralInfo( $p );
52 $this->appendNamespaces( $p );
54 case 'namespacealiases':
55 $this->appendNamespaceAliases( $p );
57 case 'specialpagealiases':
58 $this->appendSpecialPageAliases( $p );
61 $this->appendMagicWords( $p );
64 $filteriw = isset( $params['filteriw'] ) ?
$params['filteriw'] : false;
65 $this->appendInterwikiMap( $p, $filteriw );
68 $this->appendDbReplLagInfo( $p, $params['showalldb'] );
71 $this->appendStatistics( $p );
74 $this->appendUserGroups( $p );
77 $this->appendExtensions( $p );
79 case 'fileextensions':
80 $this->appendFileExtensions( $p );
83 ApiBase
:: dieDebug( __METHOD__
, "Unknown prop=$p" );
88 protected function appendGeneralInfo( $property ) {
89 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgContLang;
90 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
91 global $wgLegalTitleChars;
94 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
95 $data['mainpage'] = $mainPage->getPrefixedText();
96 $data['base'] = $mainPage->getFullUrl();
97 $data['sitename'] = $wgSitename;
98 $data['generator'] = "MediaWiki $wgVersion";
100 $svn = SpecialVersion
::getSvnRevision( $IP );
104 $data['case'] = $wgCapitalLinks ?
'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
106 if( isset( $wgRightsCode ) )
107 $data['rightscode'] = $wgRightsCode;
108 $data['rights'] = $wgRightsText;
109 $data['lang'] = $wgLanguageCode;
110 if( $wgContLang->isRTL() )
112 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
113 $data['legaltitlechars'] = $wgLegalTitleChars;
116 $data['readonly'] = '';
117 if( $wgEnableWriteAPI )
118 $data['writeapi'] = '';
120 $tz = $wgLocaltimezone;
121 $offset = $wgLocalTZoffset;
122 if( is_null( $tz ) ) {
125 } elseif( is_null( $offset ) ) {
128 $data['timezone'] = $tz;
129 $data['timeoffset'] = $offset;
131 $this->getResult()->addValue( 'query', $property, $data );
134 protected function appendNamespaces( $property ) {
137 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
142 ApiResult
:: setContent( $data[$ns], $title );
143 $canonical = MWNamespace
::getCanonicalName( $ns );
145 if( MWNamespace
::hasSubpages( $ns ) )
146 $data[$ns]['subpages'] = '';
149 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
152 $this->getResult()->setIndexedTagName( $data, 'ns' );
153 $this->getResult()->addValue( 'query', $property, $data );
156 protected function appendNamespaceAliases( $property ) {
157 global $wgNamespaceAliases, $wgContLang;
159 $aliases = array_merge($wgNamespaceAliases, $wgContLang->namespaceAliases
);
161 foreach( $aliases as $title => $ns ) {
165 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
169 $this->getResult()->setIndexedTagName( $data, 'ns' );
170 $this->getResult()->addValue( 'query', $property, $data );
173 protected function appendSpecialPageAliases( $property ) {
176 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
178 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
179 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
182 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
183 $this->getResult()->addValue( 'query', $property, $data );
186 protected function appendMagicWords( $property ) {
189 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
191 $caseSensitive = array_shift($aliases);
192 $arr = array('name' => $magicword, 'aliases' => $aliases);
194 $arr['case-sensitive'] = '';
195 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
198 $this->getResult()->setIndexedTagName($data, 'magicword');
199 $this->getResult()->addValue('query', $property, $data);
202 protected function appendInterwikiMap( $property, $filter ) {
203 $this->resetQueryParams();
204 $this->addTables( 'interwiki' );
205 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
207 if( $filter === 'local' )
208 $this->addWhere( 'iw_local = 1' );
209 elseif( $filter === '!local' )
210 $this->addWhere( 'iw_local = 0' );
212 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
214 $this->addOption( 'ORDER BY', 'iw_prefix' );
216 $db = $this->getDB();
217 $res = $this->select( __METHOD__
);
220 $langNames = Language
::getLanguageNames();
221 while( $row = $db->fetchObject($res) )
224 $val['prefix'] = $row->iw_prefix
;
225 if( $row->iw_local
== '1' )
227 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
228 if( isset( $langNames[$row->iw_prefix
] ) )
229 $val['language'] = $langNames[$row->iw_prefix
];
230 $val['url'] = $row->iw_url
;
234 $db->freeResult( $res );
236 $this->getResult()->setIndexedTagName( $data, 'iw' );
237 $this->getResult()->addValue( 'query', $property, $data );
240 protected function appendDbReplLagInfo( $property, $includeAll ) {
241 global $wgShowHostnames;
244 if ( !$wgShowHostnames )
245 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
248 $lags = $lb->getLagTimes();
249 foreach( $lags as $i => $lag ) {
251 'host' => $lb->getServerName( $i ),
256 list( $host, $lag ) = wfGetLB()->getMaxLag();
258 'host' => $wgShowHostnames ?
$host : '',
263 $result = $this->getResult();
264 $result->setIndexedTagName( $data, 'db' );
265 $result->addValue( 'query', $property, $data );
268 protected function appendStatistics( $property ) {
270 $data['pages'] = intval( SiteStats
::pages() );
271 $data['articles'] = intval( SiteStats
::articles() );
272 $data['views'] = intval( SiteStats
::views() );
273 $data['edits'] = intval( SiteStats
::edits() );
274 $data['images'] = intval( SiteStats
::images() );
275 $data['users'] = intval( SiteStats
::users() );
276 $data['activeusers'] = intval( SiteStats
::activeUsers() );
277 $data['admins'] = intval( SiteStats
::numberingroup('sysop') );
278 $data['jobs'] = intval( SiteStats
::jobs() );
279 $this->getResult()->addValue( 'query', $property, $data );
282 protected function appendUserGroups( $property ) {
283 global $wgGroupPermissions;
285 foreach( $wgGroupPermissions as $group => $permissions ) {
286 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
287 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
291 $this->getResult()->setIndexedTagName( $data, 'group' );
292 $this->getResult()->addValue( 'query', $property, $data );
295 protected function appendFileExtensions( $property ) {
296 global $wgFileExtensions;
299 foreach( $wgFileExtensions as $ext ) {
300 $data[] = array( 'ext' => $ext );
302 $this->getResult()->setIndexedTagName( $data, 'fe' );
303 $this->getResult()->addValue( 'query', $property, $data );
306 protected function appendExtensions( $property ) {
307 global $wgExtensionCredits;
309 foreach ( $wgExtensionCredits as $type => $extensions ) {
310 foreach ( $extensions as $ext ) {
312 $ret['type'] = $type;
313 if ( isset( $ext['name'] ) )
314 $ret['name'] = $ext['name'];
315 if ( isset( $ext['description'] ) )
316 $ret['description'] = $ext['description'];
317 if ( isset( $ext['descriptionmsg'] ) )
318 $ret['descriptionmsg'] = $ext['descriptionmsg'];
319 if ( isset( $ext['author'] ) ) {
320 $ret['author'] = is_array( $ext['author'] ) ?
321 implode( ', ', $ext['author' ] ) : $ext['author'];
323 if ( isset( $ext['version'] ) ) {
324 $ret['version'] = $ext['version'];
325 } elseif ( isset( $ext['svn-revision'] ) &&
326 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
327 $ext['svn-revision'], $m ) )
329 $ret['version'] = 'r' . $m[1];
335 $this->getResult()->setIndexedTagName( $data, 'ext' );
336 $this->getResult()->addValue( 'query', $property, $data );
340 public function getAllowedParams() {
343 ApiBase
:: PARAM_DFLT
=> 'general',
344 ApiBase
:: PARAM_ISMULTI
=> true,
345 ApiBase
:: PARAM_TYPE
=> array(
349 'specialpagealiases',
360 ApiBase
:: PARAM_TYPE
=> array(
365 'showalldb' => false,
369 public function getParamDescription() {
372 'Which sysinfo properties to get:',
373 ' "general" - Overall system information',
374 ' "namespaces" - List of registered namespaces and their canonical names',
375 ' "namespacealiases" - List of registered namespace aliases',
376 ' "specialpagealiases" - List of special page aliases',
377 ' "magicwords" - List of magic words and their aliases',
378 ' "statistics" - Returns site statistics',
379 ' "interwikimap" - Returns interwiki map (optionally filtered)',
380 ' "dbrepllag" - Returns database server with the highest replication lag',
381 ' "usergroups" - Returns user groups and the associated permissions',
382 ' "extensions" - Returns extensions installed on the wiki',
383 ' "fileextensions" - Returns list of file extensions allowed to be uploaded',
385 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
386 'showalldb' => 'List all database servers, not just the one lagging the most',
390 public function getDescription() {
391 return 'Return general information about the site.';
394 protected function getExamples() {
396 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
397 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
398 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
402 public function getVersion() {
403 return __CLASS__
. ': $Id$';