MediaWiki
REL1_24
|
00001 <?php 00028 abstract class LBFactory { 00030 protected static $instance; 00031 00036 public static function disableBackend() { 00037 global $wgLBFactoryConf; 00038 self::$instance = new LBFactoryFake( $wgLBFactoryConf ); 00039 } 00040 00046 static function &singleton() { 00047 global $wgLBFactoryConf; 00048 00049 if ( is_null( self::$instance ) ) { 00050 $class = self::getLBFactoryClass( $wgLBFactoryConf ); 00051 00052 self::$instance = new $class( $wgLBFactoryConf ); 00053 } 00054 00055 return self::$instance; 00056 } 00057 00064 public static function getLBFactoryClass( array $config ) { 00065 // For configuration backward compatibility after removing 00066 // underscores from class names in MediaWiki 1.23. 00067 $bcClasses = array( 00068 'LBFactory_Simple' => 'LBFactorySimple', 00069 'LBFactory_Single' => 'LBFactorySingle', 00070 'LBFactory_Multi' => 'LBFactoryMulti', 00071 'LBFactory_Fake' => 'LBFactoryFake', 00072 ); 00073 00074 $class = $config['class']; 00075 00076 if ( isset( $bcClasses[$class] ) ) { 00077 $class = $bcClasses[$class]; 00078 wfDeprecated( 00079 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details', 00080 '1.23' 00081 ); 00082 } 00083 00084 return $class; 00085 } 00086 00090 static function destroyInstance() { 00091 if ( self::$instance ) { 00092 self::$instance->shutdown(); 00093 self::$instance->forEachLBCallMethod( 'closeAll' ); 00094 self::$instance = null; 00095 } 00096 } 00097 00103 static function setInstance( $instance ) { 00104 self::destroyInstance(); 00105 self::$instance = $instance; 00106 } 00107 00112 abstract function __construct( $conf ); 00113 00121 abstract function newMainLB( $wiki = false ); 00122 00129 abstract function getMainLB( $wiki = false ); 00130 00140 abstract function newExternalLB( $cluster, $wiki = false ); 00141 00149 abstract function &getExternalLB( $cluster, $wiki = false ); 00150 00159 abstract function forEachLB( $callback, $params = array() ); 00160 00165 function shutdown() { 00166 } 00167 00174 function forEachLBCallMethod( $methodName, $args = array() ) { 00175 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) ); 00176 } 00177 00184 function callMethod( $loadBalancer, $methodName, $args ) { 00185 call_user_func_array( array( $loadBalancer, $methodName ), $args ); 00186 } 00187 00191 function commitMasterChanges() { 00192 $this->forEachLBCallMethod( 'commitMasterChanges' ); 00193 } 00194 00199 function rollbackMasterChanges() { 00200 $this->forEachLBCallMethod( 'rollbackMasterChanges' ); 00201 } 00202 00208 function hasMasterChanges() { 00209 $ret = false; 00210 $this->forEachLB( function ( $lb ) use ( &$ret ) { 00211 $ret = $ret || $lb->hasMasterChanges(); 00212 } ); 00213 return $ret; 00214 } 00215 } 00216 00220 class LBFactorySimple extends LBFactory { 00222 protected $mainLB; 00223 00225 protected $extLBs = array(); 00226 00228 protected $chronProt; 00229 00230 function __construct( $conf ) { 00231 $this->chronProt = new ChronologyProtector; 00232 } 00233 00238 function newMainLB( $wiki = false ) { 00239 global $wgDBservers; 00240 if ( $wgDBservers ) { 00241 $servers = $wgDBservers; 00242 } else { 00243 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql; 00244 global $wgDBssl, $wgDBcompress; 00245 00246 $flags = DBO_DEFAULT; 00247 if ( $wgDebugDumpSql ) { 00248 $flags |= DBO_DEBUG; 00249 } 00250 if ( $wgDBssl ) { 00251 $flags |= DBO_SSL; 00252 } 00253 if ( $wgDBcompress ) { 00254 $flags |= DBO_COMPRESS; 00255 } 00256 00257 $servers = array( array( 00258 'host' => $wgDBserver, 00259 'user' => $wgDBuser, 00260 'password' => $wgDBpassword, 00261 'dbname' => $wgDBname, 00262 'type' => $wgDBtype, 00263 'load' => 1, 00264 'flags' => $flags 00265 ) ); 00266 } 00267 00268 return new LoadBalancer( array( 00269 'servers' => $servers, 00270 ) ); 00271 } 00272 00277 function getMainLB( $wiki = false ) { 00278 if ( !isset( $this->mainLB ) ) { 00279 $this->mainLB = $this->newMainLB( $wiki ); 00280 $this->mainLB->parentInfo( array( 'id' => 'main' ) ); 00281 $this->chronProt->initLB( $this->mainLB ); 00282 } 00283 00284 return $this->mainLB; 00285 } 00286 00293 function newExternalLB( $cluster, $wiki = false ) { 00294 global $wgExternalServers; 00295 if ( !isset( $wgExternalServers[$cluster] ) ) { 00296 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" ); 00297 } 00298 00299 return new LoadBalancer( array( 00300 'servers' => $wgExternalServers[$cluster] 00301 ) ); 00302 } 00303 00309 function &getExternalLB( $cluster, $wiki = false ) { 00310 if ( !isset( $this->extLBs[$cluster] ) ) { 00311 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki ); 00312 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) ); 00313 $this->chronProt->initLB( $this->extLBs[$cluster] ); 00314 } 00315 00316 return $this->extLBs[$cluster]; 00317 } 00318 00327 function forEachLB( $callback, $params = array() ) { 00328 if ( isset( $this->mainLB ) ) { 00329 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) ); 00330 } 00331 foreach ( $this->extLBs as $lb ) { 00332 call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); 00333 } 00334 } 00335 00336 function shutdown() { 00337 if ( $this->mainLB ) { 00338 $this->chronProt->shutdownLB( $this->mainLB ); 00339 } 00340 foreach ( $this->extLBs as $extLB ) { 00341 $this->chronProt->shutdownLB( $extLB ); 00342 } 00343 $this->chronProt->shutdown(); 00344 $this->commitMasterChanges(); 00345 } 00346 } 00347 00354 class LBFactoryFake extends LBFactory { 00355 function __construct( $conf ) { 00356 } 00357 00358 function newMainLB( $wiki = false ) { 00359 throw new DBAccessError; 00360 } 00361 00362 function getMainLB( $wiki = false ) { 00363 throw new DBAccessError; 00364 } 00365 00366 function newExternalLB( $cluster, $wiki = false ) { 00367 throw new DBAccessError; 00368 } 00369 00370 function &getExternalLB( $cluster, $wiki = false ) { 00371 throw new DBAccessError; 00372 } 00373 00374 function forEachLB( $callback, $params = array() ) { 00375 } 00376 } 00377 00381 class DBAccessError extends MWException { 00382 function __construct() { 00383 parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " . 00384 "This is not allowed." ); 00385 } 00386 }