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 if (
41 !$this->tempUserConfig->isTempName( $user->getName() ) ||
42 !$user->isRegistered()
43 ) {
44 return false;
45 }
46
47 $userId = $user->getId();
48
49 if ( !$this->expiryCache->has( $userId ) ) {
50 $registration = $this->userRegistrationLookup->getFirstRegistration( $user );
51
52 $this->expiryCache->set( $userId, $this->getExpirationState( $registration ) );
53 }
54
55 return $this->expiryCache->get( $userId );
56 }
57
63 public function preloadExpirationStatus( iterable $users ): void {
64 $users = is_array( $users ) ? new ArrayIterator( $users ) : new IteratorIterator( $users );
65 $timestampsById = $this->userRegistrationLookup->getFirstRegistrationBatch(
66 new CallbackFilterIterator(
67 $users,
68 fn ( UserIdentity $user ): bool =>
69 $user->isRegistered() && $this->tempUserConfig->isTempName( $user->getName() )
70 )
71 );
72
73 foreach ( $timestampsById as $userId => $registrationTimestamp ) {
74 $this->expiryCache->set(
75 $userId,
76 $this->getExpirationState( $registrationTimestamp )
77 );
78 }
79 }
80
87 private function getExpirationState( $registration ): bool {
88 if ( !is_string( $registration ) ) {
89 return false;
90 }
91
92 $expireAfterDays = $this->tempUserConfig->getExpireAfterDays();
93 $expiresAt = (int)wfTimestamp( TS_UNIX, $registration ) + $expireAfterDays * 86400;
94 return $expiresAt < wfTimestamp( TS_UNIX );
95 }
96}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
Caching lookup service for metadata related to temporary accounts, such as expiration.
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.
getId( $wikiId=self::LOCAL)