MediaWiki  master
ActorStoreFactory.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\User;
22 
25 use Psr\Log\LoggerInterface;
26 use WikiMap;
29 
37 
39  public const CONSTRUCTOR_OPTIONS = [
40  'SharedDB',
41  'SharedTables',
42  ];
43 
46 
48  private $userNameUtils;
49 
51  private $logger;
52 
54  private $sharedDB;
55 
57  private $sharedTables;
58 
60  private $storeCache = [];
61 
68  public function __construct(
69  ServiceOptions $options,
72  LoggerInterface $logger
73  ) {
74  $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
75  $this->loadBalancerFactory = $loadBalancerFactory;
76  $this->sharedDB = $options->get( 'SharedDB' );
77  $this->sharedTables = $options->get( 'SharedTables' );
78  $this->userNameUtils = $userNameUtils;
79  $this->logger = $logger;
80  }
81 
86  public function getActorNormalization( $wikiId = WikiAwareEntity::LOCAL ): ActorNormalization {
87  return $this->getActorStore( $wikiId );
88  }
89 
94  public function getActorStore( $wikiId = WikiAwareEntity::LOCAL ): ActorStore {
95  // During the transition from User, we still have old User objects
96  // representing users from a different wiki, so we still have IDatabase::getDomainId
97  // passed as $wikiId, so we need to remap it back to LOCAL.
98  if ( is_string( $wikiId ) && WikiMap::isCurrentWikiDbDomain( $wikiId ) ) {
99  $wikiId = WikiAwareEntity::LOCAL;
100  }
101 
102  $storeCacheKey = $this->getStoreCacheKey( $wikiId );
103  if ( !isset( $this->storeCache[$storeCacheKey] ) ) {
104  $this->storeCache[$storeCacheKey] = new ActorStore(
105  $this->getLoadBalancerForTable( 'actor', $wikiId ),
106  $this->userNameUtils,
107  $this->logger,
108  $wikiId
109  );
110  }
111  return $this->storeCache[$storeCacheKey];
112  }
113 
118  public function getUserIdentityLookup(
119  $wikiId = WikiAwareEntity::LOCAL
120  ): UserIdentityLookup {
121  return $this->getActorStore( $wikiId );
122  }
123 
128  private function getStoreCacheKey( $wikiId ): string {
129  return $wikiId === WikiAwareEntity::LOCAL ? 'LOCAL' : $wikiId;
130  }
131 
140  private function getLoadBalancerForTable(
141  string $table,
142  $wikiId = WikiAwareEntity::LOCAL
143  ): ILoadBalancer {
144  if ( $this->sharedDB && in_array( $table, $this->sharedTables ) ) {
145  // The main LB is already properly set up for shared databases early in Setup.php
146  return $this->loadBalancerFactory->getMainLB();
147  }
148  return $this->loadBalancerFactory->getMainLB( $wikiId );
149  }
150 }
WikiMap\isCurrentWikiDbDomain
static isCurrentWikiDbDomain( $domain)
Definition: WikiMap.php:312
MediaWiki\User\ActorStoreFactory\$sharedTables
string[] $sharedTables
Definition: ActorStoreFactory.php:57
MediaWiki\User\ActorStoreFactory\$storeCache
ActorStore[] $storeCache
Definition: ActorStoreFactory.php:60
MediaWiki\User\ActorStoreFactory\$sharedDB
string false $sharedDB
Definition: ActorStoreFactory.php:54
MediaWiki\User\ActorStoreFactory\getStoreCacheKey
getStoreCacheKey( $wikiId)
Definition: ActorStoreFactory.php:128
MediaWiki\User\ActorStoreFactory\CONSTRUCTOR_OPTIONS
const CONSTRUCTOR_OPTIONS
Definition: ActorStoreFactory.php:39
MediaWiki\User\ActorStoreFactory
Definition: ActorStoreFactory.php:36
MediaWiki\DAO\WikiAwareEntity
Marker interface for entities aware of the wiki they belong to.
Definition: WikiAwareEntity.php:34
MediaWiki\User\ActorStoreFactory\getUserIdentityLookup
getUserIdentityLookup( $wikiId=WikiAwareEntity::LOCAL)
Definition: ActorStoreFactory.php:118
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:27
MediaWiki\User\ActorStoreFactory\$userNameUtils
UserNameUtils $userNameUtils
Definition: ActorStoreFactory.php:48
MediaWiki\User\ActorStoreFactory\$logger
LoggerInterface $logger
Definition: ActorStoreFactory.php:51
MediaWiki\User\ActorStoreFactory\getLoadBalancerForTable
getLoadBalancerForTable(string $table, $wikiId=WikiAwareEntity::LOCAL)
Returns a load balancer for the database that has the $table for the given $wikiId.
Definition: ActorStoreFactory.php:140
MediaWiki\User\ActorStoreFactory\getActorNormalization
getActorNormalization( $wikiId=WikiAwareEntity::LOCAL)
Definition: ActorStoreFactory.php:86
MediaWiki\User\UserIdentityLookup
Definition: UserIdentityLookup.php:33
MediaWiki\User\ActorStoreFactory\$loadBalancerFactory
ILBFactory $loadBalancerFactory
Definition: ActorStoreFactory.php:45
MediaWiki\User
Definition: ActorCache.php:21
WikiMap
Helper tools for dealing with other locally-hosted wikis.
Definition: WikiMap.php:29
MediaWiki\User\ActorStoreFactory\__construct
__construct(ServiceOptions $options, ILBFactory $loadBalancerFactory, UserNameUtils $userNameUtils, LoggerInterface $logger)
Definition: ActorStoreFactory.php:68
MediaWiki\User\UserNameUtils
UserNameUtils service.
Definition: UserNameUtils.php:42
MediaWiki\Config\ServiceOptions\get
get( $key)
Definition: ServiceOptions.php:88
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
MediaWiki\User\ActorStoreFactory\getActorStore
getActorStore( $wikiId=WikiAwareEntity::LOCAL)
Definition: ActorStoreFactory.php:94
MediaWiki\User\ActorStore
Definition: ActorStore.php:43
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:66
Wikimedia\Rdbms\ILBFactory
An interface for generating database load balancers.
Definition: ILBFactory.php:33
MediaWiki\User\ActorNormalization
Service for dealing with the actor table.
Definition: ActorNormalization.php:32