| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications; |
| 4 |
|
| 5 |
use MediaWiki\MediaWikiServices; |
| 6 |
use Wikimedia\Rdbms\IDatabase; |
| 7 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class DbFactory { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
private $cluster; |
| 20 |
|
| 21 |
|
| 22 |
private $shared; |
| 23 |
|
| 24 |
|
| 25 |
private $sharedCluster; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public function __construct( $cluster = false, $shared = false, $sharedCluster = false ) { |
| 33 |
$this->cluster = $cluster; |
| 34 |
$this->shared = $shared; |
| 35 |
$this->sharedCluster = $sharedCluster; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static function newFromDefault() { |
| 46 |
global $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster; |
| 47 |
|
| 48 |
return new self( $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function isReadOnly() { |
| 55 |
return ( $this->getLB()->getReadOnlyReason() !== false ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
protected function getLB() { |
| 63 |
|
| 64 |
if ( $this->cluster ) { |
| 65 |
$lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->cluster ); |
| 66 |
} else { |
| 67 |
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); |
| 68 |
} |
| 69 |
|
| 70 |
return $lb; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
protected function getSharedLB() { |
| 77 |
if ( $this->sharedCluster ) { |
| 78 |
$lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->sharedCluster ); |
| 79 |
} else { |
| 80 |
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); |
| 81 |
} |
| 82 |
|
| 83 |
return $lb; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function getEchoDb( $db, array $groups = [] ) { |
| 93 |
return $this->getLB()->getConnection( $db, $groups ); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function getSharedDb( $db, array $groups = [] ) { |
| 103 |
if ( !$this->shared ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
return $this->getSharedLB()->getConnection( $db, $groups, $this->shared ); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static function getDB( $db, array $groups = [], $wiki = false ) { |
| 123 |
global $wgEchoCluster; |
| 124 |
|
| 125 |
$services = MediaWikiServices::getInstance(); |
| 126 |
|
| 127 |
|
| 128 |
if ( $wgEchoCluster ) { |
| 129 |
$lb = $services->getDBLoadBalancerFactory()->getExternalLB( $wgEchoCluster ); |
| 130 |
} else { |
| 131 |
if ( $wiki === false ) { |
| 132 |
$lb = $services->getDBLoadBalancer(); |
| 133 |
} else { |
| 134 |
$lb = $services->getDBLoadBalancerFactory()->getMainLB( $wiki ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return $lb->getConnection( $db, $groups, $wiki ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function canRetryPrimary() { |
| 146 |
return $this->getLB()->getServerCount() > 1 && $this->getLB()->hasOrMadeRecentPrimaryChanges(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
class_alias( DbFactory::class, 'MWEchoDbFactory' ); |