Rewrite ajaxwatch.js to use the API watch action, and JQuery. Allows it to be used...
[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 * 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 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, $wgLang;
103
104 $data = array();
105 $mainPage = Title::newFromText( wfMsgForContent( 'mainpage' ) );
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'] = $wgLang->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 $wgLang;
213 $data = array();
214 foreach ( $wgLang->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 while ( $row = $db->fetchObject( $res ) ) {
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 $db->freeResult( $res );
275
276 $this->getResult()->setIndexedTagName( $data, 'iw' );
277 return $this->getResult()->addValue( 'query', $property, $data );
278 }
279
280 protected function appendDbReplLagInfo( $property, $includeAll ) {
281 global $wgShowHostnames;
282 $data = array();
283 if ( $includeAll ) {
284 if ( !$wgShowHostnames ) {
285 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
286 }
287
288 $lb = wfGetLB();
289 $lags = $lb->getLagTimes();
290 foreach ( $lags as $i => $lag ) {
291 $data[] = array(
292 'host' => $lb->getServerName( $i ),
293 'lag' => $lag
294 );
295 }
296 } else {
297 list( $host, $lag ) = wfGetLB()->getMaxLag();
298 $data[] = array(
299 'host' => $wgShowHostnames ? $host : '',
300 'lag' => intval( $lag )
301 );
302 }
303
304 $result = $this->getResult();
305 $result->setIndexedTagName( $data, 'db' );
306 return $this->getResult()->addValue( 'query', $property, $data );
307 }
308
309 protected function appendStatistics( $property ) {
310 global $wgDisableCounters;
311 $data = array();
312 $data['pages'] = intval( SiteStats::pages() );
313 $data['articles'] = intval( SiteStats::articles() );
314 if ( !$wgDisableCounters ) {
315 $data['views'] = intval( SiteStats::views() );
316 }
317 $data['edits'] = intval( SiteStats::edits() );
318 $data['images'] = intval( SiteStats::images() );
319 $data['users'] = intval( SiteStats::users() );
320 $data['activeusers'] = intval( SiteStats::activeUsers() );
321 $data['admins'] = intval( SiteStats::numberingroup( 'sysop' ) );
322 $data['jobs'] = intval( SiteStats::jobs() );
323 return $this->getResult()->addValue( 'query', $property, $data );
324 }
325
326 protected function appendUserGroups( $property, $numberInGroup ) {
327 global $wgGroupPermissions;
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 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
339 $data[] = $arr;
340 }
341
342 $this->getResult()->setIndexedTagName( $data, 'group' );
343 return $this->getResult()->addValue( 'query', $property, $data );
344 }
345
346 protected function appendFileExtensions( $property ) {
347 global $wgFileExtensions;
348
349 $data = array();
350 foreach ( $wgFileExtensions as $ext ) {
351 $data[] = array( 'ext' => $ext );
352 }
353 $this->getResult()->setIndexedTagName( $data, 'fe' );
354 return $this->getResult()->addValue( 'query', $property, $data );
355 }
356
357 protected function appendExtensions( $property ) {
358 global $wgExtensionCredits;
359 $data = array();
360 foreach ( $wgExtensionCredits as $type => $extensions ) {
361 foreach ( $extensions as $ext ) {
362 $ret = array();
363 $ret['type'] = $type;
364 if ( isset( $ext['name'] ) ) {
365 $ret['name'] = $ext['name'];
366 }
367 if ( isset( $ext['description'] ) ) {
368 $ret['description'] = $ext['description'];
369 }
370 if ( isset( $ext['descriptionmsg'] ) ) {
371 // Can be a string or array( key, param1, param2, ... )
372 if ( is_array( $ext['descriptionmsg'] ) ) {
373 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
374 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
375 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
376 } else {
377 $ret['descriptionmsg'] = $ext['descriptionmsg'];
378 }
379 }
380 if ( isset( $ext['author'] ) ) {
381 $ret['author'] = is_array( $ext['author'] ) ?
382 implode( ', ', $ext['author' ] ) : $ext['author'];
383 }
384 if ( isset( $ext['url'] ) ) {
385 $ret['url'] = $ext['url'];
386 }
387 if ( isset( $ext['version'] ) ) {
388 $ret['version'] = $ext['version'];
389 } elseif ( isset( $ext['svn-revision'] ) &&
390 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
391 $ext['svn-revision'], $m ) )
392 {
393 $ret['version'] = 'r' . $m[1];
394 }
395 $data[] = $ret;
396 }
397 }
398
399 $this->getResult()->setIndexedTagName( $data, 'ext' );
400 return $this->getResult()->addValue( 'query', $property, $data );
401 }
402
403
404 protected function appendRightsInfo( $property ) {
405 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
406 $title = Title::newFromText( $wgRightsPage );
407 $url = $title ? $title->getFullURL() : $wgRightsUrl;
408 $text = $wgRightsText;
409 if ( !$text && $title ) {
410 $text = $title->getPrefixedText();
411 }
412
413 $data = array(
414 'url' => $url ? $url : '',
415 'text' => $text ? $text : ''
416 );
417
418 return $this->getResult()->addValue( 'query', $property, $data );
419 }
420
421 public function appendLanguages( $property ) {
422 $data = array();
423 foreach ( Language::getLanguageNames() as $code => $name ) {
424 $lang = array( 'code' => $code );
425 ApiResult::setContent( $lang, $name );
426 $data[] = $lang;
427 }
428 $this->getResult()->setIndexedTagName( $data, 'lang' );
429 return $this->getResult()->addValue( 'query', $property, $data );
430 }
431
432 public function getAllowedParams() {
433 return array(
434 'prop' => array(
435 ApiBase::PARAM_DFLT => 'general',
436 ApiBase::PARAM_ISMULTI => true,
437 ApiBase::PARAM_TYPE => array(
438 'general',
439 'namespaces',
440 'namespacealiases',
441 'specialpagealiases',
442 'magicwords',
443 'interwikimap',
444 'dbrepllag',
445 'statistics',
446 'usergroups',
447 'extensions',
448 'fileextensions',
449 'rightsinfo',
450 'languages',
451 )
452 ),
453 'filteriw' => array(
454 ApiBase::PARAM_TYPE => array(
455 'local',
456 '!local',
457 )
458 ),
459 'showalldb' => false,
460 'numberingroup' => false,
461 );
462 }
463
464 public function getParamDescription() {
465 return array(
466 'prop' => array(
467 'Which sysinfo properties to get:',
468 ' general - Overall system information',
469 ' namespaces - List of registered namespaces and their canonical names',
470 ' namespacealiases - List of registered namespace aliases',
471 ' specialpagealiases - List of special page aliases',
472 ' magicwords - List of magic words and their aliases',
473 ' statistics - Returns site statistics',
474 ' interwikimap - Returns interwiki map (optionally filtered)',
475 ' dbrepllag - Returns database server with the highest replication lag',
476 ' usergroups - Returns user groups and the associated permissions',
477 ' extensions - Returns extensions installed on the wiki',
478 ' fileextensions - Returns list of file extensions allowed to be uploaded',
479 ' rightsinfo - Returns wiki rights (license) information if available',
480 ' languages - Returns a list of languages MediaWiki supports',
481 ),
482 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
483 'showalldb' => 'List all database servers, not just the one lagging the most',
484 'numberingroup' => 'Lists the number of users in user groups',
485 );
486 }
487
488 public function getDescription() {
489 return 'Return general information about the site.';
490 }
491
492 public function getPossibleErrors() {
493 return array_merge( parent::getPossibleErrors(), array(
494 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
495 ) );
496 }
497
498 protected function getExamples() {
499 return array(
500 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
501 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
502 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
503 );
504 }
505
506 public function getVersion() {
507 return __CLASS__ . ': $Id$';
508 }
509 }