Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
TempUserDetailsLookup
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
4 / 4
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isExpired
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 preloadExpirationStatus
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
 getExpirationState
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2declare( strict_types=1 );
3namespace MediaWiki\User\TempUser;
4
5use ArrayIterator;
6use CallbackFilterIterator;
7use IteratorIterator;
8use MediaWiki\User\Registration\UserRegistrationLookup;
9use MediaWiki\User\UserIdentity;
10use Wikimedia\ObjectCache\MapCacheLRU;
11use Wikimedia\Timestamp\ConvertibleTimestamp;
12use Wikimedia\Timestamp\TimestampFormat as TS;
13
14/**
15 * Caching lookup service for metadata related to temporary accounts, such as expiration.
16 * @since 1.44
17 */
18class TempUserDetailsLookup {
19    private readonly MapCacheLRU $expiryCache;
20
21    public function __construct(
22        private readonly TempUserConfig $tempUserConfig,
23        private readonly UserRegistrationLookup $userRegistrationLookup,
24    ) {
25        // Use a relatively large cache size to account for pages with a high number of user links,
26        // such as Special:RecentChanges or history pages.
27        $this->expiryCache = new MapCacheLRU( 1_000 );
28    }
29
30    /**
31     * Check if a temporary user account is expired.
32     * @param UserIdentity $user
33     * @return bool `true` if the account is expired, `false` otherwise.
34     */
35    public function isExpired( UserIdentity $user ): bool {
36        $userName = $user->getName();
37        if (
38            !$this->tempUserConfig->isTempName( $userName ) ||
39            !$user->isRegistered()
40        ) {
41            return false;
42        }
43
44        if ( !$this->expiryCache->has( $userName ) ) {
45            $registration = $this->userRegistrationLookup->getFirstRegistration( $user );
46
47            $this->expiryCache->set( $userName, $this->getExpirationState( $registration ) );
48        }
49
50        return $this->expiryCache->get( $userName );
51    }
52
53    /**
54     * Preload the expiration status of temporary accounts within a set of users.
55     *
56     * @param iterable<UserIdentity> $users The users to preload the expiration status for.
57     */
58    public function preloadExpirationStatus( iterable $users ): void {
59        // Save the user name associated with each user id for use later.
60        $usersById = [];
61        foreach ( $users as $user ) {
62            $usersById[$user->getId()] = $user->getName();
63        }
64
65        $users = is_array( $users ) ? new ArrayIterator( $users ) : new IteratorIterator( $users );
66        $timestampsById = $this->userRegistrationLookup->getFirstRegistrationBatch(
67            new CallbackFilterIterator(
68                $users,
69                fn ( UserIdentity $user ): bool =>
70                    $user->isRegistered() && $this->tempUserConfig->isTempName( $user->getName() )
71            )
72        );
73
74        foreach ( $timestampsById as $userId => $registrationTimestamp ) {
75            // Cache expiry by user name instead of user id. See T398722.
76            $this->expiryCache->set(
77                $usersById[$userId],
78                $this->getExpirationState( $registrationTimestamp )
79            );
80        }
81    }
82
83    /**
84     * Check whether a temporary account registered at the given timestamp is expired now.
85     * @param string|null|false $registration DB timestamp of the registration time.
86     * May be `null` or `false` if not known.
87     * @return bool `true` if the account is expired, `false` otherwise.
88     */
89    public function getExpirationState( $registration ): bool {
90        if ( !is_string( $registration ) ) {
91            return false;
92        }
93
94        $expireAfterDays = $this->tempUserConfig->getExpireAfterDays();
95        $expiresAt = (int)wfTimestamp( TS::UNIX, $registration ) + $expireAfterDays * 86400;
96        return $expiresAt < ConvertibleTimestamp::now( TS::UNIX );
97    }
98}