27965aa9faaaf8e2c5a4e39c58a48c24932a17ac
[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 default :
87 ApiBase :: dieDebug( __METHOD__, "Unknown prop=$p" );
88 }
89 if(!$fit)
90 {
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 $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgContLang;
103 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
104
105 $data = array();
106 $mainPage = Title :: newFromText(wfMsgForContent('mainpage'));
107 $data['mainpage'] = $mainPage->getPrefixedText();
108 $data['base'] = $mainPage->getFullUrl();
109 $data['sitename'] = $wgSitename;
110 $data['generator'] = "MediaWiki $wgVersion";
111
112 $svn = SpecialVersion::getSvnRevision( $IP );
113 if( $svn )
114 $data['rev'] = $svn;
115
116 $data['case'] = $wgCapitalLinks ? 'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
117
118 if( isset( $wgRightsCode ) )
119 $data['rightscode'] = $wgRightsCode;
120 $data['rights'] = $wgRightsText;
121 $data['lang'] = $wgLanguageCode;
122 if( $wgContLang->isRTL() )
123 $data['rtl'] = '';
124 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
125
126 if( wfReadOnly() )
127 $data['readonly'] = '';
128 if( $wgEnableWriteAPI )
129 $data['writeapi'] = '';
130
131 $tz = $wgLocaltimezone;
132 $offset = $wgLocalTZoffset;
133 if( is_null( $tz ) ) {
134 $tz = 'UTC';
135 $offset = 0;
136 } elseif( is_null( $offset ) ) {
137 $offset = 0;
138 }
139 $data['timezone'] = $tz;
140 $data['timeoffset'] = $offset;
141
142 return $this->getResult()->addValue( 'query', $property, $data );
143 }
144
145 protected function appendNamespaces( $property ) {
146 global $wgContLang;
147 $data = array();
148 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
149 {
150 $data[$ns] = array(
151 'id' => $ns
152 );
153 ApiResult :: setContent( $data[$ns], $title );
154 $canonical = MWNamespace::getCanonicalName( $ns );
155
156 if( MWNamespace::hasSubpages( $ns ) )
157 $data[$ns]['subpages'] = '';
158
159 if( $canonical )
160 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
161 }
162
163 $this->getResult()->setIndexedTagName( $data, 'ns' );
164 return $this->getResult()->addValue( 'query', $property, $data );
165 }
166
167 protected function appendNamespaceAliases( $property ) {
168 global $wgNamespaceAliases, $wgContLang;
169 $wgContLang->load();
170 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->namespaceAliases );
171 $namespaces = $wgContLang->getNamespaces();
172 $data = array();
173 foreach( $aliases as $title => $ns ) {
174 if( $namespaces[$ns] == $title ) {
175 // Don't list duplicates
176 continue;
177 }
178 $item = array(
179 'id' => $ns
180 );
181 ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
182 $data[] = $item;
183 }
184
185 $this->getResult()->setIndexedTagName( $data, 'ns' );
186 return $this->getResult()->addValue( 'query', $property, $data );
187 }
188
189 protected function appendSpecialPageAliases( $property ) {
190 global $wgLang;
191 $data = array();
192 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
193 {
194 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
195 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
196 $data[] = $arr;
197 }
198 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
199 return $this->getResult()->addValue( 'query', $property, $data );
200 }
201
202 protected function appendMagicWords( $property ) {
203 global $wgContLang;
204 $data = array();
205 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
206 {
207 $caseSensitive = array_shift($aliases);
208 $arr = array('name' => $magicword, 'aliases' => $aliases);
209 if($caseSensitive)
210 $arr['case-sensitive'] = '';
211 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
212 $data[] = $arr;
213 }
214 $this->getResult()->setIndexedTagName($data, 'magicword');
215 return $this->getResult()->addValue( 'query', $property, $data );
216 }
217
218 protected function appendInterwikiMap( $property, $filter ) {
219 $this->resetQueryParams();
220 $this->addTables( 'interwiki' );
221 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
222
223 if( $filter === 'local' )
224 $this->addWhere( 'iw_local = 1' );
225 elseif( $filter === '!local' )
226 $this->addWhere( 'iw_local = 0' );
227 elseif( $filter )
228 ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
229
230 $this->addOption( 'ORDER BY', 'iw_prefix' );
231
232 $db = $this->getDB();
233 $res = $this->select( __METHOD__ );
234
235 $data = array();
236 $langNames = Language::getLanguageNames();
237 while( $row = $db->fetchObject($res) )
238 {
239 $val = array();
240 $val['prefix'] = $row->iw_prefix;
241 if( $row->iw_local == '1' )
242 $val['local'] = '';
243 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
244 if( isset( $langNames[$row->iw_prefix] ) )
245 $val['language'] = $langNames[$row->iw_prefix];
246 $val['url'] = $row->iw_url;
247
248 $data[] = $val;
249 }
250 $db->freeResult( $res );
251
252 $this->getResult()->setIndexedTagName( $data, 'iw' );
253 return $this->getResult()->addValue( 'query', $property, $data );
254 }
255
256 protected function appendDbReplLagInfo( $property, $includeAll ) {
257 global $wgShowHostnames;
258 $data = array();
259 if( $includeAll ) {
260 if ( !$wgShowHostnames )
261 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
262
263 $lb = wfGetLB();
264 $lags = $lb->getLagTimes();
265 foreach( $lags as $i => $lag ) {
266 $data[] = array(
267 'host' => $lb->getServerName( $i ),
268 'lag' => $lag
269 );
270 }
271 } else {
272 list( $host, $lag ) = wfGetLB()->getMaxLag();
273 $data[] = array(
274 'host' => $wgShowHostnames ? $host : '',
275 'lag' => $lag
276 );
277 }
278
279 $result = $this->getResult();
280 $result->setIndexedTagName( $data, 'db' );
281 return $this->getResult()->addValue( 'query', $property, $data );
282 }
283
284 protected function appendStatistics( $property ) {
285 $data = array();
286 $data['pages'] = intval( SiteStats::pages() );
287 $data['articles'] = intval( SiteStats::articles() );
288 $data['views'] = intval( SiteStats::views() );
289 $data['edits'] = intval( SiteStats::edits() );
290 $data['images'] = intval( SiteStats::images() );
291 $data['users'] = intval( SiteStats::users() );
292 $data['activeusers'] = intval( SiteStats::activeUsers() );
293 $data['admins'] = intval( SiteStats::numberingroup('sysop') );
294 $data['jobs'] = intval( SiteStats::jobs() );
295 return $this->getResult()->addValue( 'query', $property, $data );
296 }
297
298 protected function appendUserGroups( $property ) {
299 global $wgGroupPermissions;
300 $data = array();
301 foreach( $wgGroupPermissions as $group => $permissions ) {
302 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
303 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
304 $data[] = $arr;
305 }
306
307 $this->getResult()->setIndexedTagName( $data, 'group' );
308 return $this->getResult()->addValue( 'query', $property, $data );
309 }
310
311 protected function appendFileExtensions( $property ) {
312 global $wgFileExtensions;
313
314 $data = array();
315 foreach( $wgFileExtensions as $ext ) {
316 $data[] = array( 'ext' => $ext );
317 }
318 $this->getResult()->setIndexedTagName( $data, 'fe' );
319 return $this->getResult()->addValue( 'query', $property, $data );
320 }
321
322 protected function appendExtensions( $property ) {
323 global $wgExtensionCredits;
324 $data = array();
325 foreach ( $wgExtensionCredits as $type => $extensions ) {
326 foreach ( $extensions as $ext ) {
327 $ret = array();
328 $ret['type'] = $type;
329 if ( isset( $ext['name'] ) )
330 $ret['name'] = $ext['name'];
331 if ( isset( $ext['description'] ) )
332 $ret['description'] = $ext['description'];
333 if ( isset( $ext['descriptionmsg'] ) )
334 $ret['descriptionmsg'] = $ext['descriptionmsg'];
335 if ( isset( $ext['author'] ) ) {
336 $ret['author'] = is_array( $ext['author'] ) ?
337 implode( ', ', $ext['author' ] ) : $ext['author'];
338 }
339 if ( isset( $ext['version'] ) ) {
340 $ret['version'] = $ext['version'];
341 } elseif ( isset( $ext['svn-revision'] ) &&
342 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
343 $ext['svn-revision'], $m ) )
344 {
345 $ret['version'] = 'r' . $m[1];
346 }
347 $data[] = $ret;
348 }
349 }
350
351 $this->getResult()->setIndexedTagName( $data, 'ext' );
352 return $this->getResult()->addValue( 'query', $property, $data );
353 }
354
355
356 protected function appendRightsInfo( $property ) {
357 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
358 $title = Title::newFromText( $wgRightsPage );
359 $url = $title ? $title->getFullURL() : $wgRightsUrl;
360 $text = $wgRightsText;
361 if( !$text && $title ) {
362 $text = $title->getPrefixedText();
363 }
364
365 $data = array(
366 'url' => $url ? $url : '',
367 'text' => $text ? $text : ''
368 );
369
370 return $this->getResult()->addValue( 'query', $property, $data );
371 }
372
373
374 public function getAllowedParams() {
375 return array(
376 'prop' => array(
377 ApiBase :: PARAM_DFLT => 'general',
378 ApiBase :: PARAM_ISMULTI => true,
379 ApiBase :: PARAM_TYPE => array(
380 'general',
381 'namespaces',
382 'namespacealiases',
383 'specialpagealiases',
384 'magicwords',
385 'interwikimap',
386 'dbrepllag',
387 'statistics',
388 'usergroups',
389 'extensions',
390 'fileextensions',
391 'rightsinfo',
392 )
393 ),
394 'filteriw' => array(
395 ApiBase :: PARAM_TYPE => array(
396 'local',
397 '!local',
398 )
399 ),
400 'showalldb' => false,
401 );
402 }
403
404 public function getParamDescription() {
405 return array(
406 'prop' => array(
407 'Which sysinfo properties to get:',
408 ' general - Overall system information',
409 ' namespaces - List of registered namespaces and their canonical names',
410 ' namespacealiases - List of registered namespace aliases',
411 ' specialpagealiases - List of special page aliases',
412 ' magicwords - List of magic words and their aliases',
413 ' statistics - Returns site statistics',
414 ' interwikimap - Returns interwiki map (optionally filtered)',
415 ' dbrepllag - Returns database server with the highest replication lag',
416 ' usergroups - Returns user groups and the associated permissions',
417 ' extensions - Returns extensions installed on the wiki',
418 ' fileextensions - Returns list of file extensions allowed to be uploaded',
419 ' rightsinfo - Returns wiki rights (license) information if available',
420 ),
421 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
422 'showalldb' => 'List all database servers, not just the one lagging the most',
423 );
424 }
425
426 public function getDescription() {
427 return 'Return general information about the site.';
428 }
429
430 protected function getExamples() {
431 return array(
432 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
433 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
434 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
435 );
436 }
437
438 public function getVersion() {
439 return __CLASS__ . ': $Id$';
440 }
441 }