Moved contribs rev parent ID batch query into doBatchLookups()
[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 $fld_implicitgroups = isset( $prop['implicitgroups'] );
55 } else {
56 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = $fld_rights = $fld_implicitgroups = false;
57 }
58
59 $limit = $params['limit'];
60
61 $this->addTables( 'user' );
62 $useIndex = true;
63
64 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
65 $from = is_null( $params['from'] ) ? null : $this->keyToTitle( $params['from'] );
66 $to = is_null( $params['to'] ) ? null : $this->keyToTitle( $params['to'] );
67
68 $this->addWhereRange( 'user_name', $dir, $from, $to );
69
70 if ( !is_null( $params['prefix'] ) ) {
71 $this->addWhere( 'user_name' . $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
72 }
73
74 if ( !is_null( $params['rights'] ) ) {
75 $groups = array();
76 foreach( $params['rights'] as $r ) {
77 $groups = array_merge( $groups, User::getGroupsWithPermission( $r ) );
78 }
79
80 $groups = array_unique( $groups );
81
82 if ( is_null( $params['group'] ) ) {
83 $params['group'] = $groups;
84 } else {
85 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
86 }
87 }
88
89 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
90 $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
91 }
92
93 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
94 $useIndex = false;
95 // Filter only users that belong to a given group
96 $this->addTables( 'user_groups', 'ug1' );
97 $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
98 'ug1.ug_group' => $params['group'] ) ) ) );
99 }
100
101 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
102 $useIndex = false;
103 // Filter only users don't belong to a given group
104 $this->addTables( 'user_groups', 'ug1' );
105
106 if ( count( $params['excludegroup'] ) == 1 ) {
107 $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
108 } else {
109 $exclude = array( $db->makeList( array( 'ug1.ug_group' => $params['excludegroup'] ), LIST_OR ) );
110 }
111 $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
112 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
113 )
114 ) );
115 $this->addWhere( 'ug1.ug_user IS NULL' );
116 }
117
118 if ( $params['witheditsonly'] ) {
119 $this->addWhere( 'user_editcount > 0' );
120 }
121
122 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
123
124 if ( $fld_groups || $fld_rights ) {
125 // Show the groups the given users belong to
126 // request more than needed to avoid not getting all rows that belong to one user
127 $groupCount = count( User::getAllGroups() );
128 $sqlLimit = $limit + $groupCount + 1;
129
130 $this->addTables( 'user_groups', 'ug2' );
131 $this->addJoinConds( array( 'ug2' => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
132 $this->addFields( 'ug2.ug_group ug_group2' );
133 } else {
134 $sqlLimit = $limit + 1;
135 }
136
137 if ( $params['activeusers'] ) {
138 global $wgActiveUserDays;
139 $this->addTables( 'recentchanges' );
140
141 $this->addJoinConds( array( 'recentchanges' => array(
142 'INNER JOIN', 'rc_user_text=user_name'
143 ) ) );
144
145 $this->addFields( 'COUNT(*) AS recentedits' );
146
147 $this->addWhere( "rc_log_type IS NULL OR rc_log_type != 'newusers'" );
148 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 );
149 $this->addWhere( "rc_timestamp >= {$db->addQuotes( $timestamp )}" );
150
151 $this->addOption( 'GROUP BY', 'user_name' );
152 }
153
154 $this->addOption( 'LIMIT', $sqlLimit );
155
156 $this->addFields( array(
157 'user_name',
158 'user_id'
159 ) );
160 $this->addFieldsIf( 'user_editcount', $fld_editcount );
161 $this->addFieldsIf( 'user_registration', $fld_registration );
162
163 if ( $useIndex ) {
164 $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
165 }
166
167 $res = $this->select( __METHOD__ );
168
169 $count = 0;
170 $lastUserData = false;
171 $lastUser = false;
172 $result = $this->getResult();
173
174 //
175 // This loop keeps track of the last entry.
176 // For each new row, if the new row is for different user then the last, the last entry is added to results.
177 // Otherwise, the group of the new row is appended to the last entry.
178 // The setContinue... is more complex because of this, and takes into account the higher sql limit
179 // to make sure all rows that belong to the same user are received.
180
181 foreach ( $res as $row ) {
182 $count++;
183
184 if ( $lastUser !== $row->user_name ) {
185 // Save the last pass's user data
186 if ( is_array( $lastUserData ) ) {
187 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
188 null, $lastUserData );
189
190 $lastUserData = null;
191
192 if ( !$fit ) {
193 $this->setContinueEnumParameter( 'from',
194 $this->keyToTitle( $lastUserData['name'] ) );
195 break;
196 }
197 }
198
199 if ( $count > $limit ) {
200 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
201 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
202 break;
203 }
204
205 // Record new user's data
206 $lastUser = $row->user_name;
207 $lastUserData = array(
208 'userid' => $row->user_id,
209 'name' => $lastUser,
210 );
211 if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
212 $lastUserData['blockedby'] = $row->ipb_by_text;
213 $lastUserData['blockreason'] = $row->ipb_reason;
214 $lastUserData['blockexpiry'] = $row->ipb_expiry;
215 }
216 if ( $row->ipb_deleted ) {
217 $lastUserData['hidden'] = '';
218 }
219 if ( $fld_editcount ) {
220 $lastUserData['editcount'] = intval( $row->user_editcount );
221 }
222 if ( $params['activeusers'] ) {
223 $lastUserData['recenteditcount'] = intval( $row->recentedits );
224 }
225 if ( $fld_registration ) {
226 $lastUserData['registration'] = $row->user_registration ?
227 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
228 }
229 }
230
231 if ( $sqlLimit == $count ) {
232 // BUG! database contains group name that User::getAllGroups() does not return
233 // TODO: should handle this more gracefully
234 ApiBase::dieDebug( __METHOD__,
235 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
236 }
237
238 $lastUserObj = User::newFromName( $lastUser );
239
240 // Add user's group info
241 if ( $fld_groups ) {
242 if ( !isset( $lastUserData['groups'] ) && $lastUserObj ) {
243 $lastUserData['groups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
244 }
245
246 if ( !is_null( $row->ug_group2 ) ) {
247 $lastUserData['groups'][] = $row->ug_group2;
248 }
249 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
250 }
251
252 if ( $fld_implicitgroups && !isset( $lastUserData['implicitgroups'] ) && $lastUserObj ) {
253 $lastUserData['implicitgroups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
254 $result->setIndexedTagName( $lastUserData['implicitgroups'], 'g' );
255 }
256 if ( $fld_rights ) {
257 if ( !isset( $lastUserData['rights'] ) && $lastUserObj ) {
258 $lastUserData['rights'] = User::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
259 }
260 if ( !is_null( $row->ug_group2 ) ) {
261 $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
262 User::getGroupPermissions( array( $row->ug_group2 ) ) ) );
263 }
264 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
265 }
266 }
267
268 if ( is_array( $lastUserData ) ) {
269 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
270 null, $lastUserData );
271 if ( !$fit ) {
272 $this->setContinueEnumParameter( 'from',
273 $this->keyToTitle( $lastUserData['name'] ) );
274 }
275 }
276
277 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
278 }
279
280 public function getCacheMode( $params ) {
281 return 'anon-public-user-private';
282 }
283
284 public function getAllowedParams() {
285 $userGroups = User::getAllGroups();
286 return array(
287 'from' => null,
288 'to' => null,
289 'prefix' => null,
290 'dir' => array(
291 ApiBase::PARAM_DFLT => 'ascending',
292 ApiBase::PARAM_TYPE => array(
293 'ascending',
294 'descending'
295 ),
296 ),
297 'group' => array(
298 ApiBase::PARAM_TYPE => $userGroups,
299 ApiBase::PARAM_ISMULTI => true,
300 ),
301 'excludegroup' => array(
302 ApiBase::PARAM_TYPE => $userGroups,
303 ApiBase::PARAM_ISMULTI => true,
304 ),
305 'rights' => array(
306 ApiBase::PARAM_TYPE => User::getAllRights(),
307 ApiBase::PARAM_ISMULTI => true,
308 ),
309 'prop' => array(
310 ApiBase::PARAM_ISMULTI => true,
311 ApiBase::PARAM_TYPE => array(
312 'blockinfo',
313 'groups',
314 'implicitgroups',
315 'rights',
316 'editcount',
317 'registration'
318 )
319 ),
320 'limit' => array(
321 ApiBase::PARAM_DFLT => 10,
322 ApiBase::PARAM_TYPE => 'limit',
323 ApiBase::PARAM_MIN => 1,
324 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
325 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
326 ),
327 'witheditsonly' => false,
328 'activeusers' => false,
329 );
330 }
331
332 public function getParamDescription() {
333 global $wgActiveUserDays;
334 return array(
335 'from' => 'The user name to start enumerating from',
336 'to' => 'The user name to stop enumerating at',
337 'prefix' => 'Search for all users that begin with this value',
338 'dir' => 'Direction to sort in',
339 'group' => 'Limit users to given group name(s)',
340 'excludegroup' => 'Exclude users in given group name(s)',
341 'rights' => 'Limit users to given right(s)',
342 'prop' => array(
343 'What pieces of information to include.',
344 ' blockinfo - Adds the information about a current block on the user',
345 ' groups - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
346 ' implicitgroups - Lists all the groups the user is automatically in',
347 ' rights - Lists rights that the user has',
348 ' editcount - Adds the edit count of the user',
349 ' registration - Adds the timestamp of when the user registered if available (may be blank)',
350 ),
351 'limit' => 'How many total user names to return',
352 'witheditsonly' => 'Only list users who have made edits',
353 'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
354 );
355 }
356
357 public function getDescription() {
358 return 'Enumerate all registered users';
359 }
360
361 public function getPossibleErrors() {
362 return array_merge( parent::getPossibleErrors(), array(
363 array( 'code' => 'group-excludegroup', 'info' => 'group and excludegroup cannot be used together' ),
364 ) );
365 }
366
367 public function getExamples() {
368 return array(
369 'api.php?action=query&list=allusers&aufrom=Y',
370 );
371 }
372
373 public function getHelpUrls() {
374 return 'http://www.mediawiki.org/wiki/API:Allusers';
375 }
376
377 public function getVersion() {
378 return __CLASS__ . ': $Id$';
379 }
380 }