Don't use public-audience-only function
[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 foreach( $params['prop'] as $p )
45 {
46 switch ( $p )
47 {
48 case 'general':
49 $this->appendGeneralInfo( $p );
50 break;
51 case 'namespaces':
52 $this->appendNamespaces( $p );
53 break;
54 case 'namespacealiases':
55 $this->appendNamespaceAliases( $p );
56 break;
57 case 'specialpagealiases':
58 $this->appendSpecialPageAliases( $p );
59 break;
60 case 'magicwords':
61 $this->appendMagicWords( $p );
62 break;
63 case 'interwikimap':
64 $filteriw = isset( $params['filteriw'] ) ? $params['filteriw'] : false;
65 $this->appendInterwikiMap( $p, $filteriw );
66 break;
67 case 'dbrepllag':
68 $this->appendDbReplLagInfo( $p, $params['showalldb'] );
69 break;
70 case 'statistics':
71 $this->appendStatistics( $p );
72 break;
73 case 'usergroups':
74 $this->appendUserGroups( $p );
75 break;
76 default :
77 ApiBase :: dieDebug( __METHOD__, "Unknown prop=$p" );
78 }
79 }
80 }
81
82 protected function appendGeneralInfo( $property ) {
83 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgContLang;
84 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
85
86 $data = array();
87 $mainPage = Title :: newFromText(wfMsgForContent('mainpage'));
88 $data['mainpage'] = $mainPage->getPrefixedText();
89 $data['base'] = $mainPage->getFullUrl();
90 $data['sitename'] = $wgSitename;
91 $data['generator'] = "MediaWiki $wgVersion";
92
93 $svn = SpecialVersion::getSvnRevision( $IP );
94 if( $svn )
95 $data['rev'] = $svn;
96
97 $data['case'] = $wgCapitalLinks ? 'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
98
99 if( isset( $wgRightsCode ) )
100 $data['rightscode'] = $wgRightsCode;
101 $data['rights'] = $wgRightsText;
102 $data['lang'] = $wgLanguageCode;
103 if( $wgContLang->isRTL() )
104 $data['rtl'] = '';
105 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
106
107 if( wfReadOnly() )
108 $data['readonly'] = '';
109 if( $wgEnableWriteAPI )
110 $data['writeapi'] = '';
111
112 $tz = $wgLocaltimezone;
113 $offset = $wgLocalTZoffset;
114 if( is_null( $tz ) ) {
115 $tz = 'UTC';
116 $offset = 0;
117 } elseif( is_null( $offset ) ) {
118 $offset = 0;
119 }
120 $data['timezone'] = $tz;
121 $data['timeoffset'] = $offset;
122
123 $this->getResult()->addValue( 'query', $property, $data );
124 }
125
126 protected function appendNamespaces( $property ) {
127 global $wgContLang;
128 $data = array();
129 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
130 {
131 $data[$ns] = array(
132 'id' => $ns
133 );
134 ApiResult :: setContent( $data[$ns], $title );
135 if( MWNamespace::hasSubpages($ns) )
136 $data[$ns]['subpages'] = '';
137 }
138
139 $this->getResult()->setIndexedTagName( $data, 'ns' );
140 $this->getResult()->addValue( 'query', $property, $data );
141 }
142
143 protected function appendNamespaceAliases( $property ) {
144 global $wgNamespaceAliases;
145 $data = array();
146 foreach( $wgNamespaceAliases as $title => $ns ) {
147 $item = array(
148 'id' => $ns
149 );
150 ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
151 $data[] = $item;
152 }
153
154 $this->getResult()->setIndexedTagName( $data, 'ns' );
155 $this->getResult()->addValue( 'query', $property, $data );
156 }
157
158 protected function appendSpecialPageAliases( $property ) {
159 global $wgLang;
160 $data = array();
161 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
162 {
163 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
164 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
165 $data[] = $arr;
166 }
167 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
168 $this->getResult()->addValue( 'query', $property, $data );
169 }
170
171 protected function appendMagicWords( $property ) {
172 global $wgContLang;
173 $data = array();
174 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
175 {
176 $caseSensitive = array_shift($aliases);
177 $arr = array('name' => $magicword, 'aliases' => $aliases);
178 if($caseSensitive)
179 $arr['case-sensitive'] = '';
180 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
181 $data[] = $arr;
182 }
183 $this->getResult()->setIndexedTagName($data, 'magicword');
184 $this->getResult()->addValue('query', $property, $data);
185 }
186
187
188 protected function appendInterwikiMap( $property, $filter ) {
189 $this->resetQueryParams();
190 $this->addTables( 'interwiki' );
191 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
192
193 if( $filter === 'local' )
194 $this->addWhere( 'iw_local = 1' );
195 elseif( $filter === '!local' )
196 $this->addWhere( 'iw_local = 0' );
197 elseif( $filter !== false )
198 ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
199
200 $this->addOption( 'ORDER BY', 'iw_prefix' );
201
202 $db = $this->getDB();
203 $res = $this->select( __METHOD__ );
204
205 $data = array();
206 $langNames = Language::getLanguageNames();
207 while( $row = $db->fetchObject($res) )
208 {
209 $val = array();
210 $val['prefix'] = $row->iw_prefix;
211 if( $row->iw_local == '1' )
212 $val['local'] = '';
213 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
214 if( isset( $langNames[$row->iw_prefix] ) )
215 $val['language'] = $langNames[$row->iw_prefix];
216 $val['url'] = $row->iw_url;
217
218 $data[] = $val;
219 }
220 $db->freeResult( $res );
221
222 $this->getResult()->setIndexedTagName( $data, 'iw' );
223 $this->getResult()->addValue( 'query', $property, $data );
224 }
225
226 protected function appendDbReplLagInfo( $property, $includeAll ) {
227 global $wgShowHostnames;
228 $data = array();
229 if( $includeAll ) {
230 if ( !$wgShowHostnames )
231 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
232
233 $lb = wfGetLB();
234 $lags = $lb->getLagTimes();
235 foreach( $lags as $i => $lag ) {
236 $data[] = array(
237 'host' => $lb->getServerName( $i ),
238 'lag' => $lag
239 );
240 }
241 } else {
242 list( $host, $lag ) = wfGetLB()->getMaxLag();
243 $data[] = array(
244 'host' => $wgShowHostnames ? $host : '',
245 'lag' => $lag
246 );
247 }
248
249 $result = $this->getResult();
250 $result->setIndexedTagName( $data, 'db' );
251 $result->addValue( 'query', $property, $data );
252 }
253
254 protected function appendStatistics( $property ) {
255 $data = array();
256 $data['pages'] = intval( SiteStats::pages() );
257 $data['articles'] = intval( SiteStats::articles() );
258 $data['views'] = intval( SiteStats::views() );
259 $data['edits'] = intval( SiteStats::edits() );
260 $data['images'] = intval( SiteStats::images() );
261 $data['users'] = intval( SiteStats::users() );
262 $data['activeusers'] = intval( SiteStats::activeUsers() );
263 $data['admins'] = intval( SiteStats::numberingroup('sysop') );
264 $data['jobs'] = intval( SiteStats::jobs() );
265 $this->getResult()->addValue( 'query', $property, $data );
266 }
267
268 protected function appendUserGroups( $property ) {
269 global $wgGroupPermissions;
270 $data = array();
271 foreach( $wgGroupPermissions as $group => $permissions ) {
272 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
273 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
274 $data[] = $arr;
275 }
276
277 $this->getResult()->setIndexedTagName( $data, 'group' );
278 $this->getResult()->addValue( 'query', $property, $data );
279 }
280
281 public function getAllowedParams() {
282 return array(
283 'prop' => array(
284 ApiBase :: PARAM_DFLT => 'general',
285 ApiBase :: PARAM_ISMULTI => true,
286 ApiBase :: PARAM_TYPE => array(
287 'general',
288 'namespaces',
289 'namespacealiases',
290 'specialpagealiases',
291 'magicwords',
292 'interwikimap',
293 'dbrepllag',
294 'statistics',
295 'usergroups',
296 )
297 ),
298 'filteriw' => array(
299 ApiBase :: PARAM_TYPE => array(
300 'local',
301 '!local',
302 )
303 ),
304 'showalldb' => false,
305 );
306 }
307
308 public function getParamDescription() {
309 return array(
310 'prop' => array(
311 'Which sysinfo properties to get:',
312 ' "general" - Overall system information',
313 ' "namespaces" - List of registered namespaces (localized)',
314 ' "namespacealiases" - List of registered namespace aliases',
315 ' "specialpagealiases" - List of special page aliases',
316 ' "magicwords" - List of magic words and their aliases',
317 ' "statistics" - Returns site statistics',
318 ' "interwikimap" - Returns interwiki map (optionally filtered)',
319 ' "dbrepllag" - Returns database server with the highest replication lag',
320 ' "usergroups" - Returns user groups and the associated permissions',
321 ),
322 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
323 'showalldb' => 'List all database servers, not just the one lagging the most',
324 );
325 }
326
327 public function getDescription() {
328 return 'Return general information about the site.';
329 }
330
331 protected function getExamples() {
332 return array(
333 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
334 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
335 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
336 );
337 }
338
339 public function getVersion() {
340 return __CLASS__ . ': $Id$';
341 }
342 }