<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[15537] trunk/wp-includes/wp-db.php: Remove WP_USE_MULTIPLE_DB.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/15537">15537</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2010-08-26 18:34:18 +0000 (Thu, 26 Aug 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Remove WP_USE_MULTIPLE_DB. Introduce init_charset() and set_charset(). fixes <a href="http://trac.wordpress.org/ticket/14672">#14672</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludeswpdbphp">trunk/wp-includes/wp-db.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludeswpdbphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/wp-db.php (15536 => 15537)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/wp-db.php        2010-08-26 15:43:32 UTC (rev 15536)
+++ trunk/wp-includes/wp-db.php        2010-08-26 18:34:18 UTC (rev 15537)
</span><span class="lines">@@ -476,8 +476,6 @@
</span><span class="cx">          * @param string $dbhost MySQL database host
</span><span class="cx">          */
</span><span class="cx">         function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) {
</span><del>-                if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB )
-                        $this->db_connect();
</del><span class="cx">                 return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost );
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -502,7 +500,32 @@
</span><span class="cx">                 if ( WP_DEBUG )
</span><span class="cx">                         $this->show_errors();
</span><span class="cx">
</span><del>-                if ( is_multisite() ) {
</del><ins>+                $this->init_charset();
+
+                $this->dbuser = $dbuser;
+                $this->dbpassword = $dbpassword;
+                $this->dbname = $dbname;
+                $this->dbhost = $dbhost;
+
+                $this->db_connect();
+        }
+
+        /**
+         * PHP5 style destructor and will run when database object is destroyed.
+         *
+         * @see wpdb::__construct()
+         * @since 2.0.8
+         * @return bool true
+         */
+        function __destruct() {
+                return true;
+        }
+
+        /**
+         * Set $this->charset and $this->collate
+         */
+        function init_charset() {
+                if ( function_exists('is_multisite') && is_multisite() ) {
</ins><span class="cx">                         $this->charset = 'utf8';
</span><span class="cx">                         if ( defined( 'DB_COLLATE' ) && DB_COLLATE )
</span><span class="cx">                                 $this->collate = DB_COLLATE;
</span><span class="lines">@@ -514,53 +537,34 @@
</span><span class="cx">
</span><span class="cx">                 if ( defined( 'DB_CHARSET' ) )
</span><span class="cx">                         $this->charset = DB_CHARSET;
</span><ins>+        }
</ins><span class="cx">
</span><del>-                $this->dbuser = $dbuser;
-
-                $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true );
-                if ( !$this->dbh ) {
-                        $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/"
-<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>%s</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>
-"/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' );
-                        return;
-                }
-
-                $this->ready = true;
-
-                if ( $this->has_cap( 'collation' ) && !empty( $this->charset ) ) {
-                        if ( function_exists( 'mysql_set_charset' ) ) {
-                                mysql_set_charset( $this->charset, $this->dbh );
</del><ins>+        /**
+         * Sets the connection's character set.
+         *
+         * @param resource $dbh The resource given by mysql_connect
+         * @param string $charset The character set (optional)
+         * @param string $collate The collation (optional)
+         */
+        function set_charset($dbh, $charset = null, $collate = null) {
+                if ( !isset($charset) )
+                        $charset = $this->charset;
+                if ( !isset($collate) )
+                        $collate = $this->collate;
+                if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) {
+                        if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) {
+                                mysql_set_charset( $charset, $dbh );
</ins><span class="cx">                                 $this->real_escape = true;
</span><span class="cx">                         } else {
</span><del>-                                $query = $this->prepare( 'SET NAMES %s', $this->charset );
-                                if ( ! empty( $this->collate ) )
-                                        $query .= $this->prepare( ' COLLATE %s', $this->collate );
-                                $this->query( $query );
</del><ins>+                                $query = $this->prepare( 'SET NAMES %s', $charset );
+                                if ( ! empty( $collate ) )
+                                        $query .= $this->prepare( ' COLLATE %s', $collate );
+                                mysql_query( $query, $dbh );
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-
-                $this->select( $dbname, $this->dbh );
</del><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /**
</span><del>-         * PHP5 style destructor and will run when database object is destroyed.
-         *
-         * @see wpdb::__construct()
-         * @since 2.0.8
-         * @return bool true
-         */
-        function __destruct() {
-                return true;
-        }
-
-        /**
</del><span class="cx">          * Sets the table prefix for the WordPress tables.
</span><span class="cx">          *
</span><span class="cx">          * @since 2.5.0
</span><span class="lines">@@ -1017,28 +1021,15 @@
</span><span class="cx">                 $this->last_query = null;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        function db_connect( $query = "SELECT" ) {
</del><ins>+        /**
+         * Connect to and select database
+         */
+        function db_connect() {
</ins><span class="cx">                 global $db_list, $global_db_list;
</span><del>-                if ( ! is_array( $db_list ) )
-                        return true;
</del><span class="cx">
</span><del>-                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;
-                }
</del><ins>+                $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
</ins><span class="cx">
</span><del>-                $dbhname = "dbh" . $action;
-                $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
-                if (!$this->$dbhname ) {
</del><ins>+                if ( !$this->dbh ) {
</ins><span class="cx">                         $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/"
</span><span class="cx"> <h1>Error establishing a database connection</h1>
</span><span class="cx"> <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>%s</code>. This could mean your host's database server is down.</p>
</span><span class="lines">@@ -1048,9 +1039,14 @@
</span><span class="cx">         <li>Are you sure that the database server is running?</li>
</span><span class="cx"> </ul>
</span><span class="cx"> <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>
</span><del>-"/*/WP_I18N_DB_CONN_ERROR*/, $details['db_host'] ), 'db_connect_fail' );
</del><ins>+"/*/WP_I18N_DB_CONN_ERROR*/, $this->dbhost ), 'db_connect_fail' );
</ins><span class="cx">                 }
</span><del>-                $this->select( $details[ 'db_name' ], $this->$dbhname );
</del><ins>+
+                $this->set_charset( $this->dbh );
+
+                $this->ready = true;
+
+                $this->select( $this->dbname, $this->dbh );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /**
</span><span class="lines">@@ -1083,48 +1079,23 @@
</span><span class="cx">                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
</span><span class="cx">                         $this->timer_start();
</span><span class="cx">
</span><del>-                // 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' ) && WP_USE_MULTIPLE_DB ) {
-                        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, $dbh );
</del><ins>+                $this->result = @mysql_query( $query, $this->dbh );
</ins><span class="cx">                 $this->num_queries++;
</span><span class="cx">
</span><span class="cx">                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
</span><span class="cx">                         $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
</span><span class="cx">
</span><span class="cx">                 // If there is an error then take note of it..
</span><del>-                if ( $this->last_error = mysql_error( $dbh ) ) {
</del><ins>+                if ( $this->last_error = mysql_error( $this->dbh ) ) {
</ins><span class="cx">                         $this->print_error();
</span><span class="cx">                         return false;
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) {
</span><del>-                        $this->rows_affected = mysql_affected_rows( $dbh );
</del><ins>+                        $this->rows_affected = mysql_affected_rows( $this->dbh );
</ins><span class="cx">                         // Take note of the insert_id
</span><span class="cx">                         if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) {
</span><del>-                                $this->insert_id = mysql_insert_id($dbh);
</del><ins>+                                $this->insert_id = mysql_insert_id($this->dbh);
</ins><span class="cx">                         }
</span><span class="cx">                         // Return number of rows affected
</span><span class="cx">                         $return_val = $this->rows_affected;
</span><span class="lines">@@ -1544,6 +1515,8 @@
</span><span class="cx">                         case 'group_concat' : // @since 2.7
</span><span class="cx">                         case 'subqueries' : // @since 2.7
</span><span class="cx">                                 return version_compare( $version, '4.1', '>=' );
</span><ins>+                        case 'set_charset' :
+                                return version_compare($version, '5.0.7', '>=');
</ins><span class="cx">                 };
</span><span class="cx">
</span><span class="cx">                 return false;
</span></span></pre>
</div>
</div>
</body>
</html>