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