diff options
Diffstat (limited to 'includes/db/LBFactory.php')
-rw-r--r-- | includes/db/LBFactory.php | 76 |
1 files changed, 68 insertions, 8 deletions
diff --git a/includes/db/LBFactory.php b/includes/db/LBFactory.php index 10c87133..f84a70e5 100644 --- a/includes/db/LBFactory.php +++ b/includes/db/LBFactory.php @@ -1,5 +1,7 @@ <?php /** + * Generator of database load balancing objects + * * @file * @ingroup Database */ @@ -12,6 +14,23 @@ abstract class LBFactory { static $instance; /** + * Disables all access to the load balancer, will cause all database access + * to throw a DBAccessError + */ + public static function disableBackend() { + global $wgLBFactoryConf; + self::$instance = new LBFactory_Fake( $wgLBFactoryConf ); + } + + /** + * Resets the singleton for use if it's been disabled. Does nothing otherwise + */ + public static function enableBackend() { + if( self::$instance instanceof LBFactory_Fake ) + self::$instance = null; + } + + /** * Get an LBFactory instance */ static function &singleton() { @@ -36,6 +55,14 @@ abstract class LBFactory { } /** + * Set the instance to be the given object + */ + static function setInstance( $instance ) { + self::destroyInstance(); + self::$instance = $instance; + } + + /** * Construct a factory based on a configuration array (typically from $wgLBFactoryConf) */ abstract function __construct( $conf ); @@ -44,7 +71,7 @@ abstract class LBFactory { * Create a new load balancer object. The resulting object will be untracked, * not chronology-protected, and the caller is responsible for cleaning it up. * - * @param string $wiki Wiki ID, or false for the current wiki + * @param $wiki String: wiki ID, or false for the current wiki * @return LoadBalancer */ abstract function newMainLB( $wiki = false ); @@ -52,7 +79,7 @@ abstract class LBFactory { /** * Get a cached (tracked) load balancer object. * - * @param string $wiki Wiki ID, or false for the current wiki + * @param $wiki String: wiki ID, or false for the current wiki * @return LoadBalancer */ abstract function getMainLB( $wiki = false ); @@ -62,16 +89,16 @@ abstract class LBFactory { * untracked, not chronology-protected, and the caller is responsible for * cleaning it up. * - * @param string $cluster External storage cluster, or false for core - * @param string $wiki Wiki ID, or false for the current wiki + * @param $cluster String: external storage cluster, or false for core + * @param $wiki String: wiki ID, or false for the current wiki */ abstract function newExternalLB( $cluster, $wiki = false ); /* * Get a cached (tracked) load balancer for external storage * - * @param string $cluster External storage cluster, or false for core - * @param string $wiki Wiki ID, or false for the current wiki + * @param $cluster String: external storage cluster, or false for core + * @param $wiki String: wiki ID, or false for the current wiki */ abstract function &getExternalLB( $cluster, $wiki = false ); @@ -198,6 +225,39 @@ class LBFactory_Simple extends LBFactory { } /** + * LBFactory class that throws an error on any attempt to use it. + * This will typically be done via wfGetDB(). + * Call LBFactory::disableBackend() to start using this, and + * LBFactory::enableBackend() to return to normal behavior + */ +class LBFactory_Fake extends LBFactory { + function __construct( $conf ) {} + + function newMainLB( $wiki = false) { + throw new DBAccessError; + } + function getMainLB( $wiki = false ) { + throw new DBAccessError; + } + function newExternalLB( $cluster, $wiki = false ) { + throw new DBAccessError; + } + function &getExternalLB( $cluster, $wiki = false ) { + throw new DBAccessError; + } + function forEachLB( $callback, $params = array() ) {} +} + +/** + * Exception class for attempted DB access + */ +class DBAccessError extends MWException { + function __construct() { + parent::__construct( "Mediawiki tried to access the database via wfGetDB(). This is not allowed." ); + } +} + +/** * Class for ensuring a consistent ordering of events as seen by the user, despite replication. * Kind of like Hawking's [[Chronology Protection Agency]]. */ @@ -208,7 +268,7 @@ class ChronologyProtector { /** * Initialise a LoadBalancer to give it appropriate chronology protection. * - * @param LoadBalancer $lb + * @param $lb LoadBalancer */ function initLB( $lb ) { if ( $this->startupPos === null ) { @@ -233,7 +293,7 @@ class ChronologyProtector { * Notify the ChronologyProtector that the LoadBalancer is about to shut * down. Saves replication positions. * - * @param LoadBalancer $lb + * @param $lb LoadBalancer */ function shutdownLB( $lb ) { // Don't start a session, don't bother with non-replicated setups |