Start of blanket coverage of dieUsageMsg in getPossibleErrors
[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 (C) 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 {
47 switch ( $p )
48 {
49 case 'general':
50 $fit = $this->appendGeneralInfo( $p );
51 break;
52 case 'namespaces':
53 $fit = $this->appendNamespaces( $p );
54 break;
55 case 'namespacealiases':
56 $fit = $this->appendNamespaceAliases( $p );
57 break;
58 case 'specialpagealiases':
59 $fit = $this->appendSpecialPageAliases( $p );
60 break;
61 case 'magicwords':
62 $fit = $this->appendMagicWords( $p );
63 break;
64 case 'interwikimap':
65 $filteriw = isset( $params['filteriw'] ) ? $params['filteriw'] : false;
66 $fit = $this->appendInterwikiMap( $p, $filteriw );
67 break;
68 case 'dbrepllag':
69 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
70 break;
71 case 'statistics':
72 $fit = $this->appendStatistics( $p );
73 break;
74 case 'usergroups':
75 $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
76 break;
77 case 'extensions':
78 $fit = $this->appendExtensions( $p );
79 break;
80 case 'fileextensions':
81 $fit = $this->appendFileExtensions( $p );
82 break;
83 case 'rightsinfo':
84 $fit = $this->appendRightsInfo( $p );
85 break;
86 case 'languages':
87 $fit = $this->appendLanguages( $p );
88 break;
89 default :
90 ApiBase :: dieDebug( __METHOD__, "Unknown prop=$p" );
91 }
92 if ( !$fit )
93 {
94 // Abuse siprop as a query-continue parameter
95 // and set it to all unprocessed props
96 $this->setContinueEnumParameter( 'prop', implode( '|',
97 array_diff( $params['prop'], $done ) ) );
98 break;
99 }
100 $done[] = $p;
101 }
102 }
103
104 protected function appendGeneralInfo( $property ) {
105 global $wgContLang;
106 global $wgLang;
107
108 $data = array();
109 $mainPage = Title :: newFromText( wfMsgForContent( 'mainpage' ) );
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 // 'case-insensitive' option is reserved for future
124 $data['case'] = $GLOBALS['wgCapitalLinks'] ? 'first-letter' : 'case-sensitive';
125
126 if ( isset( $GLOBALS['wgRightsCode'] ) )
127 $data['rightscode'] = $GLOBALS['wgRightsCode'];
128 $data['rights'] = $GLOBALS['wgRightsText'];
129 $data['lang'] = $GLOBALS['wgLanguageCode'];
130 if ( $wgContLang->isRTL() )
131 $data['rtl'] = '';
132 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
133
134 if ( wfReadOnly() ) {
135 $data['readonly'] = '';
136 $data['readonlyreason'] = wfReadOnlyReason();
137 }
138 if ( $GLOBALS['wgEnableWriteAPI'] )
139 $data['writeapi'] = '';
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 {
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 if ( $canonical )
178 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
179
180 if ( MWNamespace::isContent( $ns ) )
181 $data[$ns]['content'] = '';
182 }
183
184 $this->getResult()->setIndexedTagName( $data, 'ns' );
185 return $this->getResult()->addValue( 'query', $property, $data );
186 }
187
188 protected function appendNamespaceAliases( $property ) {
189 global $wgNamespaceAliases, $wgContLang;
190 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
191 $namespaces = $wgContLang->getNamespaces();
192 $data = array();
193 foreach ( $aliases as $title => $ns ) {
194 if ( $namespaces[$ns] == $title ) {
195 // Don't list duplicates
196 continue;
197 }
198 $item = array(
199 'id' => intval( $ns )
200 );
201 ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
202 $data[] = $item;
203 }
204
205 $this->getResult()->setIndexedTagName( $data, 'ns' );
206 return $this->getResult()->addValue( 'query', $property, $data );
207 }
208
209 protected function appendSpecialPageAliases( $property ) {
210 global $wgLang;
211 $data = array();
212 foreach ( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
213 {
214 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
215 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
216 $data[] = $arr;
217 }
218 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
219 return $this->getResult()->addValue( 'query', $property, $data );
220 }
221
222 protected function appendMagicWords( $property ) {
223 global $wgContLang;
224 $data = array();
225 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases )
226 {
227 $caseSensitive = array_shift( $aliases );
228 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
229 if ( $caseSensitive )
230 $arr['case-sensitive'] = '';
231 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
232 $data[] = $arr;
233 }
234 $this->getResult()->setIndexedTagName( $data, 'magicword' );
235 return $this->getResult()->addValue( 'query', $property, $data );
236 }
237
238 protected function appendInterwikiMap( $property, $filter ) {
239 $this->resetQueryParams();
240 $this->addTables( 'interwiki' );
241 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
242
243 if ( $filter === 'local' )
244 $this->addWhere( 'iw_local = 1' );
245 elseif ( $filter === '!local' )
246 $this->addWhere( 'iw_local = 0' );
247 elseif ( $filter )
248 ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
249
250 $this->addOption( 'ORDER BY', 'iw_prefix' );
251
252 $db = $this->getDB();
253 $res = $this->select( __METHOD__ );
254
255 $data = array();
256 $langNames = Language::getLanguageNames();
257 while ( $row = $db->fetchObject( $res ) )
258 {
259 $val = array();
260 $val['prefix'] = $row->iw_prefix;
261 if ( $row->iw_local == '1' )
262 $val['local'] = '';
263 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
264 if ( isset( $langNames[$row->iw_prefix] ) )
265 $val['language'] = $langNames[$row->iw_prefix];
266 $val['url'] = $row->iw_url;
267
268 $data[] = $val;
269 }
270 $db->freeResult( $res );
271
272 $this->getResult()->setIndexedTagName( $data, 'iw' );
273 return $this->getResult()->addValue( 'query', $property, $data );
274 }
275
276 protected function appendDbReplLagInfo( $property, $includeAll ) {
277 global $wgShowHostnames;
278 $data = array();
279 if ( $includeAll ) {
280 if ( !$wgShowHostnames )
281 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
282
283 $lb = wfGetLB();
284 $lags = $lb->getLagTimes();
285 foreach ( $lags as $i => $lag ) {
286 $data[] = array(
287 'host' => $lb->getServerName( $i ),
288 'lag' => $lag
289 );
290 }
291 } else {
292 list( $host, $lag ) = wfGetLB()->getMaxLag();
293 $data[] = array(
294 'host' => $wgShowHostnames ? $host : '',
295 'lag' => intval( $lag )
296 );
297 }
298
299 $result = $this->getResult();
300 $result->setIndexedTagName( $data, 'db' );
301 return $this->getResult()->addValue( 'query', $property, $data );
302 }
303
304 protected function appendStatistics( $property ) {
305 global $wgDisableCounters;
306 $data = array();
307 $data['pages'] = intval( SiteStats::pages() );
308 $data['articles'] = intval( SiteStats::articles() );
309 if ( !$wgDisableCounters ) {
310 $data['views'] = intval( SiteStats::views() );
311 }
312 $data['edits'] = intval( SiteStats::edits() );
313 $data['images'] = intval( SiteStats::images() );
314 $data['users'] = intval( SiteStats::users() );
315 $data['activeusers'] = intval( SiteStats::activeUsers() );
316 $data['admins'] = intval( SiteStats::numberingroup( 'sysop' ) );
317 $data['jobs'] = intval( SiteStats::jobs() );
318 return $this->getResult()->addValue( 'query', $property, $data );
319 }
320
321 protected function appendUserGroups( $property, $numberInGroup ) {
322 global $wgGroupPermissions;
323 $data = array();
324 foreach ( $wgGroupPermissions as $group => $permissions ) {
325 $arr = array(
326 'name' => $group,
327 'rights' => array_keys( $permissions, true ),
328 );
329 if ( $numberInGroup )
330 $arr['number'] = SiteStats::numberInGroup( $group );
331
332 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
333 $data[] = $arr;
334 }
335
336 $this->getResult()->setIndexedTagName( $data, 'group' );
337 return $this->getResult()->addValue( 'query', $property, $data );
338 }
339
340 protected function appendFileExtensions( $property ) {
341 global $wgFileExtensions;
342
343 $data = array();
344 foreach ( $wgFileExtensions as $ext ) {
345 $data[] = array( 'ext' => $ext );
346 }
347 $this->getResult()->setIndexedTagName( $data, 'fe' );
348 return $this->getResult()->addValue( 'query', $property, $data );
349 }
350
351 protected function appendExtensions( $property ) {
352 global $wgExtensionCredits;
353 $data = array();
354 foreach ( $wgExtensionCredits as $type => $extensions ) {
355 foreach ( $extensions as $ext ) {
356 $ret = array();
357 $ret['type'] = $type;
358 if ( isset( $ext['name'] ) )
359 $ret['name'] = $ext['name'];
360 if ( isset( $ext['description'] ) )
361 $ret['description'] = $ext['description'];
362 if ( isset( $ext['descriptionmsg'] ) ) {
363 // Can be a string or array( key, param1, param2, ... )
364 if ( is_array( $ext['descriptionmsg'] ) ) {
365 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
366 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
367 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
368 } else {
369 $ret['descriptionmsg'] = $ext['descriptionmsg'];
370 }
371 }
372 if ( isset( $ext['author'] ) ) {
373 $ret['author'] = is_array( $ext['author'] ) ?
374 implode( ', ', $ext['author' ] ) : $ext['author'];
375 }
376 if ( isset( $ext['url'] ) ) {
377 $ret['url'] = $ext['url'];
378 }
379 if ( isset( $ext['version'] ) ) {
380 $ret['version'] = $ext['version'];
381 } elseif ( isset( $ext['svn-revision'] ) &&
382 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
383 $ext['svn-revision'], $m ) )
384 {
385 $ret['version'] = 'r' . $m[1];
386 }
387 $data[] = $ret;
388 }
389 }
390
391 $this->getResult()->setIndexedTagName( $data, 'ext' );
392 return $this->getResult()->addValue( 'query', $property, $data );
393 }
394
395
396 protected function appendRightsInfo( $property ) {
397 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
398 $title = Title::newFromText( $wgRightsPage );
399 $url = $title ? $title->getFullURL() : $wgRightsUrl;
400 $text = $wgRightsText;
401 if ( !$text && $title ) {
402 $text = $title->getPrefixedText();
403 }
404
405 $data = array(
406 'url' => $url ? $url : '',
407 'text' => $text ? $text : ''
408 );
409
410 return $this->getResult()->addValue( 'query', $property, $data );
411 }
412
413 public function appendLanguages( $property ) {
414 $data = array();
415 foreach ( Language::getLanguageNames() as $code => $name ) {
416 $lang = array( 'code' => $code );
417 ApiResult::setContent( $lang, $name );
418 $data[] = $lang;
419 }
420 $this->getResult()->setIndexedTagName( $data, 'lang' );
421 return $this->getResult()->addValue( 'query', $property, $data );
422 }
423
424
425 public function getAllowedParams() {
426 return array(
427 'prop' => array(
428 ApiBase :: PARAM_DFLT => 'general',
429 ApiBase :: PARAM_ISMULTI => true,
430 ApiBase :: PARAM_TYPE => array(
431 'general',
432 'namespaces',
433 'namespacealiases',
434 'specialpagealiases',
435 'magicwords',
436 'interwikimap',
437 'dbrepllag',
438 'statistics',
439 'usergroups',
440 'extensions',
441 'fileextensions',
442 'rightsinfo',
443 'languages',
444 )
445 ),
446 'filteriw' => array(
447 ApiBase :: PARAM_TYPE => array(
448 'local',
449 '!local',
450 )
451 ),
452 'showalldb' => false,
453 'numberingroup' => false,
454 );
455 }
456
457 public function getParamDescription() {
458 return array(
459 'prop' => array(
460 'Which sysinfo properties to get:',
461 ' general - Overall system information',
462 ' namespaces - List of registered namespaces and their canonical names',
463 ' namespacealiases - List of registered namespace aliases',
464 ' specialpagealiases - List of special page aliases',
465 ' magicwords - List of magic words and their aliases',
466 ' statistics - Returns site statistics',
467 ' interwikimap - Returns interwiki map (optionally filtered)',
468 ' dbrepllag - Returns database server with the highest replication lag',
469 ' usergroups - Returns user groups and the associated permissions',
470 ' extensions - Returns extensions installed on the wiki',
471 ' fileextensions - Returns list of file extensions allowed to be uploaded',
472 ' rightsinfo - Returns wiki rights (license) information if available',
473 ' languages - Returns a list of languages MediaWiki supports',
474 ),
475 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
476 'showalldb' => 'List all database servers, not just the one lagging the most',
477 'numberingroup' => 'Lists the number of users in user groups',
478 );
479 }
480
481 public function getDescription() {
482 return 'Return general information about the site.';
483 }
484
485 public function getPossibleErrors() {
486 return array_merge( parent::getPossibleErrors(), array(
487 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true', ),
488 ) );
489 }
490
491 protected function getExamples() {
492 return array(
493 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
494 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
495 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
496 );
497 }
498
499 public function getVersion() {
500 return __CLASS__ . ': $Id$';
501 }
502 }