Index: trunk/extensions/CentralAuth/CentralAuthUser.php |
— | — | @@ -11,6 +11,36 @@ |
12 | 12 | |
13 | 13 | */ |
14 | 14 | |
| 15 | +class CentralAuthHelper { |
| 16 | + private static $connections = array(); |
| 17 | + |
| 18 | + function get( $dbname ) { |
| 19 | + global $wgDBname; |
| 20 | + if( $dbname == $wgDBname ) { |
| 21 | + return wfGetDB( DB_MASTER ); |
| 22 | + } |
| 23 | + if( !isset( self::$connections[$dbname] ) ) { |
| 24 | + self::$connections[$dbname] = self::openConnection( $dbname ); |
| 25 | + } |
| 26 | + return self::$connections[$dbname]; |
| 27 | + } |
| 28 | + |
| 29 | + private function getServer( $dbname ) { |
| 30 | + global $wgAlternateMaster, $wgDBserver; |
| 31 | + if( isset( $wgAlternateMaster[$dbname] ) ) { |
| 32 | + return $wgAlternateMaster[$dbname]; |
| 33 | + } elseif( isset( $wgAlternateMaster['DEFAULT'] ) ) { |
| 34 | + return $wgAlternateMaster['DEFAULT']; |
| 35 | + } |
| 36 | + return $wgDBserver; |
| 37 | + } |
| 38 | + |
| 39 | + private function openConnection( $dbname ) { |
| 40 | + global $wgDBuser, $wgDBpassword; |
| 41 | + $server = self::getServer( $dbname ); |
| 42 | + return new Database( $server, $wgDBuser, $wgDBpassword, $dbname ); |
| 43 | + } |
| 44 | +} |
15 | 45 | |
16 | 46 | class CentralAuthUser { |
17 | 47 | |
— | — | @@ -28,14 +58,14 @@ |
29 | 59 | * @fixme Make use of some info to get the appropriate master DB |
30 | 60 | */ |
31 | 61 | public static function getCentralDB() { |
32 | | - return wfGetDB( DB_MASTER, 'Central' ); |
| 62 | + return CentralAuthHelper::get( 'centralauth' ); |
33 | 63 | } |
34 | 64 | |
35 | 65 | /** |
36 | 66 | * @fixme Make use of some info to get the appropriate master DB |
37 | 67 | */ |
38 | 68 | public static function getLocalDB( $dbname ) { |
39 | | - return wfGetDB( DB_MASTER ); |
| 69 | + return CentralAuthHelper::get( $dbname ); |
40 | 70 | } |
41 | 71 | |
42 | 72 | public static function tableName( $name ) { |