Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[lhc/web/wiklou.git] / includes / api / ApiQueryAllUsers.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 7, 2007
6 *
7 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to enumerate all registered users.
34 *
35 * @ingroup API
36 */
37 class ApiQueryAllUsers extends ApiQueryBase {
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'au' );
40 }
41
42 public function execute() {
43 $db = $this->getDB();
44 $params = $this->extractRequestParams();
45
46 $prop = $params['prop'];
47 if ( !is_null( $prop ) ) {
48 $prop = array_flip( $prop );
49 $fld_blockinfo = isset( $prop['blockinfo'] );
50 $fld_editcount = isset( $prop['editcount'] );
51 $fld_groups = isset( $prop['groups'] );
52 $fld_rights = isset( $prop['rights'] );
53 $fld_registration = isset( $prop['registration'] );
54 } else {
55 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = $fld_rights = false;
56 }
57
58 $limit = $params['limit'];
59
60 $this->addTables( 'user' );
61 $useIndex = true;
62
63 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
64 $from = is_null( $params['from'] ) ? null : $this->keyToTitle( $params['from'] );
65 $to = is_null( $params['to'] ) ? null : $this->keyToTitle( $params['to'] );
66
67 $this->addWhereRange( 'user_name', $dir, $from, $to );
68
69 if ( !is_null( $params['prefix'] ) ) {
70 $this->addWhere( 'user_name' . $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
71 }
72
73 if ( !is_null( $params['rights'] ) ) {
74 $groups = array();
75 foreach( $params['rights'] as $r ) {
76 $groups = array_merge( $groups, User::getGroupsWithPermission( $r ) );
77 }
78
79 $groups = array_unique( $groups );
80
81 if ( is_null( $params['group'] ) ) {
82 $params['group'] = $groups;
83 } else {
84 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
85 }
86 }
87
88 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
89 $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
90 }
91
92 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
93 $useIndex = false;
94 // Filter only users that belong to a given group
95 $this->addTables( 'user_groups', 'ug1' );
96 $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
97 'ug1.ug_group' => $params['group'] ) ) ) );
98 }
99
100 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
101 $useIndex = false;
102 // Filter only users don't belong to a given group
103 $this->addTables( 'user_groups', 'ug1' );
104
105 if ( count( $params['excludegroup'] ) == 1 ) {
106 $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
107 } else {
108 $exclude = array( $db->makeList( array( 'ug1.ug_group' => $params['excludegroup'] ), LIST_OR ) );
109 }
110 $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
111 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
112 )
113 ) );
114 $this->addWhere( 'ug1.ug_user IS NULL' );
115 }
116
117 if ( $params['witheditsonly'] ) {
118 $this->addWhere( 'user_editcount > 0' );
119 }
120
121 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
122
123 if ( $fld_groups || $fld_rights ) {
124 // Show the groups the given users belong to
125 // request more than needed to avoid not getting all rows that belong to one user
126 $groupCount = count( User::getAllGroups() );
127 $sqlLimit = $limit + $groupCount + 1;
128
129 $this->addTables( 'user_groups', 'ug2' );
130 $this->addJoinConds( array( 'ug2' => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
131 $this->addFields( 'ug2.ug_group ug_group2' );
132 } else {
133 $sqlLimit = $limit + 1;
134 }
135
136 if ( $params['activeusers'] ) {
137 global $wgActiveUserDays;
138 $this->addTables( 'recentchanges' );
139
140 $this->addJoinConds( array( 'recentchanges' => array(
141 'INNER JOIN', 'rc_user_text=user_name'
142 ) ) );
143
144 $this->addFields( 'COUNT(*) AS recentedits' );
145
146 $this->addWhere( "rc_log_type IS NULL OR rc_log_type != 'newusers'" );
147 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 );
148 $this->addWhere( "rc_timestamp >= {$db->addQuotes( $timestamp )}" );
149
150 $this->addOption( 'GROUP BY', 'user_name' );
151 }
152
153 $this->addOption( 'LIMIT', $sqlLimit );
154
155 $this->addFields( array(
156 'user_name',
157 'user_id'
158 ) );
159 $this->addFieldsIf( 'user_editcount', $fld_editcount );
160 $this->addFieldsIf( 'user_registration', $fld_registration );
161
162 if ( $useIndex ) {
163 $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
164 }
165
166 $res = $this->select( __METHOD__ );
167
168 $count = 0;
169 $lastUserData = false;
170 $lastUser = false;
171 $result = $this->getResult();
172
173 //
174 // This loop keeps track of the last entry.
175 // For each new row, if the new row is for different user then the last, the last entry is added to results.
176 // Otherwise, the group of the new row is appended to the last entry.
177 // The setContinue... is more complex because of this, and takes into account the higher sql limit
178 // to make sure all rows that belong to the same user are received.
179
180 foreach ( $res as $row ) {
181 $count++;
182
183 if ( $lastUser !== $row->user_name ) {
184 // Save the last pass's user data
185 if ( is_array( $lastUserData ) ) {
186 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
187 null, $lastUserData );
188
189 $lastUserData = null;
190
191 if ( !$fit ) {
192 $this->setContinueEnumParameter( 'from',
193 $this->keyToTitle( $lastUserData['name'] ) );
194 break;
195 }
196 }
197
198 if ( $count > $limit ) {
199 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
200 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
201 break;
202 }
203
204 // Record new user's data
205 $lastUser = $row->user_name;
206 $lastUserData = array(
207 'userid' => $row->user_id,
208 'name' => $lastUser,
209 );
210 if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
211 $lastUserData['blockedby'] = $row->ipb_by_text;
212 $lastUserData['blockreason'] = $row->ipb_reason;
213 $lastUserData['blockexpiry'] = $row->ipb_expiry;
214 }
215 if ( $row->ipb_deleted ) {
216 $lastUserData['hidden'] = '';
217 }
218 if ( $fld_editcount ) {
219 $lastUserData['editcount'] = intval( $row->user_editcount );
220 }
221 if ( $params['activeusers'] ) {
222 $lastUserData['recenteditcount'] = intval( $row->recentedits );
223 }
224 if ( $fld_registration ) {
225 $lastUserData['registration'] = $row->user_registration ?
226 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
227 }
228 }
229
230 if ( $sqlLimit == $count ) {
231 // BUG! database contains group name that User::getAllGroups() does not return
232 // TODO: should handle this more gracefully
233 ApiBase::dieDebug( __METHOD__,
234 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
235 }
236
237 // Add user's group info
238 if ( $fld_groups ) {
239 if ( !isset( $lastUserData['groups'] ) ) {
240 $lastUserData['groups'] = ApiQueryUsers::getAutoGroups( User::newFromName( $lastUser ) );
241 }
242
243 if ( !is_null( $row->ug_group2 ) ) {
244 $lastUserData['groups'][] = $row->ug_group2;
245 }
246 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
247 }
248
249 if ( $fld_rights ) {
250 if ( !isset( $lastUserData['rights'] ) ) {
251 $lastUserData['rights'] = User::getGroupPermissions( User::newFromName( $lastUser )->getAutomaticGroups() );
252 }
253 if ( !is_null( $row->ug_group2 ) ) {
254 $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
255 User::getGroupPermissions( array( $row->ug_group2 ) ) ) );
256 }
257 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
258 }
259 }
260
261 if ( is_array( $lastUserData ) ) {
262 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
263 null, $lastUserData );
264 if ( !$fit ) {
265 $this->setContinueEnumParameter( 'from',
266 $this->keyToTitle( $lastUserData['name'] ) );
267 }
268 }
269
270 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
271 }
272
273 public function getCacheMode( $params ) {
274 return 'anon-public-user-private';
275 }
276
277 public function getAllowedParams() {
278 $userGroups = User::getAllGroups();
279 return array(
280 'from' => null,
281 'to' => null,
282 'prefix' => null,
283 'dir' => array(
284 ApiBase::PARAM_DFLT => 'ascending',
285 ApiBase::PARAM_TYPE => array(
286 'ascending',
287 'descending'
288 ),
289 ),
290 'group' => array(
291 ApiBase::PARAM_TYPE => $userGroups,
292 ApiBase::PARAM_ISMULTI => true,
293 ),
294 'excludegroup' => array(
295 ApiBase::PARAM_TYPE => $userGroups,
296 ApiBase::PARAM_ISMULTI => true,
297 ),
298 'rights' => array(
299 ApiBase::PARAM_TYPE => User::getAllRights(),
300 ApiBase::PARAM_ISMULTI => true,
301 ),
302 'prop' => array(
303 ApiBase::PARAM_ISMULTI => true,
304 ApiBase::PARAM_TYPE => array(
305 'blockinfo',
306 'groups',
307 'rights',
308 'editcount',
309 'registration'
310 )
311 ),
312 'limit' => array(
313 ApiBase::PARAM_DFLT => 10,
314 ApiBase::PARAM_TYPE => 'limit',
315 ApiBase::PARAM_MIN => 1,
316 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
317 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
318 ),
319 'witheditsonly' => false,
320 'activeusers' => false,
321 );
322 }
323
324 public function getParamDescription() {
325 global $wgActiveUserDays;
326 return array(
327 'from' => 'The user name to start enumerating from',
328 'to' => 'The user name to stop enumerating at',
329 'prefix' => 'Search for all users that begin with this value',
330 'dir' => 'Direction to sort in',
331 'group' => 'Limit users to given group name(s)',
332 'excludegroup' => 'Exclude users in given group name(s)',
333 'rights' => 'Limit users to given right(s)',
334 'prop' => array(
335 'What pieces of information to include.',
336 ' blockinfo - Adds the information about a current block on the user',
337 ' groups - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
338 ' rights - Lists rights that the user has',
339 ' editcount - Adds the edit count of the user',
340 ' registration - Adds the timestamp of when the user registered if available (may be blank)',
341 ),
342 'limit' => 'How many total user names to return',
343 'witheditsonly' => 'Only list users who have made edits',
344 'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
345 );
346 }
347
348 public function getDescription() {
349 return 'Enumerate all registered users';
350 }
351
352 public function getPossibleErrors() {
353 return array_merge( parent::getPossibleErrors(), array(
354 array( 'code' => 'group-excludegroup', 'info' => 'group and excludegroup cannot be used together' ),
355 ) );
356 }
357
358 public function getExamples() {
359 return array(
360 'api.php?action=query&list=allusers&aufrom=Y',
361 );
362 }
363
364 public function getHelpUrls() {
365 return 'http://www.mediawiki.org/wiki/API:Allusers';
366 }
367
368 public function getVersion() {
369 return __CLASS__ . ': $Id$';
370 }
371 }