|
MediaWiki
REL1_23
|
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, $wgMasterWaitTimeout; 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 'masterWaitTimeout' => $wgMasterWaitTimeout 00271 ) ); 00272 } 00273 00278 function getMainLB( $wiki = false ) { 00279 if ( !isset( $this->mainLB ) ) { 00280 $this->mainLB = $this->newMainLB( $wiki ); 00281 $this->mainLB->parentInfo( array( 'id' => 'main' ) ); 00282 $this->chronProt->initLB( $this->mainLB ); 00283 } 00284 00285 return $this->mainLB; 00286 } 00287 00294 function newExternalLB( $cluster, $wiki = false ) { 00295 global $wgExternalServers; 00296 if ( !isset( $wgExternalServers[$cluster] ) ) { 00297 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" ); 00298 } 00299 00300 return new LoadBalancer( array( 00301 'servers' => $wgExternalServers[$cluster] 00302 ) ); 00303 } 00304 00310 function &getExternalLB( $cluster, $wiki = false ) { 00311 if ( !isset( $this->extLBs[$cluster] ) ) { 00312 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki ); 00313 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) ); 00314 $this->chronProt->initLB( $this->extLBs[$cluster] ); 00315 } 00316 00317 return $this->extLBs[$cluster]; 00318 } 00319 00328 function forEachLB( $callback, $params = array() ) { 00329 if ( isset( $this->mainLB ) ) { 00330 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) ); 00331 } 00332 foreach ( $this->extLBs as $lb ) { 00333 call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); 00334 } 00335 } 00336 00337 function shutdown() { 00338 if ( $this->mainLB ) { 00339 $this->chronProt->shutdownLB( $this->mainLB ); 00340 } 00341 foreach ( $this->extLBs as $extLB ) { 00342 $this->chronProt->shutdownLB( $extLB ); 00343 } 00344 $this->chronProt->shutdown(); 00345 $this->commitMasterChanges(); 00346 } 00347 } 00348 00355 class LBFactoryFake extends LBFactory { 00356 function __construct( $conf ) { 00357 } 00358 00359 function newMainLB( $wiki = false ) { 00360 throw new DBAccessError; 00361 } 00362 00363 function getMainLB( $wiki = false ) { 00364 throw new DBAccessError; 00365 } 00366 00367 function newExternalLB( $cluster, $wiki = false ) { 00368 throw new DBAccessError; 00369 } 00370 00371 function &getExternalLB( $cluster, $wiki = false ) { 00372 throw new DBAccessError; 00373 } 00374 00375 function forEachLB( $callback, $params = array() ) { 00376 } 00377 } 00378 00382 class DBAccessError extends MWException { 00383 function __construct() { 00384 parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " . 00385 "This is not allowed." ); 00386 } 00387 }