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