3 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Query module to enumerate all registered users.
28 class ApiQueryAllUsers
extends ApiQueryBase
{
29 public function __construct( ApiQuery
$query, $moduleName ) {
30 parent
::__construct( $query, $moduleName, 'au' );
34 * This function converts the user name to a canonical form
35 * which is stored in the database.
39 private function getCanonicalUserName( $name ) {
40 return strtr( $name, '_', ' ' );
43 public function execute() {
44 global $wgActorTableSchemaMigrationStage;
46 $params = $this->extractRequestParams();
47 $activeUserDays = $this->getConfig()->get( 'ActiveUserDays' );
50 $commentStore = CommentStore
::getStore();
52 $prop = $params['prop'];
53 if ( !is_null( $prop ) ) {
54 $prop = array_flip( $prop );
55 $fld_blockinfo = isset( $prop['blockinfo'] );
56 $fld_editcount = isset( $prop['editcount'] );
57 $fld_groups = isset( $prop['groups'] );
58 $fld_rights = isset( $prop['rights'] );
59 $fld_registration = isset( $prop['registration'] );
60 $fld_implicitgroups = isset( $prop['implicitgroups'] );
61 $fld_centralids = isset( $prop['centralids'] );
63 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration =
64 $fld_rights = $fld_implicitgroups = $fld_centralids = false;
67 $limit = $params['limit'];
69 $this->addTables( 'user' );
71 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
72 $from = is_null( $params['from'] ) ?
null : $this->getCanonicalUserName( $params['from'] );
73 $to = is_null( $params['to'] ) ?
null : $this->getCanonicalUserName( $params['to'] );
75 # MySQL can't figure out that 'user_name' and 'qcc_title' are the same
76 # despite the JOIN condition, so manually sort on the correct one.
77 $userFieldToSort = $params['activeusers'] ?
'qcc_title' : 'user_name';
79 # Some of these subtable joins are going to give us duplicate rows, so
80 # calculate the maximum number of duplicates we might see.
81 $maxDuplicateRows = 1;
83 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
85 if ( !is_null( $params['prefix'] ) ) {
86 $this->addWhere( $userFieldToSort .
87 $db->buildLike( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() ) );
90 if ( !is_null( $params['rights'] ) && count( $params['rights'] ) ) {
92 foreach ( $params['rights'] as $r ) {
93 $groups = array_merge( $groups, User
::getGroupsWithPermission( $r ) );
96 // no group with the given right(s) exists, no need for a query
97 if ( !count( $groups ) ) {
98 $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], '' );
103 $groups = array_unique( $groups );
105 if ( is_null( $params['group'] ) ) {
106 $params['group'] = $groups;
108 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
112 $this->requireMaxOneParameter( $params, 'group', 'excludegroup' );
114 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
115 // Filter only users that belong to a given group. This might
116 // produce as many rows-per-user as there are groups being checked.
117 $this->addTables( 'user_groups', 'ug1' );
118 $this->addJoinConds( [
122 'ug1.ug_user=user_id',
123 'ug1.ug_group' => $params['group'],
124 'ug1.ug_expiry IS NULL OR ug1.ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
128 $maxDuplicateRows *= count( $params['group'] );
131 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
132 // Filter only users don't belong to a given group. This can only
133 // produce one row-per-user, because we only keep on "no match".
134 $this->addTables( 'user_groups', 'ug1' );
136 if ( count( $params['excludegroup'] ) == 1 ) {
137 $exclude = [ 'ug1.ug_group' => $params['excludegroup'][0] ];
139 $exclude = [ $db->makeList(
140 [ 'ug1.ug_group' => $params['excludegroup'] ],
144 $this->addJoinConds( [ 'ug1' => [ 'LEFT OUTER JOIN',
146 'ug1.ug_user=user_id',
147 'ug1.ug_expiry IS NULL OR ug1.ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
150 $this->addWhere( 'ug1.ug_user IS NULL' );
153 if ( $params['witheditsonly'] ) {
154 $this->addWhere( 'user_editcount > 0' );
157 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
159 if ( $fld_groups ||
$fld_rights ) {
160 $this->addFields( [ 'groups' =>
161 $db->buildGroupConcatField( '|', 'user_groups', 'ug_group', [
163 'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
168 if ( $params['activeusers'] ) {
169 $activeUserSeconds = $activeUserDays * 86400;
171 // Filter query to only include users in the active users cache.
172 // There shouldn't be any duplicate rows in querycachetwo here.
173 $this->addTables( 'querycachetwo' );
174 $this->addJoinConds( [ 'querycachetwo' => [
176 'qcc_type' => 'activeusers',
177 'qcc_namespace' => NS_USER
,
178 'qcc_title=user_name',
182 // Actually count the actions using a subquery (T66505 and T66507)
183 $tables = [ 'recentchanges' ];
185 if ( $wgActorTableSchemaMigrationStage === MIGRATION_OLD
) {
186 $userCond = 'rc_user_text = user_name';
190 $wgActorTableSchemaMigrationStage === MIGRATION_NEW ?
'JOIN' : 'LEFT JOIN',
191 'rc_actor = actor_id'
193 if ( $wgActorTableSchemaMigrationStage === MIGRATION_NEW
) {
194 $userCond = 'actor_user = user_id';
196 $userCond = 'actor_user = user_id OR (rc_actor = 0 AND rc_user_text = user_name)';
199 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX
) - $activeUserSeconds );
201 'recentactions' => '(' . $db->selectSQLText(
206 'rc_type != ' . $db->addQuotes( RC_EXTERNAL
), // no wikidata
207 'rc_log_type IS NULL OR rc_log_type != ' . $db->addQuotes( 'newusers' ),
208 'rc_timestamp >= ' . $db->addQuotes( $timestamp ),
217 $sqlLimit = $limit +
$maxDuplicateRows;
218 $this->addOption( 'LIMIT', $sqlLimit );
224 $this->addFieldsIf( 'user_editcount', $fld_editcount );
225 $this->addFieldsIf( 'user_registration', $fld_registration );
227 $res = $this->select( __METHOD__
);
229 $countDuplicates = 0;
231 $result = $this->getResult();
232 foreach ( $res as $row ) {
235 if ( $lastUser === $row->user_name
) {
236 // Duplicate row due to one of the needed subtable joins.
237 // Ignore it, but count the number of them to sanely handle
238 // miscalculation of $maxDuplicateRows.
240 if ( $countDuplicates == $maxDuplicateRows ) {
241 ApiBase
::dieDebug( __METHOD__
, 'Saw more duplicate rows than expected' );
246 $countDuplicates = 0;
247 $lastUser = $row->user_name
;
249 if ( $count > $limit ) {
250 // We've reached the one extra which shows that there are
251 // additional pages to be had. Stop here...
252 $this->setContinueEnumParameter( 'from', $row->user_name
);
256 if ( $count == $sqlLimit ) {
257 // Should never hit this (either the $countDuplicates check or
258 // the $count > $limit check should hit first), but check it
259 // anyway just in case.
260 ApiBase
::dieDebug( __METHOD__
, 'Saw more duplicate rows than expected' );
263 if ( $params['activeusers'] && $row->recentactions
=== 0 ) {
264 // activeusers cache was out of date
269 'userid' => (int)$row->user_id
,
270 'name' => $row->user_name
,
273 if ( $fld_centralids ) {
274 $data +
= ApiQueryUserInfo
::getCentralUserInfo(
275 $this->getConfig(), User
::newFromId( $row->user_id
), $params['attachedwiki']
279 if ( $fld_blockinfo && !is_null( $row->ipb_by_text
) ) {
280 $data['blockid'] = (int)$row->ipb_id
;
281 $data['blockedby'] = $row->ipb_by_text
;
282 $data['blockedbyid'] = (int)$row->ipb_by
;
283 $data['blockedtimestamp'] = wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
);
284 $data['blockreason'] = $commentStore->getComment( 'ipb_reason', $row )->text
;
285 $data['blockexpiry'] = $row->ipb_expiry
;
287 if ( $row->ipb_deleted
) {
288 $data['hidden'] = true;
290 if ( $fld_editcount ) {
291 $data['editcount'] = intval( $row->user_editcount
);
293 if ( $params['activeusers'] ) {
294 $data['recentactions'] = intval( $row->recentactions
);
295 // @todo 'recenteditcount' is set for BC, remove in 1.25
296 $data['recenteditcount'] = $data['recentactions'];
298 if ( $fld_registration ) {
299 $data['registration'] = $row->user_registration ?
300 wfTimestamp( TS_ISO_8601
, $row->user_registration
) : '';
303 if ( $fld_implicitgroups ||
$fld_groups ||
$fld_rights ) {
304 $implicitGroups = User
::newFromId( $row->user_id
)->getAutomaticGroups();
305 if ( isset( $row->groups
) && $row->groups
!== '' ) {
306 $groups = array_merge( $implicitGroups, explode( '|', $row->groups
) );
308 $groups = $implicitGroups;
312 $data['groups'] = $groups;
313 ApiResult
::setIndexedTagName( $data['groups'], 'g' );
314 ApiResult
::setArrayType( $data['groups'], 'array' );
317 if ( $fld_implicitgroups ) {
318 $data['implicitgroups'] = $implicitGroups;
319 ApiResult
::setIndexedTagName( $data['implicitgroups'], 'g' );
320 ApiResult
::setArrayType( $data['implicitgroups'], 'array' );
324 $data['rights'] = User
::getGroupPermissions( $groups );
325 ApiResult
::setIndexedTagName( $data['rights'], 'r' );
326 ApiResult
::setArrayType( $data['rights'], 'array' );
330 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $data );
332 $this->setContinueEnumParameter( 'from', $data['name'] );
337 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'u' );
340 public function getCacheMode( $params ) {
341 return 'anon-public-user-private';
344 public function getAllowedParams() {
345 $userGroups = User
::getAllGroups();
352 ApiBase
::PARAM_DFLT
=> 'ascending',
353 ApiBase
::PARAM_TYPE
=> [
359 ApiBase
::PARAM_TYPE
=> $userGroups,
360 ApiBase
::PARAM_ISMULTI
=> true,
363 ApiBase
::PARAM_TYPE
=> $userGroups,
364 ApiBase
::PARAM_ISMULTI
=> true,
367 ApiBase
::PARAM_TYPE
=> User
::getAllRights(),
368 ApiBase
::PARAM_ISMULTI
=> true,
371 ApiBase
::PARAM_ISMULTI
=> true,
372 ApiBase
::PARAM_TYPE
=> [
381 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
384 ApiBase
::PARAM_DFLT
=> 10,
385 ApiBase
::PARAM_TYPE
=> 'limit',
386 ApiBase
::PARAM_MIN
=> 1,
387 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
388 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
390 'witheditsonly' => false,
392 ApiBase
::PARAM_DFLT
=> false,
393 ApiBase
::PARAM_HELP_MSG
=> [
394 'apihelp-query+allusers-param-activeusers',
395 $this->getConfig()->get( 'ActiveUserDays' )
398 'attachedwiki' => null,
402 protected function getExamplesMessages() {
404 'action=query&list=allusers&aufrom=Y'
405 => 'apihelp-query+allusers-example-Y',
409 public function getHelpUrls() {
410 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allusers';