Use a separate connection for commons DB
authorTim Starling <tstarling@users.mediawiki.org>
Mon, 5 Mar 2007 11:55:29 +0000 (11:55 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Mon, 5 Mar 2007 11:55:29 +0000 (11:55 +0000)
includes/Image.php

index 9145a0d..d56a1dd 100644 (file)
@@ -347,7 +347,7 @@ class Image
                        # capitalize the first letter of the filename before
                        # looking it up in the shared repository.
                        $name = $wgContLang->ucfirst($this->name);
-                       $dbc = wfGetDB( DB_SLAVE, 'commons' );
+                       $dbc = Image::getCommonsDB();
 
                        $row = $dbc->selectRow( "`$wgSharedUploadDBname`.{$wgSharedUploadDBprefix}image",
                                array(
@@ -449,8 +449,7 @@ class Image
 
                        // Write to the other DB using selectDB, not database selectors
                        // This avoids breaking replication in MySQL
-                       $dbw = wfGetDB( DB_MASTER, 'commons' );
-                       $dbw->selectDB( $wgSharedUploadDBname );
+                       $dbw = Image::getCommonsDB();
                } else {
                        $dbw = wfGetDB( DB_MASTER );
                }
@@ -2329,6 +2328,18 @@ class Image
                }
        }
 
+       static function getCommonsDB() {
+               static $dbc;
+               global $wgLoadBalancer;
+               if ( !isset( $dbc ) ) {
+                       $i = $wgLoadBalancer->getGroupIndex( 'commons' );
+                       $dbinfo = $wgLoadBalancer->mServers[$i];
+                       $dbc = new Database( $dbinfo['host'], $dbinfo['user'], 
+                               $dbinfo['password'], $wgSharedUploadDBname );
+               }
+               return $dbc;
+       }
+
 } //class
 
 /**