LockManagerGroup.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * Class to handle file lock manager registration
  27. *
  28. * @ingroup LockManager
  29. * @since 1.19
  30. */
  31. class LockManagerGroup {
  32. /** @var LockManagerGroup[] (domain => LockManagerGroup) */
  33. protected static $instances = [];
  34. protected $domain; // string; domain (usually wiki ID)
  35. /** @var array Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...)) */
  36. protected $managers = [];
  37. /**
  38. * @param string $domain Domain (usually wiki ID)
  39. */
  40. protected function __construct( $domain ) {
  41. $this->domain = $domain;
  42. }
  43. /**
  44. * @param bool|string $domain Domain (usually wiki ID). Default: false.
  45. * @return LockManagerGroup
  46. */
  47. public static function singleton( $domain = false ) {
  48. $domain = ( $domain === false ) ? wfWikiID() : $domain;
  49. if ( !isset( self::$instances[$domain] ) ) {
  50. self::$instances[$domain] = new self( $domain );
  51. self::$instances[$domain]->initFromGlobals();
  52. }
  53. return self::$instances[$domain];
  54. }
  55. /**
  56. * Destroy the singleton instances
  57. */
  58. public static function destroySingletons() {
  59. self::$instances = [];
  60. }
  61. /**
  62. * Register lock managers from the global variables
  63. */
  64. protected function initFromGlobals() {
  65. global $wgLockManagers;
  66. $this->register( $wgLockManagers );
  67. }
  68. /**
  69. * Register an array of file lock manager configurations
  70. *
  71. * @param array $configs
  72. * @throws Exception
  73. */
  74. protected function register( array $configs ) {
  75. foreach ( $configs as $config ) {
  76. $config['domain'] = $this->domain;
  77. if ( !isset( $config['name'] ) ) {
  78. throw new Exception( "Cannot register a lock manager with no name." );
  79. }
  80. $name = $config['name'];
  81. if ( !isset( $config['class'] ) ) {
  82. throw new Exception( "Cannot register lock manager `{$name}` with no class." );
  83. }
  84. $class = $config['class'];
  85. unset( $config['class'] ); // lock manager won't need this
  86. $this->managers[$name] = [
  87. 'class' => $class,
  88. 'config' => $config,
  89. 'instance' => null
  90. ];
  91. }
  92. }
  93. /**
  94. * Get the lock manager object with a given name
  95. *
  96. * @param string $name
  97. * @return LockManager
  98. * @throws Exception
  99. */
  100. public function get( $name ) {
  101. if ( !isset( $this->managers[$name] ) ) {
  102. throw new Exception( "No lock manager defined with the name `$name`." );
  103. }
  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. $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
  110. $lb = $lbFactory->newMainLB( $config['domain'] );
  111. $dbw = $lb->getLazyConnectionRef( DB_MASTER, [], $config['domain'] );
  112. $config['dbServers']['localDBMaster'] = $dbw;
  113. $config['srvCache'] = ObjectCache::getLocalServerInstance( 'hash' );
  114. }
  115. $config['logger'] = LoggerFactory::getInstance( 'LockManager' );
  116. $this->managers[$name]['instance'] = new $class( $config );
  117. }
  118. return $this->managers[$name]['instance'];
  119. }
  120. /**
  121. * Get the config array for a lock manager object with a given name
  122. *
  123. * @param string $name
  124. * @return array
  125. * @throws Exception
  126. */
  127. public function config( $name ) {
  128. if ( !isset( $this->managers[$name] ) ) {
  129. throw new Exception( "No lock manager defined with the name `$name`." );
  130. }
  131. $class = $this->managers[$name]['class'];
  132. return [ 'class' => $class ] + $this->managers[$name]['config'];
  133. }
  134. /**
  135. * Get the default lock manager configured for the site.
  136. * Returns NullLockManager if no lock manager could be found.
  137. *
  138. * @return LockManager
  139. */
  140. public function getDefault() {
  141. return isset( $this->managers['default'] )
  142. ? $this->get( 'default' )
  143. : new NullLockManager( [] );
  144. }
  145. /**
  146. * Get the default lock manager configured for the site
  147. * or at least some other effective configured lock manager.
  148. * Throws an exception if no lock manager could be found.
  149. *
  150. * @return LockManager
  151. * @throws Exception
  152. */
  153. public function getAny() {
  154. return isset( $this->managers['default'] )
  155. ? $this->get( 'default' )
  156. : $this->get( 'fsLockManager' );
  157. }
  158. }