5 * Created on July 7, 2007
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
28 * Query module to enumerate all registered users.
32 class ApiQueryAllUsers
extends ApiQueryBase
{
33 public function __construct( $query, $moduleName ) {
34 parent
::__construct( $query, $moduleName, 'au' );
38 * This function converts the user name to a canonical form
39 * which is stored in the database.
43 private function getCanonicalUserName( $name ) {
44 return str_replace( '_', ' ', $name );
47 public function execute() {
49 $params = $this->extractRequestParams();
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'] );
61 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration =
62 $fld_rights = $fld_implicitgroups = false;
65 $limit = $params['limit'];
67 $this->addTables( 'user' );
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'] );
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';
78 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
80 if ( !is_null( $params['prefix'] ) ) {
81 $this->addWhere( $userFieldToSort .
82 $db->buildLike( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() ) );
85 if ( !is_null( $params['rights'] ) && count( $params['rights'] ) ) {
87 foreach ( $params['rights'] as $r ) {
88 $groups = array_merge( $groups, User
::getGroupsWithPermission( $r ) );
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() ), '' );
98 $groups = array_unique( $groups );
100 if ( is_null( $params['group'] ) ) {
101 $params['group'] = $groups;
103 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
107 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
108 $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
111 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
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'] ) ) ) );
119 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
121 // Filter only users don't belong to a given group
122 $this->addTables( 'user_groups', 'ug1' );
124 if ( count( $params['excludegroup'] ) == 1 ) {
125 $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
127 $exclude = array( $db->makeList(
128 array( 'ug1.ug_group' => $params['excludegroup'] ),
132 $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
133 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
135 $this->addWhere( 'ug1.ug_user IS NULL' );
138 if ( $params['witheditsonly'] ) {
139 $this->addWhere( 'user_editcount > 0' );
142 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
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;
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' );
154 $sqlLimit = $limit +
1;
157 if ( $params['activeusers'] ) {
158 global $wgActiveUserDays;
159 $this->addTables( 'recentchanges' );
161 $this->addJoinConds( array( 'recentchanges' => array(
162 'INNER JOIN', 'rc_user_text=user_name'
165 $this->addFields( array( 'recentedits' => 'COUNT(*)' ) );
167 $this->addWhere( 'rc_log_type IS NULL OR rc_log_type != ' . $db->addQuotes( 'newusers' ) );
168 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX
) - $wgActiveUserDays * 24 * 3600 );
169 $this->addWhere( 'rc_timestamp >= ' . $db->addQuotes( $timestamp ) );
171 $this->addOption( 'GROUP BY', $userFieldToSort );
174 $this->addOption( 'LIMIT', $sqlLimit );
176 $this->addFields( array(
180 $this->addFieldsIf( 'user_editcount', $fld_editcount );
181 $this->addFieldsIf( 'user_registration', $fld_registration );
184 $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
187 $res = $this->select( __METHOD__
);
190 $lastUserData = false;
192 $result = $this->getResult();
194 // This loop keeps track of the last entry. For each new row, if the
195 // new row is for different user then the last, the last entry is added
196 // to results. Otherwise, the group of the new row is appended to the
197 // last entry. The setContinue... is more complex because of this, and
198 // takes into account the higher sql limit to make sure all rows that
199 // belong to the same user are received.
201 foreach ( $res as $row ) {
204 if ( $lastUser !== $row->user_name
) {
205 // Save the last pass's user data
206 if ( is_array( $lastUserData ) ) {
207 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
208 null, $lastUserData );
210 $lastUserData = null;
213 $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
218 if ( $count > $limit ) {
219 // We've reached the one extra which shows that there are
220 // additional pages to be had. Stop here...
221 $this->setContinueEnumParameter( 'from', $row->user_name
);
225 // Record new user's data
226 $lastUser = $row->user_name
;
227 $lastUserData = array(
228 'userid' => $row->user_id
,
231 if ( $fld_blockinfo && !is_null( $row->ipb_by_text
) ) {
232 $lastUserData['blockid'] = $row->ipb_id
;
233 $lastUserData['blockedby'] = $row->ipb_by_text
;
234 $lastUserData['blockedbyid'] = $row->ipb_by
;
235 $lastUserData['blockreason'] = $row->ipb_reason
;
236 $lastUserData['blockexpiry'] = $row->ipb_expiry
;
238 if ( $row->ipb_deleted
) {
239 $lastUserData['hidden'] = '';
241 if ( $fld_editcount ) {
242 $lastUserData['editcount'] = intval( $row->user_editcount
);
244 if ( $params['activeusers'] ) {
245 $lastUserData['recenteditcount'] = intval( $row->recentedits
);
247 if ( $fld_registration ) {
248 $lastUserData['registration'] = $row->user_registration ?
249 wfTimestamp( TS_ISO_8601
, $row->user_registration
) : '';
253 if ( $sqlLimit == $count ) {
254 // @todo BUG! database contains group name that User::getAllGroups() does not return
255 // Should handle this more gracefully
258 'MediaWiki configuration error: The database contains more ' .
259 'user groups than known to User::getAllGroups() function'
263 $lastUserObj = User
::newFromId( $row->user_id
);
265 // Add user's group info
267 if ( !isset( $lastUserData['groups'] ) ) {
268 if ( $lastUserObj ) {
269 $lastUserData['groups'] = $lastUserObj->getAutomaticGroups();
271 // This should not normally happen
272 $lastUserData['groups'] = array();
276 if ( !is_null( $row->ug_group2
) ) {
277 $lastUserData['groups'][] = $row->ug_group2
;
280 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
283 if ( $fld_implicitgroups && !isset( $lastUserData['implicitgroups'] ) && $lastUserObj ) {
284 $lastUserData['implicitgroups'] = $lastUserObj->getAutomaticGroups();
285 $result->setIndexedTagName( $lastUserData['implicitgroups'], 'g' );
288 if ( !isset( $lastUserData['rights'] ) ) {
289 if ( $lastUserObj ) {
290 $lastUserData['rights'] = User
::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
292 // This should not normally happen
293 $lastUserData['rights'] = array();
297 if ( !is_null( $row->ug_group2
) ) {
298 $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
299 User
::getGroupPermissions( array( $row->ug_group2
) ) ) );
302 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
306 if ( is_array( $lastUserData ) ) {
307 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
308 null, $lastUserData );
310 $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
314 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
317 public function getCacheMode( $params ) {
318 return 'anon-public-user-private';
321 public function getAllowedParams() {
322 $userGroups = User
::getAllGroups();
329 ApiBase
::PARAM_DFLT
=> 'ascending',
330 ApiBase
::PARAM_TYPE
=> array(
336 ApiBase
::PARAM_TYPE
=> $userGroups,
337 ApiBase
::PARAM_ISMULTI
=> true,
339 'excludegroup' => array(
340 ApiBase
::PARAM_TYPE
=> $userGroups,
341 ApiBase
::PARAM_ISMULTI
=> true,
344 ApiBase
::PARAM_TYPE
=> User
::getAllRights(),
345 ApiBase
::PARAM_ISMULTI
=> true,
348 ApiBase
::PARAM_ISMULTI
=> true,
349 ApiBase
::PARAM_TYPE
=> array(
359 ApiBase
::PARAM_DFLT
=> 10,
360 ApiBase
::PARAM_TYPE
=> 'limit',
361 ApiBase
::PARAM_MIN
=> 1,
362 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
363 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
365 'witheditsonly' => false,
366 'activeusers' => false,
370 public function getParamDescription() {
371 global $wgActiveUserDays;
374 'from' => 'The user name to start enumerating from',
375 'to' => 'The user name to stop enumerating at',
376 'prefix' => 'Search for all users that begin with this value',
377 'dir' => 'Direction to sort in',
378 'group' => 'Limit users to given group name(s)',
379 'excludegroup' => 'Exclude users in given group name(s)',
380 'rights' => 'Limit users to given right(s) (does not include rights ' .
381 'granted by implicit or auto-promoted groups like *, user, or autoconfirmed)',
383 'What pieces of information to include.',
384 ' blockinfo - Adds the information about a current block on the user',
385 ' groups - Lists groups that the user is in. This uses ' .
386 'more server resources and may return fewer results than the limit',
387 ' implicitgroups - Lists all the groups the user is automatically in',
388 ' rights - Lists rights that the user has',
389 ' editcount - Adds the edit count of the user',
390 ' registration - Adds the timestamp of when the user registered if available (may be blank)',
392 'limit' => 'How many total user names to return',
393 'witheditsonly' => 'Only list users who have made edits',
394 'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
398 public function getResultProperties() {
401 'userid' => 'integer',
403 'recenteditcount' => array(
404 ApiBase
::PROP_TYPE
=> 'integer',
405 ApiBase
::PROP_NULLABLE
=> true
408 'blockinfo' => array(
410 ApiBase
::PROP_TYPE
=> 'integer',
411 ApiBase
::PROP_NULLABLE
=> true
413 'blockedby' => array(
414 ApiBase
::PROP_TYPE
=> 'string',
415 ApiBase
::PROP_NULLABLE
=> true
417 'blockedbyid' => array(
418 ApiBase
::PROP_TYPE
=> 'integer',
419 ApiBase
::PROP_NULLABLE
=> true
421 'blockedreason' => array(
422 ApiBase
::PROP_TYPE
=> 'string',
423 ApiBase
::PROP_NULLABLE
=> true
425 'blockedexpiry' => array(
426 ApiBase
::PROP_TYPE
=> 'string',
427 ApiBase
::PROP_NULLABLE
=> true
429 'hidden' => 'boolean'
431 'editcount' => array(
432 'editcount' => 'integer'
434 'registration' => array(
435 'registration' => 'string'
440 public function getDescription() {
441 return 'Enumerate all registered users.';
444 public function getPossibleErrors() {
445 return array_merge( parent
::getPossibleErrors(), array(
447 'code' => 'group-excludegroup',
448 'info' => 'group and excludegroup cannot be used together'
453 public function getExamples() {
455 'api.php?action=query&list=allusers&aufrom=Y',
459 public function getHelpUrls() {
460 return 'https://www.mediawiki.org/wiki/API:Allusers';