MediaWiki master
LocalUserRegistrationProvider.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
8
10
11 public const TYPE = 'local';
12
13 public function __construct(
14 private readonly IConnectionProvider $connectionProvider
15 ) {
16 }
17
21 public function fetchRegistration( UserIdentity $user ) {
22 $id = $user->getId( $user->getWikiId() );
23
24 if ( $id === 0 ) {
25 return false;
26 }
27
28 $userRegistration = $this->connectionProvider->getReplicaDatabase( $user->getWikiId() )
29 ->newSelectQueryBuilder()
30 ->select( 'user_registration' )
31 ->from( 'user' )
32 ->where( [ 'user_id' => $id ] )
33 ->caller( __METHOD__ )
34 ->fetchField();
35
36 return wfTimestampOrNull( TS_MW, $userRegistration );
37 }
38
42 public function fetchRegistrationBatch( iterable $users ): array {
43 $timestampsById = [];
44 // The output format doesn't allow us to return the wiki ID, so we need to ensure all the input users come
45 // from the same wiki. If wikiToQuery is null, it means we haven't set it yet, so we can accept any wiki ID
46 // for the first user.
47 $wikiToQuery = null;
48
49 foreach ( $users as $user ) {
50 $wikiId = $user->getWikiId();
51
52 if ( $wikiToQuery === null ) {
53 $wikiToQuery = $wikiId;
54 } elseif ( $wikiToQuery !== $wikiId ) {
55 throw new InvalidArgumentException( 'All queried users must belong to the same wiki.' );
56 }
57
58 // Make the list of user IDs unique.
59 $timestampsById[$user->getId( $wikiId )] = null;
60 }
61
62 if ( $wikiToQuery === null ) {
63 // No users to query.
64 return [];
65 }
66
67 $batches = array_chunk( array_keys( $timestampsById ), 1_000 );
68
69 $dbr = $this->connectionProvider->getReplicaDatabase( $wikiToQuery );
70
71 foreach ( $batches as $userIdBatch ) {
72 $res = $dbr->newSelectQueryBuilder()
73 ->select( [ 'user_id', 'user_registration' ] )
74 ->from( 'user' )
75 ->where( [ 'user_id' => $userIdBatch ] )
76 ->caller( __METHOD__ )
77 ->fetchResultSet();
78
79 foreach ( $res as $row ) {
80 $timestampsById[$row->user_id] = wfTimestampOrNull( TS_MW, $row->user_registration );
81 }
82 }
83
84 return $timestampsById;
85 }
86}
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), null if not availab...
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.