Standardised file description headers, added @file
[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 if ( $numberInGroup ) {
335 $arr['number'] = SiteStats::numberInGroup( $group );
336 }
337
338 $groupArr = array(
339 'add' => $wgAddGroups,
340 'remove' => $wgRemoveGroups,
341 'add-self' => $wgGroupsAddToSelf,
342 'remove-self' => $wgGroupsRemoveFromSelf
343 );
344
345 foreach( $groupArr as $type => $rights ) {
346 if( isset( $rights[$group] ) ) {
347 $arr[$type] = $rights[$group];
348 $this->getResult()->setIndexedTagName( $arr[$type], 'group' );
349 }
350 }
351
352 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
353 $data[] = $arr;
354 }
355
356 $this->getResult()->setIndexedTagName( $data, 'group' );
357 return $this->getResult()->addValue( 'query', $property, $data );
358 }
359
360 protected function appendFileExtensions( $property ) {
361 global $wgFileExtensions;
362
363 $data = array();
364 foreach ( $wgFileExtensions as $ext ) {
365 $data[] = array( 'ext' => $ext );
366 }
367 $this->getResult()->setIndexedTagName( $data, 'fe' );
368 return $this->getResult()->addValue( 'query', $property, $data );
369 }
370
371 protected function appendExtensions( $property ) {
372 global $wgExtensionCredits;
373 $data = array();
374 foreach ( $wgExtensionCredits as $type => $extensions ) {
375 foreach ( $extensions as $ext ) {
376 $ret = array();
377 $ret['type'] = $type;
378 if ( isset( $ext['name'] ) ) {
379 $ret['name'] = $ext['name'];
380 }
381 if ( isset( $ext['description'] ) ) {
382 $ret['description'] = $ext['description'];
383 }
384 if ( isset( $ext['descriptionmsg'] ) ) {
385 // Can be a string or array( key, param1, param2, ... )
386 if ( is_array( $ext['descriptionmsg'] ) ) {
387 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
388 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
389 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
390 } else {
391 $ret['descriptionmsg'] = $ext['descriptionmsg'];
392 }
393 }
394 if ( isset( $ext['author'] ) ) {
395 $ret['author'] = is_array( $ext['author'] ) ?
396 implode( ', ', $ext['author' ] ) : $ext['author'];
397 }
398 if ( isset( $ext['url'] ) ) {
399 $ret['url'] = $ext['url'];
400 }
401 if ( isset( $ext['version'] ) ) {
402 $ret['version'] = $ext['version'];
403 } elseif ( isset( $ext['svn-revision'] ) &&
404 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
405 $ext['svn-revision'], $m ) )
406 {
407 $ret['version'] = 'r' . $m[1];
408 }
409 $data[] = $ret;
410 }
411 }
412
413 $this->getResult()->setIndexedTagName( $data, 'ext' );
414 return $this->getResult()->addValue( 'query', $property, $data );
415 }
416
417
418 protected function appendRightsInfo( $property ) {
419 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
420 $title = Title::newFromText( $wgRightsPage );
421 $url = $title ? $title->getFullURL() : $wgRightsUrl;
422 $text = $wgRightsText;
423 if ( !$text && $title ) {
424 $text = $title->getPrefixedText();
425 }
426
427 $data = array(
428 'url' => $url ? $url : '',
429 'text' => $text ? $text : ''
430 );
431
432 return $this->getResult()->addValue( 'query', $property, $data );
433 }
434
435 public function appendLanguages( $property ) {
436 $data = array();
437 foreach ( Language::getLanguageNames() as $code => $name ) {
438 $lang = array( 'code' => $code );
439 ApiResult::setContent( $lang, $name );
440 $data[] = $lang;
441 }
442 $this->getResult()->setIndexedTagName( $data, 'lang' );
443 return $this->getResult()->addValue( 'query', $property, $data );
444 }
445
446 public function getCacheMode( $params ) {
447 return 'public';
448 }
449
450 public function getAllowedParams() {
451 return array(
452 'prop' => array(
453 ApiBase::PARAM_DFLT => 'general',
454 ApiBase::PARAM_ISMULTI => true,
455 ApiBase::PARAM_TYPE => array(
456 'general',
457 'namespaces',
458 'namespacealiases',
459 'specialpagealiases',
460 'magicwords',
461 'interwikimap',
462 'dbrepllag',
463 'statistics',
464 'usergroups',
465 'extensions',
466 'fileextensions',
467 'rightsinfo',
468 'languages',
469 )
470 ),
471 'filteriw' => array(
472 ApiBase::PARAM_TYPE => array(
473 'local',
474 '!local',
475 )
476 ),
477 'showalldb' => false,
478 'numberingroup' => false,
479 );
480 }
481
482 public function getParamDescription() {
483 return array(
484 'prop' => array(
485 'Which sysinfo properties to get:',
486 ' general - Overall system information',
487 ' namespaces - List of registered namespaces and their canonical names',
488 ' namespacealiases - List of registered namespace aliases',
489 ' specialpagealiases - List of special page aliases',
490 ' magicwords - List of magic words and their aliases',
491 ' statistics - Returns site statistics',
492 ' interwikimap - Returns interwiki map (optionally filtered)',
493 ' dbrepllag - Returns database server with the highest replication lag',
494 ' usergroups - Returns user groups and the associated permissions',
495 ' extensions - Returns extensions installed on the wiki',
496 ' fileextensions - Returns list of file extensions allowed to be uploaded',
497 ' rightsinfo - Returns wiki rights (license) information if available',
498 ' languages - Returns a list of languages MediaWiki supports',
499 ),
500 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
501 'showalldb' => 'List all database servers, not just the one lagging the most',
502 'numberingroup' => 'Lists the number of users in user groups',
503 );
504 }
505
506 public function getDescription() {
507 return 'Return general information about the site';
508 }
509
510 public function getPossibleErrors() {
511 return array_merge( parent::getPossibleErrors(), array(
512 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
513 ) );
514 }
515
516 protected function getExamples() {
517 return array(
518 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
519 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
520 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
521 );
522 }
523
524 public function getVersion() {
525 return __CLASS__ . ': $Id$';
526 }
527 }