Normalise comment usage (# --> //)
[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, $params['numberingroup'] );
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 $data['time'] = wfTimestampNow( TS_ISO_8601 );
158
159 return $this->getResult()->addValue( 'query', $property, $data );
160 }
161
162 protected function appendNamespaces( $property ) {
163 global $wgContLang;
164 $data = array();
165 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title )
166 {
167 $data[$ns] = array(
168 'id' => intval( $ns ),
169 'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
170 );
171 ApiResult :: setContent( $data[$ns], $title );
172 $canonical = MWNamespace::getCanonicalName( $ns );
173
174 if ( MWNamespace::hasSubpages( $ns ) )
175 $data[$ns]['subpages'] = '';
176
177 if ( $canonical )
178 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
179
180 if ( MWNamespace::isContent( $ns ) )
181 $data[$ns]['content'] = '';
182 }
183
184 $this->getResult()->setIndexedTagName( $data, 'ns' );
185 return $this->getResult()->addValue( 'query', $property, $data );
186 }
187
188 protected function appendNamespaceAliases( $property ) {
189 global $wgNamespaceAliases, $wgContLang;
190 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
191 $namespaces = $wgContLang->getNamespaces();
192 $data = array();
193 foreach ( $aliases as $title => $ns ) {
194 if ( $namespaces[$ns] == $title ) {
195 // Don't list duplicates
196 continue;
197 }
198 $item = array(
199 'id' => intval( $ns )
200 );
201 ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
202 $data[] = $item;
203 }
204
205 $this->getResult()->setIndexedTagName( $data, 'ns' );
206 return $this->getResult()->addValue( 'query', $property, $data );
207 }
208
209 protected function appendSpecialPageAliases( $property ) {
210 global $wgLang;
211 $data = array();
212 foreach ( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
213 {
214 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
215 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
216 $data[] = $arr;
217 }
218 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
219 return $this->getResult()->addValue( 'query', $property, $data );
220 }
221
222 protected function appendMagicWords( $property ) {
223 global $wgContLang;
224 $data = array();
225 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases )
226 {
227 $caseSensitive = array_shift( $aliases );
228 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
229 if ( $caseSensitive )
230 $arr['case-sensitive'] = '';
231 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
232 $data[] = $arr;
233 }
234 $this->getResult()->setIndexedTagName( $data, 'magicword' );
235 return $this->getResult()->addValue( 'query', $property, $data );
236 }
237
238 protected function appendInterwikiMap( $property, $filter ) {
239 $this->resetQueryParams();
240 $this->addTables( 'interwiki' );
241 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
242
243 if ( $filter === 'local' )
244 $this->addWhere( 'iw_local = 1' );
245 elseif ( $filter === '!local' )
246 $this->addWhere( 'iw_local = 0' );
247 elseif ( $filter )
248 ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
249
250 $this->addOption( 'ORDER BY', 'iw_prefix' );
251
252 $db = $this->getDB();
253 $res = $this->select( __METHOD__ );
254
255 $data = array();
256 $langNames = Language::getLanguageNames();
257 while ( $row = $db->fetchObject( $res ) )
258 {
259 $val = array();
260 $val['prefix'] = $row->iw_prefix;
261 if ( $row->iw_local == '1' )
262 $val['local'] = '';
263 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
264 if ( isset( $langNames[$row->iw_prefix] ) )
265 $val['language'] = $langNames[$row->iw_prefix];
266 $val['url'] = $row->iw_url;
267
268 $data[] = $val;
269 }
270 $db->freeResult( $res );
271
272 $this->getResult()->setIndexedTagName( $data, 'iw' );
273 return $this->getResult()->addValue( 'query', $property, $data );
274 }
275
276 protected function appendDbReplLagInfo( $property, $includeAll ) {
277 global $wgShowHostnames;
278 $data = array();
279 if ( $includeAll ) {
280 if ( !$wgShowHostnames )
281 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
282
283 $lb = wfGetLB();
284 $lags = $lb->getLagTimes();
285 foreach ( $lags as $i => $lag ) {
286 $data[] = array(
287 'host' => $lb->getServerName( $i ),
288 'lag' => $lag
289 );
290 }
291 } else {
292 list( $host, $lag ) = wfGetLB()->getMaxLag();
293 $data[] = array(
294 'host' => $wgShowHostnames ? $host : '',
295 'lag' => intval( $lag )
296 );
297 }
298
299 $result = $this->getResult();
300 $result->setIndexedTagName( $data, 'db' );
301 return $this->getResult()->addValue( 'query', $property, $data );
302 }
303
304 protected function appendStatistics( $property ) {
305 global $wgDisableCounters;
306 $data = array();
307 $data['pages'] = intval( SiteStats::pages() );
308 $data['articles'] = intval( SiteStats::articles() );
309 if ( !$wgDisableCounters ) {
310 $data['views'] = intval( SiteStats::views() );
311 }
312 $data['edits'] = intval( SiteStats::edits() );
313 $data['images'] = intval( SiteStats::images() );
314 $data['users'] = intval( SiteStats::users() );
315 $data['activeusers'] = intval( SiteStats::activeUsers() );
316 $data['admins'] = intval( SiteStats::numberingroup( 'sysop' ) );
317 $data['jobs'] = intval( SiteStats::jobs() );
318 return $this->getResult()->addValue( 'query', $property, $data );
319 }
320
321 protected function appendUserGroups( $property, $numberInGroup ) {
322 global $wgGroupPermissions;
323 $data = array();
324 foreach ( $wgGroupPermissions as $group => $permissions ) {
325 $arr = array(
326 'name' => $group,
327 'rights' => array_keys( $permissions, true ),
328 );
329 if ( $numberInGroup )
330 $arr['number'] = SiteStats::numberInGroup( $group );
331
332 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
333 $data[] = $arr;
334 }
335
336 $this->getResult()->setIndexedTagName( $data, 'group' );
337 return $this->getResult()->addValue( 'query', $property, $data );
338 }
339
340 protected function appendFileExtensions( $property ) {
341 global $wgFileExtensions;
342
343 $data = array();
344 foreach ( $wgFileExtensions as $ext ) {
345 $data[] = array( 'ext' => $ext );
346 }
347 $this->getResult()->setIndexedTagName( $data, 'fe' );
348 return $this->getResult()->addValue( 'query', $property, $data );
349 }
350
351 protected function appendExtensions( $property ) {
352 global $wgExtensionCredits;
353 $data = array();
354 foreach ( $wgExtensionCredits as $type => $extensions ) {
355 foreach ( $extensions as $ext ) {
356 $ret = array();
357 $ret['type'] = $type;
358 if ( isset( $ext['name'] ) )
359 $ret['name'] = $ext['name'];
360 if ( isset( $ext['description'] ) )
361 $ret['description'] = $ext['description'];
362 if ( isset( $ext['descriptionmsg'] ) )
363 $ret['descriptionmsg'] = $ext['descriptionmsg'];
364 if ( isset( $ext['author'] ) ) {
365 $ret['author'] = is_array( $ext['author'] ) ?
366 implode( ', ', $ext['author' ] ) : $ext['author'];
367 }
368 if ( isset( $ext['version'] ) ) {
369 $ret['version'] = $ext['version'];
370 } elseif ( isset( $ext['svn-revision'] ) &&
371 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
372 $ext['svn-revision'], $m ) )
373 {
374 $ret['version'] = 'r' . $m[1];
375 }
376 $data[] = $ret;
377 }
378 }
379
380 $this->getResult()->setIndexedTagName( $data, 'ext' );
381 return $this->getResult()->addValue( 'query', $property, $data );
382 }
383
384
385 protected function appendRightsInfo( $property ) {
386 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
387 $title = Title::newFromText( $wgRightsPage );
388 $url = $title ? $title->getFullURL() : $wgRightsUrl;
389 $text = $wgRightsText;
390 if ( !$text && $title ) {
391 $text = $title->getPrefixedText();
392 }
393
394 $data = array(
395 'url' => $url ? $url : '',
396 'text' => $text ? $text : ''
397 );
398
399 return $this->getResult()->addValue( 'query', $property, $data );
400 }
401
402 public function appendLanguages( $property ) {
403 $data = array();
404 foreach ( Language::getLanguageNames() as $code => $name ) {
405 $lang = array( 'code' => $code );
406 ApiResult::setContent( $lang, $name );
407 $data[] = $lang;
408 }
409 $this->getResult()->setIndexedTagName( $data, 'lang' );
410 return $this->getResult()->addValue( 'query', $property, $data );
411 }
412
413
414 public function getAllowedParams() {
415 return array(
416 'prop' => array(
417 ApiBase :: PARAM_DFLT => 'general',
418 ApiBase :: PARAM_ISMULTI => true,
419 ApiBase :: PARAM_TYPE => array(
420 'general',
421 'namespaces',
422 'namespacealiases',
423 'specialpagealiases',
424 'magicwords',
425 'interwikimap',
426 'dbrepllag',
427 'statistics',
428 'usergroups',
429 'extensions',
430 'fileextensions',
431 'rightsinfo',
432 'languages',
433 )
434 ),
435 'filteriw' => array(
436 ApiBase :: PARAM_TYPE => array(
437 'local',
438 '!local',
439 )
440 ),
441 'showalldb' => false,
442 'numberingroup' => false,
443 );
444 }
445
446 public function getParamDescription() {
447 return array(
448 'prop' => array(
449 'Which sysinfo properties to get:',
450 ' general - Overall system information',
451 ' namespaces - List of registered namespaces and their canonical names',
452 ' namespacealiases - List of registered namespace aliases',
453 ' specialpagealiases - List of special page aliases',
454 ' magicwords - List of magic words and their aliases',
455 ' statistics - Returns site statistics',
456 ' interwikimap - Returns interwiki map (optionally filtered)',
457 ' dbrepllag - Returns database server with the highest replication lag',
458 ' usergroups - Returns user groups and the associated permissions',
459 ' extensions - Returns extensions installed on the wiki',
460 ' fileextensions - Returns list of file extensions allowed to be uploaded',
461 ' rightsinfo - Returns wiki rights (license) information if available',
462 ' languages - Returns a list of languages MediaWiki supports',
463 ),
464 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
465 'showalldb' => 'List all database servers, not just the one lagging the most',
466 'numberingroup' => 'Lists the number of users in user groups',
467 );
468 }
469
470 public function getDescription() {
471 return 'Return general information about the site.';
472 }
473
474 protected function getExamples() {
475 return array(
476 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
477 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
478 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
479 );
480 }
481
482 public function getVersion() {
483 return __CLASS__ . ': $Id$';
484 }
485 }