3 * Lock manager registration handling.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup LockManager
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Logger\LoggerFactory
;
25 use Wikimedia\Rdbms\LBFactory
;
28 * Class to handle file lock manager registration
30 * @ingroup LockManager
33 class LockManagerGroup
{
34 /** @var string domain (usually wiki ID) */
40 /** @var array Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...)) */
41 protected $managers = [];
44 * Do not call this directly. Use LockManagerGroupFactory.
46 * @param string $domain Domain (usually wiki ID)
47 * @param array[] $lockManagerConfigs In format of $wgLockManagers
48 * @param LBFactory $lbFactory
50 public function __construct( $domain, array $lockManagerConfigs, LBFactory
$lbFactory ) {
51 $this->domain
= $domain;
52 $this->lbFactory
= $lbFactory;
54 foreach ( $lockManagerConfigs as $config ) {
55 $config['domain'] = $this->domain
;
56 if ( !isset( $config['name'] ) ) {
57 throw new Exception( "Cannot register a lock manager with no name." );
59 $name = $config['name'];
60 if ( !isset( $config['class'] ) ) {
61 throw new Exception( "Cannot register lock manager `{$name}` with no class." );
63 $class = $config['class'];
64 unset( $config['class'] ); // lock manager won't need this
65 $this->managers
[$name] = [
74 * @deprecated since 1.34, use LockManagerGroupFactory
76 * @param bool|string $domain Domain (usually wiki ID). Default: false.
77 * @return LockManagerGroup
79 public static function singleton( $domain = false ) {
80 return MediaWikiServices
::getInstance()->getLockManagerGroupFactory()
81 ->getLockManagerGroup( $domain );
85 * Destroy the singleton instances
87 * @deprecated since 1.34, use resetServiceForTesting() on LockManagerGroupFactory
89 public static function destroySingletons() {
90 MediaWikiServices
::getInstance()->resetServiceForTesting( 'LockManagerGroupFactory' );
94 * Get the lock manager object with a given name
100 public function get( $name ) {
101 if ( !isset( $this->managers
[$name] ) ) {
102 throw new Exception( "No lock manager defined with the name `$name`." );
104 // Lazy-load the actual lock manager instance
105 if ( !isset( $this->managers
[$name]['instance'] ) ) {
106 $class = $this->managers
[$name]['class'];
107 $config = $this->managers
[$name]['config'];
108 if ( $class === DBLockManager
::class ) {
109 $lb = $this->lbFactory
->getMainLB( $config['domain'] );
110 $config['dbServers']['localDBMaster'] = $lb->getLazyConnectionRef(
114 $lb::CONN_TRX_AUTOCOMMIT
116 $config['srvCache'] = ObjectCache
::getLocalServerInstance( 'hash' );
118 $config['logger'] = LoggerFactory
::getInstance( 'LockManager' );
120 // XXX Looks like phan is right, we are trying to instantiate an abstract class and it
121 // throws. Did this ever work? Presumably we need to detect the right subclass? Or
122 // should we just get rid of this? It looks like it never worked since it was first
123 // introduced by 0cf832a3394 in 2016, so if no one's complained until now, clearly it
124 // can't be very useful?
125 // @phan-suppress-next-line PhanTypeInstantiateAbstract
126 $this->managers
[$name]['instance'] = new $class( $config );
129 return $this->managers
[$name]['instance'];
133 * Get the config array for a lock manager object with a given name
135 * @param string $name
139 public function config( $name ) {
140 if ( !isset( $this->managers
[$name] ) ) {
141 throw new Exception( "No lock manager defined with the name `$name`." );
143 $class = $this->managers
[$name]['class'];
145 return [ 'class' => $class ] +
$this->managers
[$name]['config'];
149 * Get the default lock manager configured for the site.
150 * Returns NullLockManager if no lock manager could be found.
152 * XXX This looks unused, should we just get rid of it?
154 * @return LockManager
156 public function getDefault() {
157 return isset( $this->managers
['default'] )
158 ?
$this->get( 'default' )
159 : new NullLockManager( [] );
163 * Get the default lock manager configured for the site
164 * or at least some other effective configured lock manager.
165 * Throws an exception if no lock manager could be found.
167 * XXX This looks unused, should we just get rid of it?
169 * @return LockManager
172 public function getAny() {
173 return isset( $this->managers
['default'] )
174 ?
$this->get( 'default' )
175 : $this->get( 'fsLockManager' );