| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\User; |
| 22 |
|
| 23 |
use MediaWiki\Block\HideUserUtils; |
| 24 |
use MediaWiki\Config\ServiceOptions; |
| 25 |
use MediaWiki\DAO\WikiAwareEntity; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\User\TempUser\TempUserConfig; |
| 28 |
use Psr\Log\LoggerInterface; |
| 29 |
use Wikimedia\Rdbms\ILBFactory; |
| 30 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class ActorStoreFactory { |
| 39 |
|
| 40 |
|
| 41 |
public const CONSTRUCTOR_OPTIONS = [ |
| 42 |
MainConfigNames::SharedDB, |
| 43 |
MainConfigNames::SharedTables, |
| 44 |
]; |
| 45 |
|
| 46 |
private ILBFactory $loadBalancerFactory; |
| 47 |
private UserNameUtils $userNameUtils; |
| 48 |
private TempUserConfig $tempUserConfig; |
| 49 |
private LoggerInterface $logger; |
| 50 |
private HideUserUtils $hideUserUtils; |
| 51 |
|
| 52 |
|
| 53 |
private $sharedDB; |
| 54 |
|
| 55 |
|
| 56 |
private $sharedTables; |
| 57 |
|
| 58 |
|
| 59 |
private $storeCache = []; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function __construct( |
| 70 |
ServiceOptions $options, |
| 71 |
ILBFactory $loadBalancerFactory, |
| 72 |
UserNameUtils $userNameUtils, |
| 73 |
TempUserConfig $tempUserConfig, |
| 74 |
LoggerInterface $logger, |
| 75 |
HideUserUtils $hideUserUtils |
| 76 |
) { |
| 77 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 78 |
$this->loadBalancerFactory = $loadBalancerFactory; |
| 79 |
$this->sharedDB = $options->get( MainConfigNames::SharedDB ); |
| 80 |
$this->sharedTables = $options->get( MainConfigNames::SharedTables ); |
| 81 |
$this->userNameUtils = $userNameUtils; |
| 82 |
$this->tempUserConfig = $tempUserConfig; |
| 83 |
$this->logger = $logger; |
| 84 |
$this->hideUserUtils = $hideUserUtils; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function getActorNormalization( $wikiId = WikiAwareEntity::LOCAL ): ActorNormalization { |
| 92 |
return $this->getActorStore( $wikiId ); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function getActorNormalizationForImport( |
| 101 |
$wikiId = WikiAwareEntity::LOCAL |
| 102 |
): ActorNormalization { |
| 103 |
return $this->getActorStoreForImport( $wikiId ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function getActorStore( $wikiId = WikiAwareEntity::LOCAL ): ActorStore { |
| 111 |
return $this->getStore( $wikiId, false ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function getActorStoreForImport( $wikiId = WikiAwareEntity::LOCAL ): ActorStore { |
| 120 |
return $this->getStore( $wikiId, true ); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function getActorStoreForUndelete( $wikiId = WikiAwareEntity::LOCAL ): ActorStore { |
| 129 |
return $this->getStore( $wikiId, true ); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
private function getStore( $wikiId, bool $allowingIpActorCreation ): ActorStore { |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
if ( is_string( $wikiId ) && $this->loadBalancerFactory->getLocalDomainID() === $wikiId ) { |
| 142 |
$wikiId = WikiAwareEntity::LOCAL; |
| 143 |
} |
| 144 |
|
| 145 |
$storeCacheKey = ( $allowingIpActorCreation ? 'allowing-ip-actor-creation-' : '' ) . |
| 146 |
( $wikiId === WikiAwareEntity::LOCAL ? 'LOCAL' : $wikiId ); |
| 147 |
|
| 148 |
if ( !isset( $this->storeCache[$storeCacheKey] ) ) { |
| 149 |
$store = new ActorStore( |
| 150 |
$this->getLoadBalancerForTable( 'actor', $wikiId ), |
| 151 |
$this->userNameUtils, |
| 152 |
$this->tempUserConfig, |
| 153 |
$this->logger, |
| 154 |
$this->hideUserUtils, |
| 155 |
$wikiId |
| 156 |
); |
| 157 |
if ( $allowingIpActorCreation ) { |
| 158 |
$store->setAllowCreateIpActors( true ); |
| 159 |
} |
| 160 |
$this->storeCache[$storeCacheKey] = $store; |
| 161 |
} |
| 162 |
return $this->storeCache[$storeCacheKey]; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function getUserIdentityLookup( |
| 170 |
$wikiId = WikiAwareEntity::LOCAL |
| 171 |
): UserIdentityLookup { |
| 172 |
return $this->getActorStore( $wikiId ); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
private function getLoadBalancerForTable( |
| 184 |
string $table, |
| 185 |
$wikiId = WikiAwareEntity::LOCAL |
| 186 |
): ILoadBalancer { |
| 187 |
if ( $this->sharedDB && in_array( $table, $this->sharedTables ) ) { |
| 188 |
|
| 189 |
return $this->loadBalancerFactory->getMainLB(); |
| 190 |
} |
| 191 |
return $this->loadBalancerFactory->getMainLB( $wikiId ); |
| 192 |
} |
| 193 |
} |