Merge "Special:PagesWithProp: Distinguish content from interface"
[lhc/web/wiklou.git] / includes / db / LoadBalancer.php
index db709b5..3358488 100644 (file)
@@ -545,6 +545,38 @@ class LoadBalancer {
                }
        }
 
+       /**
+        * Get a database connection handle reference
+        *
+        * The handle's methods wrap simply wrap those of a DatabaseBase handle
+        *
+        * @see LoadBalancer::getConnection() for parameter information
+        *
+        * @param integer $db
+        * @param mixed $groups
+        * @param string $wiki
+        * @return DBConnRef
+        */
+       public function getConnectionRef( $db, $groups = array(), $wiki = false ) {
+               return new DBConnRef( $this, $this->getConnection( $db, $groups, $wiki ) );
+       }
+
+       /**
+        * Get a database connection handle reference without connecting yet
+        *
+        * The handle's methods wrap simply wrap those of a DatabaseBase handle
+        *
+        * @see LoadBalancer::getConnection() for parameter information
+        *
+        * @param integer $db
+        * @param mixed $groups
+        * @param string $wiki
+        * @return DBConnRef
+        */
+       public function getLazyConnectionRef( $db, $groups = array(), $wiki = false ) {
+               return new DBConnRef( $this, array( $db, $groups, $wiki ) );
+       }
+
        /**
         * Open a connection to the server given by the specified index
         * Index must be an actual index into the array.
@@ -637,6 +669,7 @@ class LoadBalancer {
                        $server = $this->mServers[$i];
                        $server['serverIndex'] = $i;
                        $server['foreignPoolRefCount'] = 0;
+                       $server['foreign'] = true;
                        $conn = $this->reallyOpenConnection( $server, $dbName );
                        if ( !$conn->isOpen() ) {
                                wfDebug( __METHOD__ . ": error opening connection for $i/$wiki\n" );
@@ -1068,3 +1101,46 @@ class LoadBalancer {
                $this->mLagTimes = null;
        }
 }
+
+/**
+ * Helper class to handle automatically marking connectons as reusable (via RAII pattern)
+ * as well handling deferring the actual network connection until the handle is used
+ *
+ * @ingroup Database
+ * @since 1.22
+ */
+class DBConnRef implements IDatabase {
+       /** @var LoadBalancer */
+       protected $lb;
+       /** @var DatabaseBase|null */
+       protected $conn;
+       /** @var Array|null */
+       protected $params;
+
+       /**
+        * @param $lb LoadBalancer
+        * @param $conn DatabaseBase|array Connection or (server index, group, wiki ID) array
+        */
+       public function __construct( LoadBalancer $lb, $conn ) {
+               $this->lb = $lb;
+               if ( $conn instanceof DatabaseBase ) {
+                       $this->conn = $conn;
+               } else {
+                       $this->params = $conn;
+               }
+       }
+
+       public function __call( $name, $arguments ) {
+               if ( $this->conn === null ) {
+                       list( $db, $groups, $wiki ) = $this->params;
+                       $this->conn = $this->lb->getConnection( $db, $groups, $wiki );
+               }
+               return call_user_func_array( array( $this->conn, $name ), $arguments );
+       }
+
+       function __destruct() {
+               if ( $this->conn !== null ) {
+                       $this->lb->reuseConnection( $this->conn );
+               }
+       }
+}