Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.43% covered (success)
96.43%
27 / 28
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserGroupManagerFactory
96.43% covered (success)
96.43%
27 / 28
50.00% covered (danger)
50.00%
1 / 2
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 getUserGroupManager
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
4.00
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\Config\ServiceOptions;
24use MediaWiki\HookContainer\HookContainer;
25use MediaWiki\JobQueue\JobQueueGroupFactory;
26use MediaWiki\Permissions\GroupPermissionsLookup;
27use MediaWiki\User\TempUser\TempUserConfig;
28use Psr\Log\LoggerInterface;
29use Wikimedia\Rdbms\ILBFactory;
30use Wikimedia\Rdbms\ReadOnlyMode;
31
32/**
33 * Factory service for UserGroupManager instances. This allows UserGroupManager to be created for
34 * cross-wiki access.
35 *
36 * @since 1.35
37 * @ingroup User
38 */
39class UserGroupManagerFactory {
40    private ServiceOptions $options;
41    private ReadOnlyMode $readOnlyMode;
42    private ILBFactory $dbLoadBalancerFactory;
43    private UserEditTracker $userEditTracker;
44    private GroupPermissionsLookup $groupPermissionLookup;
45    private JobQueueGroupFactory $jobQueueGroupFactory;
46    private LoggerInterface $logger;
47
48    /** @var callable[] */
49    private $clearCacheCallbacks;
50
51    private HookContainer $hookContainer;
52    private TempUserConfig $tempUserConfig;
53
54    /**
55     * @var UserGroupManager[] User group manager instances indexed by wiki
56     */
57    private $instances = [];
58
59    /**
60     * @param ServiceOptions $options
61     * @param ReadOnlyMode $readOnlyMode
62     * @param ILBFactory $dbLoadBalancerFactory
63     * @param HookContainer $hookContainer
64     * @param UserEditTracker $userEditTracker
65     * @param GroupPermissionsLookup $groupPermissionsLookup
66     * @param JobQueueGroupFactory $jobQueueGroupFactory
67     * @param LoggerInterface $logger
68     * @param TempUserConfig $tempUserConfig Assumed to be the same across all domains.
69     * @param callable[] $clearCacheCallbacks
70     */
71    public function __construct(
72        ServiceOptions $options,
73        ReadOnlyMode $readOnlyMode,
74        ILBFactory $dbLoadBalancerFactory,
75        HookContainer $hookContainer,
76        UserEditTracker $userEditTracker,
77        GroupPermissionsLookup $groupPermissionsLookup,
78        JobQueueGroupFactory $jobQueueGroupFactory,
79        LoggerInterface $logger,
80        TempUserConfig $tempUserConfig,
81        array $clearCacheCallbacks = []
82    ) {
83        $this->options = $options;
84        $this->readOnlyMode = $readOnlyMode;
85        $this->dbLoadBalancerFactory = $dbLoadBalancerFactory;
86        $this->hookContainer = $hookContainer;
87        $this->userEditTracker = $userEditTracker;
88        $this->groupPermissionLookup = $groupPermissionsLookup;
89        $this->jobQueueGroupFactory = $jobQueueGroupFactory;
90        $this->logger = $logger;
91        $this->tempUserConfig = $tempUserConfig;
92        $this->clearCacheCallbacks = $clearCacheCallbacks;
93    }
94
95    /**
96     * @param string|false $wikiId
97     * @return UserGroupManager
98     */
99    public function getUserGroupManager( $wikiId = UserIdentity::LOCAL ): UserGroupManager {
100        if ( is_string( $wikiId ) && $this->dbLoadBalancerFactory->getLocalDomainID() === $wikiId ) {
101            $wikiId = UserIdentity::LOCAL;
102        }
103        $key = (string)$wikiId;
104        if ( !isset( $this->instances[$key] ) ) {
105            $this->instances[$key] = new UserGroupManager(
106                $this->options,
107                $this->readOnlyMode,
108                $this->dbLoadBalancerFactory,
109                $this->hookContainer,
110                $this->userEditTracker,
111                $this->groupPermissionLookup,
112                $this->jobQueueGroupFactory->makeJobQueueGroup( $wikiId ),
113                $this->logger,
114                $this->tempUserConfig,
115                $this->clearCacheCallbacks,
116                $wikiId
117            );
118        }
119        return $this->instances[$key];
120    }
121}