Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.18% |
31 / 34 |
|
77.78% |
7 / 9 |
CRAP | |
0.00% |
0 / 1 |
ActorStoreFactory | |
91.18% |
31 / 34 |
|
77.78% |
7 / 9 |
17.20 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
getActorNormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getActorNormalizationForImport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getActorStore | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getActorStoreForImport | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getActorStoreForUndelete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getStore | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
7.08 | |||
getUserIdentityLookup | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLoadBalancerForTable | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
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 | * ActorStore factory for various domains. |
34 | * |
35 | * @package MediaWiki\User |
36 | * @since 1.36 |
37 | */ |
38 | class ActorStoreFactory { |
39 | |
40 | /** @internal */ |
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 | /** @var string|false */ |
53 | private $sharedDB; |
54 | |
55 | /** @var string[] */ |
56 | private $sharedTables; |
57 | |
58 | /** @var ActorStore[] */ |
59 | private $storeCache = []; |
60 | |
61 | /** |
62 | * @param ServiceOptions $options |
63 | * @param ILBFactory $loadBalancerFactory |
64 | * @param UserNameUtils $userNameUtils |
65 | * @param TempUserConfig $tempUserConfig |
66 | * @param LoggerInterface $logger |
67 | * @param HideUserUtils $hideUserUtils |
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 | * @param string|false $wikiId |
89 | * @return ActorNormalization |
90 | */ |
91 | public function getActorNormalization( $wikiId = WikiAwareEntity::LOCAL ): ActorNormalization { |
92 | return $this->getActorStore( $wikiId ); |
93 | } |
94 | |
95 | /** |
96 | * @since 1.42 |
97 | * @param string|false $wikiId |
98 | * @return ActorNormalization |
99 | */ |
100 | public function getActorNormalizationForImport( |
101 | $wikiId = WikiAwareEntity::LOCAL |
102 | ): ActorNormalization { |
103 | return $this->getActorStoreForImport( $wikiId ); |
104 | } |
105 | |
106 | /** |
107 | * @param string|false $wikiId |
108 | * @return ActorStore |
109 | */ |
110 | public function getActorStore( $wikiId = WikiAwareEntity::LOCAL ): ActorStore { |
111 | return $this->getStore( $wikiId, false ); |
112 | } |
113 | |
114 | /** |
115 | * @since 1.42 |
116 | * @param string|false $wikiId |
117 | * @return ActorStore |
118 | */ |
119 | public function getActorStoreForImport( $wikiId = WikiAwareEntity::LOCAL ): ActorStore { |
120 | return $this->getStore( $wikiId, true ); |
121 | } |
122 | |
123 | /** |
124 | * @since 1.43 |
125 | * @param string|false $wikiId |
126 | * @return ActorStore |
127 | */ |
128 | public function getActorStoreForUndelete( $wikiId = WikiAwareEntity::LOCAL ): ActorStore { |
129 | return $this->getStore( $wikiId, true ); |
130 | } |
131 | |
132 | /** |
133 | * @param string|false $wikiId |
134 | * @param bool $allowingIpActorCreation |
135 | * @return ActorStore |
136 | */ |
137 | private function getStore( $wikiId, bool $allowingIpActorCreation ): ActorStore { |
138 | // During the transition from User, we still have old User objects |
139 | // representing users from a different wiki, so we still have IDatabase::getDomainId |
140 | // passed as $wikiId, so we need to remap it back to LOCAL. |
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 | * @param string|false $wikiId |
167 | * @return UserIdentityLookup |
168 | */ |
169 | public function getUserIdentityLookup( |
170 | $wikiId = WikiAwareEntity::LOCAL |
171 | ): UserIdentityLookup { |
172 | return $this->getActorStore( $wikiId ); |
173 | } |
174 | |
175 | /** |
176 | * Returns a load balancer for the database that has the $table |
177 | * for the given $wikiId. |
178 | * |
179 | * @param string $table |
180 | * @param string|false $wikiId |
181 | * @return ILoadBalancer |
182 | */ |
183 | private function getLoadBalancerForTable( |
184 | string $table, |
185 | $wikiId = WikiAwareEntity::LOCAL |
186 | ): ILoadBalancer { |
187 | if ( $this->sharedDB && in_array( $table, $this->sharedTables ) ) { |
188 | // The main LB is already properly set up for shared databases early in Setup.php |
189 | return $this->loadBalancerFactory->getMainLB(); |
190 | } |
191 | return $this->loadBalancerFactory->getMainLB( $wikiId ); |
192 | } |
193 | } |