Revert r73879 for now, not ready yet.
[lhc/web/wiklou.git] / includes / api / ApiQuerySiteinfo.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Sep 25, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * A query action to return meta information about the wiki site.
34 *
35 * @ingroup API
36 */
37 class ApiQuerySiteinfo extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'si' );
41 }
42
43 public function execute() {
44 $params = $this->extractRequestParams();
45 $done = array();
46 foreach ( $params['prop'] as $p ) {
47 switch ( $p ) {
48 case 'general':
49 $fit = $this->appendGeneralInfo( $p );
50 break;
51 case 'namespaces':
52 $fit = $this->appendNamespaces( $p );
53 break;
54 case 'namespacealiases':
55 $fit = $this->appendNamespaceAliases( $p );
56 break;
57 case 'specialpagealiases':
58 $fit = $this->appendSpecialPageAliases( $p );
59 break;
60 case 'magicwords':
61 $fit = $this->appendMagicWords( $p );
62 break;
63 case 'interwikimap':
64 $filteriw = isset( $params['filteriw'] ) ? $params['filteriw'] : false;
65 $fit = $this->appendInterwikiMap( $p, $filteriw );
66 break;
67 case 'dbrepllag':
68 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
69 break;
70 case 'statistics':
71 $fit = $this->appendStatistics( $p );
72 break;
73 case 'usergroups':
74 $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
75 break;
76 case 'extensions':
77 $fit = $this->appendExtensions( $p );
78 break;
79 case 'fileextensions':
80 $fit = $this->appendFileExtensions( $p );
81 break;
82 case 'rightsinfo':
83 $fit = $this->appendRightsInfo( $p );
84 break;
85 case 'languages':
86 $fit = $this->appendLanguages( $p );
87 break;
88 default:
89 ApiBase::dieDebug( __METHOD__, "Unknown prop=$p" );
90 }
91 if ( !$fit ) {
92 // Abuse siprop as a query-continue parameter
93 // and set it to all unprocessed props
94 $this->setContinueEnumParameter( 'prop', implode( '|',
95 array_diff( $params['prop'], $done ) ) );
96 break;
97 }
98 $done[] = $p;
99 }
100 }
101
102 protected function appendGeneralInfo( $property ) {
103 global $wgContLang;
104
105 $data = array();
106 $mainPage = Title::newMainPage();
107 $data['mainpage'] = $mainPage->getPrefixedText();
108 $data['base'] = $mainPage->getFullUrl();
109 $data['sitename'] = $GLOBALS['wgSitename'];
110 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
111 $data['phpversion'] = phpversion();
112 $data['phpsapi'] = php_sapi_name();
113 $data['dbtype'] = $GLOBALS['wgDBtype'];
114 $data['dbversion'] = $this->getDB()->getServerVersion();
115
116 $svn = SpecialVersion::getSvnRevision( $GLOBALS['IP'] );
117 if ( $svn ) {
118 $data['rev'] = $svn;
119 }
120
121 // 'case-insensitive' option is reserved for future
122 $data['case'] = $GLOBALS['wgCapitalLinks'] ? 'first-letter' : 'case-sensitive';
123
124 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
125 $data['rightscode'] = $GLOBALS['wgRightsCode'];
126 }
127 $data['rights'] = $GLOBALS['wgRightsText'];
128 $data['lang'] = $GLOBALS['wgLanguageCode'];
129 if ( $wgContLang->isRTL() ) {
130 $data['rtl'] = '';
131 }
132 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
133
134 if ( wfReadOnly() ) {
135 $data['readonly'] = '';
136 $data['readonlyreason'] = wfReadOnlyReason();
137 }
138 if ( $GLOBALS['wgEnableWriteAPI'] ) {
139 $data['writeapi'] = '';
140 }
141
142 $tz = $GLOBALS['wgLocaltimezone'];
143 $offset = $GLOBALS['wgLocalTZoffset'];
144 if ( is_null( $tz ) ) {
145 $tz = 'UTC';
146 $offset = 0;
147 } elseif ( is_null( $offset ) ) {
148 $offset = 0;
149 }
150 $data['timezone'] = $tz;
151 $data['timeoffset'] = intval( $offset );
152 $data['articlepath'] = $GLOBALS['wgArticlePath'];
153 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
154 $data['script'] = $GLOBALS['wgScript'];
155 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
156 $data['server'] = $GLOBALS['wgServer'];
157 $data['wikiid'] = wfWikiID();
158 $data['time'] = wfTimestamp( TS_ISO_8601, time() );
159
160 return $this->getResult()->addValue( 'query', $property, $data );
161 }
162
163 protected function appendNamespaces( $property ) {
164 global $wgContLang;
165 $data = array();
166 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
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
178 if ( $canonical ) {
179 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
180 }
181
182 if ( MWNamespace::isContent( $ns ) ) {
183 $data[$ns]['content'] = '';
184 }
185 }
186
187 $this->getResult()->setIndexedTagName( $data, 'ns' );
188 return $this->getResult()->addValue( 'query', $property, $data );
189 }
190
191 protected function appendNamespaceAliases( $property ) {
192 global $wgNamespaceAliases, $wgContLang;
193 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
194 $namespaces = $wgContLang->getNamespaces();
195 $data = array();
196 foreach ( $aliases as $title => $ns ) {
197 if ( $namespaces[$ns] == $title ) {
198 // Don't list duplicates
199 continue;
200 }
201 $item = array(
202 'id' => intval( $ns )
203 );
204 ApiResult::setContent( $item, strtr( $title, '_', ' ' ) );
205 $data[] = $item;
206 }
207
208 $this->getResult()->setIndexedTagName( $data, 'ns' );
209 return $this->getResult()->addValue( 'query', $property, $data );
210 }
211
212 protected function appendSpecialPageAliases( $property ) {
213 global $wgContLang;
214 $data = array();
215 foreach ( $wgContLang->getSpecialPageAliases() as $specialpage => $aliases )
216 {
217 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
218 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
219 $data[] = $arr;
220 }
221 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
222 return $this->getResult()->addValue( 'query', $property, $data );
223 }
224
225 protected function appendMagicWords( $property ) {
226 global $wgContLang;
227 $data = array();
228 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
229 $caseSensitive = array_shift( $aliases );
230 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
231 if ( $caseSensitive ) {
232 $arr['case-sensitive'] = '';
233 }
234 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
235 $data[] = $arr;
236 }
237 $this->getResult()->setIndexedTagName( $data, 'magicword' );
238 return $this->getResult()->addValue( 'query', $property, $data );
239 }
240
241 protected function appendInterwikiMap( $property, $filter ) {
242 $this->resetQueryParams();
243 $this->addTables( 'interwiki' );
244 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
245
246 if ( $filter === 'local' ) {
247 $this->addWhere( 'iw_local = 1' );
248 } elseif ( $filter === '!local' ) {
249 $this->addWhere( 'iw_local = 0' );
250 } elseif ( $filter ) {
251 ApiBase::dieDebug( __METHOD__, "Unknown filter=$filter" );
252 }
253
254 $this->addOption( 'ORDER BY', 'iw_prefix' );
255
256 $res = $this->select( __METHOD__ );
257
258 $data = array();
259 $langNames = Language::getLanguageNames();
260 foreach ( $res as $row ) {
261 $val = array();
262 $val['prefix'] = $row->iw_prefix;
263 if ( $row->iw_local == '1' ) {
264 $val['local'] = '';
265 }
266 // $val['trans'] = intval( $row->iw_trans ); // should this be exposed?
267 if ( isset( $langNames[$row->iw_prefix] ) ) {
268 $val['language'] = $langNames[$row->iw_prefix];
269 }
270 $val['url'] = $row->iw_url;
271
272 $data[] = $val;
273 }
274
275 $this->getResult()->setIndexedTagName( $data, 'iw' );
276 return $this->getResult()->addValue( 'query', $property, $data );
277 }
278
279 protected function appendDbReplLagInfo( $property, $includeAll ) {
280 global $wgShowHostnames;
281 $data = array();
282 if ( $includeAll ) {
283 if ( !$wgShowHostnames ) {
284 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
285 }
286
287 $lb = wfGetLB();
288 $lags = $lb->getLagTimes();
289 foreach ( $lags as $i => $lag ) {
290 $data[] = array(
291 'host' => $lb->getServerName( $i ),
292 'lag' => $lag
293 );
294 }
295 } else {
296 list( $host, $lag ) = wfGetLB()->getMaxLag();
297 $data[] = array(
298 'host' => $wgShowHostnames ? $host : '',
299 'lag' => intval( $lag )
300 );
301 }
302
303 $result = $this->getResult();
304 $result->setIndexedTagName( $data, 'db' );
305 return $this->getResult()->addValue( 'query', $property, $data );
306 }
307
308 protected function appendStatistics( $property ) {
309 global $wgDisableCounters;
310 $data = array();
311 $data['pages'] = intval( SiteStats::pages() );
312 $data['articles'] = intval( SiteStats::articles() );
313 if ( !$wgDisableCounters ) {
314 $data['views'] = intval( SiteStats::views() );
315 }
316 $data['edits'] = intval( SiteStats::edits() );
317 $data['images'] = intval( SiteStats::images() );
318 $data['users'] = intval( SiteStats::users() );
319 $data['activeusers'] = intval( SiteStats::activeUsers() );
320 $data['admins'] = intval( SiteStats::numberingroup( 'sysop' ) );
321 $data['jobs'] = intval( SiteStats::jobs() );
322 return $this->getResult()->addValue( 'query', $property, $data );
323 }
324
325 protected function appendUserGroups( $property, $numberInGroup ) {
326 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
327
328 $data = array();
329 foreach ( $wgGroupPermissions as $group => $permissions ) {
330 $arr = array(
331 'name' => $group,
332 'rights' => array_keys( $permissions, true ),
333 );
334
335 if ( $numberInGroup ) {
336 global $wgAutopromote;
337
338 if ( $group == 'user' ) {
339 $arr['number'] = SiteStats::users();
340
341 // '*' and autopromote groups have no size
342 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
343 $arr['number'] = SiteStats::numberInGroup( $group );
344 }
345 }
346
347 $groupArr = array(
348 'add' => $wgAddGroups,
349 'remove' => $wgRemoveGroups,
350 'add-self' => $wgGroupsAddToSelf,
351 'remove-self' => $wgGroupsRemoveFromSelf
352 );
353
354 foreach( $groupArr as $type => $rights ) {
355 if( isset( $rights[$group] ) ) {
356 $arr[$type] = $rights[$group];
357 $this->getResult()->setIndexedTagName( $arr[$type], 'group' );
358 }
359 }
360
361 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
362 $data[] = $arr;
363 }
364
365 $this->getResult()->setIndexedTagName( $data, 'group' );
366 return $this->getResult()->addValue( 'query', $property, $data );
367 }
368
369 protected function appendFileExtensions( $property ) {
370 global $wgFileExtensions;
371
372 $data = array();
373 foreach ( $wgFileExtensions as $ext ) {
374 $data[] = array( 'ext' => $ext );
375 }
376 $this->getResult()->setIndexedTagName( $data, 'fe' );
377 return $this->getResult()->addValue( 'query', $property, $data );
378 }
379
380 protected function appendExtensions( $property ) {
381 global $wgExtensionCredits;
382 $data = array();
383 foreach ( $wgExtensionCredits as $type => $extensions ) {
384 foreach ( $extensions as $ext ) {
385 $ret = array();
386 $ret['type'] = $type;
387 if ( isset( $ext['name'] ) ) {
388 $ret['name'] = $ext['name'];
389 }
390 if ( isset( $ext['description'] ) ) {
391 $ret['description'] = $ext['description'];
392 }
393 if ( isset( $ext['descriptionmsg'] ) ) {
394 // Can be a string or array( key, param1, param2, ... )
395 if ( is_array( $ext['descriptionmsg'] ) ) {
396 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
397 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
398 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
399 } else {
400 $ret['descriptionmsg'] = $ext['descriptionmsg'];
401 }
402 }
403 if ( isset( $ext['author'] ) ) {
404 $ret['author'] = is_array( $ext['author'] ) ?
405 implode( ', ', $ext['author' ] ) : $ext['author'];
406 }
407 if ( isset( $ext['url'] ) ) {
408 $ret['url'] = $ext['url'];
409 }
410 if ( isset( $ext['version'] ) ) {
411 $ret['version'] = $ext['version'];
412 } elseif ( isset( $ext['svn-revision'] ) &&
413 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
414 $ext['svn-revision'], $m ) )
415 {
416 $ret['version'] = 'r' . $m[1];
417 }
418 $data[] = $ret;
419 }
420 }
421
422 $this->getResult()->setIndexedTagName( $data, 'ext' );
423 return $this->getResult()->addValue( 'query', $property, $data );
424 }
425
426
427 protected function appendRightsInfo( $property ) {
428 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
429 $title = Title::newFromText( $wgRightsPage );
430 $url = $title ? $title->getFullURL() : $wgRightsUrl;
431 $text = $wgRightsText;
432 if ( !$text && $title ) {
433 $text = $title->getPrefixedText();
434 }
435
436 $data = array(
437 'url' => $url ? $url : '',
438 'text' => $text ? $text : ''
439 );
440
441 return $this->getResult()->addValue( 'query', $property, $data );
442 }
443
444 public function appendLanguages( $property ) {
445 $data = array();
446 foreach ( Language::getLanguageNames() as $code => $name ) {
447 $lang = array( 'code' => $code );
448 ApiResult::setContent( $lang, $name );
449 $data[] = $lang;
450 }
451 $this->getResult()->setIndexedTagName( $data, 'lang' );
452 return $this->getResult()->addValue( 'query', $property, $data );
453 }
454
455 public function getCacheMode( $params ) {
456 return 'public';
457 }
458
459 public function getAllowedParams() {
460 return array(
461 'prop' => array(
462 ApiBase::PARAM_DFLT => 'general',
463 ApiBase::PARAM_ISMULTI => true,
464 ApiBase::PARAM_TYPE => array(
465 'general',
466 'namespaces',
467 'namespacealiases',
468 'specialpagealiases',
469 'magicwords',
470 'interwikimap',
471 'dbrepllag',
472 'statistics',
473 'usergroups',
474 'extensions',
475 'fileextensions',
476 'rightsinfo',
477 'languages',
478 )
479 ),
480 'filteriw' => array(
481 ApiBase::PARAM_TYPE => array(
482 'local',
483 '!local',
484 )
485 ),
486 'showalldb' => false,
487 'numberingroup' => false,
488 );
489 }
490
491 public function getParamDescription() {
492 return array(
493 'prop' => array(
494 'Which sysinfo properties to get:',
495 ' general - Overall system information',
496 ' namespaces - List of registered namespaces and their canonical names',
497 ' namespacealiases - List of registered namespace aliases',
498 ' specialpagealiases - List of special page aliases',
499 ' magicwords - List of magic words and their aliases',
500 ' statistics - Returns site statistics',
501 ' interwikimap - Returns interwiki map (optionally filtered)',
502 ' dbrepllag - Returns database server with the highest replication lag',
503 ' usergroups - Returns user groups and the associated permissions',
504 ' extensions - Returns extensions installed on the wiki',
505 ' fileextensions - Returns list of file extensions allowed to be uploaded',
506 ' rightsinfo - Returns wiki rights (license) information if available',
507 ' languages - Returns a list of languages MediaWiki supports',
508 ),
509 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
510 'showalldb' => 'List all database servers, not just the one lagging the most',
511 'numberingroup' => 'Lists the number of users in user groups',
512 );
513 }
514
515 public function getDescription() {
516 return 'Return general information about the site';
517 }
518
519 public function getPossibleErrors() {
520 return array_merge( parent::getPossibleErrors(), array(
521 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
522 ) );
523 }
524
525 protected function getExamples() {
526 return array(
527 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
528 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
529 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
530 );
531 }
532
533 public function getVersion() {
534 return __CLASS__ . ': $Id$';
535 }
536 }