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