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 );
80 ApiBase :: dieDebug( __METHOD__, "Unknown prop=$p" );
85 protected function appendGeneralInfo( $property ) {
86 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgContLang;
87 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
90 $mainPage = Title :: newFromText(wfMsgForContent('mainpage'));
91 $data['mainpage'] = $mainPage->getPrefixedText();
92 $data['base'] = $mainPage->getFullUrl();
93 $data['sitename'] = $wgSitename;
94 $data['generator'] = "MediaWiki $wgVersion";
96 $svn = SpecialVersion::getSvnRevision( $IP );
100 $data['case'] = $wgCapitalLinks ? 'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
102 if( isset( $wgRightsCode ) )
103 $data['rightscode'] = $wgRightsCode;
104 $data['rights'] = $wgRightsText;
105 $data['lang'] = $wgLanguageCode;
106 if( $wgContLang->isRTL() )
108 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
111 $data['readonly'] = '';
112 if( $wgEnableWriteAPI )
113 $data['writeapi'] = '';
115 $tz = $wgLocaltimezone;
116 $offset = $wgLocalTZoffset;
117 if( is_null( $tz ) ) {
120 } elseif( is_null( $offset ) ) {
123 $data['timezone'] = $tz;
124 $data['timeoffset'] = $offset;
126 $this->getResult()->addValue( 'query', $property, $data );
129 protected function appendNamespaces( $property ) {
132 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
137 ApiResult :: setContent( $data[$ns], $title );
138 $canonical = MWNamespace::getCanonicalName( $ns );
140 if( MWNamespace::hasSubpages( $ns ) )
141 $data[$ns]['subpages'] = '';
144 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
147 $this->getResult()->setIndexedTagName( $data, 'ns' );
148 $this->getResult()->addValue( 'query', $property, $data );
151 protected function appendNamespaceAliases( $property ) {
152 global $wgNamespaceAliases, $wgContLang;
154 $aliases = array_merge($wgNamespaceAliases, $wgContLang->namespaceAliases);
156 foreach( $aliases as $title => $ns ) {
160 ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
164 $this->getResult()->setIndexedTagName( $data, 'ns' );
165 $this->getResult()->addValue( 'query', $property, $data );
168 protected function appendSpecialPageAliases( $property ) {
171 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
173 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
174 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
177 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
178 $this->getResult()->addValue( 'query', $property, $data );
181 protected function appendMagicWords( $property ) {
184 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
186 $caseSensitive = array_shift($aliases);
187 $arr = array('name' => $magicword, 'aliases' => $aliases);
189 $arr['case-sensitive'] = '';
190 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
193 $this->getResult()->setIndexedTagName($data, 'magicword');
194 $this->getResult()->addValue('query', $property, $data);
197 protected function appendInterwikiMap( $property, $filter ) {
198 $this->resetQueryParams();
199 $this->addTables( 'interwiki' );
200 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
202 if( $filter === 'local' )
203 $this->addWhere( 'iw_local = 1' );
204 elseif( $filter === '!local' )
205 $this->addWhere( 'iw_local = 0' );
207 ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
209 $this->addOption( 'ORDER BY', 'iw_prefix' );
211 $db = $this->getDB();
212 $res = $this->select( __METHOD__ );
215 $langNames = Language::getLanguageNames();
216 while( $row = $db->fetchObject($res) )
219 $val['prefix'] = $row->iw_prefix;
220 if( $row->iw_local == '1' )
222 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
223 if( isset( $langNames[$row->iw_prefix] ) )
224 $val['language'] = $langNames[$row->iw_prefix];
225 $val['url'] = $row->iw_url;
229 $db->freeResult( $res );
231 $this->getResult()->setIndexedTagName( $data, 'iw' );
232 $this->getResult()->addValue( 'query', $property, $data );
235 protected function appendDbReplLagInfo( $property, $includeAll ) {
236 global $wgShowHostnames;
239 if ( !$wgShowHostnames )
240 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
243 $lags = $lb->getLagTimes();
244 foreach( $lags as $i => $lag ) {
246 'host' => $lb->getServerName( $i ),
251 list( $host, $lag ) = wfGetLB()->getMaxLag();
253 'host' => $wgShowHostnames ? $host : '',
258 $result = $this->getResult();
259 $result->setIndexedTagName( $data, 'db' );
260 $result->addValue( 'query', $property, $data );
263 protected function appendStatistics( $property ) {
265 $data['pages'] = intval( SiteStats::pages() );
266 $data['articles'] = intval( SiteStats::articles() );
267 $data['views'] = intval( SiteStats::views() );
268 $data['edits'] = intval( SiteStats::edits() );
269 $data['images'] = intval( SiteStats::images() );
270 $data['users'] = intval( SiteStats::users() );
271 $data['activeusers'] = intval( SiteStats::activeUsers() );
272 $data['admins'] = intval( SiteStats::numberingroup('sysop') );
273 $data['jobs'] = intval( SiteStats::jobs() );
274 $this->getResult()->addValue( 'query', $property, $data );
277 protected function appendUserGroups( $property ) {
278 global $wgGroupPermissions;
280 foreach( $wgGroupPermissions as $group => $permissions ) {
281 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
282 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
286 $this->getResult()->setIndexedTagName( $data, 'group' );
287 $this->getResult()->addValue( 'query', $property, $data );
290 protected function appendExtensions( $property ) {
291 global $wgExtensionCredits;
293 foreach ( $wgExtensionCredits as $type => $extensions ) {
294 foreach ( $extensions as $ext ) {
296 $ret['type'] = $type;
297 if ( isset( $ext['name'] ) )
298 $ret['name'] = $ext['name'];
299 if ( isset( $ext['description'] ) )
300 $ret['description'] = $ext['description'];
301 if ( isset( $ext['descriptionmsg'] ) )
302 $ret['descriptionmsg'] = $ext['descriptionmsg'];
303 if ( isset( $ext['author'] ) ) {
304 $ret['author'] = is_array( $ext['author'] ) ?
305 implode( ', ', $ext['author' ] ) : $ext['author'];
307 if ( isset( $ext['version'] ) ) {
308 $ret['version'] = $ext['version'];
309 } elseif ( isset( $ext['svn-revision'] ) &&
310 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
311 $ext['svn-revision'], $m ) )
313 $ret['version'] = 'r' . $m[1];
319 $this->getResult()->setIndexedTagName( $data, 'ext' );
320 $this->getResult()->addValue( 'query', $property, $data );
324 public function getAllowedParams() {
327 ApiBase :: PARAM_DFLT => 'general',
328 ApiBase :: PARAM_ISMULTI => true,
329 ApiBase :: PARAM_TYPE => array(
333 'specialpagealiases',
343 ApiBase :: PARAM_TYPE => array(
348 'showalldb' => false,
352 public function getParamDescription() {
355 'Which sysinfo properties to get:',
356 ' "general" - Overall system information',
357 ' "namespaces" - List of registered namespaces and their canonical names',
358 ' "namespacealiases" - List of registered namespace aliases',
359 ' "specialpagealiases" - List of special page aliases',
360 ' "magicwords" - List of magic words and their aliases',
361 ' "statistics" - Returns site statistics',
362 ' "interwikimap" - Returns interwiki map (optionally filtered)',
363 ' "dbrepllag" - Returns database server with the highest replication lag',
364 ' "usergroups" - Returns user groups and the associated permissions',
365 ' "extensions" - Returns extensions installed on the wiki',
367 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
368 'showalldb' => 'List all database servers, not just the one lagging the most',
372 public function getDescription() {
373 return 'Return general information about the site.';
376 protected function getExamples() {
378 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
379 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
380 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
384 public function getVersion() {
385 return __CLASS__ . ': $Id: ApiQuerySiteinfo.php 44862 2008-12-20 23:49:16Z catrope $';