MediaWiki master
LocalUserRegistrationProvider.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
8use Wikimedia\Timestamp\TimestampFormat as TS;
9
11
12 public const TYPE = 'local';
13
14 public function __construct(
15 private readonly IConnectionProvider $connectionProvider
16 ) {
17 }
18
22 public function fetchRegistration( UserIdentity $user ) {
23 $id = $user->getId( $user->getWikiId() );
24
25 if ( $id === 0 ) {
26 return false;
27 }
28
29 $userRegistration = $this->connectionProvider->getReplicaDatabase( $user->getWikiId() )
30 ->newSelectQueryBuilder()
31 ->select( 'user_registration' )
32 ->from( 'user' )
33 ->where( [ 'user_id' => $id ] )
34 ->caller( __METHOD__ )
35 ->fetchField();
36
37 return wfTimestampOrNull( TS::MW, $userRegistration );
38 }
39
43 public function fetchRegistrationBatch( iterable $users ): array {
44 $timestampsById = [];
45 // The output format doesn't allow us to return the wiki ID, so we need to ensure all the input users come
46 // from the same wiki. If wikiToQuery is null, it means we haven't set it yet, so we can accept any wiki ID
47 // for the first user.
48 $wikiToQuery = null;
49
50 foreach ( $users as $user ) {
51 $wikiId = $user->getWikiId();
52
53 if ( $wikiToQuery === null ) {
54 $wikiToQuery = $wikiId;
55 } elseif ( $wikiToQuery !== $wikiId ) {
56 throw new InvalidArgumentException( 'All queried users must belong to the same wiki.' );
57 }
58
59 // Make the list of user IDs unique.
60 $timestampsById[$user->getId( $wikiId )] = null;
61 }
62
63 if ( $wikiToQuery === null ) {
64 // No users to query.
65 return [];
66 }
67
68 $batches = array_chunk( array_keys( $timestampsById ), 1_000 );
69
70 $dbr = $this->connectionProvider->getReplicaDatabase( $wikiToQuery );
71
72 foreach ( $batches as $userIdBatch ) {
73 $res = $dbr->newSelectQueryBuilder()
74 ->select( [ 'user_id', 'user_registration' ] )
75 ->from( 'user' )
76 ->where( [ 'user_id' => $userIdBatch ] )
77 ->caller( __METHOD__ )
78 ->fetchResultSet();
79
80 foreach ( $res as $row ) {
81 $timestampsById[$row->user_id] = wfTimestampOrNull( TS::MW, $row->user_registration );
82 }
83 }
84
85 return $timestampsById;
86 }
87}
wfTimestampOrNull( $outputtype=TS::UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
__construct(private readonly IConnectionProvider $connectionProvider)
fetchRegistration(UserIdentity $user)
Get user registration timestamp.string|false|null Registration timestamp (TS::MW),...
fetchRegistrationBatch(iterable $users)
Get user registration timestamps for a batch of users.1.44 string[]|null[] Map of registration timest...
getWikiId()
Get the ID of the wiki this page belongs to.
Interface for objects representing user identity.
getId( $wikiId=self::LOCAL)
Provide primary and replica IDatabase connections.