Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
30 / 33
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ActorStoreFactory
90.91% covered (success)
90.91%
30 / 33
75.00% covered (warning)
75.00%
6 / 8
16.19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 getActorNormalization
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActorNormalizationForImport
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getActorStore
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActorStoreForImport
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStore
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
7.08
 getUserIdentityLookup
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLoadBalancerForTable
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
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
21namespace MediaWiki\User;
22
23use MediaWiki\Block\HideUserUtils;
24use MediaWiki\Config\ServiceOptions;
25use MediaWiki\DAO\WikiAwareEntity;
26use MediaWiki\MainConfigNames;
27use MediaWiki\User\TempUser\TempUserConfig;
28use Psr\Log\LoggerInterface;
29use Wikimedia\Rdbms\ILBFactory;
30use Wikimedia\Rdbms\ILoadBalancer;
31
32/**
33 * ActorStore factory for various domains.
34 *
35 * @package MediaWiki\User
36 * @since 1.36
37 */
38class 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     * @param string|false $wikiId
125     * @param bool $forImport
126     * @return ActorStore
127     */
128    private function getStore( $wikiId, $forImport ): ActorStore {
129        // During the transition from User, we still have old User objects
130        // representing users from a different wiki, so we still have IDatabase::getDomainId
131        // passed as $wikiId, so we need to remap it back to LOCAL.
132        if ( is_string( $wikiId ) && $this->loadBalancerFactory->getLocalDomainID() === $wikiId ) {
133            $wikiId = WikiAwareEntity::LOCAL;
134        }
135
136        $storeCacheKey = ( $forImport ? 'import' : '' ) .
137            ( $wikiId === WikiAwareEntity::LOCAL ? 'LOCAL' : $wikiId );
138
139        if ( !isset( $this->storeCache[$storeCacheKey] ) ) {
140            $store = $this->storeCache[$storeCacheKey] = new ActorStore(
141                $this->getLoadBalancerForTable( 'actor', $wikiId ),
142                $this->userNameUtils,
143                $this->tempUserConfig,
144                $this->logger,
145                $this->hideUserUtils,
146                $wikiId
147            );
148            if ( $forImport ) {
149                $store->setAllowCreateIpActors( true );
150            }
151            $this->storeCache[$storeCacheKey] = $store;
152        }
153        return $this->storeCache[$storeCacheKey];
154    }
155
156    /**
157     * @param string|false $wikiId
158     * @return UserIdentityLookup
159     */
160    public function getUserIdentityLookup(
161        $wikiId = WikiAwareEntity::LOCAL
162    ): UserIdentityLookup {
163        return $this->getActorStore( $wikiId );
164    }
165
166    /**
167     * Returns a load balancer for the database that has the $table
168     * for the given $wikiId.
169     *
170     * @param string $table
171     * @param string|false $wikiId
172     * @return ILoadBalancer
173     */
174    private function getLoadBalancerForTable(
175        string $table,
176        $wikiId = WikiAwareEntity::LOCAL
177    ): ILoadBalancer {
178        if ( $this->sharedDB && in_array( $table, $this->sharedTables ) ) {
179            // The main LB is already properly set up for shared databases early in Setup.php
180            return $this->loadBalancerFactory->getMainLB();
181        }
182        return $this->loadBalancerFactory->getMainLB( $wikiId );
183    }
184}