Merge "Make database classes handle hyphens in $wgDBname"
[lhc/web/wiklou.git] / includes / db / loadbalancer / 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 /** @var LoadBalancerSingle */
29 private $lb;
30
31 /**
32 * @param array $conf An associative array with one member:
33 * - connection: The IDatabase connection object
34 */
35 public function __construct( array $conf ) {
36 parent::__construct( $conf );
37
38 $this->lb = new LoadBalancerSingle( array_merge( $this->baseLoadBalancerParams(), $conf ) );
39 }
40
41 /**
42 * @param bool|string $wiki
43 * @return LoadBalancerSingle
44 */
45 public function newMainLB( $wiki = false ) {
46 return $this->lb;
47 }
48
49 /**
50 * @param bool|string $wiki
51 * @return LoadBalancerSingle
52 */
53 public function getMainLB( $wiki = false ) {
54 return $this->lb;
55 }
56
57 /**
58 * @param string $cluster External storage cluster, or false for core
59 * @param bool|string $wiki Wiki ID, or false for the current wiki
60 * @return LoadBalancerSingle
61 */
62 protected function newExternalLB( $cluster, $wiki = false ) {
63 return $this->lb;
64 }
65
66 /**
67 * @param string $cluster External storage cluster, or false for core
68 * @param bool|string $wiki Wiki ID, or false for the current wiki
69 * @return LoadBalancerSingle
70 */
71 public function getExternalLB( $cluster, $wiki = false ) {
72 return $this->lb;
73 }
74
75 /**
76 * @param string|callable $callback
77 * @param array $params
78 */
79 public function forEachLB( $callback, array $params = [] ) {
80 call_user_func_array( $callback, array_merge( [ $this->lb ], $params ) );
81 }
82 }
83
84 /**
85 * Helper class for LBFactorySingle.
86 */
87 class LoadBalancerSingle extends LoadBalancer {
88 /** @var IDatabase */
89 private $db;
90
91 /**
92 * @param array $params
93 */
94 public function __construct( array $params ) {
95 $this->db = $params['connection'];
96
97 parent::__construct( [
98 'servers' => [
99 [
100 'type' => $this->db->getType(),
101 'host' => $this->db->getServer(),
102 'dbname' => $this->db->getDBname(),
103 'load' => 1,
104 ]
105 ],
106 'trxProfiler' => isset( $params['trxProfiler'] ) ? $params['trxProfiler'] : null,
107 'srvCache' => isset( $params['srvCache'] ) ? $params['srvCache'] : null,
108 'wanCache' => isset( $params['wanCache'] ) ? $params['wanCache'] : null
109 ] );
110
111 if ( isset( $params['readOnlyReason'] ) ) {
112 $this->db->setLBInfo( 'readOnlyReason', $params['readOnlyReason'] );
113 }
114 }
115
116 /**
117 *
118 * @param string $server
119 * @param bool $dbNameOverride
120 *
121 * @return IDatabase
122 */
123 protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
124 return $this->db;
125 }
126 }