Merge "[FileBackend] Added container stat caching to reduce RTTs to high latency...
[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 public function execute() {
38 $db = $this->getDB();
39 $params = $this->extractRequestParams();
40
41 $prop = $params['prop'];
42 if ( !is_null( $prop ) ) {
43 $prop = array_flip( $prop );
44 $fld_blockinfo = isset( $prop['blockinfo'] );
45 $fld_editcount = isset( $prop['editcount'] );
46 $fld_groups = isset( $prop['groups'] );
47 $fld_rights = isset( $prop['rights'] );
48 $fld_registration = isset( $prop['registration'] );
49 $fld_implicitgroups = isset( $prop['implicitgroups'] );
50 } else {
51 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = $fld_rights = $fld_implicitgroups = false;
52 }
53
54 $limit = $params['limit'];
55
56 $this->addTables( 'user' );
57 $useIndex = true;
58
59 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
60 $from = is_null( $params['from'] ) ? null : $this->keyToTitle( $params['from'] );
61 $to = is_null( $params['to'] ) ? null : $this->keyToTitle( $params['to'] );
62
63 # MySQL doesn't seem to use 'equality propagation' here, so like the
64 # ActiveUsers special page, we have to use rc_user_text for some cases.
65 $userFieldToSort = $params['activeusers'] ? 'rc_user_text' : 'user_name';
66
67 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
68
69 if ( !is_null( $params['prefix'] ) ) {
70 $this->addWhere( $userFieldToSort .
71 $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', $userFieldToSort );
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', $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', $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 $lastUserObj = User::newFromId( $row->user_id );
238
239 // Add user's group info
240 if ( $fld_groups ) {
241 if ( !isset( $lastUserData['groups'] ) ) {
242 if ( $lastUserObj ) {
243 $lastUserData['groups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
244 } else {
245 // This should not normally happen
246 $lastUserData['groups'] = array();
247 }
248 }
249
250 if ( !is_null( $row->ug_group2 ) ) {
251 $lastUserData['groups'][] = $row->ug_group2;
252 }
253
254 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
255 }
256
257 if ( $fld_implicitgroups && !isset( $lastUserData['implicitgroups'] ) && $lastUserObj ) {
258 $lastUserData['implicitgroups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
259 $result->setIndexedTagName( $lastUserData['implicitgroups'], 'g' );
260 }
261 if ( $fld_rights ) {
262 if ( !isset( $lastUserData['rights'] ) ) {
263 if ( $lastUserObj ) {
264 $lastUserData['rights'] = User::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
265 } else {
266 // This should not normally happen
267 $lastUserData['rights'] = array();
268 }
269 }
270
271 if ( !is_null( $row->ug_group2 ) ) {
272 $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
273 User::getGroupPermissions( array( $row->ug_group2 ) ) ) );
274 }
275
276 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
277 }
278 }
279
280 if ( is_array( $lastUserData ) ) {
281 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
282 null, $lastUserData );
283 if ( !$fit ) {
284 $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
285 }
286 }
287
288 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
289 }
290
291 public function getCacheMode( $params ) {
292 return 'anon-public-user-private';
293 }
294
295 public function getAllowedParams() {
296 $userGroups = User::getAllGroups();
297 return array(
298 'from' => null,
299 'to' => null,
300 'prefix' => null,
301 'dir' => array(
302 ApiBase::PARAM_DFLT => 'ascending',
303 ApiBase::PARAM_TYPE => array(
304 'ascending',
305 'descending'
306 ),
307 ),
308 'group' => array(
309 ApiBase::PARAM_TYPE => $userGroups,
310 ApiBase::PARAM_ISMULTI => true,
311 ),
312 'excludegroup' => array(
313 ApiBase::PARAM_TYPE => $userGroups,
314 ApiBase::PARAM_ISMULTI => true,
315 ),
316 'rights' => array(
317 ApiBase::PARAM_TYPE => User::getAllRights(),
318 ApiBase::PARAM_ISMULTI => true,
319 ),
320 'prop' => array(
321 ApiBase::PARAM_ISMULTI => true,
322 ApiBase::PARAM_TYPE => array(
323 'blockinfo',
324 'groups',
325 'implicitgroups',
326 'rights',
327 'editcount',
328 'registration'
329 )
330 ),
331 'limit' => array(
332 ApiBase::PARAM_DFLT => 10,
333 ApiBase::PARAM_TYPE => 'limit',
334 ApiBase::PARAM_MIN => 1,
335 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
336 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
337 ),
338 'witheditsonly' => false,
339 'activeusers' => false,
340 );
341 }
342
343 public function getParamDescription() {
344 global $wgActiveUserDays;
345 return array(
346 'from' => 'The user name to start enumerating from',
347 'to' => 'The user name to stop enumerating at',
348 'prefix' => 'Search for all users that begin with this value',
349 'dir' => 'Direction to sort in',
350 'group' => 'Limit users to given group name(s)',
351 'excludegroup' => 'Exclude users in given group name(s)',
352 'rights' => 'Limit users to given right(s)',
353 'prop' => array(
354 'What pieces of information to include.',
355 ' blockinfo - Adds the information about a current block on the user',
356 ' groups - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
357 ' implicitgroups - Lists all the groups the user is automatically in',
358 ' rights - Lists rights that the user has',
359 ' editcount - Adds the edit count of the user',
360 ' registration - Adds the timestamp of when the user registered if available (may be blank)',
361 ),
362 'limit' => 'How many total user names to return',
363 'witheditsonly' => 'Only list users who have made edits',
364 'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
365 );
366 }
367
368 public function getDescription() {
369 return 'Enumerate all registered users';
370 }
371
372 public function getPossibleErrors() {
373 return array_merge( parent::getPossibleErrors(), array(
374 array( 'code' => 'group-excludegroup', 'info' => 'group and excludegroup cannot be used together' ),
375 ) );
376 }
377
378 public function getExamples() {
379 return array(
380 'api.php?action=query&list=allusers&aufrom=Y',
381 );
382 }
383
384 public function getHelpUrls() {
385 return 'https://www.mediawiki.org/wiki/API:Allusers';
386 }
387
388 public function getVersion() {
389 return __CLASS__ . ': $Id$';
390 }
391 }