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( ApiQuery
$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() {
48 $params = $this->extractRequestParams();
49 $activeUserDays = $this->getConfig()->get( 'ActiveUserDays' );
51 if ( $params['activeusers'] ) {
52 // Update active user cache
53 SpecialActiveUsers
::mergeActiveUsers( 600, $activeUserDays );
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'] );
68 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration =
69 $fld_rights = $fld_implicitgroups = false;
72 $limit = $params['limit'];
74 $this->addTables( 'user' );
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'] );
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';
85 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
87 if ( !is_null( $params['prefix'] ) ) {
88 $this->addWhere( $userFieldToSort .
89 $db->buildLike( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() ) );
92 if ( !is_null( $params['rights'] ) && count( $params['rights'] ) ) {
94 foreach ( $params['rights'] as $r ) {
95 $groups = array_merge( $groups, User
::getGroupsWithPermission( $r ) );
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() ), '' );
105 $groups = array_unique( $groups );
107 if ( is_null( $params['group'] ) ) {
108 $params['group'] = $groups;
110 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
114 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
115 $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
118 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
120 // Filter only users that belong to a given group
121 $this->addTables( 'user_groups', 'ug1' );
122 $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
123 'ug1.ug_group' => $params['group'] ) ) ) );
126 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
128 // Filter only users don't belong to a given group
129 $this->addTables( 'user_groups', 'ug1' );
131 if ( count( $params['excludegroup'] ) == 1 ) {
132 $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
134 $exclude = array( $db->makeList(
135 array( 'ug1.ug_group' => $params['excludegroup'] ),
139 $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
140 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
142 $this->addWhere( 'ug1.ug_user IS NULL' );
145 if ( $params['witheditsonly'] ) {
146 $this->addWhere( 'user_editcount > 0' );
149 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
151 if ( $fld_groups ||
$fld_rights ) {
152 // Show the groups the given users belong to
153 // request more than needed to avoid not getting all rows that belong to one user
154 $groupCount = count( User
::getAllGroups() );
155 $sqlLimit = $limit +
$groupCount +
1;
157 $this->addTables( 'user_groups', 'ug2' );
158 $this->addJoinConds( array( 'ug2' => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
159 $this->addFields( 'ug2.ug_group ug_group2' );
161 $sqlLimit = $limit +
1;
164 if ( $params['activeusers'] ) {
165 $activeUserSeconds = $activeUserDays * 86400;
167 // Filter query to only include users in the active users cache
168 $this->addTables( 'querycachetwo' );
169 $this->addJoinConds( array( 'querycachetwo' => array(
171 'qcc_type' => 'activeusers',
172 'qcc_namespace' => NS_USER
,
173 'qcc_title=user_name',
177 // Actually count the actions using a subquery (bug 64505 and bug 64507)
178 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX
) - $activeUserSeconds );
179 $this->addFields( array(
180 'recentactions' => '(' . $db->selectSQLText(
184 'rc_user_text = user_name',
185 'rc_type != ' . $db->addQuotes( RC_EXTERNAL
), // no wikidata
186 'rc_log_type IS NULL OR rc_log_type != ' . $db->addQuotes( 'newusers' ),
187 'rc_timestamp >= ' . $db->addQuotes( $timestamp ),
193 $this->addOption( 'LIMIT', $sqlLimit );
195 $this->addFields( array(
199 $this->addFieldsIf( 'user_editcount', $fld_editcount );
200 $this->addFieldsIf( 'user_registration', $fld_registration );
203 $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
206 $res = $this->select( __METHOD__
);
209 $lastUserData = false;
211 $result = $this->getResult();
213 // This loop keeps track of the last entry. For each new row, if the
214 // new row is for different user then the last, the last entry is added
215 // to results. Otherwise, the group of the new row is appended to the
216 // last entry. The setContinue... is more complex because of this, and
217 // takes into account the higher sql limit to make sure all rows that
218 // belong to the same user are received.
220 foreach ( $res as $row ) {
223 if ( $lastUser !== $row->user_name
) {
224 // Save the last pass's user data
225 if ( is_array( $lastUserData ) ) {
226 if ( $params['activeusers'] && $lastUserData['recentactions'] === 0 ) {
227 // activeusers cache was out of date
230 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
231 null, $lastUserData );
234 $lastUserData = null;
237 $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
242 if ( $count > $limit ) {
243 // We've reached the one extra which shows that there are
244 // additional pages to be had. Stop here...
245 $this->setContinueEnumParameter( 'from', $row->user_name
);
249 // Record new user's data
250 $lastUser = $row->user_name
;
251 $lastUserData = array(
252 'userid' => $row->user_id
,
255 if ( $fld_blockinfo && !is_null( $row->ipb_by_text
) ) {
256 $lastUserData['blockid'] = $row->ipb_id
;
257 $lastUserData['blockedby'] = $row->ipb_by_text
;
258 $lastUserData['blockedbyid'] = $row->ipb_by
;
259 $lastUserData['blockreason'] = $row->ipb_reason
;
260 $lastUserData['blockexpiry'] = $row->ipb_expiry
;
262 if ( $row->ipb_deleted
) {
263 $lastUserData['hidden'] = '';
265 if ( $fld_editcount ) {
266 $lastUserData['editcount'] = intval( $row->user_editcount
);
268 if ( $params['activeusers'] ) {
269 $lastUserData['recentactions'] = intval( $row->recentactions
);
270 // @todo 'recenteditcount' is set for BC, remove in 1.25
271 $lastUserData['recenteditcount'] = $lastUserData['recentactions'];
273 if ( $fld_registration ) {
274 $lastUserData['registration'] = $row->user_registration ?
275 wfTimestamp( TS_ISO_8601
, $row->user_registration
) : '';
279 if ( $sqlLimit == $count ) {
280 // @todo BUG! database contains group name that User::getAllGroups() does not return
281 // Should handle this more gracefully
284 'MediaWiki configuration error: The database contains more ' .
285 'user groups than known to User::getAllGroups() function'
289 $lastUserObj = User
::newFromId( $row->user_id
);
291 // Add user's group info
293 if ( !isset( $lastUserData['groups'] ) ) {
294 if ( $lastUserObj ) {
295 $lastUserData['groups'] = $lastUserObj->getAutomaticGroups();
297 // This should not normally happen
298 $lastUserData['groups'] = array();
302 if ( !is_null( $row->ug_group2
) ) {
303 $lastUserData['groups'][] = $row->ug_group2
;
306 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
309 if ( $fld_implicitgroups && !isset( $lastUserData['implicitgroups'] ) && $lastUserObj ) {
310 $lastUserData['implicitgroups'] = $lastUserObj->getAutomaticGroups();
311 $result->setIndexedTagName( $lastUserData['implicitgroups'], 'g' );
314 if ( !isset( $lastUserData['rights'] ) ) {
315 if ( $lastUserObj ) {
316 $lastUserData['rights'] = User
::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
318 // This should not normally happen
319 $lastUserData['rights'] = array();
323 if ( !is_null( $row->ug_group2
) ) {
324 $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
325 User
::getGroupPermissions( array( $row->ug_group2
) ) ) );
328 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
332 if ( is_array( $lastUserData ) &&
333 !( $params['activeusers'] && $lastUserData['recentactions'] === 0 )
335 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
336 null, $lastUserData );
338 $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
342 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
345 public function getCacheMode( $params ) {
346 return 'anon-public-user-private';
349 public function getAllowedParams() {
350 $userGroups = User
::getAllGroups();
357 ApiBase
::PARAM_DFLT
=> 'ascending',
358 ApiBase
::PARAM_TYPE
=> array(
364 ApiBase
::PARAM_TYPE
=> $userGroups,
365 ApiBase
::PARAM_ISMULTI
=> true,
367 'excludegroup' => array(
368 ApiBase
::PARAM_TYPE
=> $userGroups,
369 ApiBase
::PARAM_ISMULTI
=> true,
372 ApiBase
::PARAM_TYPE
=> User
::getAllRights(),
373 ApiBase
::PARAM_ISMULTI
=> true,
376 ApiBase
::PARAM_ISMULTI
=> true,
377 ApiBase
::PARAM_TYPE
=> array(
387 ApiBase
::PARAM_DFLT
=> 10,
388 ApiBase
::PARAM_TYPE
=> 'limit',
389 ApiBase
::PARAM_MIN
=> 1,
390 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
391 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
393 'witheditsonly' => false,
394 'activeusers' => false,
398 public function getParamDescription() {
400 'from' => 'The user name to start enumerating from',
401 'to' => 'The user name to stop enumerating at',
402 'prefix' => 'Search for all users that begin with this value',
403 'dir' => 'Direction to sort in',
404 'group' => 'Limit users to given group name(s)',
405 'excludegroup' => 'Exclude users in given group name(s)',
406 'rights' => 'Limit users to given right(s) (does not include rights ' .
407 'granted by implicit or auto-promoted groups like *, user, or autoconfirmed)',
409 'What pieces of information to include.',
410 ' blockinfo - Adds the information about a current block on the user',
411 ' groups - Lists groups that the user is in. This uses ' .
412 'more server resources and may return fewer results than the limit',
413 ' implicitgroups - Lists all the groups the user is automatically in',
414 ' rights - Lists rights that the user has',
415 ' editcount - Adds the edit count of the user',
416 ' registration - Adds the timestamp of when the user registered if available (may be blank)',
418 'limit' => 'How many total user names to return',
419 'witheditsonly' => 'Only list users who have made edits',
420 'activeusers' => "Only list users active in the last {$this->getConfig()->get( 'ActiveUserDays' )} days(s)"
424 public function getDescription() {
425 return 'Enumerate all registered users.';
428 public function getExamples() {
430 'api.php?action=query&list=allusers&aufrom=Y',
434 public function getHelpUrls() {
435 return 'https://www.mediawiki.org/wiki/API:Allusers';