3 use MediaWiki\MediaWikiServices
;
4 use Wikimedia\Rdbms\IDatabase
;
5 use Wikimedia\Rdbms\LoadBalancer
;
8 * Base class for objects that allow access to other wiki's databases using
9 * the foreign database access mechanism implemented by LBFactoryMulti.
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.
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.
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
31 * @license GPL-2.0-or-later
32 * @author Daniel Kinzler
34 abstract class DBAccessBase
implements IDBAccessObject
{
36 * @var string|bool $wiki The target wiki's name. This must be an ID
37 * that LBFactory can understand.
39 protected $wiki = false;
42 * @param string|bool $wiki The target wiki's name. This must be an ID
43 * that LBFactory can understand.
45 public function __construct( $wiki = false ) {
50 * Returns a database connection.
53 * @see LoadBalancer::getConnection()
57 * @param int $id Which connection to use
58 * @param array $groups Query groups
62 protected function getConnection( $id, array $groups = [] ) {
63 $loadBalancer = $this->getLoadBalancer();
65 return $loadBalancer->getConnection( $id, $groups, $this->wiki
);
69 * Releases a database connection and makes it available for recycling.
71 * @see LoadBalancer::reuseConnection()
75 * @param IDatabase $db The database connection to release.
77 protected function releaseConnection( IDatabase
$db ) {
78 if ( $this->wiki
!== false ) {
79 $loadBalancer = $this->getLoadBalancer();
80 $loadBalancer->reuseConnection( $db );
85 * Get the database type used for read operations.
87 * @see MediaWikiServices::getInstance()->getDBLoadBalancer
91 * @return LoadBalancer The database load balancer object
93 public function getLoadBalancer() {
94 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
95 return $lbFactory->getMainLB( $this->wiki
);