24use Wikimedia\Timestamp\ConvertibleTimestamp;
25use Wikimedia\Timestamp\TimestampFormat as TS;
41 private readonly
Language $contentLanguage,
46 parent::__construct( $query, $moduleName,
'au' );
55 private function getCanonicalUserName( $name ) {
56 $name = $this->contentLanguage->ucfirst( $name );
57 return strtr( $name,
'_',
' ' );
66 $prop = $params[
'prop'];
67 if ( $prop !==
null ) {
68 $prop = array_fill_keys( $prop,
true );
69 $fld_blockinfo = isset( $prop[
'blockinfo'] );
70 $fld_editcount = isset( $prop[
'editcount'] );
71 $fld_groups = isset( $prop[
'groups'] );
72 $fld_rights = isset( $prop[
'rights'] );
73 $fld_registration = isset( $prop[
'registration'] );
74 $fld_implicitgroups = isset( $prop[
'implicitgroups'] );
75 $fld_centralids = isset( $prop[
'centralids'] );
76 $fld_tempexpired = isset( $prop[
'tempexpired'] );
78 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration =
79 $fld_rights = $fld_implicitgroups = $fld_centralids = $fld_tempexpired =
false;
82 $limit = $params[
'limit'];
86 $dir = ( $params[
'dir'] ==
'descending' ?
'older' :
'newer' );
87 $from = $params[
'from'] ===
null ? null : $this->getCanonicalUserName( $params[
'from'] );
88 $to = $params[
'to'] ===
null ? null : $this->getCanonicalUserName( $params[
'to'] );
90 # MySQL can't figure out that 'user_name' and 'qcc_title' are the same
91 # despite the JOIN condition, so manually sort on the correct one.
92 $userFieldToSort = $params[
'activeusers'] ?
'qcc_title' :
'user_name';
94 # Some of these subtable joins are going to give us duplicate rows, so
95 # calculate the maximum number of duplicates we might see.
96 $maxDuplicateRows = 1;
100 if ( $params[
'prefix'] !==
null ) {
105 new LikeValue( $this->getCanonicalUserName( $params[
'prefix'] ), $db->anyString() )
110 $excludeNamed = $params[
'excludenamed'];
111 $excludeTemp = $params[
'excludetemp'];
113 if ( $this->tempUserConfig->isKnown() ) {
114 if ( $excludeTemp ) {
116 $this->tempUserConfig->getMatchCondition( $db,
'user_name', IExpression::NOT_LIKE )
119 if ( $excludeNamed ) {
121 $this->tempUserConfig->getMatchCondition( $db,
'user_name', IExpression::LIKE )
126 if ( $params[
'rights'] !==
null && count( $params[
'rights'] ) ) {
129 foreach ( $params[
'rights'] as $r ) {
133 $groups = array_merge(
135 $this->groupPermissionsLookup->getGroupsWithPermission( $r )
140 if ( $groups === [] ) {
147 $groups = array_unique( $groups );
148 if ( in_array(
'*', $groups,
true ) || in_array(
'user', $groups,
true ) ) {
153 if ( $params[
'group'] ===
null ) {
154 $params[
'group'] = $groups;
156 $params[
'group'] = array_unique( array_merge( $params[
'group'], $groups ) );
162 if ( $params[
'group'] !==
null && count( $params[
'group'] ) ) {
165 $this->
addTables(
'user_groups',
'ug1' );
170 'ug1.ug_user=user_id',
171 'ug1.ug_group' => $params[
'group'],
172 $db->expr(
'ug1.ug_expiry',
'=',
null )->or(
'ug1.ug_expiry',
'>=', $db->timestamp() ),
176 $maxDuplicateRows *= count( $params[
'group'] );
179 if ( $params[
'excludegroup'] !==
null && count( $params[
'excludegroup'] ) ) {
182 $this->
addTables(
'user_groups',
'ug1' );
186 'ug1.ug_user=user_id',
187 $db->expr(
'ug1.ug_expiry',
'=',
null )->or(
'ug1.ug_expiry',
'>=', $db->timestamp() ),
188 'ug1.ug_group' => $params[
'excludegroup'],
191 $this->
addWhere( [
'ug1.ug_user' =>
null ] );
194 if ( $params[
'witheditsonly'] ) {
195 $this->
addWhere( $db->expr(
'user_editcount',
'>', 0 ) );
198 $this->addDeletedUserFilter();
200 if ( $fld_groups || $fld_rights ) {
202 $db->newSelectQueryBuilder()
203 ->table(
'user_groups' )
204 ->field(
'ug_group' )
207 $db->expr(
'ug_expiry',
'=',
null )->or(
'ug_expiry',
'>=', $db->timestamp() )
209 ->buildGroupConcatField(
'|' )
213 if ( $params[
'activeusers'] ) {
214 $activeUserSeconds = $activeUserDays * 86400;
221 'qcc_type' =>
'activeusers',
223 'qcc_title=user_name',
228 $timestamp = $db->timestamp( (
int)ConvertibleTimestamp::now( TS::UNIX ) - $activeUserSeconds );
229 $subqueryBuilder = $db->newSelectQueryBuilder()
230 ->select(
'COUNT(*)' )
231 ->from(
'recentchanges' )
232 ->join(
'actor',
null,
'rc_actor = actor_id' )
234 'actor_user = user_id',
235 $db->expr(
'rc_source',
'=', $this->recentChangeLookup->getPrimarySources() ),
236 $db->expr(
'rc_log_type',
'=',
null )
237 ->or(
'rc_log_type',
'!=',
'newusers' ),
238 $db->expr(
'rc_timestamp',
'>=', $timestamp ),
241 'recentactions' =>
'(' . $subqueryBuilder->caller( __METHOD__ )->getSQL() .
')'
245 $sqlLimit = $limit + $maxDuplicateRows;
252 $this->
addFieldsIf(
'user_editcount', $fld_editcount );
253 $this->
addFieldsIf(
'user_registration', $fld_registration );
255 $res = $this->
select( __METHOD__ );
257 $countDuplicates = 0;
260 $blockInfos = $fld_blockinfo ? $this->getBlockDetailsForRows( $res ) :
null;
261 foreach ( $res as $row ) {
264 if ( $lastUser === $row->user_name ) {
269 if ( $countDuplicates == $maxDuplicateRows ) {
275 $countDuplicates = 0;
276 $lastUser = $row->user_name;
278 if ( $count > $limit ) {
285 if ( $count == $sqlLimit ) {
292 if ( $params[
'activeusers'] && (
int)$row->recentactions === 0 ) {
298 'userid' => (int)$row->user_id,
299 'name' => $row->user_name,
302 if ( $fld_centralids ) {
304 $this->
getConfig(), $this->userFactory->newFromId( (
int)$row->user_id ), $params[
'attachedwiki']
308 if ( $fld_blockinfo && isset( $blockInfos[$row->user_id] ) ) {
309 $data += $blockInfos[$row->user_id];
311 if ( $row->hu_deleted ) {
312 $data[
'hidden'] =
true;
314 if ( $fld_editcount ) {
315 $data[
'editcount'] = (int)$row->user_editcount;
317 if ( $params[
'activeusers'] ) {
318 $data[
'recentactions'] = (int)$row->recentactions;
320 if ( $fld_registration ) {
321 $data[
'registration'] = $row->user_registration ?
322 wfTimestamp( TS::ISO_8601, $row->user_registration ) :
'';
325 if ( $fld_implicitgroups || $fld_groups || $fld_rights ) {
326 $implicitGroups = $this->userGroupManager
327 ->getUserImplicitGroups( $this->userFactory->newFromId( (
int)$row->user_id ) );
328 if ( isset( $row->groups ) && $row->groups !==
'' ) {
329 $groups = array_merge( $implicitGroups, explode(
'|', $row->groups ) );
331 $groups = $implicitGroups;
335 $data[
'groups'] = $groups;
340 if ( $fld_implicitgroups ) {
341 $data[
'implicitgroups'] = $implicitGroups;
347 $user = $this->userFactory->newFromId( (
int)$row->user_id );
354 if ( $fld_tempexpired ) {
355 if ( $this->tempUserConfig->isTempName( $row->user_name ) ) {
356 $userIdentity = UserIdentityValue::newRegistered( $row->user_id, $row->user_name );
357 $data[
'tempexpired'] = $this->tempUserDetailsLookup->isExpired( $userIdentity );
359 $data[
'tempexpired'] =
null;
363 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $data );
370 $result->addIndexedTagName( [
'query', $this->
getModuleName() ],
'u' );
375 return 'anon-public-user-private';
380 $userGroups = $this->userGroupManager->listAllGroups();
391 ParamValidator::PARAM_DEFAULT =>
'ascending',
392 ParamValidator::PARAM_TYPE => [
398 ParamValidator::PARAM_TYPE => $userGroups,
399 ParamValidator::PARAM_ISMULTI =>
true,
402 ParamValidator::PARAM_TYPE => $userGroups,
403 ParamValidator::PARAM_ISMULTI =>
true,
406 ParamValidator::PARAM_TYPE => array_unique( array_merge(
410 ParamValidator::PARAM_ISMULTI =>
true,
413 ParamValidator::PARAM_ISMULTI =>
true,
414 ParamValidator::PARAM_TYPE => [
427 ParamValidator::PARAM_DEFAULT => 10,
428 ParamValidator::PARAM_TYPE =>
'limit',
429 IntegerDef::PARAM_MIN => 1,
433 'witheditsonly' =>
false,
435 ParamValidator::PARAM_DEFAULT =>
false,
437 'apihelp-query+allusers-param-activeusers',
441 'attachedwiki' =>
null,
443 ParamValidator::PARAM_TYPE =>
'boolean',
446 ParamValidator::PARAM_TYPE =>
'boolean',
454 'action=query&list=allusers&aufrom=Y'
455 =>
'apihelp-query+allusers-example-y',
461 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allusers';
466class_alias( ApiQueryAllUsers::class,
'ApiQueryAllUsers' );
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
A class containing constants representing the names of configuration variables.
const ActiveUserDays
Name constant for the ActiveUserDays setting, for use with Config::get()
trait ApiQueryBlockInfoTrait