MediaWiki REL1_32
LockManagerGroup.php
Go to the documentation of this file.
1<?php
25
34 protected static $instances = [];
35
36 protected $domain; // string; domain (usually wiki ID)
37
39 protected $managers = [];
40
44 protected function __construct( $domain ) {
45 $this->domain = $domain;
46 }
47
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
65 public static function destroySingletons() {
66 self::$instances = [];
67 }
68
72 protected function initFromGlobals() {
73 global $wgLockManagers;
74
75 $this->register( $wgLockManagers );
76 }
77
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
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::class ) {
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 $config['logger'] = LoggerFactory::getInstance( 'LockManager' );
128
129 $this->managers[$name]['instance'] = new $class( $config );
130 }
131
132 return $this->managers[$name]['instance'];
133 }
134
142 public function config( $name ) {
143 if ( !isset( $this->managers[$name] ) ) {
144 throw new Exception( "No lock manager defined with the name `$name`." );
145 }
146 $class = $this->managers[$name]['class'];
147
148 return [ 'class' => $class ] + $this->managers[$name]['config'];
149 }
150
157 public function getDefault() {
158 return isset( $this->managers['default'] )
159 ? $this->get( 'default' )
160 : new NullLockManager( [] );
161 }
162
171 public function getAny() {
172 return isset( $this->managers['default'] )
173 ? $this->get( 'default' )
174 : $this->get( 'fsLockManager' );
175 }
176}
$wgLockManagers
Array of configuration arrays for each lock manager.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Class to handle file lock manager registration.
getDefault()
Get the default lock manager configured for the site.
config( $name)
Get the config array for a lock manager object with a given name.
static LockManagerGroup[] $instances
(domain => LockManagerGroup)
initFromGlobals()
Register lock managers from the global variables.
getAny()
Get the default lock manager configured for the site or at least some other effective configured lock...
static singleton( $domain=false)
static destroySingletons()
Destroy the singleton instances.
array $managers
Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...))
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Simple version of LockManager that does nothing.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
const DB_MASTER
Definition defines.php:26