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