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' );
37 public function execute() {
39 $params = $this->extractRequestParams();
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'] );
51 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = $fld_rights = $fld_implicitgroups = false;
54 $limit = $params['limit'];
56 $this->addTables( 'user' );
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'] );
63 $this->addWhereRange( 'user_name', $dir, $from, $to );
65 if ( !is_null( $params['prefix'] ) ) {
66 $this->addWhere( 'user_name' . $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
69 if ( !is_null( $params['rights'] ) ) {
71 foreach( $params['rights'] as $r ) {
72 $groups = array_merge( $groups, User
::getGroupsWithPermission( $r ) );
75 $groups = array_unique( $groups );
77 if ( is_null( $params['group'] ) ) {
78 $params['group'] = $groups;
80 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
84 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
85 $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
88 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
90 // Filter only users that belong to a given group
91 $this->addTables( 'user_groups', 'ug1' );
92 $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
93 'ug1.ug_group' => $params['group'] ) ) ) );
96 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
98 // Filter only users don't belong to a given group
99 $this->addTables( 'user_groups', 'ug1' );
101 if ( count( $params['excludegroup'] ) == 1 ) {
102 $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
104 $exclude = array( $db->makeList( array( 'ug1.ug_group' => $params['excludegroup'] ), LIST_OR
) );
106 $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
107 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
110 $this->addWhere( 'ug1.ug_user IS NULL' );
113 if ( $params['witheditsonly'] ) {
114 $this->addWhere( 'user_editcount > 0' );
117 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
119 if ( $fld_groups ||
$fld_rights ) {
120 // Show the groups the given users belong to
121 // request more than needed to avoid not getting all rows that belong to one user
122 $groupCount = count( User
::getAllGroups() );
123 $sqlLimit = $limit +
$groupCount +
1;
125 $this->addTables( 'user_groups', 'ug2' );
126 $this->addJoinConds( array( 'ug2' => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
127 $this->addFields( 'ug2.ug_group ug_group2' );
129 $sqlLimit = $limit +
1;
132 if ( $params['activeusers'] ) {
133 global $wgActiveUserDays;
134 $this->addTables( 'recentchanges' );
136 $this->addJoinConds( array( 'recentchanges' => array(
137 'INNER JOIN', 'rc_user_text=user_name'
140 $this->addFields( 'COUNT(*) AS recentedits' );
142 $this->addWhere( "rc_log_type IS NULL OR rc_log_type != 'newusers'" );
143 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX
) - $wgActiveUserDays*24*3600 );
144 $this->addWhere( "rc_timestamp >= {$db->addQuotes( $timestamp )}" );
146 $this->addOption( 'GROUP BY', 'user_name' );
149 $this->addOption( 'LIMIT', $sqlLimit );
151 $this->addFields( array(
155 $this->addFieldsIf( 'user_editcount', $fld_editcount );
156 $this->addFieldsIf( 'user_registration', $fld_registration );
159 $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
162 $res = $this->select( __METHOD__
);
165 $lastUserData = false;
167 $result = $this->getResult();
170 // This loop keeps track of the last entry.
171 // For each new row, if the new row is for different user then the last, the last entry is added to results.
172 // Otherwise, the group of the new row is appended to the last entry.
173 // The setContinue... is more complex because of this, and takes into account the higher sql limit
174 // to make sure all rows that belong to the same user are received.
176 foreach ( $res as $row ) {
179 if ( $lastUser !== $row->user_name
) {
180 // Save the last pass's user data
181 if ( is_array( $lastUserData ) ) {
182 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
183 null, $lastUserData );
185 $lastUserData = null;
188 $this->setContinueEnumParameter( 'from',
189 $this->keyToTitle( $lastUserData['name'] ) );
194 if ( $count > $limit ) {
195 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
196 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name
) );
200 // Record new user's data
201 $lastUser = $row->user_name
;
202 $lastUserData = array(
203 'userid' => $row->user_id
,
206 if ( $fld_blockinfo && !is_null( $row->ipb_by_text
) ) {
207 $lastUserData['blockedby'] = $row->ipb_by_text
;
208 $lastUserData['blockreason'] = $row->ipb_reason
;
209 $lastUserData['blockexpiry'] = $row->ipb_expiry
;
211 if ( $row->ipb_deleted
) {
212 $lastUserData['hidden'] = '';
214 if ( $fld_editcount ) {
215 $lastUserData['editcount'] = intval( $row->user_editcount
);
217 if ( $params['activeusers'] ) {
218 $lastUserData['recenteditcount'] = intval( $row->recentedits
);
220 if ( $fld_registration ) {
221 $lastUserData['registration'] = $row->user_registration ?
222 wfTimestamp( TS_ISO_8601
, $row->user_registration
) : '';
226 if ( $sqlLimit == $count ) {
227 // BUG! database contains group name that User::getAllGroups() does not return
228 // TODO: should handle this more gracefully
229 ApiBase
::dieDebug( __METHOD__
,
230 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
233 $lastUserObj = User
::newFromName( $lastUser );
235 // Add user's group info
237 if ( !isset( $lastUserData['groups'] ) && $lastUserObj ) {
238 $lastUserData['groups'] = ApiQueryUsers
::getAutoGroups( $lastUserObj );
241 if ( !is_null( $row->ug_group2
) ) {
242 $lastUserData['groups'][] = $row->ug_group2
;
244 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
247 if ( $fld_implicitgroups && !isset( $lastUserData['implicitgroups'] ) && $lastUserObj ) {
248 $lastUserData['implicitgroups'] = ApiQueryUsers
::getAutoGroups( $lastUserObj );
249 $result->setIndexedTagName( $lastUserData['implicitgroups'], 'g' );
252 if ( !isset( $lastUserData['rights'] ) && $lastUserObj ) {
253 $lastUserData['rights'] = User
::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
255 if ( !is_null( $row->ug_group2
) ) {
256 $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
257 User
::getGroupPermissions( array( $row->ug_group2
) ) ) );
259 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
263 if ( is_array( $lastUserData ) ) {
264 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
265 null, $lastUserData );
267 $this->setContinueEnumParameter( 'from',
268 $this->keyToTitle( $lastUserData['name'] ) );
272 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
275 public function getCacheMode( $params ) {
276 return 'anon-public-user-private';
279 public function getAllowedParams() {
280 $userGroups = User
::getAllGroups();
286 ApiBase
::PARAM_DFLT
=> 'ascending',
287 ApiBase
::PARAM_TYPE
=> array(
293 ApiBase
::PARAM_TYPE
=> $userGroups,
294 ApiBase
::PARAM_ISMULTI
=> true,
296 'excludegroup' => array(
297 ApiBase
::PARAM_TYPE
=> $userGroups,
298 ApiBase
::PARAM_ISMULTI
=> true,
301 ApiBase
::PARAM_TYPE
=> User
::getAllRights(),
302 ApiBase
::PARAM_ISMULTI
=> true,
305 ApiBase
::PARAM_ISMULTI
=> true,
306 ApiBase
::PARAM_TYPE
=> array(
316 ApiBase
::PARAM_DFLT
=> 10,
317 ApiBase
::PARAM_TYPE
=> 'limit',
318 ApiBase
::PARAM_MIN
=> 1,
319 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
320 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
322 'witheditsonly' => false,
323 'activeusers' => false,
327 public function getParamDescription() {
328 global $wgActiveUserDays;
330 'from' => 'The user name to start enumerating from',
331 'to' => 'The user name to stop enumerating at',
332 'prefix' => 'Search for all users that begin with this value',
333 'dir' => 'Direction to sort in',
334 'group' => 'Limit users to given group name(s)',
335 'excludegroup' => 'Exclude users in given group name(s)',
336 'rights' => 'Limit users to given right(s)',
338 'What pieces of information to include.',
339 ' blockinfo - Adds the information about a current block on the user',
340 ' groups - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
341 ' implicitgroups - Lists all the groups the user is automatically in',
342 ' rights - Lists rights that the user has',
343 ' editcount - Adds the edit count of the user',
344 ' registration - Adds the timestamp of when the user registered if available (may be blank)',
346 'limit' => 'How many total user names to return',
347 'witheditsonly' => 'Only list users who have made edits',
348 'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
352 public function getDescription() {
353 return 'Enumerate all registered users';
356 public function getPossibleErrors() {
357 return array_merge( parent
::getPossibleErrors(), array(
358 array( 'code' => 'group-excludegroup', 'info' => 'group and excludegroup cannot be used together' ),
362 public function getExamples() {
364 'api.php?action=query&list=allusers&aufrom=Y',
368 public function getHelpUrls() {
369 return 'https://www.mediawiki.org/wiki/API:Allusers';
372 public function getVersion() {
373 return __CLASS__
. ': $Id$';