Merge "Use a fixed marker prefix string in the Parser and MWTidy"
[mediawiki.git] / includes / db / DBConnRef.php
blob7045494abd18947adb7ecb28573b80bc719774ff
1 <?php
2 /**
3 * Helper class to handle automatically marking connections as reusable (via RAII pattern)
4 * as well handling deferring the actual network connection until the handle is used
6 * @ingroup Database
7 * @since 1.22
8 */
9 class DBConnRef implements IDatabase {
10 /** @var LoadBalancer */
11 private $lb;
13 /** @var DatabaseBase|null */
14 private $conn;
16 /** @var array|null */
17 private $params;
19 /**
20 * @param LoadBalancer $lb
21 * @param DatabaseBase|array $conn Connection or (server index, group, wiki ID) array
23 public function __construct( LoadBalancer $lb, $conn ) {
24 $this->lb = $lb;
25 if ( $conn instanceof DatabaseBase ) {
26 $this->conn = $conn;
27 } else {
28 $this->params = $conn;
32 public function __call( $name, $arguments ) {
33 if ( $this->conn === null ) {
34 list( $db, $groups, $wiki ) = $this->params;
35 $this->conn = $this->lb->getConnection( $db, $groups, $wiki );
38 return call_user_func_array( array( $this->conn, $name ), $arguments );
41 public function __destruct() {
42 if ( $this->conn !== null ) {
43 $this->lb->reuseConnection( $this->conn );