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 global $wgAPIReadableConfigVars;
84 $this->appendConfigvars( $p, $params['configvars'] );
87 ApiBase
:: dieDebug( __METHOD__
, "Unknown prop=$p" );
92 protected function appendGeneralInfo( $property ) {
93 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgContLang;
94 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
97 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
98 $data['mainpage'] = $mainPage->getPrefixedText();
99 $data['base'] = $mainPage->getFullUrl();
100 $data['sitename'] = $wgSitename;
101 $data['generator'] = "MediaWiki $wgVersion";
103 $svn = SpecialVersion
::getSvnRevision( $IP );
107 $data['case'] = $wgCapitalLinks ?
'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
109 if( isset( $wgRightsCode ) )
110 $data['rightscode'] = $wgRightsCode;
111 $data['rights'] = $wgRightsText;
112 $data['lang'] = $wgLanguageCode;
113 if( $wgContLang->isRTL() )
115 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
118 $data['readonly'] = '';
119 if( $wgEnableWriteAPI )
120 $data['writeapi'] = '';
122 $tz = $wgLocaltimezone;
123 $offset = $wgLocalTZoffset;
124 if( is_null( $tz ) ) {
127 } elseif( is_null( $offset ) ) {
130 $data['timezone'] = $tz;
131 $data['timeoffset'] = $offset;
133 $this->getResult()->addValue( 'query', $property, $data );
136 protected function appendNamespaces( $property ) {
139 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
144 ApiResult
:: setContent( $data[$ns], $title );
145 $canonical = MWNamespace
::getCanonicalName( $ns );
147 if( MWNamespace
::hasSubpages( $ns ) )
148 $data[$ns]['subpages'] = '';
151 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
154 $this->getResult()->setIndexedTagName( $data, 'ns' );
155 $this->getResult()->addValue( 'query', $property, $data );
158 protected function appendNamespaceAliases( $property ) {
159 global $wgNamespaceAliases, $wgContLang;
161 $aliases = array_merge($wgNamespaceAliases, $wgContLang->namespaceAliases
);
163 foreach( $aliases as $title => $ns ) {
167 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
171 $this->getResult()->setIndexedTagName( $data, 'ns' );
172 $this->getResult()->addValue( 'query', $property, $data );
175 protected function appendSpecialPageAliases( $property ) {
178 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
180 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
181 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
184 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
185 $this->getResult()->addValue( 'query', $property, $data );
188 protected function appendMagicWords( $property ) {
191 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
193 $caseSensitive = array_shift($aliases);
194 $arr = array('name' => $magicword, 'aliases' => $aliases);
196 $arr['case-sensitive'] = '';
197 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
200 $this->getResult()->setIndexedTagName($data, 'magicword');
201 $this->getResult()->addValue('query', $property, $data);
204 protected function appendInterwikiMap( $property, $filter ) {
205 $this->resetQueryParams();
206 $this->addTables( 'interwiki' );
207 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
209 if( $filter === 'local' )
210 $this->addWhere( 'iw_local = 1' );
211 elseif( $filter === '!local' )
212 $this->addWhere( 'iw_local = 0' );
214 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
216 $this->addOption( 'ORDER BY', 'iw_prefix' );
218 $db = $this->getDB();
219 $res = $this->select( __METHOD__
);
222 $langNames = Language
::getLanguageNames();
223 while( $row = $db->fetchObject($res) )
226 $val['prefix'] = $row->iw_prefix
;
227 if( $row->iw_local
== '1' )
229 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
230 if( isset( $langNames[$row->iw_prefix
] ) )
231 $val['language'] = $langNames[$row->iw_prefix
];
232 $val['url'] = $row->iw_url
;
236 $db->freeResult( $res );
238 $this->getResult()->setIndexedTagName( $data, 'iw' );
239 $this->getResult()->addValue( 'query', $property, $data );
242 protected function appendDbReplLagInfo( $property, $includeAll ) {
243 global $wgShowHostnames;
246 if ( !$wgShowHostnames )
247 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
250 $lags = $lb->getLagTimes();
251 foreach( $lags as $i => $lag ) {
253 'host' => $lb->getServerName( $i ),
258 list( $host, $lag ) = wfGetLB()->getMaxLag();
260 'host' => $wgShowHostnames ?
$host : '',
265 $result = $this->getResult();
266 $result->setIndexedTagName( $data, 'db' );
267 $result->addValue( 'query', $property, $data );
270 protected function appendStatistics( $property ) {
272 $data['pages'] = intval( SiteStats
::pages() );
273 $data['articles'] = intval( SiteStats
::articles() );
274 $data['views'] = intval( SiteStats
::views() );
275 $data['edits'] = intval( SiteStats
::edits() );
276 $data['images'] = intval( SiteStats
::images() );
277 $data['users'] = intval( SiteStats
::users() );
278 $data['activeusers'] = intval( SiteStats
::activeUsers() );
279 $data['admins'] = intval( SiteStats
::numberingroup('sysop') );
280 $data['jobs'] = intval( SiteStats
::jobs() );
281 $this->getResult()->addValue( 'query', $property, $data );
284 protected function appendUserGroups( $property ) {
285 global $wgGroupPermissions;
287 foreach( $wgGroupPermissions as $group => $permissions ) {
288 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
289 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
293 $this->getResult()->setIndexedTagName( $data, 'group' );
294 $this->getResult()->addValue( 'query', $property, $data );
297 protected function appendFileExtensions( $property ) {
298 global $wgFileExtensions;
301 foreach( $wgFileExtensions as $ext ) {
302 $data[] = array( 'ext' => $ext );
304 $this->getResult()->setIndexedTagName( $data, 'fe' );
305 $this->getResult()->addValue( 'query', $property, $data );
308 protected function appendExtensions( $property ) {
309 global $wgExtensionCredits;
311 foreach ( $wgExtensionCredits as $type => $extensions ) {
312 foreach ( $extensions as $ext ) {
314 $ret['type'] = $type;
315 if ( isset( $ext['name'] ) )
316 $ret['name'] = $ext['name'];
317 if ( isset( $ext['description'] ) )
318 $ret['description'] = $ext['description'];
319 if ( isset( $ext['descriptionmsg'] ) )
320 $ret['descriptionmsg'] = $ext['descriptionmsg'];
321 if ( isset( $ext['author'] ) ) {
322 $ret['author'] = is_array( $ext['author'] ) ?
323 implode( ', ', $ext['author' ] ) : $ext['author'];
325 if ( isset( $ext['version'] ) ) {
326 $ret['version'] = $ext['version'];
327 } elseif ( isset( $ext['svn-revision'] ) &&
328 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
329 $ext['svn-revision'], $m ) )
331 $ret['version'] = 'r' . $m[1];
337 $this->getResult()->setIndexedTagName( $data, 'ext' );
338 $this->getResult()->addValue( 'query', $property, $data );
341 protected function appendConfigvars( $property, $vars ) {
342 global $wgAPIReadableConfigVars;
344 $vars = $wgAPIReadableConfigVars;
346 foreach((array)$vars as $var)
348 $value = $GLOBALS[$var];
351 $r['type'] = gettype($value);
352 if(is_object($value))
353 $r['class'] = get_class($value);
354 elseif(is_bool($value))
355 $r['value'] = ($value ?
'true' : 'false');
356 elseif(!is_null($value))
358 $r['value'] = $value;
361 $this->getResult()->setIndexedTagName($r['value'], 'elem');
362 $this->getResult()->setIndexedTagName_recursive($r['value'], 'elem');
367 $this->getResult()->setIndexedTagName($data, 'config');
368 $this->getResult()->addValue('query', $property, $data);
371 public function getAllowedParams() {
372 global $wgAPIReadableConfigVars;
375 ApiBase
:: PARAM_DFLT
=> 'general',
376 ApiBase
:: PARAM_ISMULTI
=> true,
377 ApiBase
:: PARAM_TYPE
=> array(
381 'specialpagealiases',
393 ApiBase
:: PARAM_TYPE
=> array(
398 'showalldb' => false,
399 'configvars' => array(
400 ApiBase
:: PARAM_ISMULTI
=> true,
401 ApiBase
:: PARAM_TYPE
=> $wgAPIReadableConfigVars,
406 public function getParamDescription() {
409 'Which sysinfo properties to get:',
410 ' "general" - Overall system information',
411 ' "namespaces" - List of registered namespaces and their canonical names',
412 ' "namespacealiases" - List of registered namespace aliases',
413 ' "specialpagealiases" - List of special page aliases',
414 ' "magicwords" - List of magic words and their aliases',
415 ' "statistics" - Returns site statistics',
416 ' "interwikimap" - Returns interwiki map (optionally filtered)',
417 ' "dbrepllag" - Returns database server with the highest replication lag',
418 ' "usergroups" - Returns user groups and the associated permissions',
419 ' "extensions" - Returns extensions installed on the wiki',
420 ' "fileextensions" - Returns list of file extensions allowed to be uploaded',
421 ' "configvars" - Returns the value of certain configuration variables',
423 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
424 'showalldb' => 'List all database servers, not just the one lagging the most',
425 'configvars' => 'Configuration variables to get. If empty, all configuration variables will be listed.',
429 public function getDescription() {
430 return 'Return general information about the site.';
433 protected function getExamples() {
435 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
436 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
437 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
441 public function getVersion() {
442 return __CLASS__
. ': $Id$';