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