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 $this->appendConfigvars( $p, $params['configvars'] );
86 ApiBase
:: dieDebug( __METHOD__
, "Unknown prop=$p" );
91 protected function appendGeneralInfo( $property ) {
92 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgContLang;
93 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
96 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
97 $data['mainpage'] = $mainPage->getPrefixedText();
98 $data['base'] = $mainPage->getFullUrl();
99 $data['sitename'] = $wgSitename;
100 $data['generator'] = "MediaWiki $wgVersion";
102 $svn = SpecialVersion
::getSvnRevision( $IP );
106 $data['case'] = $wgCapitalLinks ?
'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
108 if( isset( $wgRightsCode ) )
109 $data['rightscode'] = $wgRightsCode;
110 $data['rights'] = $wgRightsText;
111 $data['lang'] = $wgLanguageCode;
112 if( $wgContLang->isRTL() )
114 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
117 $data['readonly'] = '';
118 if( $wgEnableWriteAPI )
119 $data['writeapi'] = '';
121 $tz = $wgLocaltimezone;
122 $offset = $wgLocalTZoffset;
123 if( is_null( $tz ) ) {
126 } elseif( is_null( $offset ) ) {
129 $data['timezone'] = $tz;
130 $data['timeoffset'] = $offset;
132 $this->getResult()->addValue( 'query', $property, $data );
135 protected function appendNamespaces( $property ) {
138 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
143 ApiResult
:: setContent( $data[$ns], $title );
144 $canonical = MWNamespace
::getCanonicalName( $ns );
146 if( MWNamespace
::hasSubpages( $ns ) )
147 $data[$ns]['subpages'] = '';
150 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
153 $this->getResult()->setIndexedTagName( $data, 'ns' );
154 $this->getResult()->addValue( 'query', $property, $data );
157 protected function appendNamespaceAliases( $property ) {
158 global $wgNamespaceAliases, $wgContLang;
160 $aliases = array_merge($wgNamespaceAliases, $wgContLang->namespaceAliases
);
162 foreach( $aliases as $title => $ns ) {
166 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
170 $this->getResult()->setIndexedTagName( $data, 'ns' );
171 $this->getResult()->addValue( 'query', $property, $data );
174 protected function appendSpecialPageAliases( $property ) {
177 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
179 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
180 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
183 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
184 $this->getResult()->addValue( 'query', $property, $data );
187 protected function appendMagicWords( $property ) {
190 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
192 $caseSensitive = array_shift($aliases);
193 $arr = array('name' => $magicword, 'aliases' => $aliases);
195 $arr['case-sensitive'] = '';
196 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
199 $this->getResult()->setIndexedTagName($data, 'magicword');
200 $this->getResult()->addValue('query', $property, $data);
203 protected function appendInterwikiMap( $property, $filter ) {
204 $this->resetQueryParams();
205 $this->addTables( 'interwiki' );
206 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
208 if( $filter === 'local' )
209 $this->addWhere( 'iw_local = 1' );
210 elseif( $filter === '!local' )
211 $this->addWhere( 'iw_local = 0' );
213 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
215 $this->addOption( 'ORDER BY', 'iw_prefix' );
217 $db = $this->getDB();
218 $res = $this->select( __METHOD__
);
221 $langNames = Language
::getLanguageNames();
222 while( $row = $db->fetchObject($res) )
225 $val['prefix'] = $row->iw_prefix
;
226 if( $row->iw_local
== '1' )
228 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
229 if( isset( $langNames[$row->iw_prefix
] ) )
230 $val['language'] = $langNames[$row->iw_prefix
];
231 $val['url'] = $row->iw_url
;
235 $db->freeResult( $res );
237 $this->getResult()->setIndexedTagName( $data, 'iw' );
238 $this->getResult()->addValue( 'query', $property, $data );
241 protected function appendDbReplLagInfo( $property, $includeAll ) {
242 global $wgShowHostnames;
245 if ( !$wgShowHostnames )
246 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
249 $lags = $lb->getLagTimes();
250 foreach( $lags as $i => $lag ) {
252 'host' => $lb->getServerName( $i ),
257 list( $host, $lag ) = wfGetLB()->getMaxLag();
259 'host' => $wgShowHostnames ?
$host : '',
264 $result = $this->getResult();
265 $result->setIndexedTagName( $data, 'db' );
266 $result->addValue( 'query', $property, $data );
269 protected function appendStatistics( $property ) {
271 $data['pages'] = intval( SiteStats
::pages() );
272 $data['articles'] = intval( SiteStats
::articles() );
273 $data['views'] = intval( SiteStats
::views() );
274 $data['edits'] = intval( SiteStats
::edits() );
275 $data['images'] = intval( SiteStats
::images() );
276 $data['users'] = intval( SiteStats
::users() );
277 $data['activeusers'] = intval( SiteStats
::activeUsers() );
278 $data['admins'] = intval( SiteStats
::numberingroup('sysop') );
279 $data['jobs'] = intval( SiteStats
::jobs() );
280 $this->getResult()->addValue( 'query', $property, $data );
283 protected function appendUserGroups( $property ) {
284 global $wgGroupPermissions;
286 foreach( $wgGroupPermissions as $group => $permissions ) {
287 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
288 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
292 $this->getResult()->setIndexedTagName( $data, 'group' );
293 $this->getResult()->addValue( 'query', $property, $data );
296 protected function appendFileExtensions( $property ) {
297 global $wgFileExtensions;
300 foreach( $wgFileExtensions as $ext ) {
301 $data[] = array( 'ext' => $ext );
303 $this->getResult()->setIndexedTagName( $data, 'fe' );
304 $this->getResult()->addValue( 'query', $property, $data );
307 protected function appendExtensions( $property ) {
308 global $wgExtensionCredits;
310 foreach ( $wgExtensionCredits as $type => $extensions ) {
311 foreach ( $extensions as $ext ) {
313 $ret['type'] = $type;
314 if ( isset( $ext['name'] ) )
315 $ret['name'] = $ext['name'];
316 if ( isset( $ext['description'] ) )
317 $ret['description'] = $ext['description'];
318 if ( isset( $ext['descriptionmsg'] ) )
319 $ret['descriptionmsg'] = $ext['descriptionmsg'];
320 if ( isset( $ext['author'] ) ) {
321 $ret['author'] = is_array( $ext['author'] ) ?
322 implode( ', ', $ext['author' ] ) : $ext['author'];
324 if ( isset( $ext['version'] ) ) {
325 $ret['version'] = $ext['version'];
326 } elseif ( isset( $ext['svn-revision'] ) &&
327 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
328 $ext['svn-revision'], $m ) )
330 $ret['version'] = 'r' . $m[1];
336 $this->getResult()->setIndexedTagName( $data, 'ext' );
337 $this->getResult()->addValue( 'query', $property, $data );
340 protected function appendConfigvars( $property, $vars ) {
341 global $wgAPIReadableConfigVars;
343 $vars = $wgAPIReadableConfigVars;
345 foreach((array)$vars as $var)
347 $value = $GLOBALS[$var];
350 $r['type'] = gettype($value);
351 if(is_object($value))
352 $r['class'] = get_class($value);
353 elseif(is_bool($value))
354 $r['value'] = ($value ?
'true' : 'false');
355 elseif(!is_null($value))
357 $r['value'] = $value;
360 $this->getResult()->setIndexedTagName($r['value'], 'elem');
361 $this->getResult()->setIndexedTagName_recursive($r['value'], 'elem');
366 $this->getResult()->setIndexedTagName($data, 'config');
367 $this->getResult()->addValue('query', $property, $data);
370 public function getAllowedParams() {
371 global $wgAPIReadableConfigVars;
374 ApiBase
:: PARAM_DFLT
=> 'general',
375 ApiBase
:: PARAM_ISMULTI
=> true,
376 ApiBase
:: PARAM_TYPE
=> array(
380 'specialpagealiases',
392 ApiBase
:: PARAM_TYPE
=> array(
397 'showalldb' => false,
398 'configvars' => array(
399 ApiBase
:: PARAM_ISMULTI
=> true,
400 ApiBase
:: PARAM_TYPE
=> $wgAPIReadableConfigVars,
405 public function getParamDescription() {
408 'Which sysinfo properties to get:',
409 ' "general" - Overall system information',
410 ' "namespaces" - List of registered namespaces and their canonical names',
411 ' "namespacealiases" - List of registered namespace aliases',
412 ' "specialpagealiases" - List of special page aliases',
413 ' "magicwords" - List of magic words and their aliases',
414 ' "statistics" - Returns site statistics',
415 ' "interwikimap" - Returns interwiki map (optionally filtered)',
416 ' "dbrepllag" - Returns database server with the highest replication lag',
417 ' "usergroups" - Returns user groups and the associated permissions',
418 ' "extensions" - Returns extensions installed on the wiki',
419 ' "fileextensions" - Returns list of file extensions allowed to be uploaded',
420 ' "configvars" - Returns the value of certain configuration variables',
422 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
423 'showalldb' => 'List all database servers, not just the one lagging the most',
424 'configvars' => 'Configuration variables to get. If empty, all configuration variables will be listed.',
428 public function getDescription() {
429 return 'Return general information about the site.';
432 protected function getExamples() {
434 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
435 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
436 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
440 public function getVersion() {
441 return __CLASS__
. ': $Id$';