Provides access to MySQL improved instances connected to Wikimedia Tool Labs replication databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Provides access to MySQL improved instances connected to Wikimedia Tool Labs replication databases | |
*/ | |
class ReplicationDatabaseFactory { | |
/** | |
* Hashtable of stored resources | |
* | |
* @var Array each item is a mysqli instance | |
*/ | |
private static $hashtable = array(); | |
/** | |
* Initializes a MySQL improved instance or gets already available one | |
* | |
* @param string $database the database to select (e.g. enwiki) | |
* @return mysqli a MySQL improved instance | |
*/ | |
public static function get ($database) { | |
if (!array_key_exists($database, self::$hashtable)) { | |
self::$hashtable[$database] = self::connect($database); | |
} | |
return self::$hashtable[$database]; | |
} | |
/** | |
* Connects to a Wikimedia Tool Labs replication database and gets the resource | |
* | |
* @param string $database the database to select (e.g. enwiki) | |
* @return mysqli a MySQL improved instance | |
* @throws Exception if the MySQL database couldn't be selected | |
*/ | |
private static function connect ($database) { | |
$pw = posix_getpwuid(posix_getuid()); | |
$mycnf = parse_ini_file($pw['dir'] . "/replica.my.cnf"); | |
$host = $database . '.labsdb'; | |
try { | |
$db = new mysqli($host, $mycnf['user'], $mycnf['password'], $database . '_p'); | |
} catch (Exception $ex) { | |
//TODO: process errors / status (mysqli doesn't throw exception) | |
throw new Exception("Can't select $database database. " . $ex->getMessage()); | |
} | |
return $db; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment