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