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