MediaWiki  1.23.1
LBFactory.php
Go to the documentation of this file.
1 <?php
28 abstract class LBFactory {
30  protected static $instance;
31 
36  public static function disableBackend() {
37  global $wgLBFactoryConf;
38  self::$instance = new LBFactoryFake( $wgLBFactoryConf );
39  }
40 
46  static function &singleton() {
47  global $wgLBFactoryConf;
48 
49  if ( is_null( self::$instance ) ) {
50  $class = self::getLBFactoryClass( $wgLBFactoryConf );
51 
52  self::$instance = new $class( $wgLBFactoryConf );
53  }
54 
55  return self::$instance;
56  }
57 
64  public static function getLBFactoryClass( array $config ) {
65  // For configuration backward compatibility after removing
66  // underscores from class names in MediaWiki 1.23.
67  $bcClasses = array(
68  'LBFactory_Simple' => 'LBFactorySimple',
69  'LBFactory_Single' => 'LBFactorySingle',
70  'LBFactory_Multi' => 'LBFactoryMulti',
71  'LBFactory_Fake' => 'LBFactoryFake',
72  );
73 
74  $class = $config['class'];
75 
76  if ( isset( $bcClasses[$class] ) ) {
77  $class = $bcClasses[$class];
79  '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
80  '1.23'
81  );
82  }
83 
84  return $class;
85  }
86 
90  static function destroyInstance() {
91  if ( self::$instance ) {
92  self::$instance->shutdown();
93  self::$instance->forEachLBCallMethod( 'closeAll' );
94  self::$instance = null;
95  }
96  }
97 
103  static function setInstance( $instance ) {
105  self::$instance = $instance;
106  }
107 
112  abstract function __construct( $conf );
113 
121  abstract function newMainLB( $wiki = false );
122 
129  abstract function getMainLB( $wiki = false );
130 
140  abstract function newExternalLB( $cluster, $wiki = false );
141 
149  abstract function &getExternalLB( $cluster, $wiki = false );
150 
159  abstract function forEachLB( $callback, $params = array() );
160 
165  function shutdown() {
166  }
167 
174  function forEachLBCallMethod( $methodName, $args = array() ) {
175  $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
176  }
177 
184  function callMethod( $loadBalancer, $methodName, $args ) {
185  call_user_func_array( array( $loadBalancer, $methodName ), $args );
186  }
187 
191  function commitMasterChanges() {
192  $this->forEachLBCallMethod( 'commitMasterChanges' );
193  }
194 
200  $this->forEachLBCallMethod( 'rollbackMasterChanges' );
201  }
202 
208  function hasMasterChanges() {
209  $ret = false;
210  $this->forEachLB( function ( $lb ) use ( &$ret ) {
211  $ret = $ret || $lb->hasMasterChanges();
212  } );
213  return $ret;
214  }
215 }
216 
220 class LBFactorySimple extends LBFactory {
222  protected $mainLB;
223 
225  protected $extLBs = array();
226 
228  protected $chronProt;
229 
230  function __construct( $conf ) {
231  $this->chronProt = new ChronologyProtector;
232  }
233 
238  function newMainLB( $wiki = false ) {
239  global $wgDBservers, $wgMasterWaitTimeout;
240  if ( $wgDBservers ) {
241  $servers = $wgDBservers;
242  } else {
243  global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
244  global $wgDBssl, $wgDBcompress;
245 
247  if ( $wgDebugDumpSql ) {
248  $flags |= DBO_DEBUG;
249  }
250  if ( $wgDBssl ) {
251  $flags |= DBO_SSL;
252  }
253  if ( $wgDBcompress ) {
254  $flags |= DBO_COMPRESS;
255  }
256 
257  $servers = array( array(
258  'host' => $wgDBserver,
259  'user' => $wgDBuser,
260  'password' => $wgDBpassword,
261  'dbname' => $wgDBname,
262  'type' => $wgDBtype,
263  'load' => 1,
264  'flags' => $flags
265  ) );
266  }
267 
268  return new LoadBalancer( array(
269  'servers' => $servers,
270  'masterWaitTimeout' => $wgMasterWaitTimeout
271  ) );
272  }
273 
278  function getMainLB( $wiki = false ) {
279  if ( !isset( $this->mainLB ) ) {
280  $this->mainLB = $this->newMainLB( $wiki );
281  $this->mainLB->parentInfo( array( 'id' => 'main' ) );
282  $this->chronProt->initLB( $this->mainLB );
283  }
284 
285  return $this->mainLB;
286  }
287 
294  function newExternalLB( $cluster, $wiki = false ) {
295  global $wgExternalServers;
296  if ( !isset( $wgExternalServers[$cluster] ) ) {
297  throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
298  }
299 
300  return new LoadBalancer( array(
301  'servers' => $wgExternalServers[$cluster]
302  ) );
303  }
304 
310  function &getExternalLB( $cluster, $wiki = false ) {
311  if ( !isset( $this->extLBs[$cluster] ) ) {
312  $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
313  $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
314  $this->chronProt->initLB( $this->extLBs[$cluster] );
315  }
316 
317  return $this->extLBs[$cluster];
318  }
319 
328  function forEachLB( $callback, $params = array() ) {
329  if ( isset( $this->mainLB ) ) {
330  call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
331  }
332  foreach ( $this->extLBs as $lb ) {
333  call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
334  }
335  }
336 
337  function shutdown() {
338  if ( $this->mainLB ) {
339  $this->chronProt->shutdownLB( $this->mainLB );
340  }
341  foreach ( $this->extLBs as $extLB ) {
342  $this->chronProt->shutdownLB( $extLB );
343  }
344  $this->chronProt->shutdown();
345  $this->commitMasterChanges();
346  }
347 }
348 
355 class LBFactoryFake extends LBFactory {
356  function __construct( $conf ) {
357  }
358 
359  function newMainLB( $wiki = false ) {
360  throw new DBAccessError;
361  }
362 
363  function getMainLB( $wiki = false ) {
364  throw new DBAccessError;
365  }
366 
367  function newExternalLB( $cluster, $wiki = false ) {
368  throw new DBAccessError;
369  }
370 
371  function &getExternalLB( $cluster, $wiki = false ) {
372  throw new DBAccessError;
373  }
374 
375  function forEachLB( $callback, $params = array() ) {
376  }
377 }
378 
382 class DBAccessError extends MWException {
383  function __construct() {
384  parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
385  "This is not allowed." );
386  }
387 }
LBFactoryFake\getExternalLB
& getExternalLB( $cluster, $wiki=false)
Get a cached (tracked) load balancer for external storage.
Definition: LBFactory.php:368
LBFactory\disableBackend
static disableBackend()
Disables all access to the load balancer, will cause all database access to throw a DBAccessError.
Definition: LBFactory.php:36
LBFactory\hasMasterChanges
hasMasterChanges()
Detemine if any master connection has pending changes.
Definition: LBFactory.php:208
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
LBFactory\singleton
static & singleton()
Get an LBFactory instance.
Definition: LBFactory.php:46
LBFactory\__construct
__construct( $conf)
Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1530
$params
$params
Definition: styleTest.css.php:40
LBFactorySimple\__construct
__construct( $conf)
Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
Definition: LBFactory.php:227
ChronologyProtector
Class for ensuring a consistent ordering of events as seen by the user, despite replication.
Definition: ChronologyProtector.php:28
LBFactory\newExternalLB
newExternalLB( $cluster, $wiki=false)
Create a new load balancer for external storage.
LBFactory
An interface for generating database load balancers.
Definition: LBFactory.php:28
DBAccessError\__construct
__construct()
Definition: LBFactory.php:380
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2113
LBFactorySimple\forEachLB
forEachLB( $callback, $params=array())
Execute a function for each tracked load balancer The callback is called with the load balancer as th...
Definition: LBFactory.php:325
LBFactory\rollbackMasterChanges
rollbackMasterChanges()
Rollback changes on all master connections.
Definition: LBFactory.php:199
DBO_COMPRESS
const DBO_COMPRESS
Definition: Defines.php:48
LBFactory\getMainLB
getMainLB( $wiki=false)
Get a cached (tracked) load balancer object.
$lb
if( $wgAPIRequestLog) $lb
Definition: api.php:126
MWException
MediaWiki exception.
Definition: MWException.php:26
LBFactorySimple\$mainLB
LoadBalancer $mainLB
Definition: LBFactory.php:221
$wgDBname
controlled by $wgMainCacheType controlled by $wgParserCacheType controlled by $wgMessageCacheType If you set CACHE_NONE to one of the three control default value for MediaWiki still create a but requests to it are no ops and we always fall through to the database If the cache daemon can t be it should also disable itself fairly smoothly By $wgMemc is used but when it is $parserMemc or $messageMemc this is mentioned $wgDBname
Definition: memcached.txt:96
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1127
LBFactoryFake\newExternalLB
newExternalLB( $cluster, $wiki=false)
Create a new load balancer for external storage.
Definition: LBFactory.php:364
LBFactorySimple\getMainLB
getMainLB( $wiki=false)
Definition: LBFactory.php:275
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
LBFactoryFake\getMainLB
getMainLB( $wiki=false)
Get a cached (tracked) load balancer object.
Definition: LBFactory.php:360
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DBO_SSL
const DBO_SSL
Definition: Defines.php:47
LoadBalancer
Database load balancing object.
Definition: LoadBalancer.php:30
LBFactorySimple\newMainLB
newMainLB( $wiki=false)
Definition: LBFactory.php:235
DBAccessError
Exception class for attempted DB access.
Definition: LBFactory.php:379
LBFactory\setInstance
static setInstance( $instance)
Set the instance to be the given object.
Definition: LBFactory.php:103
LBFactory\forEachLB
forEachLB( $callback, $params=array())
Execute a function for each tracked load balancer The callback is called with the load balancer as th...
LBFactorySimple\newExternalLB
newExternalLB( $cluster, $wiki=false)
Definition: LBFactory.php:291
LBFactory\commitMasterChanges
commitMasterChanges()
Commit changes on all master connections.
Definition: LBFactory.php:191
LBFactoryFake\__construct
__construct( $conf)
Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
Definition: LBFactory.php:353
LBFactorySimple\$chronProt
ChronologyProtector $chronProt
Definition: LBFactory.php:225
LBFactory\getExternalLB
& getExternalLB( $cluster, $wiki=false)
Get a cached (tracked) load balancer for external storage.
LBFactoryFake\forEachLB
forEachLB( $callback, $params=array())
Execute a function for each tracked load balancer The callback is called with the load balancer as th...
Definition: LBFactory.php:372
LBFactory\shutdown
shutdown()
Prepare all tracked load balancers for shutdown STUB.
Definition: LBFactory.php:165
LBFactorySimple\getExternalLB
& getExternalLB( $cluster, $wiki=false)
Definition: LBFactory.php:307
LBFactory\getLBFactoryClass
static getLBFactoryClass(array $config)
Returns the LBFactory class to use and the load balancer configuration.
Definition: LBFactory.php:64
LBFactorySimple\shutdown
shutdown()
Prepare all tracked load balancers for shutdown STUB.
Definition: LBFactory.php:334
DBO_DEFAULT
const DBO_DEFAULT
Definition: Defines.php:43
$args
if( $line===false) $args
Definition: cdb.php:62
LBFactorySimple\$extLBs
LoadBalancer[] $extLBs
Definition: LBFactory.php:223
LBFactory\newMainLB
newMainLB( $wiki=false)
Create a new load balancer object.
LBFactory\callMethod
callMethod( $loadBalancer, $methodName, $args)
Private helper for forEachLBCallMethod.
Definition: LBFactory.php:184
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
LBFactoryFake\newMainLB
newMainLB( $wiki=false)
Create a new load balancer object.
Definition: LBFactory.php:356
LBFactory\forEachLBCallMethod
forEachLBCallMethod( $methodName, $args=array())
Call a method of each tracked load balancer.
Definition: LBFactory.php:174
LBFactorySimple
A simple single-master LBFactory that gets its configuration from the b/c globals.
Definition: LBFactory.php:220
DBO_DEBUG
const DBO_DEBUG
Definition: Defines.php:39
LBFactory\$instance
static $instance
Definition: LBFactory.php:30
LBFactory\destroyInstance
static destroyInstance()
Shut down, close connections and destroy the cached instance.
Definition: LBFactory.php:90
LBFactoryFake
LBFactory class that throws an error on any attempt to use it.
Definition: LBFactory.php:352