8b84b701726ae13fc405897bc225bd5d25272565
[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
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, $numberInGroup ) {
320 global $wgGroupPermissions;
321 $data = array();
322 foreach( $wgGroupPermissions as $group => $permissions ) {
323 $arr = array(
324 'name' => $group,
325 'rights' => array_keys( $permissions, true ),
326 );
327 if ( $numberInGroup )
328 $arr['number'] = SiteStats::numberInGroup( $group );
329
330 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
331 $data[] = $arr;
332 }
333
334 $this->getResult()->setIndexedTagName( $data, 'group' );
335 return $this->getResult()->addValue( 'query', $property, $data );
336 }
337
338 protected function appendFileExtensions( $property ) {
339 global $wgFileExtensions;
340
341 $data = array();
342 foreach( $wgFileExtensions as $ext ) {
343 $data[] = array( 'ext' => $ext );
344 }
345 $this->getResult()->setIndexedTagName( $data, 'fe' );
346 return $this->getResult()->addValue( 'query', $property, $data );
347 }
348
349 protected function appendExtensions( $property ) {
350 global $wgExtensionCredits;
351 $data = array();
352 foreach ( $wgExtensionCredits as $type => $extensions ) {
353 foreach ( $extensions as $ext ) {
354 $ret = array();
355 $ret['type'] = $type;
356 if ( isset( $ext['name'] ) )
357 $ret['name'] = $ext['name'];
358 if ( isset( $ext['description'] ) )
359 $ret['description'] = $ext['description'];
360 if ( isset( $ext['descriptionmsg'] ) )
361 $ret['descriptionmsg'] = $ext['descriptionmsg'];
362 if ( isset( $ext['author'] ) ) {
363 $ret['author'] = is_array( $ext['author'] ) ?
364 implode( ', ', $ext['author' ] ) : $ext['author'];
365 }
366 if ( isset( $ext['version'] ) ) {
367 $ret['version'] = $ext['version'];
368 } elseif ( isset( $ext['svn-revision'] ) &&
369 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
370 $ext['svn-revision'], $m ) )
371 {
372 $ret['version'] = 'r' . $m[1];
373 }
374 $data[] = $ret;
375 }
376 }
377
378 $this->getResult()->setIndexedTagName( $data, 'ext' );
379 return $this->getResult()->addValue( 'query', $property, $data );
380 }
381
382
383 protected function appendRightsInfo( $property ) {
384 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
385 $title = Title::newFromText( $wgRightsPage );
386 $url = $title ? $title->getFullURL() : $wgRightsUrl;
387 $text = $wgRightsText;
388 if( !$text && $title ) {
389 $text = $title->getPrefixedText();
390 }
391
392 $data = array(
393 'url' => $url ? $url : '',
394 'text' => $text ? $text : ''
395 );
396
397 return $this->getResult()->addValue( 'query', $property, $data );
398 }
399
400 public function appendLanguages( $property ) {
401 $data = array();
402 foreach( Language::getLanguageNames() as $code => $name ) {
403 $lang = array( 'code' => $code );
404 ApiResult::setContent( $lang, $name );
405 $data[] = $lang;
406 }
407 $this->getResult()->setIndexedTagName( $data, 'lang' );
408 return $this->getResult()->addValue( 'query', $property, $data );
409 }
410
411
412 public function getAllowedParams() {
413 return array(
414 'prop' => array(
415 ApiBase :: PARAM_DFLT => 'general',
416 ApiBase :: PARAM_ISMULTI => true,
417 ApiBase :: PARAM_TYPE => array(
418 'general',
419 'namespaces',
420 'namespacealiases',
421 'specialpagealiases',
422 'magicwords',
423 'interwikimap',
424 'dbrepllag',
425 'statistics',
426 'usergroups',
427 'extensions',
428 'fileextensions',
429 'rightsinfo',
430 'languages',
431 )
432 ),
433 'filteriw' => array(
434 ApiBase :: PARAM_TYPE => array(
435 'local',
436 '!local',
437 )
438 ),
439 'showalldb' => false,
440 'numberingroup' => false,
441 );
442 }
443
444 public function getParamDescription() {
445 return array(
446 'prop' => array(
447 'Which sysinfo properties to get:',
448 ' general - Overall system information',
449 ' namespaces - List of registered namespaces and their canonical names',
450 ' namespacealiases - List of registered namespace aliases',
451 ' specialpagealiases - List of special page aliases',
452 ' magicwords - List of magic words and their aliases',
453 ' statistics - Returns site statistics',
454 ' interwikimap - Returns interwiki map (optionally filtered)',
455 ' dbrepllag - Returns database server with the highest replication lag',
456 ' usergroups - Returns user groups and the associated permissions',
457 ' extensions - Returns extensions installed on the wiki',
458 ' fileextensions - Returns list of file extensions allowed to be uploaded',
459 ' rightsinfo - Returns wiki rights (license) information if available',
460 ' languages - Returns a list of languages MediaWiki supports',
461 ),
462 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
463 'showalldb' => 'List all database servers, not just the one lagging the most',
464 'numberingroup' => 'Lists the number of users in user groups',
465 );
466 }
467
468 public function getDescription() {
469 return 'Return general information about the site.';
470 }
471
472 protected function getExamples() {
473 return array(
474 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
475 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
476 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
477 );
478 }
479
480 public function getVersion() {
481 return __CLASS__ . ': $Id$';
482 }
483 }