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
;
27 * Class to handle file lock manager registration
29 * @ingroup LockManager
32 class LockManagerGroup
{
33 /** @var LockManagerGroup[] (domain => LockManagerGroup) */
34 protected static $instances = [];
36 protected $domain; // string; domain (usually wiki ID)
38 /** @var array Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...)) */
39 protected $managers = [];
42 * @param string $domain Domain (usually wiki ID)
44 protected function __construct( $domain ) {
45 $this->domain
= $domain;
49 * @param bool|string $domain Domain (usually wiki ID). Default: false.
50 * @return LockManagerGroup
52 public static function singleton( $domain = false ) {
53 if ( $domain === false ) {
54 $domain = WikiMap
::getCurrentWikiDbDomain()->getId();
57 if ( !isset( self
::$instances[$domain] ) ) {
58 self
::$instances[$domain] = new self( $domain );
59 self
::$instances[$domain]->initFromGlobals();
62 return self
::$instances[$domain];
66 * Destroy the singleton instances
68 public static function destroySingletons() {
69 self
::$instances = [];
73 * Register lock managers from the global variables
75 protected function initFromGlobals() {
76 global $wgLockManagers;
78 $this->register( $wgLockManagers );
82 * Register an array of file lock manager configurations
84 * @param array $configs
87 protected function register( array $configs ) {
88 foreach ( $configs as $config ) {
89 $config['domain'] = $this->domain
;
90 if ( !isset( $config['name'] ) ) {
91 throw new Exception( "Cannot register a lock manager with no name." );
93 $name = $config['name'];
94 if ( !isset( $config['class'] ) ) {
95 throw new Exception( "Cannot register lock manager `{$name}` with no class." );
97 $class = $config['class'];
98 unset( $config['class'] ); // lock manager won't need this
99 $this->managers
[$name] = [
108 * Get the lock manager object with a given name
110 * @param string $name
111 * @return LockManager
114 public function get( $name ) {
115 if ( !isset( $this->managers
[$name] ) ) {
116 throw new Exception( "No lock manager defined with the name `$name`." );
118 // Lazy-load the actual lock manager instance
119 if ( !isset( $this->managers
[$name]['instance'] ) ) {
120 $class = $this->managers
[$name]['class'];
121 $config = $this->managers
[$name]['config'];
122 if ( $class === DBLockManager
::class ) {
123 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
124 $lb = $lbFactory->getMainLB( $config['domain'] );
125 $config['dbServers']['localDBMaster'] = $lb->getLazyConnectionRef(
129 $lb::CONN_TRX_AUTOCOMMIT
131 $config['srvCache'] = ObjectCache
::getLocalServerInstance( 'hash' );
133 $config['logger'] = LoggerFactory
::getInstance( 'LockManager' );
135 // @phan-suppress-next-line PhanTypeInstantiateAbstract
136 $this->managers
[$name]['instance'] = new $class( $config );
139 return $this->managers
[$name]['instance'];
143 * Get the config array for a lock manager object with a given name
145 * @param string $name
149 public function config( $name ) {
150 if ( !isset( $this->managers
[$name] ) ) {
151 throw new Exception( "No lock manager defined with the name `$name`." );
153 $class = $this->managers
[$name]['class'];
155 return [ 'class' => $class ] +
$this->managers
[$name]['config'];
159 * Get the default lock manager configured for the site.
160 * Returns NullLockManager if no lock manager could be found.
162 * @return LockManager
164 public function getDefault() {
165 return isset( $this->managers
['default'] )
166 ?
$this->get( 'default' )
167 : new NullLockManager( [] );
171 * Get the default lock manager configured for the site
172 * or at least some other effective configured lock manager.
173 * Throws an exception if no lock manager could be found.
175 * @return LockManager
178 public function getAny() {
179 return isset( $this->managers
['default'] )
180 ?
$this->get( 'default' )
181 : $this->get( 'fsLockManager' );