| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | namespace Wikimedia\Rdbms; |
| 21 | |
| 22 | use InvalidArgumentException; |
| 23 | |
| 24 | /** |
| 25 | * Database connection manager. |
| 26 | * |
| 27 | * This manages access to primary and replica databases. |
| 28 | * |
| 29 | * @since 1.29 |
| 30 | * @ingroup Database |
| 31 | * @author Addshore |
| 32 | */ |
| 33 | class ConnectionManager { |
| 34 | |
| 35 | /** |
| 36 | * @var ILoadBalancer |
| 37 | */ |
| 38 | private $loadBalancer; |
| 39 | |
| 40 | /** |
| 41 | * The symbolic name of the target database, or false for the local wiki's database. |
| 42 | * |
| 43 | * @var string|false |
| 44 | */ |
| 45 | private $domain; |
| 46 | |
| 47 | /** |
| 48 | * @var string[] |
| 49 | */ |
| 50 | private $groups = []; |
| 51 | |
| 52 | /** |
| 53 | * @param ILoadBalancer $loadBalancer |
| 54 | * @param string|false $domain Optional logical DB name, defaults to current wiki. |
| 55 | * This follows the convention for database names used by $loadBalancer. |
| 56 | * @param string[] $groups see LoadBalancer::getConnection |
| 57 | * |
| 58 | * @throws InvalidArgumentException |
| 59 | */ |
| 60 | public function __construct( ILoadBalancer $loadBalancer, $domain = false, array $groups = [] ) { |
| 61 | if ( !is_string( $domain ) && $domain !== false ) { |
| 62 | throw new InvalidArgumentException( '$dbName must be a string, or false.' ); |
| 63 | } |
| 64 | |
| 65 | $this->loadBalancer = $loadBalancer; |
| 66 | $this->domain = $domain; |
| 67 | $this->groups = $groups; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param int $i |
| 72 | * @param string[]|null $groups |
| 73 | * @param int $flags |
| 74 | * @return IDatabase |
| 75 | */ |
| 76 | private function getConnection( $i, ?array $groups = null, int $flags = 0 ) { |
| 77 | $groups ??= $this->groups; |
| 78 | return $this->loadBalancer->getConnection( $i, $groups, $this->domain, $flags ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns a connection to the primary DB, for updating. |
| 83 | * |
| 84 | * @since 1.29 |
| 85 | * @since 1.37 Added optional $flags parameter |
| 86 | * @param int $flags |
| 87 | * @return IDatabase |
| 88 | */ |
| 89 | public function getWriteConnection( int $flags = 0 ) { |
| 90 | return $this->getConnection( DB_PRIMARY, null, $flags ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns a database connection for reading. |
| 95 | * |
| 96 | * @since 1.29 |
| 97 | * @since 1.37 Added optional $flags parameter |
| 98 | * @param string[]|null $groups |
| 99 | * @param int $flags |
| 100 | * @return IReadableDatabase |
| 101 | */ |
| 102 | public function getReadConnection( ?array $groups = null, int $flags = 0 ) { |
| 103 | $groups ??= $this->groups; |
| 104 | return $this->getConnection( DB_REPLICA, $groups, $flags ); |
| 105 | } |
| 106 | } |