MediaWiki master
TempUserDetailsLookup.php
Go to the documentation of this file.
1<?php
2declare( strict_types=1 );
4
5use ArrayIterator;
6use CallbackFilterIterator;
7use IteratorIterator;
11use Wikimedia\Timestamp\ConvertibleTimestamp;
12use Wikimedia\Timestamp\TimestampFormat as TS;
13
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
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
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
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}
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
Caching lookup service for metadata related to temporary accounts, such as expiration.
getExpirationState( $registration)
Check whether a temporary account registered at the given timestamp is expired now.
isExpired(UserIdentity $user)
Check if a temporary user account is expired.
__construct(private readonly TempUserConfig $tempUserConfig, private readonly UserRegistrationLookup $userRegistrationLookup,)
preloadExpirationStatus(iterable $users)
Preload the expiration status of temporary accounts within a set of users.
Store key-value entries in a size-limited in-memory LRU cache.
Interface for temporary user creation config and name matching.
Interface for objects representing user identity.
isRegistered()
This must be equivalent to getId() != 0 and is provided for code readability.