3 namespace MediaWiki\User\Registration
;
5 use InvalidArgumentException
;
6 use MediaWiki\Config\ServiceOptions
;
7 use MediaWiki\MainConfigNames
;
8 use MediaWiki\MainConfigSchema
;
9 use MediaWiki\User\UserIdentity
;
10 use Wikimedia\ObjectFactory\ObjectFactory
;
15 class UserRegistrationLookup
{
18 * @internal for use in ServiceWiring
19 * @var string[] Config names to require
21 public const CONSTRUCTOR_OPTIONS
= [
22 MainConfigNames
::UserRegistrationProviders
,
25 /** @var array ObjectFactory specs indexed by provider name */
26 private array $providersSpecs;
28 /** @var IUserRegistrationProvider[] Constructed registration providers indexed by name */
29 private array $providers = [];
31 private ObjectFactory
$objectFactory;
33 public function __construct(
34 ServiceOptions
$options,
35 ObjectFactory
$objectFactory
37 $options->assertRequiredOptions( self
::CONSTRUCTOR_OPTIONS
);
38 $this->providersSpecs
= $options->get( MainConfigNames
::UserRegistrationProviders
);
39 $this->objectFactory
= $objectFactory;
43 * Is a registration provider registered?
45 * @see MainConfigSchema::UserRegistrationLookupProviders
49 public function isRegistered( string $type ): bool {
50 return array_key_exists( $type, $this->providersSpecs
);
54 * Construct a registration provider, if needed
57 * @return IUserRegistrationProvider
59 private function getProvider( string $type ): IUserRegistrationProvider
{
60 if ( !$this->isRegistered( $type ) ) {
61 throw new InvalidArgumentException( 'Registration provider ' . $type . ' is not registered' );
63 if ( !array_key_exists( $type, $this->providers
) ) {
64 $this->providers
[$type] = $this->objectFactory
->createObject(
65 $this->providersSpecs
[$type],
66 [ 'assertClass' => IUserRegistrationProvider
::class ]
69 return $this->providers
[$type];
73 * @param UserIdentity $user User for which registration should be fetched.
74 * @param string $type Name of a registered registration provider
75 * @return string|null|false Registration timestamp, null if not available or false if it
76 * cannot be fetched (anonymous users, for example).
78 public function getRegistration(
80 string $type = LocalUserRegistrationProvider
::TYPE
82 return $this->getProvider( $type )->fetchRegistration( $user );
86 * Find the first registration timestamp for a given user
88 * Note this invokes _all_ registered providers.
90 * @param UserIdentity $user
91 * @return string|null Earliest registration timestamp, null if not available.
93 public function getFirstRegistration( UserIdentity
$user ): ?
string {
94 $registrationTimestampsUnix = [];
95 foreach ( $this->providersSpecs
as $providerKey => $_ ) {
96 $registrationTimestampRaw = $this->getRegistration( $user, $providerKey );
97 if ( !is_string( $registrationTimestampRaw ) ) {
98 // Provider was unable to return a registration timestamp for $providerKey, skip
102 $registrationTimestampsUnix[] = (int)wfTimestamp( TS_UNIX
, $registrationTimestampRaw );
105 if ( $registrationTimestampsUnix === [] ) {
109 return wfTimestamp( TS_MW
, min( $registrationTimestampsUnix ) );