API: Add PHP and database version to meta=siteinfo output
[lhc/web/wiklou.git] / includes / api / ApiQuerySiteinfo.php
1 <?php
2
3 /*
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if( !defined('MEDIAWIKI') ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * A query action to return meta information about the wiki site.
33 *
34 * @ingroup API
35 */
36 class ApiQuerySiteinfo extends ApiQueryBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent :: __construct( $query, $moduleName, 'si' );
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $done = array();
45 foreach( $params['prop'] as $p )
46 {
47 switch ( $p )
48 {
49 case 'general':
50 $fit = $this->appendGeneralInfo( $p );
51 break;
52 case 'namespaces':
53 $fit = $this->appendNamespaces( $p );
54 break;
55 case 'namespacealiases':
56 $fit = $this->appendNamespaceAliases( $p );
57 break;
58 case 'specialpagealiases':
59 $fit = $this->appendSpecialPageAliases( $p );
60 break;
61 case 'magicwords':
62 $fit = $this->appendMagicWords( $p );
63 break;
64 case 'interwikimap':
65 $filteriw = isset( $params['filteriw'] ) ? $params['filteriw'] : false;
66 $fit = $this->appendInterwikiMap( $p, $filteriw );
67 break;
68 case 'dbrepllag':
69 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
70 break;
71 case 'statistics':
72 $fit = $this->appendStatistics( $p );
73 break;
74 case 'usergroups':
75 $fit = $this->appendUserGroups( $p );
76 break;
77 case 'extensions':
78 $fit = $this->appendExtensions( $p );
79 break;
80 case 'fileextensions':
81 $fit = $this->appendFileExtensions( $p );
82 break;
83 case 'rightsinfo':
84 $fit = $this->appendRightsInfo( $p );
85 break;
86 default :
87 ApiBase :: dieDebug( __METHOD__, "Unknown prop=$p" );
88 }
89 if(!$fit)
90 {
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)));
95 break;
96 }
97 $done[] = $p;
98 }
99 }
100
101 protected function appendGeneralInfo( $property ) {
102 global $wgContLang;
103 global $wgLang;
104
105 $data = array();
106 $mainPage = Title :: newFromText(wfMsgForContent('mainpage'));
107 $data['mainpage'] = $mainPage->getPrefixedText();
108 $data['base'] = $mainPage->getFullUrl();
109 $data['sitename'] = $GLOBALS['wgSitename'];
110 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
111 $data['phpversion'] = phpversion();
112 $data['phpsapi'] = php_sapi_name();
113 $data['dbclass'] = get_class($this->getDB());
114 $data['dbversion'] = $this->getDB()->getServerVersion();
115
116 $svn = SpecialVersion::getSvnRevision( $GLOBALS['IP'] );
117 if( $svn )
118 $data['rev'] = $svn;
119
120 // 'case-insensitive' option is reserved for future
121 $data['case'] = $GLOBALS['wgCapitalLinks'] ? 'first-letter' : 'case-sensitive';
122
123 if( isset( $GLOBALS['wgRightsCode'] ) )
124 $data['rightscode'] = $GLOBALS['wgRightsCode'];
125 $data['rights'] = $GLOBALS['wgRightsText'];
126 $data['lang'] = $GLOBALS['wgLanguageCode'];
127 if( $wgContLang->isRTL() )
128 $data['rtl'] = '';
129 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
130
131 if( wfReadOnly() )
132 $data['readonly'] = '';
133 if( $GLOBALS['wgEnableWriteAPI'] )
134 $data['writeapi'] = '';
135
136 $tz = $GLOBALS['wgLocaltimezone'];
137 $offset = $GLOBALS['wgLocalTZoffset'];
138 if( is_null( $tz ) ) {
139 $tz = 'UTC';
140 $offset = 0;
141 } elseif( is_null( $offset ) ) {
142 $offset = 0;
143 }
144 $data['timezone'] = $tz;
145 $data['timeoffset'] = intval($offset);
146 $data['articlepath'] = $GLOBALS['wgArticlePath'];
147 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
148 $data['script'] = $GLOBALS['wgScript'];
149 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
150 $data['server'] = $GLOBALS['wgServer'];
151 $data['wikiid'] = wfWikiID();
152
153 return $this->getResult()->addValue( 'query', $property, $data );
154 }
155
156 protected function appendNamespaces( $property ) {
157 global $wgContLang;
158 $data = array();
159 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
160 {
161 $data[$ns] = array(
162 'id' => intval($ns)
163 );
164 ApiResult :: setContent( $data[$ns], $title );
165 $canonical = MWNamespace::getCanonicalName( $ns );
166
167 if( MWNamespace::hasSubpages( $ns ) )
168 $data[$ns]['subpages'] = '';
169
170 if( $canonical )
171 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
172 }
173
174 $this->getResult()->setIndexedTagName( $data, 'ns' );
175 return $this->getResult()->addValue( 'query', $property, $data );
176 }
177
178 protected function appendNamespaceAliases( $property ) {
179 global $wgNamespaceAliases, $wgContLang;
180 $wgContLang->load();
181 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->namespaceAliases );
182 $namespaces = $wgContLang->getNamespaces();
183 $data = array();
184 foreach( $aliases as $title => $ns ) {
185 if( $namespaces[$ns] == $title ) {
186 // Don't list duplicates
187 continue;
188 }
189 $item = array(
190 'id' => intval($ns)
191 );
192 ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
193 $data[] = $item;
194 }
195
196 $this->getResult()->setIndexedTagName( $data, 'ns' );
197 return $this->getResult()->addValue( 'query', $property, $data );
198 }
199
200 protected function appendSpecialPageAliases( $property ) {
201 global $wgLang;
202 $data = array();
203 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
204 {
205 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
206 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
207 $data[] = $arr;
208 }
209 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
210 return $this->getResult()->addValue( 'query', $property, $data );
211 }
212
213 protected function appendMagicWords( $property ) {
214 global $wgContLang;
215 $data = array();
216 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
217 {
218 $caseSensitive = array_shift($aliases);
219 $arr = array('name' => $magicword, 'aliases' => $aliases);
220 if($caseSensitive)
221 $arr['case-sensitive'] = '';
222 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
223 $data[] = $arr;
224 }
225 $this->getResult()->setIndexedTagName($data, 'magicword');
226 return $this->getResult()->addValue( 'query', $property, $data );
227 }
228
229 protected function appendInterwikiMap( $property, $filter ) {
230 $this->resetQueryParams();
231 $this->addTables( 'interwiki' );
232 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
233
234 if( $filter === 'local' )
235 $this->addWhere( 'iw_local = 1' );
236 elseif( $filter === '!local' )
237 $this->addWhere( 'iw_local = 0' );
238 elseif( $filter )
239 ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
240
241 $this->addOption( 'ORDER BY', 'iw_prefix' );
242
243 $db = $this->getDB();
244 $res = $this->select( __METHOD__ );
245
246 $data = array();
247 $langNames = Language::getLanguageNames();
248 while( $row = $db->fetchObject($res) )
249 {
250 $val = array();
251 $val['prefix'] = $row->iw_prefix;
252 if( $row->iw_local == '1' )
253 $val['local'] = '';
254 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
255 if( isset( $langNames[$row->iw_prefix] ) )
256 $val['language'] = $langNames[$row->iw_prefix];
257 $val['url'] = $row->iw_url;
258
259 $data[] = $val;
260 }
261 $db->freeResult( $res );
262
263 $this->getResult()->setIndexedTagName( $data, 'iw' );
264 return $this->getResult()->addValue( 'query', $property, $data );
265 }
266
267 protected function appendDbReplLagInfo( $property, $includeAll ) {
268 global $wgShowHostnames;
269 $data = array();
270 if( $includeAll ) {
271 if ( !$wgShowHostnames )
272 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
273
274 $lb = wfGetLB();
275 $lags = $lb->getLagTimes();
276 foreach( $lags as $i => $lag ) {
277 $data[] = array(
278 'host' => $lb->getServerName( $i ),
279 'lag' => $lag
280 );
281 }
282 } else {
283 list( $host, $lag ) = wfGetLB()->getMaxLag();
284 $data[] = array(
285 'host' => $wgShowHostnames ? $host : '',
286 'lag' => intval( $lag )
287 );
288 }
289
290 $result = $this->getResult();
291 $result->setIndexedTagName( $data, 'db' );
292 return $this->getResult()->addValue( 'query', $property, $data );
293 }
294
295 protected function appendStatistics( $property ) {
296 global $wgDisableCounters;
297 $data = array();
298 $data['pages'] = intval( SiteStats::pages() );
299 $data['articles'] = intval( SiteStats::articles() );
300 if ( !$wgDisableCounters ) {
301 $data['views'] = intval( SiteStats::views() );
302 }
303 $data['edits'] = intval( SiteStats::edits() );
304 $data['images'] = intval( SiteStats::images() );
305 $data['users'] = intval( SiteStats::users() );
306 $data['activeusers'] = intval( SiteStats::activeUsers() );
307 $data['admins'] = intval( SiteStats::numberingroup('sysop') );
308 $data['jobs'] = intval( SiteStats::jobs() );
309 return $this->getResult()->addValue( 'query', $property, $data );
310 }
311
312 protected function appendUserGroups( $property ) {
313 global $wgGroupPermissions;
314 $data = array();
315 foreach( $wgGroupPermissions as $group => $permissions ) {
316 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
317 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
318 $data[] = $arr;
319 }
320
321 $this->getResult()->setIndexedTagName( $data, 'group' );
322 return $this->getResult()->addValue( 'query', $property, $data );
323 }
324
325 protected function appendFileExtensions( $property ) {
326 global $wgFileExtensions;
327
328 $data = array();
329 foreach( $wgFileExtensions as $ext ) {
330 $data[] = array( 'ext' => $ext );
331 }
332 $this->getResult()->setIndexedTagName( $data, 'fe' );
333 return $this->getResult()->addValue( 'query', $property, $data );
334 }
335
336 protected function appendExtensions( $property ) {
337 global $wgExtensionCredits;
338 $data = array();
339 foreach ( $wgExtensionCredits as $type => $extensions ) {
340 foreach ( $extensions as $ext ) {
341 $ret = array();
342 $ret['type'] = $type;
343 if ( isset( $ext['name'] ) )
344 $ret['name'] = $ext['name'];
345 if ( isset( $ext['description'] ) )
346 $ret['description'] = $ext['description'];
347 if ( isset( $ext['descriptionmsg'] ) )
348 $ret['descriptionmsg'] = $ext['descriptionmsg'];
349 if ( isset( $ext['author'] ) ) {
350 $ret['author'] = is_array( $ext['author'] ) ?
351 implode( ', ', $ext['author' ] ) : $ext['author'];
352 }
353 if ( isset( $ext['version'] ) ) {
354 $ret['version'] = $ext['version'];
355 } elseif ( isset( $ext['svn-revision'] ) &&
356 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
357 $ext['svn-revision'], $m ) )
358 {
359 $ret['version'] = 'r' . $m[1];
360 }
361 $data[] = $ret;
362 }
363 }
364
365 $this->getResult()->setIndexedTagName( $data, 'ext' );
366 return $this->getResult()->addValue( 'query', $property, $data );
367 }
368
369
370 protected function appendRightsInfo( $property ) {
371 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
372 $title = Title::newFromText( $wgRightsPage );
373 $url = $title ? $title->getFullURL() : $wgRightsUrl;
374 $text = $wgRightsText;
375 if( !$text && $title ) {
376 $text = $title->getPrefixedText();
377 }
378
379 $data = array(
380 'url' => $url ? $url : '',
381 'text' => $text ? $text : ''
382 );
383
384 return $this->getResult()->addValue( 'query', $property, $data );
385 }
386
387
388 public function getAllowedParams() {
389 return array(
390 'prop' => array(
391 ApiBase :: PARAM_DFLT => 'general',
392 ApiBase :: PARAM_ISMULTI => true,
393 ApiBase :: PARAM_TYPE => array(
394 'general',
395 'namespaces',
396 'namespacealiases',
397 'specialpagealiases',
398 'magicwords',
399 'interwikimap',
400 'dbrepllag',
401 'statistics',
402 'usergroups',
403 'extensions',
404 'fileextensions',
405 'rightsinfo',
406 )
407 ),
408 'filteriw' => array(
409 ApiBase :: PARAM_TYPE => array(
410 'local',
411 '!local',
412 )
413 ),
414 'showalldb' => false,
415 );
416 }
417
418 public function getParamDescription() {
419 return array(
420 'prop' => array(
421 'Which sysinfo properties to get:',
422 ' general - Overall system information',
423 ' namespaces - List of registered namespaces and their canonical names',
424 ' namespacealiases - List of registered namespace aliases',
425 ' specialpagealiases - List of special page aliases',
426 ' magicwords - List of magic words and their aliases',
427 ' statistics - Returns site statistics',
428 ' interwikimap - Returns interwiki map (optionally filtered)',
429 ' dbrepllag - Returns database server with the highest replication lag',
430 ' usergroups - Returns user groups and the associated permissions',
431 ' extensions - Returns extensions installed on the wiki',
432 ' fileextensions - Returns list of file extensions allowed to be uploaded',
433 ' rightsinfo - Returns wiki rights (license) information if available',
434 ),
435 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
436 'showalldb' => 'List all database servers, not just the one lagging the most',
437 );
438 }
439
440 public function getDescription() {
441 return 'Return general information about the site.';
442 }
443
444 protected function getExamples() {
445 return array(
446 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
447 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
448 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
449 );
450 }
451
452 public function getVersion() {
453 return __CLASS__ . ': $Id$';
454 }
455 }