Merge "maintenance: Document secondary purpose of --server"
[lhc/web/wiklou.git] / includes / dao / DBAccessBase.php
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4 use Wikimedia\Rdbms\LoadBalancer;
5
6 /**
7 * Base class for objects that allow access to other wiki's databases using
8 * the foreign database access mechanism implemented by LBFactoryMulti.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @since 1.21
26 *
27 * @file
28 * @ingroup Database
29 *
30 * @license GNU GPL v2+
31 * @author Daniel Kinzler
32 */
33 abstract class DBAccessBase implements IDBAccessObject {
34 /**
35 * @var string|bool $wiki The target wiki's name. This must be an ID
36 * that LBFactory can understand.
37 */
38 protected $wiki = false;
39
40 /**
41 * @param string|bool $wiki The target wiki's name. This must be an ID
42 * that LBFactory can understand.
43 */
44 public function __construct( $wiki = false ) {
45 $this->wiki = $wiki;
46 }
47
48 /**
49 * Returns a database connection.
50 *
51 * @see wfGetDB()
52 * @see LoadBalancer::getConnection()
53 *
54 * @since 1.21
55 *
56 * @param int $id Which connection to use
57 * @param array $groups Query groups
58 *
59 * @return IDatabase
60 */
61 protected function getConnection( $id, $groups = [] ) {
62 $loadBalancer = wfGetLB( $this->wiki );
63
64 return $loadBalancer->getConnection( $id, $groups, $this->wiki );
65 }
66
67 /**
68 * Releases a database connection and makes it available for recycling.
69 *
70 * @see LoadBalancer::reuseConnection()
71 *
72 * @since 1.21
73 *
74 * @param IDatabase $db The database connection to release.
75 */
76 protected function releaseConnection( IDatabase $db ) {
77 if ( $this->wiki !== false ) {
78 $loadBalancer = $this->getLoadBalancer();
79 $loadBalancer->reuseConnection( $db );
80 }
81 }
82
83 /**
84 * Get the database type used for read operations.
85 *
86 * @see wfGetLB
87 *
88 * @since 1.21
89 *
90 * @return LoadBalancer The database load balancer object
91 */
92 public function getLoadBalancer() {
93 return wfGetLB( $this->wiki );
94 }
95 }