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