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