mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-22 17:18:32 +01:00
db class compatible with WP, MU & multi site - See #11644
git-svn-id: http://svn.automattic.com/wordpress/trunk@12667 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
533244e72d
commit
45cf3431f2
@ -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*/'
|
||||
<h1>Can’t select database</h1>
|
||||
@ -605,14 +664,22 @@ class wpdb {
|
||||
if ( !$this->show_errors )
|
||||
return false;
|
||||
|
||||
$str = htmlspecialchars($str, ENT_QUOTES);
|
||||
$query = htmlspecialchars($this->last_query, ENT_QUOTES);
|
||||
// If there is an error then take note of it
|
||||
if( is_multisite() ) {
|
||||
$msg = "WordPress database error: [$str]\n{$this->query}\n";
|
||||
if( defined( 'ERRORLOGFILE' ) )
|
||||
error_log( $msg, 3, CONSTANT( 'ERRORLOGFILE' ) );
|
||||
if( defined( 'DIEONDBERROR' ) )
|
||||
die( $msg );
|
||||
} else {
|
||||
$str = htmlspecialchars($str, ENT_QUOTES);
|
||||
$query = htmlspecialchars($this->last_query, ENT_QUOTES);
|
||||
|
||||
// If there is an error then take note of it
|
||||
print "<div id='error'>
|
||||
<p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
|
||||
<code>$query</code></p>
|
||||
</div>";
|
||||
print "<div id='error'>
|
||||
<p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
|
||||
<code>$query</code></p>
|
||||
</div>";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -670,6 +737,42 @@ class wpdb {
|
||||
$this->last_query = null;
|
||||
}
|
||||
|
||||
function db_connect( $query = "SELECT" ) {
|
||||
global $db_list, $global_db_list;
|
||||
if( is_array( $db_list ) == false )
|
||||
return true;
|
||||
|
||||
if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
|
||||
$action = 'global';
|
||||
$details = $global_db_list[ mt_rand( 0, count( $global_db_list ) -1 ) ];
|
||||
$this->db_global = $details;
|
||||
} elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
|
||||
$action = 'write';
|
||||
$details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ];
|
||||
$this->db_write = $details;
|
||||
} else {
|
||||
$action = '';
|
||||
$details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ];
|
||||
$this->db_read = $details;
|
||||
}
|
||||
|
||||
$dbhname = "dbh" . $action;
|
||||
$this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
|
||||
if (!$this->$dbhname ) {
|
||||
$this->bail("
|
||||
<h1>Error establishing a database connection</h1>
|
||||
<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>$dbhost</code>. This could mean your host's database server is down.</p>
|
||||
<ul>
|
||||
<li>Are you sure you have the correct username and password?</li>
|
||||
<li>Are you sure that you have typed the correct hostname?</li>
|
||||
<li>Are you sure that the database server is running?</li>
|
||||
</ul>
|
||||
<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
|
||||
");
|
||||
}
|
||||
$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
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user