MediaWiki master
TempUserDetailsLookup.php
Go to the documentation of this file.
1<?php
2declare( strict_types=1 );
4
5use ArrayIterator;
6use CallbackFilterIterator;
7use IteratorIterator;
11
17 private TempUserConfig $tempUserConfig;
18 private UserRegistrationLookup $userRegistrationLookup;
19
20 private MapCacheLRU $expiryCache;
21
22 public function __construct(
23 TempUserConfig $tempUserConfig,
24 UserRegistrationLookup $userRegistrationLookup
25 ) {
26 $this->tempUserConfig = $tempUserConfig;
27 $this->userRegistrationLookup = $userRegistrationLookup;
28
29 // Use a relatively large cache size to account for pages with a high number of user links,
30 // such as Special:RecentChanges or history pages.
31 $this->expiryCache = new MapCacheLRU( 1_000 );
32 }
33
39 public function isExpired( UserIdentity $user ): bool {
40 $userName = $user->getName();
41 if (
42 !$this->tempUserConfig->isTempName( $userName ) ||
43 !$user->isRegistered()
44 ) {
45 return false;
46 }
47
48 if ( !$this->expiryCache->has( $userName ) ) {
49 $registration = $this->userRegistrationLookup->getFirstRegistration( $user );
50
51 $this->expiryCache->set( $userName, $this->getExpirationState( $registration ) );
52 }
53
54 return $this->expiryCache->get( $userName );
55 }
56
62 public function preloadExpirationStatus( iterable $users ): void {
63 // Save the user name associated with each user id for use later.
64 $usersById = [];
65 foreach ( $users as $user ) {
66 $usersById[$user->getId()] = $user->getName();
67 }
68
69 $users = is_array( $users ) ? new ArrayIterator( $users ) : new IteratorIterator( $users );
70 $timestampsById = $this->userRegistrationLookup->getFirstRegistrationBatch(
71 new CallbackFilterIterator(
72 $users,
73 fn ( UserIdentity $user ): bool =>
74 $user->isRegistered() && $this->tempUserConfig->isTempName( $user->getName() )
75 )
76 );
77
78 foreach ( $timestampsById as $userId => $registrationTimestamp ) {
79 // Cache expiry by user name instead of user id. See T398722.
80 $this->expiryCache->set(
81 $usersById[$userId],
82 $this->getExpirationState( $registrationTimestamp )
83 );
84 }
85 }
86
93 public function getExpirationState( $registration ): bool {
94 if ( !is_string( $registration ) ) {
95 return false;
96 }
97
98 $expireAfterDays = $this->tempUserConfig->getExpireAfterDays();
99 $expiresAt = (int)wfTimestamp( TS_UNIX, $registration ) + $expireAfterDays * 86400;
100 return $expiresAt < wfTimestamp( TS_UNIX );
101 }
102}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
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(TempUserConfig $tempUserConfig, 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.