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 = $fld_rights = $fld_implicitgroups = false;
64 $limit = $params['limit'];
66 $this->addTables( 'user' );
69 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
70 $from = is_null( $params['from'] ) ?
null : $this->getCanonicalUserName( $params['from'] );
71 $to = is_null( $params['to'] ) ?
null : $this->getCanonicalUserName( $params['to'] );
73 # MySQL doesn't seem to use 'equality propagation' here, so like the
74 # ActiveUsers special page, we have to use rc_user_text for some cases.
75 $userFieldToSort = $params['activeusers'] ?
'rc_user_text' : 'user_name';
77 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
79 if ( !is_null( $params['prefix'] ) ) {
80 $this->addWhere( $userFieldToSort .
81 $db->buildLike( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() ) );
84 if ( !is_null( $params['rights'] ) ) {
86 foreach( $params['rights'] as $r ) {
87 $groups = array_merge( $groups, User
::getGroupsWithPermission( $r ) );
90 $groups = array_unique( $groups );
92 if ( is_null( $params['group'] ) ) {
93 $params['group'] = $groups;
95 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
99 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
100 $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
103 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
105 // Filter only users that belong to a given group
106 $this->addTables( 'user_groups', 'ug1' );
107 $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
108 'ug1.ug_group' => $params['group'] ) ) ) );
111 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
113 // Filter only users don't belong to a given group
114 $this->addTables( 'user_groups', 'ug1' );
116 if ( count( $params['excludegroup'] ) == 1 ) {
117 $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
119 $exclude = array( $db->makeList( array( 'ug1.ug_group' => $params['excludegroup'] ), LIST_OR
) );
121 $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
122 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
125 $this->addWhere( 'ug1.ug_user IS NULL' );
128 if ( $params['witheditsonly'] ) {
129 $this->addWhere( 'user_editcount > 0' );
132 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
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;
140 $this->addTables( 'user_groups', 'ug2' );
141 $this->addJoinConds( array( 'ug2' => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
142 $this->addFields( 'ug2.ug_group ug_group2' );
144 $sqlLimit = $limit +
1;
147 if ( $params['activeusers'] ) {
148 global $wgActiveUserDays;
149 $this->addTables( 'recentchanges' );
151 $this->addJoinConds( array( 'recentchanges' => array(
152 'INNER JOIN', 'rc_user_text=user_name'
155 $this->addFields( 'COUNT(*) AS recentedits' );
157 $this->addWhere( "rc_log_type IS NULL OR rc_log_type != 'newusers'" );
158 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX
) - $wgActiveUserDays*24*3600 );
159 $this->addWhere( "rc_timestamp >= {$db->addQuotes( $timestamp )}" );
161 $this->addOption( 'GROUP BY', $userFieldToSort );
164 $this->addOption( 'LIMIT', $sqlLimit );
166 $this->addFields( array(
170 $this->addFieldsIf( 'user_editcount', $fld_editcount );
171 $this->addFieldsIf( 'user_registration', $fld_registration );
174 $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
177 $res = $this->select( __METHOD__
);
180 $lastUserData = false;
182 $result = $this->getResult();
185 // This loop keeps track of the last entry.
186 // For each new row, if the new row is for different user then the last, the last entry is added to results.
187 // Otherwise, the group of the new row is appended to the last entry.
188 // The setContinue... is more complex because of this, and takes into account the higher sql limit
189 // to make sure all rows that belong to the same user are received.
191 foreach ( $res as $row ) {
194 if ( $lastUser !== $row->user_name
) {
195 // Save the last pass's user data
196 if ( is_array( $lastUserData ) ) {
197 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
198 null, $lastUserData );
200 $lastUserData = null;
203 $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
208 if ( $count > $limit ) {
209 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
210 $this->setContinueEnumParameter( 'from', $row->user_name
);
214 // Record new user's data
215 $lastUser = $row->user_name
;
216 $lastUserData = array(
217 'userid' => $row->user_id
,
220 if ( $fld_blockinfo && !is_null( $row->ipb_by_text
) ) {
221 $lastUserData['blockedby'] = $row->ipb_by_text
;
222 $lastUserData['blockreason'] = $row->ipb_reason
;
223 $lastUserData['blockexpiry'] = $row->ipb_expiry
;
225 if ( $row->ipb_deleted
) {
226 $lastUserData['hidden'] = '';
228 if ( $fld_editcount ) {
229 $lastUserData['editcount'] = intval( $row->user_editcount
);
231 if ( $params['activeusers'] ) {
232 $lastUserData['recenteditcount'] = intval( $row->recentedits
);
234 if ( $fld_registration ) {
235 $lastUserData['registration'] = $row->user_registration ?
236 wfTimestamp( TS_ISO_8601
, $row->user_registration
) : '';
240 if ( $sqlLimit == $count ) {
241 // BUG! database contains group name that User::getAllGroups() does not return
242 // TODO: should handle this more gracefully
243 ApiBase
::dieDebug( __METHOD__
,
244 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
247 $lastUserObj = User
::newFromId( $row->user_id
);
249 // Add user's group info
251 if ( !isset( $lastUserData['groups'] ) ) {
252 if ( $lastUserObj ) {
253 $lastUserData['groups'] = ApiQueryUsers
::getAutoGroups( $lastUserObj );
255 // This should not normally happen
256 $lastUserData['groups'] = array();
260 if ( !is_null( $row->ug_group2
) ) {
261 $lastUserData['groups'][] = $row->ug_group2
;
264 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
267 if ( $fld_implicitgroups && !isset( $lastUserData['implicitgroups'] ) && $lastUserObj ) {
268 $lastUserData['implicitgroups'] = ApiQueryUsers
::getAutoGroups( $lastUserObj );
269 $result->setIndexedTagName( $lastUserData['implicitgroups'], 'g' );
272 if ( !isset( $lastUserData['rights'] ) ) {
273 if ( $lastUserObj ) {
274 $lastUserData['rights'] = User
::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
276 // This should not normally happen
277 $lastUserData['rights'] = array();
281 if ( !is_null( $row->ug_group2
) ) {
282 $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
283 User
::getGroupPermissions( array( $row->ug_group2
) ) ) );
286 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
290 if ( is_array( $lastUserData ) ) {
291 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
292 null, $lastUserData );
294 $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
298 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
301 public function getCacheMode( $params ) {
302 return 'anon-public-user-private';
305 public function getAllowedParams() {
306 $userGroups = User
::getAllGroups();
312 ApiBase
::PARAM_DFLT
=> 'ascending',
313 ApiBase
::PARAM_TYPE
=> array(
319 ApiBase
::PARAM_TYPE
=> $userGroups,
320 ApiBase
::PARAM_ISMULTI
=> true,
322 'excludegroup' => array(
323 ApiBase
::PARAM_TYPE
=> $userGroups,
324 ApiBase
::PARAM_ISMULTI
=> true,
327 ApiBase
::PARAM_TYPE
=> User
::getAllRights(),
328 ApiBase
::PARAM_ISMULTI
=> true,
331 ApiBase
::PARAM_ISMULTI
=> true,
332 ApiBase
::PARAM_TYPE
=> array(
342 ApiBase
::PARAM_DFLT
=> 10,
343 ApiBase
::PARAM_TYPE
=> 'limit',
344 ApiBase
::PARAM_MIN
=> 1,
345 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
346 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
348 'witheditsonly' => false,
349 'activeusers' => false,
353 public function getParamDescription() {
354 global $wgActiveUserDays;
356 'from' => 'The user name to start enumerating from',
357 'to' => 'The user name to stop enumerating at',
358 'prefix' => 'Search for all users that begin with this value',
359 'dir' => 'Direction to sort in',
360 'group' => 'Limit users to given group name(s)',
361 'excludegroup' => 'Exclude users in given group name(s)',
362 'rights' => 'Limit users to given right(s)',
364 'What pieces of information to include.',
365 ' blockinfo - Adds the information about a current block on the user',
366 ' groups - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
367 ' implicitgroups - Lists all the groups the user is automatically in',
368 ' rights - Lists rights that the user has',
369 ' editcount - Adds the edit count of the user',
370 ' registration - Adds the timestamp of when the user registered if available (may be blank)',
372 'limit' => 'How many total user names to return',
373 'witheditsonly' => 'Only list users who have made edits',
374 'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
378 public function getDescription() {
379 return 'Enumerate all registered users';
382 public function getPossibleErrors() {
383 return array_merge( parent
::getPossibleErrors(), array(
384 array( 'code' => 'group-excludegroup', 'info' => 'group and excludegroup cannot be used together' ),
388 public function getExamples() {
390 'api.php?action=query&list=allusers&aufrom=Y',
394 public function getHelpUrls() {
395 return 'https://www.mediawiki.org/wiki/API:Allusers';
398 public function getVersion() {
399 return __CLASS__
. ': $Id$';