Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.97% covered (success)
96.97%
32 / 33
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DatabaseBlockStoreFactory
96.97% covered (success)
96.97%
32 / 33
50.00% covered (danger)
50.00%
1 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 getDatabaseBlockStore
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
5
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\Block;
22
23use MediaWiki\CommentStore\CommentStore;
24use MediaWiki\Config\ServiceOptions;
25use MediaWiki\HookContainer\HookContainer;
26use MediaWiki\User\ActorStoreFactory;
27use MediaWiki\User\TempUser\TempUserConfig;
28use MediaWiki\User\UserFactory;
29use Psr\Log\LoggerInterface;
30use Wikimedia\Rdbms\LBFactory;
31use Wikimedia\Rdbms\ReadOnlyMode;
32
33/**
34 * @author Zabe
35 *
36 * @since 1.40
37 */
38class DatabaseBlockStoreFactory {
39    /**
40     * @internal For use by ServiceWiring
41     */
42    public const CONSTRUCTOR_OPTIONS = DatabaseBlockStore::CONSTRUCTOR_OPTIONS;
43
44    /** @var ServiceOptions */
45    private $options;
46
47    /** @var LoggerInterface */
48    private $logger;
49
50    /** @var ActorStoreFactory */
51    private $actorStoreFactory;
52
53    /** @var BlockRestrictionStoreFactory */
54    private $blockRestrictionStoreFactory;
55
56    /** @var CommentStore */
57    private $commentStore;
58
59    /** @var HookContainer */
60    private $hookContainer;
61
62    /** @var LBFactory */
63    private $loadBalancerFactory;
64
65    /** @var ReadOnlyMode */
66    private $readOnlyMode;
67
68    /** @var UserFactory */
69    private $userFactory;
70
71    /** @var TempUserConfig */
72    private $tempUserConfig;
73
74    /** @var BlockUtilsFactory */
75    private $blockUtilsFactory;
76
77    /** @var AutoblockExemptionList */
78    private $autoblockExemptionList;
79
80    /** @var DatabaseBlockStore[] */
81    private $storeCache = [];
82
83    /**
84     * @param ServiceOptions $options
85     * @param LoggerInterface $logger
86     * @param ActorStoreFactory $actorStoreFactory
87     * @param BlockRestrictionStoreFactory $blockRestrictionStoreFactory
88     * @param CommentStore $commentStore
89     * @param HookContainer $hookContainer
90     * @param LBFactory $loadBalancerFactory
91     * @param ReadOnlyMode $readOnlyMode
92     * @param UserFactory $userFactory
93     * @param TempUserConfig $tempUserConfig
94     * @param BlockUtilsFactory $blockUtilsFactory
95     * @param AutoblockExemptionList $autoblockExemptionList
96     */
97    public function __construct(
98        ServiceOptions $options,
99        LoggerInterface $logger,
100        ActorStoreFactory $actorStoreFactory,
101        BlockRestrictionStoreFactory $blockRestrictionStoreFactory,
102        CommentStore $commentStore,
103        HookContainer $hookContainer,
104        LBFactory $loadBalancerFactory,
105        ReadOnlyMode $readOnlyMode,
106        UserFactory $userFactory,
107        TempUserConfig $tempUserConfig,
108        BlockUtilsFactory $blockUtilsFactory,
109        AutoblockExemptionList $autoblockExemptionList
110    ) {
111        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
112
113        $this->options = $options;
114        $this->logger = $logger;
115        $this->actorStoreFactory = $actorStoreFactory;
116        $this->blockRestrictionStoreFactory = $blockRestrictionStoreFactory;
117        $this->commentStore = $commentStore;
118        $this->hookContainer = $hookContainer;
119        $this->loadBalancerFactory = $loadBalancerFactory;
120        $this->readOnlyMode = $readOnlyMode;
121        $this->userFactory = $userFactory;
122        $this->tempUserConfig = $tempUserConfig;
123        $this->blockUtilsFactory = $blockUtilsFactory;
124        $this->autoblockExemptionList = $autoblockExemptionList;
125    }
126
127    /**
128     * @param string|false $wikiId
129     * @return DatabaseBlockStore
130     */
131    public function getDatabaseBlockStore( $wikiId = DatabaseBlock::LOCAL ): DatabaseBlockStore {
132        if ( is_string( $wikiId ) && $this->loadBalancerFactory->getLocalDomainID() === $wikiId ) {
133            $wikiId = DatabaseBlock::LOCAL;
134        }
135
136        $storeCacheKey = $wikiId === DatabaseBlock::LOCAL ? 'LOCAL' : 'crosswikistore-' . $wikiId;
137        if ( !isset( $this->storeCache[$storeCacheKey] ) ) {
138            $this->storeCache[$storeCacheKey] = new DatabaseBlockStore(
139                $this->options,
140                $this->logger,
141                $this->actorStoreFactory,
142                $this->blockRestrictionStoreFactory->getBlockRestrictionStore( $wikiId ),
143                $this->commentStore,
144                $this->hookContainer,
145                $this->loadBalancerFactory,
146                $this->readOnlyMode,
147                $this->userFactory,
148                $this->tempUserConfig,
149                $this->blockUtilsFactory->getBlockUtils( $wikiId ),
150                $this->autoblockExemptionList,
151                $wikiId
152            );
153        }
154        return $this->storeCache[$storeCacheKey];
155    }
156}