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