MediaWiki  master
CentralIdLookupFactory.php
Go to the documentation of this file.
1 <?php
22 
23 use CentralIdLookup;
24 use InvalidArgumentException;
25 use LocalIdLookup;
28 use Wikimedia\ObjectFactory;
29 
35 
40  public const CONSTRUCTOR_OPTIONS = [
41  'CentralIdLookupProviders',
42  'CentralIdLookupProvider',
43  ];
44 
46  private $providers;
47 
50 
52  private $objectFactory;
53 
56 
58  private $instanceCache = [];
59 
65  public function __construct(
66  ServiceOptions $options,
67  ObjectFactory $objectFactory,
69  ) {
70  $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
71  $this->providers = $options->get( 'CentralIdLookupProviders' );
72  $this->defaultProvider = $options->get( 'CentralIdLookupProvider' );
73  $this->objectFactory = $objectFactory;
74  $this->userIdentityLookup = $userIdentityLookup;
75  }
76 
82  public function getProviderIds(): array {
83  return array_keys( $this->providers );
84  }
85 
91  public function getDefaultProviderId(): string {
93  }
94 
103  public function getLookup( string $providerId = null ): CentralIdLookup {
104  $providerId = $providerId ?? $this->defaultProvider;
105 
106  if ( !array_key_exists( $providerId, $this->instanceCache ) ) {
107  $providerSpec = $this->providers[$providerId] ?? null;
108  if ( !$providerSpec ) {
109  throw new InvalidArgumentException( "Invalid central ID provider $providerId" );
110  }
111  $provider = $this->objectFactory->createObject(
112  $providerSpec,
113  [ 'assertClass' => CentralIdLookup::class ]
114  );
115  $provider->init( $providerId, $this->userIdentityLookup );
116  $this->instanceCache[$providerId] = $provider;
117  }
118  return $this->instanceCache[$providerId];
119  }
120 
135  public function getNonLocalLookup( string $providerID = null ): ?CentralIdLookup {
136  $centralIdLookup = $this->getLookup( $providerID );
137  if ( $centralIdLookup instanceof LocalIdLookup ) {
138  /*
139  * A LocalIdLookup (which is the default) may actually be non-local,
140  * if shared user tables are used.
141  * However, we cannot know that here, so play it safe and refuse to return it.
142  * See also T163277 and T170996.
143  */
144  return null;
145  }
146  return $centralIdLookup;
147  }
148 }
MediaWiki\User\CentralId\CentralIdLookupFactory\$objectFactory
ObjectFactory $objectFactory
Definition: CentralIdLookupFactory.php:52
MediaWiki\User\CentralId\CentralIdLookupFactory\getNonLocalLookup
getNonLocalLookup(string $providerID=null)
Returns a CentralIdLookup that is guaranteed to be non-local.
Definition: CentralIdLookupFactory.php:135
MediaWiki\User\CentralId\CentralIdLookupFactory\$instanceCache
CentralIdLookup[] $instanceCache
Definition: CentralIdLookupFactory.php:58
MediaWiki\User\CentralId\CentralIdLookupFactory\$providers
array $providers
ObjectFactory specs indexed by provider name.
Definition: CentralIdLookupFactory.php:36
MediaWiki\User\CentralId
Definition: CentralIdLookupFactory.php:21
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:27
MediaWiki\User\CentralId\CentralIdLookupFactory\getDefaultProviderId
getDefaultProviderId()
Get the ID of the default central ID provider.
Definition: CentralIdLookupFactory.php:91
MediaWiki\User\CentralId\CentralIdLookupFactory\$userIdentityLookup
UserIdentityLookup $userIdentityLookup
Definition: CentralIdLookupFactory.php:55
MediaWiki\User\UserIdentityLookup
Definition: UserIdentityLookup.php:33
LocalIdLookup
A CentralIdLookup provider that just uses local IDs.
Definition: LocalIdLookup.php:36
MediaWiki\User\CentralId\CentralIdLookupFactory\$defaultProvider
string $defaultProvider
Definition: CentralIdLookupFactory.php:49
MediaWiki\User\CentralId\CentralIdLookupFactory
Definition: CentralIdLookupFactory.php:34
MediaWiki\User\CentralId\CentralIdLookupFactory\__construct
__construct(ServiceOptions $options, ObjectFactory $objectFactory, UserIdentityLookup $userIdentityLookup)
Definition: CentralIdLookupFactory.php:65
CentralIdLookup
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
Definition: CentralIdLookup.php:35
MediaWiki\User\CentralId\CentralIdLookupFactory\getProviderIds
getProviderIds()
Get the IDs of the registered central ID lookup providers.
Definition: CentralIdLookupFactory.php:82
MediaWiki\Config\ServiceOptions\get
get( $key)
Definition: ServiceOptions.php:93
MediaWiki\User\CentralId\CentralIdLookupFactory\getLookup
getLookup(string $providerId=null)
Get an instance of a CentralIdLookup.
Definition: CentralIdLookupFactory.php:103
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:71