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