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