r86001, now with less scariness :P I took out the delete action and did purge instea...
[lhc/web/wiklou.git] / includes / api / ApiQuerySiteinfo.php
1 <?php
2 /**
3 *
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 case 'skins':
89 $fit = $this->appendSkins( $p );
90 break;
91 default:
92 ApiBase::dieDebug( __METHOD__, "Unknown prop=$p" );
93 }
94 if ( !$fit ) {
95 // Abuse siprop as a query-continue parameter
96 // and set it to all unprocessed props
97 $this->setContinueEnumParameter( 'prop', implode( '|',
98 array_diff( $params['prop'], $done ) ) );
99 break;
100 }
101 $done[] = $p;
102 }
103 }
104
105 protected function appendGeneralInfo( $property ) {
106 global $wgContLang;
107
108 $data = array();
109 $mainPage = Title::newMainPage();
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
124 // 'case-insensitive' option is reserved for future
125 $data['case'] = $GLOBALS['wgCapitalLinks'] ? 'first-letter' : 'case-sensitive';
126
127 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
128 $data['rightscode'] = $GLOBALS['wgRightsCode'];
129 }
130 $data['rights'] = $GLOBALS['wgRightsText'];
131 $data['lang'] = $GLOBALS['wgLanguageCode'];
132 if ( $wgContLang->isRTL() ) {
133 $data['rtl'] = '';
134 }
135 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
136
137 if ( wfReadOnly() ) {
138 $data['readonly'] = '';
139 $data['readonlyreason'] = wfReadOnlyReason();
140 }
141 if ( $GLOBALS['wgEnableWriteAPI'] ) {
142 $data['writeapi'] = '';
143 }
144
145 $tz = $GLOBALS['wgLocaltimezone'];
146 $offset = $GLOBALS['wgLocalTZoffset'];
147 if ( is_null( $tz ) ) {
148 $tz = 'UTC';
149 $offset = 0;
150 } elseif ( is_null( $offset ) ) {
151 $offset = 0;
152 }
153 $data['timezone'] = $tz;
154 $data['timeoffset'] = intval( $offset );
155 $data['articlepath'] = $GLOBALS['wgArticlePath'];
156 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
157 $data['script'] = $GLOBALS['wgScript'];
158 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
159 $data['server'] = $GLOBALS['wgServer'];
160 $data['wikiid'] = wfWikiID();
161 $data['time'] = wfTimestamp( TS_ISO_8601, time() );
162
163 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
164
165 return $this->getResult()->addValue( 'query', $property, $data );
166 }
167
168 protected function appendNamespaces( $property ) {
169 global $wgContLang;
170 $data = array();
171 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
172 $data[$ns] = array(
173 'id' => intval( $ns ),
174 'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
175 );
176 ApiResult::setContent( $data[$ns], $title );
177 $canonical = MWNamespace::getCanonicalName( $ns );
178
179 if ( MWNamespace::hasSubpages( $ns ) ) {
180 $data[$ns]['subpages'] = '';
181 }
182
183 if ( $canonical ) {
184 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
185 }
186
187 if ( MWNamespace::isContent( $ns ) ) {
188 $data[$ns]['content'] = '';
189 }
190 }
191
192 $this->getResult()->setIndexedTagName( $data, 'ns' );
193 return $this->getResult()->addValue( 'query', $property, $data );
194 }
195
196 protected function appendNamespaceAliases( $property ) {
197 global $wgNamespaceAliases, $wgContLang;
198 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
199 $namespaces = $wgContLang->getNamespaces();
200 $data = array();
201 foreach ( $aliases as $title => $ns ) {
202 if ( $namespaces[$ns] == $title ) {
203 // Don't list duplicates
204 continue;
205 }
206 $item = array(
207 'id' => intval( $ns )
208 );
209 ApiResult::setContent( $item, strtr( $title, '_', ' ' ) );
210 $data[] = $item;
211 }
212
213 $this->getResult()->setIndexedTagName( $data, 'ns' );
214 return $this->getResult()->addValue( 'query', $property, $data );
215 }
216
217 protected function appendSpecialPageAliases( $property ) {
218 global $wgContLang;
219 $data = array();
220 foreach ( $wgContLang->getSpecialPageAliases() as $specialpage => $aliases )
221 {
222 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
223 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
224 $data[] = $arr;
225 }
226 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
227 return $this->getResult()->addValue( 'query', $property, $data );
228 }
229
230 protected function appendMagicWords( $property ) {
231 global $wgContLang;
232 $data = array();
233 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
234 $caseSensitive = array_shift( $aliases );
235 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
236 if ( $caseSensitive ) {
237 $arr['case-sensitive'] = '';
238 }
239 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
240 $data[] = $arr;
241 }
242 $this->getResult()->setIndexedTagName( $data, 'magicword' );
243 return $this->getResult()->addValue( 'query', $property, $data );
244 }
245
246 protected function appendInterwikiMap( $property, $filter ) {
247 $this->resetQueryParams();
248 $this->addTables( 'interwiki' );
249 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
250
251 if ( $filter === 'local' ) {
252 $this->addWhere( 'iw_local = 1' );
253 } elseif ( $filter === '!local' ) {
254 $this->addWhere( 'iw_local = 0' );
255 } elseif ( $filter ) {
256 ApiBase::dieDebug( __METHOD__, "Unknown filter=$filter" );
257 }
258
259 $this->addOption( 'ORDER BY', 'iw_prefix' );
260
261 $res = $this->select( __METHOD__ );
262
263 $data = array();
264 $langNames = Language::getLanguageNames();
265 foreach ( $res as $row ) {
266 $val = array();
267 $val['prefix'] = $row->iw_prefix;
268 if ( $row->iw_local == '1' ) {
269 $val['local'] = '';
270 }
271 // $val['trans'] = intval( $row->iw_trans ); // should this be exposed?
272 if ( isset( $langNames[$row->iw_prefix] ) ) {
273 $val['language'] = $langNames[$row->iw_prefix];
274 }
275 $val['url'] = $row->iw_url;
276
277 $data[] = $val;
278 }
279
280 $this->getResult()->setIndexedTagName( $data, 'iw' );
281 return $this->getResult()->addValue( 'query', $property, $data );
282 }
283
284 protected function appendDbReplLagInfo( $property, $includeAll ) {
285 global $wgShowHostnames;
286 $data = array();
287 if ( $includeAll ) {
288 if ( !$wgShowHostnames ) {
289 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
290 }
291
292 $lb = wfGetLB();
293 $lags = $lb->getLagTimes();
294 foreach ( $lags as $i => $lag ) {
295 $data[] = array(
296 'host' => $lb->getServerName( $i ),
297 'lag' => $lag
298 );
299 }
300 } else {
301 list( $host, $lag ) = wfGetLB()->getMaxLag();
302 $data[] = array(
303 'host' => $wgShowHostnames ? $host : '',
304 'lag' => intval( $lag )
305 );
306 }
307
308 $result = $this->getResult();
309 $result->setIndexedTagName( $data, 'db' );
310 return $this->getResult()->addValue( 'query', $property, $data );
311 }
312
313 protected function appendStatistics( $property ) {
314 global $wgDisableCounters;
315 $data = array();
316 $data['pages'] = intval( SiteStats::pages() );
317 $data['articles'] = intval( SiteStats::articles() );
318 if ( !$wgDisableCounters ) {
319 $data['views'] = intval( SiteStats::views() );
320 }
321 $data['edits'] = intval( SiteStats::edits() );
322 $data['images'] = intval( SiteStats::images() );
323 $data['users'] = intval( SiteStats::users() );
324 $data['activeusers'] = intval( SiteStats::activeUsers() );
325 $data['admins'] = intval( SiteStats::numberingroup( 'sysop' ) );
326 $data['jobs'] = intval( SiteStats::jobs() );
327 return $this->getResult()->addValue( 'query', $property, $data );
328 }
329
330 protected function appendUserGroups( $property, $numberInGroup ) {
331 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
332
333 $data = array();
334 foreach ( $wgGroupPermissions as $group => $permissions ) {
335 $arr = array(
336 'name' => $group,
337 'rights' => array_keys( $permissions, true ),
338 );
339
340 if ( $numberInGroup ) {
341 global $wgAutopromote;
342
343 if ( $group == 'user' ) {
344 $arr['number'] = SiteStats::users();
345
346 // '*' and autopromote groups have no size
347 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
348 $arr['number'] = SiteStats::numberInGroup( $group );
349 }
350 }
351
352 $groupArr = array(
353 'add' => $wgAddGroups,
354 'remove' => $wgRemoveGroups,
355 'add-self' => $wgGroupsAddToSelf,
356 'remove-self' => $wgGroupsRemoveFromSelf
357 );
358
359 foreach ( $groupArr as $type => $rights ) {
360 if ( isset( $rights[$group] ) ) {
361 $arr[$type] = $rights[$group];
362 $this->getResult()->setIndexedTagName( $arr[$type], 'group' );
363 }
364 }
365
366 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
367 $data[] = $arr;
368 }
369
370 $this->getResult()->setIndexedTagName( $data, 'group' );
371 return $this->getResult()->addValue( 'query', $property, $data );
372 }
373
374 protected function appendFileExtensions( $property ) {
375 global $wgFileExtensions;
376
377 $data = array();
378 foreach ( $wgFileExtensions as $ext ) {
379 $data[] = array( 'ext' => $ext );
380 }
381 $this->getResult()->setIndexedTagName( $data, 'fe' );
382 return $this->getResult()->addValue( 'query', $property, $data );
383 }
384
385 protected function appendExtensions( $property ) {
386 global $wgExtensionCredits;
387 $data = array();
388 foreach ( $wgExtensionCredits as $type => $extensions ) {
389 foreach ( $extensions as $ext ) {
390 $ret = array();
391 $ret['type'] = $type;
392 if ( isset( $ext['name'] ) ) {
393 $ret['name'] = $ext['name'];
394 }
395 if ( isset( $ext['description'] ) ) {
396 $ret['description'] = $ext['description'];
397 }
398 if ( isset( $ext['descriptionmsg'] ) ) {
399 // Can be a string or array( key, param1, param2, ... )
400 if ( is_array( $ext['descriptionmsg'] ) ) {
401 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
402 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
403 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
404 } else {
405 $ret['descriptionmsg'] = $ext['descriptionmsg'];
406 }
407 }
408 if ( isset( $ext['author'] ) ) {
409 $ret['author'] = is_array( $ext['author'] ) ?
410 implode( ', ', $ext['author' ] ) : $ext['author'];
411 }
412 if ( isset( $ext['url'] ) ) {
413 $ret['url'] = $ext['url'];
414 }
415 if ( isset( $ext['version'] ) ) {
416 $ret['version'] = $ext['version'];
417 } elseif ( isset( $ext['svn-revision'] ) &&
418 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
419 $ext['svn-revision'], $m ) )
420 {
421 $ret['version'] = 'r' . $m[1];
422 }
423 $data[] = $ret;
424 }
425 }
426
427 $this->getResult()->setIndexedTagName( $data, 'ext' );
428 return $this->getResult()->addValue( 'query', $property, $data );
429 }
430
431
432 protected function appendRightsInfo( $property ) {
433 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
434 $title = Title::newFromText( $wgRightsPage );
435 $url = $title ? $title->getFullURL() : $wgRightsUrl;
436 $text = $wgRightsText;
437 if ( !$text && $title ) {
438 $text = $title->getPrefixedText();
439 }
440
441 $data = array(
442 'url' => $url ? $url : '',
443 'text' => $text ? $text : ''
444 );
445
446 return $this->getResult()->addValue( 'query', $property, $data );
447 }
448
449 public function appendLanguages( $property ) {
450 $data = array();
451 foreach ( Language::getLanguageNames() as $code => $name ) {
452 $lang = array( 'code' => $code );
453 ApiResult::setContent( $lang, $name );
454 $data[] = $lang;
455 }
456 $this->getResult()->setIndexedTagName( $data, 'lang' );
457 return $this->getResult()->addValue( 'query', $property, $data );
458 }
459
460 public function appendSkins( $property ) {
461 $data = array();
462 foreach ( Skin::getSkinNames() as $name => $displayName ) {
463 $skin = array( 'code' => $name );
464 ApiResult::setContent( $skin, $displayName );
465 $data[] = $skin;
466 }
467 $this->getResult()->setIndexedTagName( $data, 'skin' );
468 return $this->getResult()->addValue( 'query', $property, $data );
469 }
470
471 public function getCacheMode( $params ) {
472 return 'public';
473 }
474
475 public function getAllowedParams() {
476 return array(
477 'prop' => array(
478 ApiBase::PARAM_DFLT => 'general',
479 ApiBase::PARAM_ISMULTI => true,
480 ApiBase::PARAM_TYPE => array(
481 'general',
482 'namespaces',
483 'namespacealiases',
484 'specialpagealiases',
485 'magicwords',
486 'interwikimap',
487 'dbrepllag',
488 'statistics',
489 'usergroups',
490 'extensions',
491 'fileextensions',
492 'rightsinfo',
493 'languages',
494 'skins',
495 )
496 ),
497 'filteriw' => array(
498 ApiBase::PARAM_TYPE => array(
499 'local',
500 '!local',
501 )
502 ),
503 'showalldb' => false,
504 'numberingroup' => false,
505 );
506 }
507
508 public function getParamDescription() {
509 return array(
510 'prop' => array(
511 'Which sysinfo properties to get:',
512 ' general - Overall system information',
513 ' namespaces - List of registered namespaces and their canonical names',
514 ' namespacealiases - List of registered namespace aliases',
515 ' specialpagealiases - List of special page aliases',
516 ' magicwords - List of magic words and their aliases',
517 ' statistics - Returns site statistics',
518 ' interwikimap - Returns interwiki map (optionally filtered)',
519 ' dbrepllag - Returns database server with the highest replication lag',
520 ' usergroups - Returns user groups and the associated permissions',
521 ' extensions - Returns extensions installed on the wiki',
522 ' fileextensions - Returns list of file extensions allowed to be uploaded',
523 ' rightsinfo - Returns wiki rights (license) information if available',
524 ' languages - Returns a list of languages MediaWiki supports',
525 ' skins - Returns a list of all enabled skins',
526 ),
527 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
528 'showalldb' => 'List all database servers, not just the one lagging the most',
529 'numberingroup' => 'Lists the number of users in user groups',
530 );
531 }
532
533 public function getDescription() {
534 return 'Return general information about the site';
535 }
536
537 public function getPossibleErrors() {
538 return array_merge( parent::getPossibleErrors(), array(
539 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
540 ) );
541 }
542
543 protected function getExamples() {
544 return array(
545 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
546 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
547 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
548 );
549 }
550
551 public function getVersion() {
552 return __CLASS__ . ': $Id$';
553 }
554 }