Merge "Purge Squid variant pages based on page language (not $wgContLang)"
[lhc/web/wiklou.git] / includes / db / LBFactory.php
index 10c8713..aaca12c 100644 (file)
@@ -1,5 +1,22 @@
 <?php
 /**
+ * Generator of database load balancing objects.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup Database
  */
  * @ingroup Database
  */
 abstract class LBFactory {
+
+       /**
+        * @var 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 );
+       }
+
        /**
         * Get an LBFactory instance
+        *
+        * @return LBFactory
         */
        static function &singleton() {
                if ( is_null( self::$instance ) ) {
@@ -25,7 +57,6 @@ abstract class LBFactory {
 
        /**
         * Shut down, close connections and destroy the cached instance.
-        *
         */
        static function destroyInstance() {
                if ( self::$instance ) {
@@ -35,8 +66,19 @@ abstract class LBFactory {
                }
        }
 
+       /**
+        * Set the instance to be the given object
+        *
+        * @param $instance LBFactory
+        */
+       static function setInstance( $instance ) {
+               self::destroyInstance();
+               self::$instance = $instance;
+       }
+
        /**
         * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
+        * @param $conf
         */
        abstract function __construct( $conf );
 
@@ -44,7 +86,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,26 +94,30 @@ 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 );
 
-       /*
+       /**
         * Create a new load balancer for external storage. The resulting object will be
         * 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
+        *
+        * @return LoadBalancer
         */
        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
+        *
+        * @return LoadBalancer
         */
        abstract function &getExternalLB( $cluster, $wiki = false );
 
@@ -79,6 +125,8 @@ abstract class LBFactory {
         * Execute a function for each tracked load balancer
         * The callback is called with the load balancer as the first parameter,
         * and $params passed as the subsequent parameters.
+        * @param $callback string|array
+        * @param array $params
         */
        abstract function forEachLB( $callback, $params = array() );
 
@@ -90,6 +138,8 @@ abstract class LBFactory {
 
        /**
         * Call a method of each tracked load balancer
+        * @param $methodName string
+        * @param $args array
         */
        function forEachLBCallMethod( $methodName, $args = array() ) {
                $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
@@ -97,6 +147,9 @@ abstract class LBFactory {
 
        /**
         * Private helper for forEachLBCallMethod
+        * @param $loadBalancer
+        * @param $methodName string
+        * @param $args
         */
        function callMethod( $loadBalancer, $methodName, $args ) {
                call_user_func_array( array( $loadBalancer, $methodName ), $args );
@@ -114,6 +167,10 @@ abstract class LBFactory {
  * A simple single-master LBFactory that gets its configuration from the b/c globals
  */
 class LBFactory_Simple extends LBFactory {
+
+       /**
+        * @var LoadBalancer
+        */
        var $mainLB;
        var $extLBs = array();
 
@@ -124,6 +181,10 @@ class LBFactory_Simple extends LBFactory {
                $this->chronProt = new ChronologyProtector;
        }
 
+       /**
+        * @param $wiki
+        * @return LoadBalancer
+        */
        function newMainLB( $wiki = false ) {
                global $wgDBservers, $wgMasterWaitTimeout;
                if ( $wgDBservers ) {
@@ -147,6 +208,10 @@ class LBFactory_Simple extends LBFactory {
                ));
        }
 
+       /**
+        * @param $wiki
+        * @return LoadBalancer
+        */
        function getMainLB( $wiki = false ) {
                if ( !isset( $this->mainLB ) ) {
                        $this->mainLB = $this->newMainLB( $wiki );
@@ -156,6 +221,12 @@ class LBFactory_Simple extends LBFactory {
                return $this->mainLB;
        }
 
+       /**
+        * @throws MWException
+        * @param $cluster
+        * @param $wiki
+        * @return LoadBalancer
+        */
        function newExternalLB( $cluster, $wiki = false ) {
                global $wgExternalServers;
                if ( !isset( $wgExternalServers[$cluster] ) ) {
@@ -166,6 +237,11 @@ class LBFactory_Simple extends LBFactory {
                ));
        }
 
+       /**
+        * @param $cluster
+        * @param $wiki
+        * @return array
+        */
        function &getExternalLB( $cluster, $wiki = false ) {
                if ( !isset( $this->extLBs[$cluster] ) ) {
                        $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
@@ -178,6 +254,8 @@ class LBFactory_Simple extends LBFactory {
         * Execute a function for each tracked load balancer
         * The callback is called with the load balancer as the first parameter,
         * and $params passed as the subsequent parameters.
+        * @param $callback
+        * @param $params array
         */
        function forEachLB( $callback, $params = array() ) {
                if ( isset( $this->mainLB ) ) {
@@ -197,6 +275,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 +319,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 +344,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