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