Followup r70587: Remove unnecessary references
[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 © 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 switch ( $p ) {
47 case 'general':
48 $fit = $this->appendGeneralInfo( $p );
49 break;
50 case 'namespaces':
51 $fit = $this->appendNamespaces( $p );
52 break;
53 case 'namespacealiases':
54 $fit = $this->appendNamespaceAliases( $p );
55 break;
56 case 'specialpagealiases':
57 $fit = $this->appendSpecialPageAliases( $p );
58 break;
59 case 'magicwords':
60 $fit = $this->appendMagicWords( $p );
61 break;
62 case 'interwikimap':
63 $filteriw = isset( $params['filteriw'] ) ? $params['filteriw'] : false;
64 $fit = $this->appendInterwikiMap( $p, $filteriw );
65 break;
66 case 'dbrepllag':
67 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
68 break;
69 case 'statistics':
70 $fit = $this->appendStatistics( $p );
71 break;
72 case 'usergroups':
73 $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
74 break;
75 case 'extensions':
76 $fit = $this->appendExtensions( $p );
77 break;
78 case 'fileextensions':
79 $fit = $this->appendFileExtensions( $p );
80 break;
81 case 'rightsinfo':
82 $fit = $this->appendRightsInfo( $p );
83 break;
84 case 'languages':
85 $fit = $this->appendLanguages( $p );
86 break;
87 default:
88 ApiBase::dieDebug( __METHOD__, "Unknown prop=$p" );
89 }
90 if ( !$fit ) {
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
104 $data = array();
105 $mainPage = Title::newMainPage();
106 $data['mainpage'] = $mainPage->getPrefixedText();
107 $data['base'] = $mainPage->getFullUrl();
108 $data['sitename'] = $GLOBALS['wgSitename'];
109 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
110 $data['phpversion'] = phpversion();
111 $data['phpsapi'] = php_sapi_name();
112 $data['dbtype'] = $GLOBALS['wgDBtype'];
113 $data['dbversion'] = $this->getDB()->getServerVersion();
114
115 $svn = SpecialVersion::getSvnRevision( $GLOBALS['IP'] );
116 if ( $svn ) {
117 $data['rev'] = $svn;
118 }
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 }
126 $data['rights'] = $GLOBALS['wgRightsText'];
127 $data['lang'] = $GLOBALS['wgLanguageCode'];
128 if ( $wgContLang->isRTL() ) {
129 $data['rtl'] = '';
130 }
131 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
132
133 if ( wfReadOnly() ) {
134 $data['readonly'] = '';
135 $data['readonlyreason'] = wfReadOnlyReason();
136 }
137 if ( $GLOBALS['wgEnableWriteAPI'] ) {
138 $data['writeapi'] = '';
139 }
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'] = wfTimestamp( TS_ISO_8601, time() );
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 $data[$ns] = array(
167 'id' => intval( $ns ),
168 'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
169 );
170 ApiResult::setContent( $data[$ns], $title );
171 $canonical = MWNamespace::getCanonicalName( $ns );
172
173 if ( MWNamespace::hasSubpages( $ns ) ) {
174 $data[$ns]['subpages'] = '';
175 }
176
177 if ( $canonical ) {
178 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
179 }
180
181 if ( MWNamespace::isContent( $ns ) ) {
182 $data[$ns]['content'] = '';
183 }
184 }
185
186 $this->getResult()->setIndexedTagName( $data, 'ns' );
187 return $this->getResult()->addValue( 'query', $property, $data );
188 }
189
190 protected function appendNamespaceAliases( $property ) {
191 global $wgNamespaceAliases, $wgContLang;
192 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
193 $namespaces = $wgContLang->getNamespaces();
194 $data = array();
195 foreach ( $aliases as $title => $ns ) {
196 if ( $namespaces[$ns] == $title ) {
197 // Don't list duplicates
198 continue;
199 }
200 $item = array(
201 'id' => intval( $ns )
202 );
203 ApiResult::setContent( $item, strtr( $title, '_', ' ' ) );
204 $data[] = $item;
205 }
206
207 $this->getResult()->setIndexedTagName( $data, 'ns' );
208 return $this->getResult()->addValue( 'query', $property, $data );
209 }
210
211 protected function appendSpecialPageAliases( $property ) {
212 global $wgContLang;
213 $data = array();
214 foreach ( $wgContLang->getSpecialPageAliases() as $specialpage => $aliases )
215 {
216 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
217 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
218 $data[] = $arr;
219 }
220 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
221 return $this->getResult()->addValue( 'query', $property, $data );
222 }
223
224 protected function appendMagicWords( $property ) {
225 global $wgContLang;
226 $data = array();
227 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
228 $caseSensitive = array_shift( $aliases );
229 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
230 if ( $caseSensitive ) {
231 $arr['case-sensitive'] = '';
232 }
233 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
234 $data[] = $arr;
235 }
236 $this->getResult()->setIndexedTagName( $data, 'magicword' );
237 return $this->getResult()->addValue( 'query', $property, $data );
238 }
239
240 protected function appendInterwikiMap( $property, $filter ) {
241 $this->resetQueryParams();
242 $this->addTables( 'interwiki' );
243 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
244
245 if ( $filter === 'local' ) {
246 $this->addWhere( 'iw_local = 1' );
247 } elseif ( $filter === '!local' ) {
248 $this->addWhere( 'iw_local = 0' );
249 } elseif ( $filter ) {
250 ApiBase::dieDebug( __METHOD__, "Unknown filter=$filter" );
251 }
252
253 $this->addOption( 'ORDER BY', 'iw_prefix' );
254
255 $res = $this->select( __METHOD__ );
256
257 $data = array();
258 $langNames = Language::getLanguageNames();
259 foreach ( $res as $row ) {
260 $val = array();
261 $val['prefix'] = $row->iw_prefix;
262 if ( $row->iw_local == '1' ) {
263 $val['local'] = '';
264 }
265 // $val['trans'] = intval( $row->iw_trans ); // should this be exposed?
266 if ( isset( $langNames[$row->iw_prefix] ) ) {
267 $val['language'] = $langNames[$row->iw_prefix];
268 }
269 $val['url'] = $row->iw_url;
270
271 $data[] = $val;
272 }
273
274 $this->getResult()->setIndexedTagName( $data, 'iw' );
275 return $this->getResult()->addValue( 'query', $property, $data );
276 }
277
278 protected function appendDbReplLagInfo( $property, $includeAll ) {
279 global $wgShowHostnames;
280 $data = array();
281 if ( $includeAll ) {
282 if ( !$wgShowHostnames ) {
283 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
284 }
285
286 $lb = wfGetLB();
287 $lags = $lb->getLagTimes();
288 foreach ( $lags as $i => $lag ) {
289 $data[] = array(
290 'host' => $lb->getServerName( $i ),
291 'lag' => $lag
292 );
293 }
294 } else {
295 list( $host, $lag ) = wfGetLB()->getMaxLag();
296 $data[] = array(
297 'host' => $wgShowHostnames ? $host : '',
298 'lag' => intval( $lag )
299 );
300 }
301
302 $result = $this->getResult();
303 $result->setIndexedTagName( $data, 'db' );
304 return $this->getResult()->addValue( 'query', $property, $data );
305 }
306
307 protected function appendStatistics( $property ) {
308 global $wgDisableCounters;
309 $data = array();
310 $data['pages'] = intval( SiteStats::pages() );
311 $data['articles'] = intval( SiteStats::articles() );
312 if ( !$wgDisableCounters ) {
313 $data['views'] = intval( SiteStats::views() );
314 }
315 $data['edits'] = intval( SiteStats::edits() );
316 $data['images'] = intval( SiteStats::images() );
317 $data['users'] = intval( SiteStats::users() );
318 $data['activeusers'] = intval( SiteStats::activeUsers() );
319 $data['admins'] = intval( SiteStats::numberingroup( 'sysop' ) );
320 $data['jobs'] = intval( SiteStats::jobs() );
321 return $this->getResult()->addValue( 'query', $property, $data );
322 }
323
324 protected function appendUserGroups( $property, $numberInGroup ) {
325 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
326
327 $data = array();
328 foreach ( $wgGroupPermissions as $group => $permissions ) {
329 $arr = array(
330 'name' => $group,
331 'rights' => array_keys( $permissions, true ),
332 );
333 if ( $numberInGroup ) {
334 $arr['number'] = SiteStats::numberInGroup( $group );
335 }
336
337 $groupArr = array(
338 'add' => $wgAddGroups,
339 'remove' => $wgRemoveGroups,
340 'add-self' => $wgGroupsAddToSelf,
341 'remove-self' => $wgGroupsRemoveFromSelf
342 );
343
344 foreach( $groupArr as $type => $rights ) {
345 if( isset( $rights[$group] ) ) {
346 $arr[$type] = $rights[$group];
347 $this->getResult()->setIndexedTagName( $arr[$type], 'group' );
348 }
349 }
350
351 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
352 $data[] = $arr;
353 }
354
355 $this->getResult()->setIndexedTagName( $data, 'group' );
356 return $this->getResult()->addValue( 'query', $property, $data );
357 }
358
359 protected function appendFileExtensions( $property ) {
360 global $wgFileExtensions;
361
362 $data = array();
363 foreach ( $wgFileExtensions as $ext ) {
364 $data[] = array( 'ext' => $ext );
365 }
366 $this->getResult()->setIndexedTagName( $data, 'fe' );
367 return $this->getResult()->addValue( 'query', $property, $data );
368 }
369
370 protected function appendExtensions( $property ) {
371 global $wgExtensionCredits;
372 $data = array();
373 foreach ( $wgExtensionCredits as $type => $extensions ) {
374 foreach ( $extensions as $ext ) {
375 $ret = array();
376 $ret['type'] = $type;
377 if ( isset( $ext['name'] ) ) {
378 $ret['name'] = $ext['name'];
379 }
380 if ( isset( $ext['description'] ) ) {
381 $ret['description'] = $ext['description'];
382 }
383 if ( isset( $ext['descriptionmsg'] ) ) {
384 // Can be a string or array( key, param1, param2, ... )
385 if ( is_array( $ext['descriptionmsg'] ) ) {
386 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
387 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
388 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
389 } else {
390 $ret['descriptionmsg'] = $ext['descriptionmsg'];
391 }
392 }
393 if ( isset( $ext['author'] ) ) {
394 $ret['author'] = is_array( $ext['author'] ) ?
395 implode( ', ', $ext['author' ] ) : $ext['author'];
396 }
397 if ( isset( $ext['url'] ) ) {
398 $ret['url'] = $ext['url'];
399 }
400 if ( isset( $ext['version'] ) ) {
401 $ret['version'] = $ext['version'];
402 } elseif ( isset( $ext['svn-revision'] ) &&
403 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
404 $ext['svn-revision'], $m ) )
405 {
406 $ret['version'] = 'r' . $m[1];
407 }
408 $data[] = $ret;
409 }
410 }
411
412 $this->getResult()->setIndexedTagName( $data, 'ext' );
413 return $this->getResult()->addValue( 'query', $property, $data );
414 }
415
416
417 protected function appendRightsInfo( $property ) {
418 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
419 $title = Title::newFromText( $wgRightsPage );
420 $url = $title ? $title->getFullURL() : $wgRightsUrl;
421 $text = $wgRightsText;
422 if ( !$text && $title ) {
423 $text = $title->getPrefixedText();
424 }
425
426 $data = array(
427 'url' => $url ? $url : '',
428 'text' => $text ? $text : ''
429 );
430
431 return $this->getResult()->addValue( 'query', $property, $data );
432 }
433
434 public function appendLanguages( $property ) {
435 $data = array();
436 foreach ( Language::getLanguageNames() as $code => $name ) {
437 $lang = array( 'code' => $code );
438 ApiResult::setContent( $lang, $name );
439 $data[] = $lang;
440 }
441 $this->getResult()->setIndexedTagName( $data, 'lang' );
442 return $this->getResult()->addValue( 'query', $property, $data );
443 }
444
445 public function getCacheMode( $params ) {
446 return 'public';
447 }
448
449 public function getAllowedParams() {
450 return array(
451 'prop' => array(
452 ApiBase::PARAM_DFLT => 'general',
453 ApiBase::PARAM_ISMULTI => true,
454 ApiBase::PARAM_TYPE => array(
455 'general',
456 'namespaces',
457 'namespacealiases',
458 'specialpagealiases',
459 'magicwords',
460 'interwikimap',
461 'dbrepllag',
462 'statistics',
463 'usergroups',
464 'extensions',
465 'fileextensions',
466 'rightsinfo',
467 'languages',
468 )
469 ),
470 'filteriw' => array(
471 ApiBase::PARAM_TYPE => array(
472 'local',
473 '!local',
474 )
475 ),
476 'showalldb' => false,
477 'numberingroup' => false,
478 );
479 }
480
481 public function getParamDescription() {
482 return array(
483 'prop' => array(
484 'Which sysinfo properties to get:',
485 ' general - Overall system information',
486 ' namespaces - List of registered namespaces and their canonical names',
487 ' namespacealiases - List of registered namespace aliases',
488 ' specialpagealiases - List of special page aliases',
489 ' magicwords - List of magic words and their aliases',
490 ' statistics - Returns site statistics',
491 ' interwikimap - Returns interwiki map (optionally filtered)',
492 ' dbrepllag - Returns database server with the highest replication lag',
493 ' usergroups - Returns user groups and the associated permissions',
494 ' extensions - Returns extensions installed on the wiki',
495 ' fileextensions - Returns list of file extensions allowed to be uploaded',
496 ' rightsinfo - Returns wiki rights (license) information if available',
497 ' languages - Returns a list of languages MediaWiki supports',
498 ),
499 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
500 'showalldb' => 'List all database servers, not just the one lagging the most',
501 'numberingroup' => 'Lists the number of users in user groups',
502 );
503 }
504
505 public function getDescription() {
506 return 'Return general information about the site';
507 }
508
509 public function getPossibleErrors() {
510 return array_merge( parent::getPossibleErrors(), array(
511 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
512 ) );
513 }
514
515 protected function getExamples() {
516 return array(
517 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
518 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
519 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
520 );
521 }
522
523 public function getVersion() {
524 return __CLASS__ . ': $Id$';
525 }
526 }