diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 6ad88a49cf..8d0a8f29a6 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -135,6 +135,14 @@ class wpdb { * @var bool */ var $ready = false; + var $blogid = 0; + var $siteid = 0; + var $blogs; + var $signups; + var $site; + var $sitemeta; + var $sitecategories; + var $global_tables = array('blogs', 'signups', 'site', 'sitemeta', 'users', 'usermeta', 'sitecategories', 'registration_log', 'blog_versions'); /** * WordPress Posts table @@ -260,7 +268,7 @@ class wpdb { * @access private * @var array */ - var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options', + var $tables = array('posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options', 'postmeta', 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta'); /** @@ -337,6 +345,8 @@ class wpdb { * @param string $dbhost MySQL database host */ function wpdb($dbuser, $dbpassword, $dbname, $dbhost) { + if( defined( "WP_USE_MULTIPLE_DB" ) && CONSTANT( "WP_USE_MULTIPLE_DB" ) == true ) + $this->db_connect(); return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost); } @@ -360,6 +370,15 @@ class wpdb { if ( WP_DEBUG ) $this->show_errors(); + if( is_multisite() ) { + $this->charset = 'utf8'; + if( defined( 'DB_COLLATE' ) && constant( 'DB_COLLATE' ) != '' ) { + $this->collate = constant( 'DB_COLLATE' ); + } else { + $this->collate = 'utf8_general_ci'; + } + } + if ( defined('DB_CHARSET') ) $this->charset = DB_CHARSET; @@ -397,7 +416,7 @@ class wpdb { } } - $this->select($dbname); + $this->select($dbname, $this->dbh); } /** @@ -427,8 +446,21 @@ class wpdb { if ( preg_match('|[^a-z0-9_]|i', $prefix) ) return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/); - $old_prefix = $this->prefix; - $this->prefix = $prefix; + if( is_multisite() ) { + $old_prefix = ''; + } else { + $old_prefix = $prefix; + } + if( isset( $this->base_prefix ) ) + $old_prefix = $this->base_prefix; + $this->base_prefix = $prefix; + foreach ( $this->global_tables as $table ) + $this->$table = $prefix . $table; + + if ( defined('VHOST') && empty($this->blogid) ) + return $old_prefix; + + $this->prefix = $this->get_blog_prefix( $this->blogid ); foreach ( (array) $this->tables as $table ) $this->$table = $this->prefix . $table; @@ -442,6 +474,33 @@ class wpdb { return $old_prefix; } + function set_blog_id($blog_id, $site_id = '') { + if ( !empty($site_id) ) + $this->siteid = $site_id; + + $old_blog_id = $this->blogid; + $this->blogid = $blog_id; + + $this->prefix = $this->get_blog_prefix( $this->blogid ); + + foreach ( $this->tables as $table ) + $this->$table = $this->prefix . $table; + + return $old_blog_id; + } + + function get_blog_prefix( $blog_id = '' ) { + if ( $blog_id ) { + if( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1) ) { + return $this->prefix; + } else { + return $this->base_prefix . $blog_id . '_'; + } + } else { + return $this->base_prefix; + } + } + /** * Selects a database using the current database connection. * @@ -453,8 +512,8 @@ class wpdb { * @param string $db MySQL database name * @return null Always null. */ - function select($db) { - if (!@mysql_select_db($db, $this->dbh)) { + function select($db, &$dbh) { + if (!@mysql_select_db($db, $dbh)) { $this->ready = false; $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
WordPress database error: [$str]
- $query
WordPress database error: [$str]
+ $query
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->select( $details[ 'db_name' ], $this->$dbhname ); + } + /** * Perform a MySQL database query, using current database connection. * @@ -702,24 +805,49 @@ class wpdb { // Perform the query via std mysql_query function.. if ( defined('SAVEQUERIES') && SAVEQUERIES ) $this->timer_start(); + + // use $this->dbh for read ops, and $this->dbhwrite for write ops + // use $this->dbhglobal for gloal table ops + unset( $dbh ); + if( defined( "WP_USE_MULTIPLE_DB" ) && CONSTANT( "WP_USE_MULTIPLE_DB" ) == true ) { + if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) { + if( false == isset( $this->dbhglobal ) ) { + $this->db_connect( $query ); + } + $dbh =& $this->dbhglobal; + $this->last_db_used = "global"; + } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) { + if( false == isset( $this->dbhwrite ) ) { + $this->db_connect( $query ); + } + $dbh =& $this->dbhwrite; + $this->last_db_used = "write"; + } else { + $dbh =& $this->dbh; + $this->last_db_used = "read"; + } + } else { + $dbh =& $this->dbh; + $this->last_db_used = "other/read"; + } - $this->result = @mysql_query($query, $this->dbh); + $this->result = @mysql_query($query, $dbh); ++$this->num_queries; if ( defined('SAVEQUERIES') && SAVEQUERIES ) $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); // If there is an error then take note of it.. - if ( $this->last_error = mysql_error($this->dbh) ) { + if ( $this->last_error = mysql_error($dbh) ) { $this->print_error(); return false; } if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) { - $this->rows_affected = mysql_affected_rows($this->dbh); + $this->rows_affected = mysql_affected_rows($dbh); // Take note of the insert_id if ( preg_match("/^\\s*(insert|replace) /i",$query) ) { - $this->insert_id = mysql_insert_id($this->dbh); + $this->insert_id = mysql_insert_id($dbh); } // Return number of rows affected $return_val = $this->rows_affected; @@ -1044,7 +1172,6 @@ class wpdb { * * @since 2.5.0 * @uses $wp_version - * @uses $required_mysql_version * * @return WP_Error */