Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.62% covered (warning)
76.62%
213 / 278
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryAllUsers
76.90% covered (warning)
76.90%
213 / 277
42.86% covered (danger)
42.86%
3 / 7
104.40
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCanonicalUserName
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
71.29% covered (warning)
71.29%
144 / 202
0.00% covered (danger)
0.00%
0 / 1
119.49
 getCacheMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllowedParams
98.48% covered (success)
98.48%
65 / 66
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace MediaWiki\Api;
10
11use MediaWiki\Language\Language;
12use MediaWiki\MainConfigNames;
13use MediaWiki\Permissions\GroupPermissionsLookup;
14use MediaWiki\RecentChanges\RecentChangeLookup;
15use MediaWiki\User\TempUser\TempUserConfig;
16use MediaWiki\User\TempUser\TempUserDetailsLookup;
17use MediaWiki\User\UserFactory;
18use MediaWiki\User\UserGroupManager;
19use MediaWiki\User\UserIdentityValue;
20use Wikimedia\ParamValidator\ParamValidator;
21use Wikimedia\ParamValidator\TypeDef\IntegerDef;
22use Wikimedia\Rdbms\IExpression;
23use Wikimedia\Rdbms\LikeValue;
24use Wikimedia\Timestamp\ConvertibleTimestamp;
25use Wikimedia\Timestamp\TimestampFormat as TS;
26
27/**
28 * Query module to enumerate all registered users.
29 *
30 * @ingroup API
31 */
32class ApiQueryAllUsers extends ApiQueryBase {
33    use ApiQueryBlockInfoTrait;
34
35    public function __construct(
36        ApiQuery $query,
37        string $moduleName,
38        private readonly UserFactory $userFactory,
39        private readonly UserGroupManager $userGroupManager,
40        private readonly GroupPermissionsLookup $groupPermissionsLookup,
41        private readonly Language $contentLanguage,
42        private readonly TempUserConfig $tempUserConfig,
43        private readonly RecentChangeLookup $recentChangeLookup,
44        private readonly TempUserDetailsLookup $tempUserDetailsLookup,
45    ) {
46        parent::__construct( $query, $moduleName, 'au' );
47    }
48
49    /**
50     * This function converts the user name to a canonical form
51     * which is stored in the database.
52     * @param string $name
53     * @return string
54     */
55    private function getCanonicalUserName( $name ) {
56        // T416297 Ignore leading whitespaces when looking up a username
57        $name = $this->contentLanguage->ucfirst( ltrim( $name ) );
58        return strtr( $name, '_', ' ' );
59    }
60
61    public function execute() {
62        $params = $this->extractRequestParams();
63        $activeUserDays = $this->getConfig()->get( MainConfigNames::ActiveUserDays );
64
65        $db = $this->getDB();
66
67        $prop = $params['prop'];
68        if ( $prop !== null ) {
69            $prop = array_fill_keys( $prop, true );
70            $fld_blockinfo = isset( $prop['blockinfo'] );
71            $fld_editcount = isset( $prop['editcount'] );
72            $fld_groups = isset( $prop['groups'] );
73            $fld_rights = isset( $prop['rights'] );
74            $fld_registration = isset( $prop['registration'] );
75            $fld_implicitgroups = isset( $prop['implicitgroups'] );
76            $fld_centralids = isset( $prop['centralids'] );
77            $fld_tempexpired = isset( $prop['tempexpired'] );
78        } else {
79            $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration =
80                $fld_rights = $fld_implicitgroups = $fld_centralids = $fld_tempexpired = false;
81        }
82
83        $limit = $params['limit'];
84
85        $this->addTables( 'user' );
86
87        $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
88        $from = $params['from'] === null ? null : $this->getCanonicalUserName( $params['from'] );
89        $to = $params['to'] === null ? null : $this->getCanonicalUserName( $params['to'] );
90
91        # MySQL can't figure out that 'user_name' and 'qcc_title' are the same
92        # despite the JOIN condition, so manually sort on the correct one.
93        $userFieldToSort = $params['activeusers'] ? 'qcc_title' : 'user_name';
94
95        # Some of these subtable joins are going to give us duplicate rows, so
96        # calculate the maximum number of duplicates we might see.
97        $maxDuplicateRows = 1;
98
99        $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
100
101        if ( $params['prefix'] !== null ) {
102            $this->addWhere(
103                $db->expr(
104                    $userFieldToSort,
105                    IExpression::LIKE,
106                    new LikeValue( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() )
107                )
108            );
109        }
110
111        $excludeNamed = $params['excludenamed'];
112        $excludeTemp = $params['excludetemp'];
113
114        if ( $this->tempUserConfig->isKnown() ) {
115            if ( $excludeTemp ) {
116                $this->addWhere(
117                    $this->tempUserConfig->getMatchCondition( $db, 'user_name', IExpression::NOT_LIKE )
118                );
119            }
120            if ( $excludeNamed ) {
121                $this->addWhere(
122                    $this->tempUserConfig->getMatchCondition( $db, 'user_name', IExpression::LIKE )
123                );
124            }
125        }
126
127        if ( $params['rights'] !== null && count( $params['rights'] ) ) {
128            $groups = [];
129            // TODO: this does not properly account for $wgRevokePermissions
130            foreach ( $params['rights'] as $r ) {
131                if ( in_array( $r, $this->getPermissionManager()->getImplicitRights(), true ) ) {
132                    $groups[] = '*';
133                } else {
134                    $groups = array_merge(
135                        $groups,
136                        $this->groupPermissionsLookup->getGroupsWithPermission( $r )
137                    );
138                }
139            }
140
141            if ( $groups === [] ) {
142                // No group with the given right(s) exists, no need for a query
143                $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], '' );
144
145                return;
146            }
147
148            $groups = array_unique( $groups );
149            if ( in_array( '*', $groups, true ) || in_array( 'user', $groups, true ) ) {
150                // All user rows logically match but there are no "*"/"user" user_groups rows
151                $groups = [];
152            }
153
154            if ( $params['group'] === null ) {
155                $params['group'] = $groups;
156            } else {
157                $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
158            }
159        }
160
161        $this->requireMaxOneParameter( $params, 'group', 'excludegroup' );
162
163        if ( $params['group'] !== null && count( $params['group'] ) ) {
164            // Filter only users that belong to a given group. This might
165            // produce as many rows-per-user as there are groups being checked.
166            $this->addTables( 'user_groups', 'ug1' );
167            $this->addJoinConds( [
168                'ug1' => [
169                    'JOIN',
170                    [
171                        'ug1.ug_user=user_id',
172                        'ug1.ug_group' => $params['group'],
173                        $db->expr( 'ug1.ug_expiry', '=', null )->or( 'ug1.ug_expiry', '>=', $db->timestamp() ),
174                    ]
175                ]
176            ] );
177            $maxDuplicateRows *= count( $params['group'] );
178        }
179
180        if ( $params['excludegroup'] !== null && count( $params['excludegroup'] ) ) {
181            // Filter only users don't belong to a given group. This can only
182            // produce one row-per-user, because we only keep on "no match".
183            $this->addTables( 'user_groups', 'ug1' );
184
185            $this->addJoinConds( [ 'ug1' => [ 'LEFT JOIN',
186                [
187                    'ug1.ug_user=user_id',
188                    $db->expr( 'ug1.ug_expiry', '=', null )->or( 'ug1.ug_expiry', '>=', $db->timestamp() ),
189                    'ug1.ug_group' => $params['excludegroup'],
190                ]
191            ] ] );
192            $this->addWhere( [ 'ug1.ug_user' => null ] );
193        }
194
195        if ( $params['witheditsonly'] ) {
196            $this->addWhere( $db->expr( 'user_editcount', '>', 0 ) );
197        }
198
199        $this->addDeletedUserFilter();
200
201        if ( $fld_groups || $fld_rights ) {
202            $this->addFields( [ 'groups' =>
203                $db->newSelectQueryBuilder()
204                    ->table( 'user_groups' )
205                    ->field( 'ug_group' )
206                    ->where( [
207                        'ug_user=user_id',
208                        $db->expr( 'ug_expiry', '=', null )->or( 'ug_expiry', '>=', $db->timestamp() )
209                    ] )
210                    ->buildGroupConcatField( '|' )
211            ] );
212        }
213
214        if ( $params['activeusers'] ) {
215            $activeUserSeconds = $activeUserDays * 86400;
216
217            // Filter query to only include users in the active users cache.
218            // There shouldn't be any duplicate rows in querycachetwo here.
219            $this->addTables( 'querycachetwo' );
220            $this->addJoinConds( [ 'querycachetwo' => [
221                'JOIN', [
222                    'qcc_type' => 'activeusers',
223                    'qcc_namespace' => NS_USER,
224                    'qcc_title=user_name',
225                ],
226            ] ] );
227
228            // Actually count the actions using a subquery (T66505 and T66507)
229            $timestamp = $db->timestamp( (int)ConvertibleTimestamp::now( TS::UNIX ) - $activeUserSeconds );
230            $subqueryBuilder = $db->newSelectQueryBuilder()
231                ->select( 'COUNT(*)' )
232                ->from( 'recentchanges' )
233                ->join( 'actor', null, 'rc_actor = actor_id' )
234                ->where( [
235                    'actor_user = user_id',
236                    $db->expr( 'rc_source', '=', $this->recentChangeLookup->getPrimarySources() ),
237                    $db->expr( 'rc_log_type', '=', null )
238                        ->or( 'rc_log_type', '!=', 'newusers' ),
239                    $db->expr( 'rc_timestamp', '>=', $timestamp ),
240                ] );
241            $this->addFields( [
242                'recentactions' => '(' . $subqueryBuilder->caller( __METHOD__ )->getSQL() . ')'
243            ] );
244        }
245
246        $sqlLimit = $limit + $maxDuplicateRows;
247        $this->addOption( 'LIMIT', $sqlLimit );
248
249        $this->addFields( [
250            'user_name',
251            'user_id'
252        ] );
253        $this->addFieldsIf( 'user_editcount', $fld_editcount );
254        $this->addFieldsIf( 'user_registration', $fld_registration );
255
256        $res = $this->select( __METHOD__ );
257        $count = 0;
258        $countDuplicates = 0;
259        $lastUser = false;
260        $result = $this->getResult();
261        $blockInfos = $fld_blockinfo ? $this->getBlockDetailsForRows( $res ) : null;
262        foreach ( $res as $row ) {
263            $count++;
264
265            if ( $lastUser === $row->user_name ) {
266                // Duplicate row due to one of the needed subtable joins.
267                // Ignore it, but count the number of them to sensibly handle
268                // miscalculation of $maxDuplicateRows.
269                $countDuplicates++;
270                if ( $countDuplicates == $maxDuplicateRows ) {
271                    ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
272                }
273                continue;
274            }
275
276            $countDuplicates = 0;
277            $lastUser = $row->user_name;
278
279            if ( $count > $limit ) {
280                // We've reached the one extra which shows that there are
281                // additional pages to be had. Stop here...
282                $this->setContinueEnumParameter( 'from', $row->user_name );
283                break;
284            }
285
286            if ( $count == $sqlLimit ) {
287                // Should never hit this (either the $countDuplicates check or
288                // the $count > $limit check should hit first), but check it
289                // anyway just in case.
290                ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
291            }
292
293            if ( $params['activeusers'] && (int)$row->recentactions === 0 ) {
294                // activeusers cache was out of date
295                continue;
296            }
297
298            $data = [
299                'userid' => (int)$row->user_id,
300                'name' => $row->user_name,
301            ];
302
303            if ( $fld_centralids ) {
304                $data += ApiQueryUserInfo::getCentralUserInfo(
305                    $this->getConfig(), $this->userFactory->newFromId( (int)$row->user_id ), $params['attachedwiki']
306                );
307            }
308
309            if ( $fld_blockinfo && isset( $blockInfos[$row->user_id] ) ) {
310                $data += $blockInfos[$row->user_id];
311            }
312            if ( $row->hu_deleted ) {
313                $data['hidden'] = true;
314            }
315            if ( $fld_editcount ) {
316                $data['editcount'] = (int)$row->user_editcount;
317            }
318            if ( $params['activeusers'] ) {
319                $data['recentactions'] = (int)$row->recentactions;
320            }
321            if ( $fld_registration ) {
322                $data['registration'] = $row->user_registration ?
323                    wfTimestamp( TS::ISO_8601, $row->user_registration ) : '';
324            }
325
326            if ( $fld_implicitgroups || $fld_groups || $fld_rights ) {
327                $implicitGroups = $this->userGroupManager
328                    ->getUserImplicitGroups( $this->userFactory->newFromId( (int)$row->user_id ) );
329                if ( isset( $row->groups ) && $row->groups !== '' ) {
330                    $groups = array_merge( $implicitGroups, explode( '|', $row->groups ) );
331                } else {
332                    $groups = $implicitGroups;
333                }
334
335                if ( $fld_groups ) {
336                    $data['groups'] = $groups;
337                    ApiResult::setIndexedTagName( $data['groups'], 'g' );
338                    ApiResult::setArrayType( $data['groups'], 'array' );
339                }
340
341                if ( $fld_implicitgroups ) {
342                    $data['implicitgroups'] = $implicitGroups;
343                    ApiResult::setIndexedTagName( $data['implicitgroups'], 'g' );
344                    ApiResult::setArrayType( $data['implicitgroups'], 'array' );
345                }
346
347                if ( $fld_rights ) {
348                    $user = $this->userFactory->newFromId( (int)$row->user_id );
349                    $data['rights'] = $this->getPermissionManager()->getUserPermissions( $user );
350                    ApiResult::setIndexedTagName( $data['rights'], 'r' );
351                    ApiResult::setArrayType( $data['rights'], 'array' );
352                }
353            }
354
355            if ( $fld_tempexpired ) {
356                if ( $this->tempUserConfig->isTempName( $row->user_name ) ) {
357                    $userIdentity = UserIdentityValue::newRegistered( $row->user_id, $row->user_name );
358                    $data['tempexpired'] = $this->tempUserDetailsLookup->isExpired( $userIdentity );
359                } else {
360                    $data['tempexpired'] = null;
361                }
362            }
363
364            $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $data );
365            if ( !$fit ) {
366                $this->setContinueEnumParameter( 'from', $data['name'] );
367                break;
368            }
369        }
370
371        $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'u' );
372    }
373
374    /** @inheritDoc */
375    public function getCacheMode( $params ) {
376        return 'anon-public-user-private';
377    }
378
379    /** @inheritDoc */
380    public function getAllowedParams( $flags = 0 ) {
381        $userGroups = $this->userGroupManager->listAllGroups();
382
383        if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) {
384            sort( $userGroups );
385        }
386
387        return [
388            'from' => null,
389            'to' => null,
390            'prefix' => null,
391            'dir' => [
392                ParamValidator::PARAM_DEFAULT => 'ascending',
393                ParamValidator::PARAM_TYPE => [
394                    'ascending',
395                    'descending'
396                ],
397            ],
398            'group' => [
399                ParamValidator::PARAM_TYPE => $userGroups,
400                ParamValidator::PARAM_ISMULTI => true,
401            ],
402            'excludegroup' => [
403                ParamValidator::PARAM_TYPE => $userGroups,
404                ParamValidator::PARAM_ISMULTI => true,
405            ],
406            'rights' => [
407                ParamValidator::PARAM_TYPE => array_unique( array_merge(
408                    $this->getPermissionManager()->getAllPermissions(),
409                    $this->getPermissionManager()->getImplicitRights()
410                ) ),
411                ParamValidator::PARAM_ISMULTI => true,
412            ],
413            'prop' => [
414                ParamValidator::PARAM_ISMULTI => true,
415                ParamValidator::PARAM_TYPE => [
416                    'blockinfo',
417                    'groups',
418                    'implicitgroups',
419                    'rights',
420                    'editcount',
421                    'registration',
422                    'centralids',
423                    'tempexpired',
424                ],
425                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
426            ],
427            'limit' => [
428                ParamValidator::PARAM_DEFAULT => 10,
429                ParamValidator::PARAM_TYPE => 'limit',
430                IntegerDef::PARAM_MIN => 1,
431                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
432                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
433            ],
434            'witheditsonly' => false,
435            'activeusers' => [
436                ParamValidator::PARAM_DEFAULT => false,
437                ApiBase::PARAM_HELP_MSG => [
438                    'apihelp-query+allusers-param-activeusers',
439                    $this->getConfig()->get( MainConfigNames::ActiveUserDays )
440                ],
441            ],
442            'attachedwiki' => null,
443            'excludenamed' => [
444                ParamValidator::PARAM_TYPE => 'boolean',
445            ],
446            'excludetemp' => [
447                ParamValidator::PARAM_TYPE => 'boolean',
448            ],
449        ];
450    }
451
452    /** @inheritDoc */
453    protected function getExamplesMessages() {
454        return [
455            'action=query&list=allusers&aufrom=Y'
456                => 'apihelp-query+allusers-example-y',
457        ];
458    }
459
460    /** @inheritDoc */
461    public function getHelpUrls() {
462        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allusers';
463    }
464}
465
466/** @deprecated class alias since 1.43 */
467class_alias( ApiQueryAllUsers::class, 'ApiQueryAllUsers' );