2013-04-09 22:13:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database factory class, this will determine whether to use the main database
|
|
|
|
* or an external database defined in configuration file
|
|
|
|
*/
|
|
|
|
class MWEchoDbFactory {
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* The cluster for the database
|
|
|
|
* @var string|bool
|
|
|
|
*/
|
2015-10-26 15:27:31 +00:00
|
|
|
private $cluster;
|
|
|
|
|
|
|
|
private $shared;
|
|
|
|
|
|
|
|
private $sharedCluster;
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-26 15:27:31 +00:00
|
|
|
* @param string|bool $cluster
|
|
|
|
* @param string|bool $shared
|
|
|
|
* @param string|bool $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
|
|
|
*
|
2014-07-18 03:58:21 +00:00
|
|
|
* @return MWEchoDbFactory
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the database load balancer
|
|
|
|
* @return LoadBalancer
|
|
|
|
*/
|
|
|
|
protected function getLB() {
|
|
|
|
// Use the external db defined for Echo
|
|
|
|
if ( $this->cluster ) {
|
2015-10-26 15:27:31 +00:00
|
|
|
$lb = wfGetLBFactory()->getExternalLB( $this->cluster );
|
|
|
|
} else {
|
|
|
|
$lb = wfGetLB();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $lb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return LoadBalancer
|
|
|
|
*/
|
|
|
|
protected function getSharedLB() {
|
|
|
|
if ( $this->sharedCluster ) {
|
|
|
|
$lb = wfGetLBFactory()->getExternalLB( $this->sharedCluster );
|
2014-07-18 03:58:21 +00:00
|
|
|
} else {
|
2015-10-26 15:27:31 +00:00
|
|
|
$lb = wfGetLB();
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $lb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the database connection for Echo
|
|
|
|
* @param $db int Index of the connection to get
|
|
|
|
* @param $groups mixed Query groups.
|
|
|
|
* @return DatabaseBase
|
|
|
|
*/
|
|
|
|
public function getEchoDb( $db, $groups = array() ) {
|
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
|
|
|
/**
|
|
|
|
* @param $db int Index of the connection to get
|
|
|
|
* @param array $groups Query groups
|
|
|
|
* @return bool|DatabaseBase false if no shared db is configured
|
|
|
|
*/
|
|
|
|
public function getSharedDb( $db, $groups = array() ) {
|
|
|
|
if ( !$this->shared ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getSharedLB()->getConnection( $db, $groups, $this->shared );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-09 22:13:39 +00:00
|
|
|
/**
|
2014-08-18 21:55:04 +00:00
|
|
|
* Wrapper function for wfGetDB, some extensions like MobileFrontend is
|
|
|
|
* 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
|
2013-04-09 22:13:39 +00:00
|
|
|
* @param $db int Index of the connection to get
|
|
|
|
* @param $groups mixed Query groups.
|
2013-10-08 20:48:54 +00:00
|
|
|
* @param $wiki string|bool The wiki ID, or false for the current wiki
|
2013-04-09 22:13:39 +00:00
|
|
|
* @return DatabaseBase
|
|
|
|
*/
|
|
|
|
public static function getDB( $db, $groups = array(), $wiki = false ) {
|
2013-04-18 21:33:38 +00:00
|
|
|
global $wgEchoCluster;
|
2013-04-09 22:13:39 +00:00
|
|
|
|
2013-04-18 21:33:38 +00:00
|
|
|
// Use the external db defined for Echo
|
|
|
|
if ( $wgEchoCluster ) {
|
|
|
|
$lb = wfGetLBFactory()->getExternalLB( $wgEchoCluster, $wiki );
|
|
|
|
} else {
|
|
|
|
$lb = wfGetLB( $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
|
|
|
}
|
|
|
|
|
2014-08-18 21:55:04 +00:00
|
|
|
/**
|
|
|
|
* Wait for the slaves of the database
|
|
|
|
*/
|
|
|
|
public function waitForSlaves() {
|
2014-08-02 06:41:19 +00:00
|
|
|
$this->waitFor( $this->getMasterPosition() );
|
2014-08-18 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 06:41:19 +00:00
|
|
|
/**
|
|
|
|
* Get the current master position for the wiki and echo
|
|
|
|
* db when they have at least one slave in their cluster.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMasterPosition() {
|
|
|
|
$position = array(
|
|
|
|
'wikiDb' => false,
|
|
|
|
'echoDb' => false,
|
|
|
|
);
|
|
|
|
$lb = wfGetLB();
|
|
|
|
if ( $lb->getServerCount() > 1 ) {
|
|
|
|
$position['wikiDb'] = $lb->getMasterPos();
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( $this->cluster ) {
|
|
|
|
$lb = $this->getLB();
|
|
|
|
if ( $lb->getServerCount() > 1 ) {
|
|
|
|
$position['echoDb'] = $lb->getMasterPos();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $position;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recieves the output of self::getMasterPosition. Waits
|
|
|
|
* for slaves to catch up to the master position at that
|
|
|
|
* point.
|
|
|
|
*
|
|
|
|
* @param array $position
|
|
|
|
*/
|
|
|
|
public function waitFor( array $position ) {
|
|
|
|
if ( $position['wikiDb'] ) {
|
|
|
|
wfGetLB()->waitFor( $position['wikiDb'] );
|
|
|
|
}
|
|
|
|
if ( $position['echoDb'] ) {
|
|
|
|
$this->getLB()->waitFor( $position['echoDb'] );
|
|
|
|
}
|
|
|
|
}
|
2013-04-09 22:13:39 +00:00
|
|
|
}
|