Provide command to adjust phpunit.xml for code coverage
[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 /**
36 * @var string|bool $wiki The target wiki's name. This must be an ID
37 * that LBFactory can understand.
38 */
39 protected $wiki = false;
40
41 /**
42 * @param string|bool $wiki The target wiki's name. This must be an ID
43 * that LBFactory can understand.
44 */
45 public function __construct( $wiki = false ) {
46 $this->wiki = $wiki;
47 }
48
49 /**
50 * Returns a database connection.
51 *
52 * @see wfGetDB()
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 $loadBalancer = $this->getLoadBalancer();
64
65 return $loadBalancer->getConnection( $id, $groups, $this->wiki );
66 }
67
68 /**
69 * Releases a database connection and makes it available for recycling.
70 *
71 * @see LoadBalancer::reuseConnection()
72 *
73 * @since 1.21
74 *
75 * @param IDatabase $db The database connection to release.
76 */
77 protected function releaseConnection( IDatabase $db ) {
78 if ( $this->wiki !== false ) {
79 $loadBalancer = $this->getLoadBalancer();
80 $loadBalancer->reuseConnection( $db );
81 }
82 }
83
84 /**
85 * Get the database type used for read operations.
86 *
87 * @see MediaWikiServices::getInstance()->getDBLoadBalancer
88 *
89 * @since 1.21
90 *
91 * @return ILoadBalancer The database load balancer object
92 */
93 public function getLoadBalancer() {
94 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
95 return $lbFactory->getMainLB( $this->wiki );
96 }
97 }