Merge "Make SpecialWatchlist extend SpecialRecentChanges (temporarily)"
[lhc/web/wiklou.git] / includes / db / LBFactorySingle.php
1 <?php
2 /**
3 * Simple generator of database connections that always returns the same object.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * An LBFactory class that always returns a single database object.
26 */
27 class LBFactorySingle extends LBFactory {
28 protected $lb;
29
30 /**
31 * @param array $conf An associative array with one member:
32 * - connection: The DatabaseBase connection object
33 */
34 function __construct( $conf ) {
35 $this->lb = new LoadBalancerSingle( $conf );
36 }
37
38 /**
39 * @param $wiki bool|string
40 *
41 * @return LoadBalancerSingle
42 */
43 function newMainLB( $wiki = false ) {
44 return $this->lb;
45 }
46
47 /**
48 * @param $wiki bool|string
49 *
50 * @return LoadBalancerSingle
51 */
52 function getMainLB( $wiki = false ) {
53 return $this->lb;
54 }
55
56 /**
57 * @param $cluster
58 * @param $wiki bool|string
59 *
60 * @return LoadBalancerSingle
61 */
62 function newExternalLB( $cluster, $wiki = false ) {
63 return $this->lb;
64 }
65
66 /**
67 * @param $cluster
68 * @param $wiki bool|string
69 *
70 * @return LoadBalancerSingle
71 */
72 function &getExternalLB( $cluster, $wiki = false ) {
73 return $this->lb;
74 }
75
76 /**
77 * @param $callback string|array
78 * @param $params array
79 */
80 function forEachLB( $callback, $params = array() ) {
81 call_user_func_array( $callback, array_merge( array( $this->lb ), $params ) );
82 }
83 }
84
85 /**
86 * Helper class for LBFactorySingle.
87 */
88 class LoadBalancerSingle extends LoadBalancer {
89 /** @var DatabaseBase */
90 protected $db;
91
92 /**
93 * @param $params array
94 */
95 function __construct( $params ) {
96 $this->db = $params['connection'];
97 parent::__construct( array( 'servers' => array( array(
98 'type' => $this->db->getType(),
99 'host' => $this->db->getServer(),
100 'dbname' => $this->db->getDBname(),
101 'load' => 1,
102 ) ) ) );
103 }
104
105 /**
106 *
107 * @param $server string
108 * @param $dbNameOverride bool
109 *
110 * @return DatabaseBase
111 */
112 function reallyOpenConnection( $server, $dbNameOverride = false ) {
113 return $this->db;
114 }
115 }