Merge "Inject "srvCache" and local DB connections into LockManagerDB"
[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
25 /**
26 * Class to handle file lock manager registration
27 *
28 * @ingroup LockManager
29 * @author Aaron Schulz
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 $domain = ( $domain === false ) ? wfWikiID() : $domain;
54 if ( !isset( self::$instances[$domain] ) ) {
55 self::$instances[$domain] = new self( $domain );
56 self::$instances[$domain]->initFromGlobals();
57 }
58
59 return self::$instances[$domain];
60 }
61
62 /**
63 * Destroy the singleton instances
64 */
65 public static function destroySingletons() {
66 self::$instances = [];
67 }
68
69 /**
70 * Register lock managers from the global variables
71 */
72 protected function initFromGlobals() {
73 global $wgLockManagers;
74
75 $this->register( $wgLockManagers );
76 }
77
78 /**
79 * Register an array of file lock manager configurations
80 *
81 * @param array $configs
82 * @throws Exception
83 */
84 protected function register( array $configs ) {
85 foreach ( $configs as $config ) {
86 $config['domain'] = $this->domain;
87 if ( !isset( $config['name'] ) ) {
88 throw new Exception( "Cannot register a lock manager with no name." );
89 }
90 $name = $config['name'];
91 if ( !isset( $config['class'] ) ) {
92 throw new Exception( "Cannot register lock manager `{$name}` with no class." );
93 }
94 $class = $config['class'];
95 unset( $config['class'] ); // lock manager won't need this
96 $this->managers[$name] = [
97 'class' => $class,
98 'config' => $config,
99 'instance' => null
100 ];
101 }
102 }
103
104 /**
105 * Get the lock manager object with a given name
106 *
107 * @param string $name
108 * @return LockManager
109 * @throws Exception
110 */
111 public function get( $name ) {
112 if ( !isset( $this->managers[$name] ) ) {
113 throw new Exception( "No lock manager defined with the name `$name`." );
114 }
115 // Lazy-load the actual lock manager instance
116 if ( !isset( $this->managers[$name]['instance'] ) ) {
117 $class = $this->managers[$name]['class'];
118 $config = $this->managers[$name]['config'];
119 if ( $class === 'DBLockManager' ) {
120 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
121 $lb = $lbFactory->newMainLB( $config['domain'] );
122 $dbw = $lb->getLazyConnectionRef( DB_MASTER, [], $config['domain'] );
123
124 $config['dbServers']['localDBMaster'] = $dbw;
125 $config['srvCache'] = ObjectCache::getLocalServerInstance( 'hash' );
126 }
127 $this->managers[$name]['instance'] = new $class( $config );
128 }
129
130 return $this->managers[$name]['instance'];
131 }
132
133 /**
134 * Get the config array for a lock manager object with a given name
135 *
136 * @param string $name
137 * @return array
138 * @throws Exception
139 */
140 public function config( $name ) {
141 if ( !isset( $this->managers[$name] ) ) {
142 throw new Exception( "No lock manager defined with the name `$name`." );
143 }
144 $class = $this->managers[$name]['class'];
145
146 return [ 'class' => $class ] + $this->managers[$name]['config'];
147 }
148
149 /**
150 * Get the default lock manager configured for the site.
151 * Returns NullLockManager if no lock manager could be found.
152 *
153 * @return LockManager
154 */
155 public function getDefault() {
156 return isset( $this->managers['default'] )
157 ? $this->get( 'default' )
158 : new NullLockManager( [] );
159 }
160
161 /**
162 * Get the default lock manager configured for the site
163 * or at least some other effective configured lock manager.
164 * Throws an exception if no lock manager could be found.
165 *
166 * @return LockManager
167 * @throws Exception
168 */
169 public function getAny() {
170 return isset( $this->managers['default'] )
171 ? $this->get( 'default' )
172 : $this->get( 'fsLockManager' );
173 }
174 }