From 90c586422da74ef4e56bc93c2b16aeab22bc613f Mon Sep 17 00:00:00 2001 From: donncha Date: Wed, 27 Jul 2005 16:15:29 +0000 Subject: Use multiple databases. git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@80 7be80a69-a1ef-0310-a953-fb0f7c49ff36 --- wp-inst/wp-config-sample.php | 43 ++++++++++++++++++++++++++++++++++++ wp-inst/wp-includes/wp-db.php | 51 ++++++++++++++++++++++++++++++++++++++++++- wp-inst/wp-settings.php | 34 ++++++++++++++++++++++++++++- 3 files changed, 126 insertions(+), 2 deletions(-) diff --git a/wp-inst/wp-config-sample.php b/wp-inst/wp-config-sample.php index 6ceb2da..32f9276 100644 --- a/wp-inst/wp-config-sample.php +++ b/wp-inst/wp-config-sample.php @@ -16,6 +16,49 @@ $table_prefix = 'wp_'; // example: 'wp_' or 'b2' or 'mylogin_' // to enable German language support. define ('WPLANG', ''); +define( "WP_USE_MULTIPLE_DB", false ); +$db_list = array( + "write" => array( + array( "db_name" => "WRITE_DB_NAME1", + "db_user" => "WRITE_DB_USER1", + "db_password" => "WRITE_DB_PASS1", + "db_host" => "WRITE_DB_HOST1" + ) + ), + "read" => array( + array( "db_name" => "READ_DB_NAME1", + "db_user" => "READ_DB_USER1", + "db_password" => "READ_DB_PASS1", + "db_host" => "READ_DB_HOST1" + ), + array( "db_name" => "READ_DB_NAME2", + "db_user" => "READ_DB_USER2", + "db_password" => "READ_DB_PASS2", + "db_host" => "READ_DB_HOST2" + ), + array( "db_name" => "READ_DB_NAME3", + "db_user" => "READ_DB_USER3", + "db_password" => "READ_DB_PASS3", + "db_host" => "READ_DB_HOST3" + ), + array( "db_name" => "READ_DB_NAME4", + "db_user" => "READ_DB_USER4", + "db_password" => "READ_DB_PASS4", + "db_host" => "READ_DB_HOST4" + ), + array( "db_name" => "READ_DB_NAME5", + "db_user" => "READ_DB_USER5", + "db_password" => "READ_DB_PASS5", + "db_host" => "READ_DB_HOST5" + ), + array( "db_name" => "READ_DB_NAME6", + "db_user" => "READ_DB_USER6", + "db_password" => "READ_DB_PASS6", + "db_host" => "READ_DB_HOST6" + ) + ) + ); + /* Stop editing */ define('ABSPATH', dirname(__FILE__).'/'); diff --git a/wp-inst/wp-includes/wp-db.php b/wp-inst/wp-includes/wp-db.php index 73d4be7..765b0c0 100644 --- a/wp-inst/wp-includes/wp-db.php +++ b/wp-inst/wp-includes/wp-db.php @@ -53,6 +53,10 @@ class wpdb {

If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

"); } + $this->db_host = $dbhost; + $this->db_user = $dbuser; + $this->db_password = $dbpass; + $this->db_name = $dbname; $this->select($dbname); } @@ -124,6 +128,48 @@ class wpdb { $this->last_query = null; } + function db_connect( $query ) { + global $db_list; + if( is_array( $db_list ) == false ) + return true; + + if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) { + $details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ]; + } else { + $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ]; + } + + if( $this->db_host != $details[ 'db_host' ] || $this->db_user != $details[ 'db_user' ] || $this->db_password != $details[ 'db_password' ] ) { + if ($this->dbh) { + @mysql_close( $this->dbh ); + } + $this->db_host = $details[ 'db_host' ]; + $this->db_user = $details[ 'db_user' ]; + $this->db_password = $details[ 'db_password' ]; + + $this->dbh = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] ); + if (!$this->dbh) { + $this->bail(" +

Error establishing a database connection

+

This either means that the username and password information in your wp-config.php file is incorrect or we can't contact the database server at $dbhost. This could mean your host's database server is down.

+ +

If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

+"); + } + $this->db_name = $details[ 'db_name' ]; + $this->select( $details[ 'db_name' ] ); + } + + if( $this->db_name != $details[ 'db_name' ] ) { + $this->db_name = $details[ 'db_name' ]; + $this->select( $details[ 'db_name' ] ); + } + } + // ================================================================== // Basic Query - see docs for more detail @@ -142,6 +188,9 @@ class wpdb { if (SAVEQUERIES) $this->timer_start(); + if( defined( "WP_USE_MULTIPLE_DB" ) && CONSTANT( "WP_USE_MULTIPLE_DB" ) == true ) + $this->db_connect( $query ); + $this->result = @mysql_query($query, $this->dbh); ++$this->num_queries; @@ -359,4 +408,4 @@ HEAD; } $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); -?> \ No newline at end of file +?> diff --git a/wp-inst/wp-settings.php b/wp-inst/wp-settings.php index 5cc3d63..7e51374 100644 --- a/wp-inst/wp-settings.php +++ b/wp-inst/wp-settings.php @@ -52,12 +52,38 @@ $domain = $_SERVER['HTTP_HOST']; if( substr( $domain, 0, 4 ) == 'www.' ) $domain = substr( $domain, 4 ); +function is_installed() { + global $wpdb, $domain, $base; + $check = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); + if( $check == false ) { + $msg = 'Database Tables Missing.
The table ' . DB_NAME . '::' . $wpdb->site . ' is missing. Delete the .htaccess file and run the installer again!
'; + } else { + $msg = 'Could Not Find Blog!
'; + $msg .= "Searched for " . $domain . $base . " in " . DB_NAME . "::" . $wpdb->blogs . " table. Is that right?
"; + } + $msg .= "Please check that your database contains the following tables:"; + $msg .= "If you suspect a problem please report it to support forums!"; + die( "

Fatal Error

" . $msg ); +} $current_blog = $wpdb->get_row("SELECT * FROM $wpdb->blogs WHERE domain = '$domain' AND path = '$base'"); +if( $current_blog == false ) { + is_installed(); +} $blog_id = $current_blog->blog_id; $is_public = $current_blog->is_public; $site_id = $current_blog->site_id; $current_site = $wpdb->get_row("SELECT * FROM $wpdb->site WHERE id='$site_id'"); +if( $current_site == false ) { + is_installed(); +} if( $current_site->domain == $domain && $current_site->path == $base ) { $wpblog = 'main'; } @@ -76,7 +102,13 @@ if( $blog_id == false ) { $blog_id = 1; } } else { - die( "No Blog by that name on this system." ); + $check = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); + if( $check == false ) { + $msg = ': DB Tables Missing'; + } else { + $msg = ''; + } + die( "No Blog by that name on this system." . $msg ); } } -- cgit