Update Codex from v1.20.0 to v1.20.1
[mediawiki.git] / includes / user / Registration / UserRegistrationLookup.php
blobf706000e35efc82a82118b63748f65e5c92c191f
1 <?php
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;
12 /**
13 * @since 1.41
15 class UserRegistrationLookup {
17 /**
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
36 ) {
37 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
38 $this->providersSpecs = $options->get( MainConfigNames::UserRegistrationProviders );
39 $this->objectFactory = $objectFactory;
42 /**
43 * Is a registration provider registered?
45 * @see MainConfigSchema::UserRegistrationLookupProviders
46 * @param string $type
47 * @return bool
49 public function isRegistered( string $type ): bool {
50 return array_key_exists( $type, $this->providersSpecs );
53 /**
54 * Construct a registration provider, if needed
56 * @param string $type
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];
72 /**
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(
79 UserIdentity $user,
80 string $type = LocalUserRegistrationProvider::TYPE
81 ) {
82 return $this->getProvider( $type )->fetchRegistration( $user );
85 /**
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
99 // them.
100 continue;
102 $registrationTimestampsUnix[] = (int)wfTimestamp( TS_UNIX, $registrationTimestampRaw );
105 if ( $registrationTimestampsUnix === [] ) {
106 return null;
109 return wfTimestamp( TS_MW, min( $registrationTimestampsUnix ) );