Followup to r45810: remove unused global declaration
[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 foreach( $params['prop'] as $p )
45 {
46 switch ( $p )
47 {
48 case 'general':
49 $this->appendGeneralInfo( $p );
50 break;
51 case 'namespaces':
52 $this->appendNamespaces( $p );
53 break;
54 case 'namespacealiases':
55 $this->appendNamespaceAliases( $p );
56 break;
57 case 'specialpagealiases':
58 $this->appendSpecialPageAliases( $p );
59 break;
60 case 'magicwords':
61 $this->appendMagicWords( $p );
62 break;
63 case 'interwikimap':
64 $filteriw = isset( $params['filteriw'] ) ? $params['filteriw'] : false;
65 $this->appendInterwikiMap( $p, $filteriw );
66 break;
67 case 'dbrepllag':
68 $this->appendDbReplLagInfo( $p, $params['showalldb'] );
69 break;
70 case 'statistics':
71 $this->appendStatistics( $p );
72 break;
73 case 'usergroups':
74 $this->appendUserGroups( $p );
75 break;
76 case 'extensions':
77 $this->appendExtensions( $p );
78 break;
79 case 'fileextensions':
80 $this->appendFileExtensions( $p );
81 break;
82 case 'configvars':
83 $this->appendConfigvars( $p, $params['configvars'] );
84 break;
85 default :
86 ApiBase :: dieDebug( __METHOD__, "Unknown prop=$p" );
87 }
88 }
89 }
90
91 protected function appendGeneralInfo( $property ) {
92 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgContLang;
93 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
94
95 $data = array();
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";
101
102 $svn = SpecialVersion::getSvnRevision( $IP );
103 if( $svn )
104 $data['rev'] = $svn;
105
106 $data['case'] = $wgCapitalLinks ? 'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
107
108 if( isset( $wgRightsCode ) )
109 $data['rightscode'] = $wgRightsCode;
110 $data['rights'] = $wgRightsText;
111 $data['lang'] = $wgLanguageCode;
112 if( $wgContLang->isRTL() )
113 $data['rtl'] = '';
114 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
115
116 if( wfReadOnly() )
117 $data['readonly'] = '';
118 if( $wgEnableWriteAPI )
119 $data['writeapi'] = '';
120
121 $tz = $wgLocaltimezone;
122 $offset = $wgLocalTZoffset;
123 if( is_null( $tz ) ) {
124 $tz = 'UTC';
125 $offset = 0;
126 } elseif( is_null( $offset ) ) {
127 $offset = 0;
128 }
129 $data['timezone'] = $tz;
130 $data['timeoffset'] = $offset;
131
132 $this->getResult()->addValue( 'query', $property, $data );
133 }
134
135 protected function appendNamespaces( $property ) {
136 global $wgContLang;
137 $data = array();
138 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
139 {
140 $data[$ns] = array(
141 'id' => $ns
142 );
143 ApiResult :: setContent( $data[$ns], $title );
144 $canonical = MWNamespace::getCanonicalName( $ns );
145
146 if( MWNamespace::hasSubpages( $ns ) )
147 $data[$ns]['subpages'] = '';
148
149 if( $canonical )
150 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
151 }
152
153 $this->getResult()->setIndexedTagName( $data, 'ns' );
154 $this->getResult()->addValue( 'query', $property, $data );
155 }
156
157 protected function appendNamespaceAliases( $property ) {
158 global $wgNamespaceAliases, $wgContLang;
159 $wgContLang->load();
160 $aliases = array_merge($wgNamespaceAliases, $wgContLang->namespaceAliases);
161 $data = array();
162 foreach( $aliases as $title => $ns ) {
163 $item = array(
164 'id' => $ns
165 );
166 ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
167 $data[] = $item;
168 }
169
170 $this->getResult()->setIndexedTagName( $data, 'ns' );
171 $this->getResult()->addValue( 'query', $property, $data );
172 }
173
174 protected function appendSpecialPageAliases( $property ) {
175 global $wgLang;
176 $data = array();
177 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
178 {
179 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
180 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
181 $data[] = $arr;
182 }
183 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
184 $this->getResult()->addValue( 'query', $property, $data );
185 }
186
187 protected function appendMagicWords( $property ) {
188 global $wgContLang;
189 $data = array();
190 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
191 {
192 $caseSensitive = array_shift($aliases);
193 $arr = array('name' => $magicword, 'aliases' => $aliases);
194 if($caseSensitive)
195 $arr['case-sensitive'] = '';
196 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
197 $data[] = $arr;
198 }
199 $this->getResult()->setIndexedTagName($data, 'magicword');
200 $this->getResult()->addValue('query', $property, $data);
201 }
202
203 protected function appendInterwikiMap( $property, $filter ) {
204 $this->resetQueryParams();
205 $this->addTables( 'interwiki' );
206 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
207
208 if( $filter === 'local' )
209 $this->addWhere( 'iw_local = 1' );
210 elseif( $filter === '!local' )
211 $this->addWhere( 'iw_local = 0' );
212 elseif( $filter )
213 ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
214
215 $this->addOption( 'ORDER BY', 'iw_prefix' );
216
217 $db = $this->getDB();
218 $res = $this->select( __METHOD__ );
219
220 $data = array();
221 $langNames = Language::getLanguageNames();
222 while( $row = $db->fetchObject($res) )
223 {
224 $val = array();
225 $val['prefix'] = $row->iw_prefix;
226 if( $row->iw_local == '1' )
227 $val['local'] = '';
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;
232
233 $data[] = $val;
234 }
235 $db->freeResult( $res );
236
237 $this->getResult()->setIndexedTagName( $data, 'iw' );
238 $this->getResult()->addValue( 'query', $property, $data );
239 }
240
241 protected function appendDbReplLagInfo( $property, $includeAll ) {
242 global $wgShowHostnames;
243 $data = array();
244 if( $includeAll ) {
245 if ( !$wgShowHostnames )
246 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
247
248 $lb = wfGetLB();
249 $lags = $lb->getLagTimes();
250 foreach( $lags as $i => $lag ) {
251 $data[] = array(
252 'host' => $lb->getServerName( $i ),
253 'lag' => $lag
254 );
255 }
256 } else {
257 list( $host, $lag ) = wfGetLB()->getMaxLag();
258 $data[] = array(
259 'host' => $wgShowHostnames ? $host : '',
260 'lag' => $lag
261 );
262 }
263
264 $result = $this->getResult();
265 $result->setIndexedTagName( $data, 'db' );
266 $result->addValue( 'query', $property, $data );
267 }
268
269 protected function appendStatistics( $property ) {
270 $data = array();
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 );
281 }
282
283 protected function appendUserGroups( $property ) {
284 global $wgGroupPermissions;
285 $data = array();
286 foreach( $wgGroupPermissions as $group => $permissions ) {
287 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
288 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
289 $data[] = $arr;
290 }
291
292 $this->getResult()->setIndexedTagName( $data, 'group' );
293 $this->getResult()->addValue( 'query', $property, $data );
294 }
295
296 protected function appendFileExtensions( $property ) {
297 global $wgFileExtensions;
298
299 $data = array();
300 foreach( $wgFileExtensions as $ext ) {
301 $data[] = array( 'ext' => $ext );
302 }
303 $this->getResult()->setIndexedTagName( $data, 'fe' );
304 $this->getResult()->addValue( 'query', $property, $data );
305 }
306
307 protected function appendExtensions( $property ) {
308 global $wgExtensionCredits;
309 $data = array();
310 foreach ( $wgExtensionCredits as $type => $extensions ) {
311 foreach ( $extensions as $ext ) {
312 $ret = array();
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'];
323 }
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 ) )
329 {
330 $ret['version'] = 'r' . $m[1];
331 }
332 $data[] = $ret;
333 }
334 }
335
336 $this->getResult()->setIndexedTagName( $data, 'ext' );
337 $this->getResult()->addValue( 'query', $property, $data );
338 }
339
340 protected function appendConfigvars( $property, $vars ) {
341 global $wgAPIReadableConfigVars;
342 if(empty($vars))
343 $vars = $wgAPIReadableConfigVars;
344 $data = array();
345 foreach((array)$vars as $var)
346 {
347 $value = $GLOBALS[$var];
348 $r = array();
349 $r['name'] = $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))
356 {
357 $r['value'] = $value;
358 if(is_array($value))
359 {
360 $this->getResult()->setIndexedTagName($r['value'], 'elem');
361 $this->getResult()->setIndexedTagName_recursive($r['value'], 'elem');
362 }
363 }
364 $data[] = $r;
365 }
366 $this->getResult()->setIndexedTagName($data, 'config');
367 $this->getResult()->addValue('query', $property, $data);
368 }
369
370 public function getAllowedParams() {
371 global $wgAPIReadableConfigVars;
372 return array(
373 'prop' => array(
374 ApiBase :: PARAM_DFLT => 'general',
375 ApiBase :: PARAM_ISMULTI => true,
376 ApiBase :: PARAM_TYPE => array(
377 'general',
378 'namespaces',
379 'namespacealiases',
380 'specialpagealiases',
381 'magicwords',
382 'interwikimap',
383 'dbrepllag',
384 'statistics',
385 'usergroups',
386 'extensions',
387 'fileextensions',
388 'configvars',
389 )
390 ),
391 'filteriw' => array(
392 ApiBase :: PARAM_TYPE => array(
393 'local',
394 '!local',
395 )
396 ),
397 'showalldb' => false,
398 'configvars' => array(
399 ApiBase :: PARAM_ISMULTI => true,
400 ApiBase :: PARAM_TYPE => $wgAPIReadableConfigVars,
401 ),
402 );
403 }
404
405 public function getParamDescription() {
406 return array(
407 'prop' => array(
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',
421 ),
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.',
425 );
426 }
427
428 public function getDescription() {
429 return 'Return general information about the site.';
430 }
431
432 protected function getExamples() {
433 return array(
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',
437 );
438 }
439
440 public function getVersion() {
441 return __CLASS__ . ': $Id$';
442 }
443 }