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