Add isCurrentWikiId()/isCurrentWikiDomain()/getCurrentWikiDomain() to WikiMap
[lhc/web/wiklou.git] / includes / filebackend / lockmanager / LockManagerGroup.php
1 <?php
2 /**
3 * Lock manager registration handling.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup LockManager
22 */
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\Logger\LoggerFactory;
25
26 /**
27 * Class to handle file lock manager registration
28 *
29 * @ingroup LockManager
30 * @since 1.19
31 */
32 class LockManagerGroup {
33 /** @var LockManagerGroup[] (domain => LockManagerGroup) */
34 protected static $instances = [];
35
36 protected $domain; // string; domain (usually wiki ID)
37
38 /** @var array Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...)) */
39 protected $managers = [];
40
41 /**
42 * @param string $domain Domain (usually wiki ID)
43 */
44 protected function __construct( $domain ) {
45 $this->domain = $domain;
46 }
47
48 /**
49 * @param bool|string $domain Domain (usually wiki ID). Default: false.
50 * @return LockManagerGroup
51 */
52 public static function singleton( $domain = false ) {
53 if ( $domain === false ) {
54 $domain = WikiMap::getCurrentWikiDomain()->getId();
55 }
56
57 if ( !isset( self::$instances[$domain] ) ) {
58 self::$instances[$domain] = new self( $domain );
59 self::$instances[$domain]->initFromGlobals();
60 }
61
62 return self::$instances[$domain];
63 }
64
65 /**
66 * Destroy the singleton instances
67 */
68 public static function destroySingletons() {
69 self::$instances = [];
70 }
71
72 /**
73 * Register lock managers from the global variables
74 */
75 protected function initFromGlobals() {
76 global $wgLockManagers;
77
78 $this->register( $wgLockManagers );
79 }
80
81 /**
82 * Register an array of file lock manager configurations
83 *
84 * @param array $configs
85 * @throws Exception
86 */
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." );
92 }
93 $name = $config['name'];
94 if ( !isset( $config['class'] ) ) {
95 throw new Exception( "Cannot register lock manager `{$name}` with no class." );
96 }
97 $class = $config['class'];
98 unset( $config['class'] ); // lock manager won't need this
99 $this->managers[$name] = [
100 'class' => $class,
101 'config' => $config,
102 'instance' => null
103 ];
104 }
105 }
106
107 /**
108 * Get the lock manager object with a given name
109 *
110 * @param string $name
111 * @return LockManager
112 * @throws Exception
113 */
114 public function get( $name ) {
115 if ( !isset( $this->managers[$name] ) ) {
116 throw new Exception( "No lock manager defined with the name `$name`." );
117 }
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->newMainLB( $config['domain'] );
125 $dbw = $lb->getLazyConnectionRef( DB_MASTER, [], $config['domain'] );
126
127 $config['dbServers']['localDBMaster'] = $dbw;
128 $config['srvCache'] = ObjectCache::getLocalServerInstance( 'hash' );
129 }
130 $config['logger'] = LoggerFactory::getInstance( 'LockManager' );
131
132 $this->managers[$name]['instance'] = new $class( $config );
133 }
134
135 return $this->managers[$name]['instance'];
136 }
137
138 /**
139 * Get the config array for a lock manager object with a given name
140 *
141 * @param string $name
142 * @return array
143 * @throws Exception
144 */
145 public function config( $name ) {
146 if ( !isset( $this->managers[$name] ) ) {
147 throw new Exception( "No lock manager defined with the name `$name`." );
148 }
149 $class = $this->managers[$name]['class'];
150
151 return [ 'class' => $class ] + $this->managers[$name]['config'];
152 }
153
154 /**
155 * Get the default lock manager configured for the site.
156 * Returns NullLockManager if no lock manager could be found.
157 *
158 * @return LockManager
159 */
160 public function getDefault() {
161 return isset( $this->managers['default'] )
162 ? $this->get( 'default' )
163 : new NullLockManager( [] );
164 }
165
166 /**
167 * Get the default lock manager configured for the site
168 * or at least some other effective configured lock manager.
169 * Throws an exception if no lock manager could be found.
170 *
171 * @return LockManager
172 * @throws Exception
173 */
174 public function getAny() {
175 return isset( $this->managers['default'] )
176 ? $this->get( 'default' )
177 : $this->get( 'fsLockManager' );
178 }
179 }