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