2013-04-09 22:13:39 +00:00
|
|
|
<?php
|
2018-08-13 07:29:32 +00:00
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications;
|
|
|
|
|
2016-10-09 20:21:33 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-11-12 07:19:00 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2021-09-08 18:36:35 +00:00
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
2013-04-09 22:13:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Database factory class, this will determine whether to use the main database
|
2022-11-12 07:19:00 +00:00
|
|
|
* or an external database defined in the configuration file
|
2013-04-09 22:13:39 +00:00
|
|
|
*/
|
2022-11-12 07:19:00 +00:00
|
|
|
class DbFactory {
|
2013-04-09 22:13:39 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* The cluster for the database
|
2018-08-13 07:32:22 +00:00
|
|
|
* @var string|false
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2015-10-26 15:27:31 +00:00
|
|
|
private $cluster;
|
|
|
|
|
2020-12-16 21:31:09 +00:00
|
|
|
/** @var string|false */
|
2015-10-26 15:27:31 +00:00
|
|
|
private $shared;
|
|
|
|
|
2020-12-16 21:31:09 +00:00
|
|
|
/** @var string|false */
|
2015-10-26 15:27:31 +00:00
|
|
|
private $sharedCluster;
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-13 07:32:22 +00:00
|
|
|
* @param string|false $cluster
|
|
|
|
* @param string|false $shared
|
|
|
|
* @param string|false $sharedCluster
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2015-10-26 15:27:31 +00:00
|
|
|
public function __construct( $cluster = false, $shared = false, $sharedCluster = false ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->cluster = $cluster;
|
2015-10-26 15:27:31 +00:00
|
|
|
$this->shared = $shared;
|
|
|
|
$this->sharedCluster = $sharedCluster;
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a db factory instance from default Echo configuration
|
2015-07-31 19:19:53 +00:00
|
|
|
* A singleton is not necessary because it's actually handled
|
|
|
|
* inside core database object
|
2014-08-18 21:55:04 +00:00
|
|
|
*
|
2022-11-12 07:19:00 +00:00
|
|
|
* @return DbFactory
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
public static function newFromDefault() {
|
2015-10-26 15:27:31 +00:00
|
|
|
global $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster;
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2015-10-26 15:27:31 +00:00
|
|
|
return new self( $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 03:07:45 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isReadOnly() {
|
|
|
|
return ( $this->getLB()->getReadOnlyReason() !== false );
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Get the database load balancer
|
2021-09-08 18:36:35 +00:00
|
|
|
* @return ILoadBalancer
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
protected function getLB() {
|
|
|
|
// Use the external db defined for Echo
|
|
|
|
if ( $this->cluster ) {
|
2016-10-09 20:21:33 +00:00
|
|
|
$lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->cluster );
|
2015-10-26 15:27:31 +00:00
|
|
|
} else {
|
2016-10-09 20:21:33 +00:00
|
|
|
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
|
2015-10-26 15:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $lb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-08 18:36:35 +00:00
|
|
|
* @return ILoadBalancer
|
2015-10-26 15:27:31 +00:00
|
|
|
*/
|
|
|
|
protected function getSharedLB() {
|
|
|
|
if ( $this->sharedCluster ) {
|
2016-10-09 20:21:33 +00:00
|
|
|
$lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->sharedCluster );
|
2014-07-18 03:58:21 +00:00
|
|
|
} else {
|
2016-10-09 20:21:33 +00:00
|
|
|
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $lb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the database connection for Echo
|
2017-08-23 14:38:58 +00:00
|
|
|
* @param int $db Index of the connection to get
|
2019-10-23 10:44:35 +00:00
|
|
|
* @param string[] $groups Query groups.
|
2022-11-12 07:19:00 +00:00
|
|
|
* @return IDatabase
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2019-10-23 10:44:35 +00:00
|
|
|
public function getEchoDb( $db, array $groups = [] ) {
|
2015-10-26 15:27:31 +00:00
|
|
|
return $this->getLB()->getConnection( $db, $groups );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 15:27:31 +00:00
|
|
|
/**
|
2017-08-23 14:38:58 +00:00
|
|
|
* @param int $db Index of the connection to get
|
2019-10-23 10:44:35 +00:00
|
|
|
* @param string[] $groups Query groups
|
2022-11-12 07:19:00 +00:00
|
|
|
*
|
|
|
|
* @return bool|IDatabase false if no shared db is configured
|
2015-10-26 15:27:31 +00:00
|
|
|
*/
|
2019-10-23 10:44:35 +00:00
|
|
|
public function getSharedDb( $db, array $groups = [] ) {
|
2015-10-26 15:27:31 +00:00
|
|
|
if ( !$this->shared ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getSharedLB()->getConnection( $db, $groups, $this->shared );
|
|
|
|
}
|
|
|
|
|
2013-04-09 22:13:39 +00:00
|
|
|
/**
|
2024-02-13 18:55:34 +00:00
|
|
|
* Wrapper function for LBFactory::getExternalLB/getMainLB, some extensions like MobileFrontend is
|
2014-08-18 21:55:04 +00:00
|
|
|
* using this to issue sql queries against Echo database directly. This
|
|
|
|
* is totally not accepted and should be updated to use Echo database access
|
|
|
|
* objects
|
|
|
|
*
|
2014-07-18 03:58:21 +00:00
|
|
|
* @deprecated Use newFromDefault() instead to create a db factory
|
2017-08-23 14:38:58 +00:00
|
|
|
* @param int $db Index of the connection to get
|
2019-10-23 10:44:35 +00:00
|
|
|
* @param string[] $groups Query groups.
|
2017-08-23 14:38:58 +00:00
|
|
|
* @param string|bool $wiki The wiki ID, or false for the current wiki
|
2022-11-12 07:19:00 +00:00
|
|
|
* @return IDatabase
|
2013-04-09 22:13:39 +00:00
|
|
|
*/
|
2019-10-23 10:44:35 +00:00
|
|
|
public static function getDB( $db, array $groups = [], $wiki = false ) {
|
2013-04-18 21:33:38 +00:00
|
|
|
global $wgEchoCluster;
|
2013-04-09 22:13:39 +00:00
|
|
|
|
2017-02-07 13:42:44 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
2013-04-18 21:33:38 +00:00
|
|
|
// Use the external db defined for Echo
|
|
|
|
if ( $wgEchoCluster ) {
|
2018-02-22 03:07:45 +00:00
|
|
|
$lb = $services->getDBLoadBalancerFactory()->getExternalLB( $wgEchoCluster );
|
2013-04-18 21:33:38 +00:00
|
|
|
} else {
|
2017-02-07 13:42:44 +00:00
|
|
|
if ( $wiki === false ) {
|
|
|
|
$lb = $services->getDBLoadBalancer();
|
|
|
|
} else {
|
|
|
|
$lb = $services->getDBLoadBalancerFactory()->getMainLB( $wiki );
|
|
|
|
}
|
2013-04-09 22:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 21:33:38 +00:00
|
|
|
return $lb->getConnection( $db, $groups, $wiki );
|
2013-04-09 22:13:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 00:49:33 +00:00
|
|
|
/**
|
2021-05-13 00:04:54 +00:00
|
|
|
* Check whether it makes sense to retry a failed lookup on the primary database.
|
2017-05-05 00:49:33 +00:00
|
|
|
* @return bool True if there are multiple servers and changes were made in this request; false otherwise
|
|
|
|
*/
|
2021-09-04 00:46:13 +00:00
|
|
|
public function canRetryPrimary() {
|
|
|
|
return $this->getLB()->getServerCount() > 1 && $this->getLB()->hasOrMadeRecentPrimaryChanges();
|
2017-05-05 00:49:33 +00:00
|
|
|
}
|
2013-04-09 22:13:39 +00:00
|
|
|
}
|
2022-11-12 07:19:00 +00:00
|
|
|
|
|
|
|
class_alias( DbFactory::class, 'MWEchoDbFactory' );
|