<!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' ) &amp;&amp; WP_USE_MULTIPLE_DB )
-                        $this-&gt;db_connect();
</del><span class="cx">                 return $this-&gt;__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-&gt;show_errors();
</span><span class="cx"> 
</span><del>-                if ( is_multisite() ) {
</del><ins>+                $this-&gt;init_charset();
+
+                $this-&gt;dbuser = $dbuser;
+                $this-&gt;dbpassword = $dbpassword;
+                $this-&gt;dbname = $dbname;
+                $this-&gt;dbhost = $dbhost;
+
+                $this-&gt;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-&gt;charset and $this-&gt;collate
+         */
+        function init_charset() {
+                if ( function_exists('is_multisite') &amp;&amp; is_multisite() ) {
</ins><span class="cx">                         $this-&gt;charset = 'utf8';
</span><span class="cx">                         if ( defined( 'DB_COLLATE' ) &amp;&amp; DB_COLLATE )
</span><span class="cx">                                 $this-&gt;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-&gt;charset = DB_CHARSET;
</span><ins>+        }
</ins><span class="cx"> 
</span><del>-                $this-&gt;dbuser = $dbuser;
-
-                $this-&gt;dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true );
-                if ( !$this-&gt;dbh ) {
-                        $this-&gt;bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/&quot;
-&lt;h1&gt;Error establishing a database connection&lt;/h1&gt;
-&lt;p&gt;This either means that the username and password information in your &lt;code&gt;wp-config.php&lt;/code&gt; file is incorrect or we can't contact the database server at &lt;code&gt;%s&lt;/code&gt;. This could mean your host's database server is down.&lt;/p&gt;
-&lt;ul&gt;
-        &lt;li&gt;Are you sure you have the correct username and password?&lt;/li&gt;
-        &lt;li&gt;Are you sure that you have typed the correct hostname?&lt;/li&gt;
-        &lt;li&gt;Are you sure that the database server is running?&lt;/li&gt;
-&lt;/ul&gt;
-&lt;p&gt;If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the &lt;a href='http://wordpress.org/support/'&gt;WordPress Support Forums&lt;/a&gt;.&lt;/p&gt;
-&quot;/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' );
-                        return;
-                }
-
-                $this-&gt;ready = true;
-
-                if ( $this-&gt;has_cap( 'collation' ) &amp;&amp; !empty( $this-&gt;charset ) ) {
-                        if ( function_exists( 'mysql_set_charset' ) ) {
-                                mysql_set_charset( $this-&gt;charset, $this-&gt;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-&gt;charset;
+                if ( !isset($collate) )
+                        $collate = $this-&gt;collate;
+                if ( $this-&gt;has_cap( 'collation', $dbh ) &amp;&amp; !empty( $charset ) ) {
+                        if ( function_exists( 'mysql_set_charset' ) &amp;&amp; $this-&gt;has_cap( 'set_charset', $dbh ) ) {
+                                mysql_set_charset( $charset, $dbh );
</ins><span class="cx">                                 $this-&gt;real_escape = true;
</span><span class="cx">                         } else {
</span><del>-                                $query = $this-&gt;prepare( 'SET NAMES %s', $this-&gt;charset );
-                                if ( ! empty( $this-&gt;collate ) )
-                                        $query .= $this-&gt;prepare( ' COLLATE %s', $this-&gt;collate );
-                                $this-&gt;query( $query );
</del><ins>+                                $query = $this-&gt;prepare( 'SET NAMES %s', $charset );
+                                if ( ! empty( $collate ) )
+                                        $query .= $this-&gt;prepare( ' COLLATE %s', $collate );
+                                mysql_query( $query, $dbh );
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-
-                $this-&gt;select( $dbname, $this-&gt;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-&gt;last_query  = null;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function db_connect( $query = &quot;SELECT&quot; ) {
</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-&gt;blogs != '' &amp;&amp; preg_match(&quot;/(&quot; . $this-&gt;blogs . &quot;|&quot; . $this-&gt;users . &quot;|&quot; . $this-&gt;usermeta . &quot;|&quot; . $this-&gt;site . &quot;|&quot; . $this-&gt;sitemeta . &quot;|&quot; . $this-&gt;sitecategories . &quot;)/i&quot;,$query) ) {
-                        $action = 'global';
-                        $details = $global_db_list[ mt_rand( 0, count( $global_db_list ) -1 ) ];
-                        $this-&gt;db_global = $details;
-                } elseif ( preg_match(&quot;/^\\s*(alter table|create|insert|delete|update|replace) /i&quot;,$query) ) {
-                        $action = 'write';
-                        $details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ];
-                        $this-&gt;db_write = $details;
-                } else {
-                        $action = '';
-                        $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ];
-                        $this-&gt;db_read = $details;
-                }
</del><ins>+                $this-&gt;dbh = @mysql_connect( $this-&gt;dbhost, $this-&gt;dbuser, $this-&gt;dbpassword, true );
</ins><span class="cx"> 
</span><del>-                $dbhname = &quot;dbh&quot; . $action;
-                $this-&gt;$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
-                if (!$this-&gt;$dbhname ) {
</del><ins>+                if ( !$this-&gt;dbh ) {
</ins><span class="cx">                         $this-&gt;bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/&quot;
</span><span class="cx"> &lt;h1&gt;Error establishing a database connection&lt;/h1&gt;
</span><span class="cx"> &lt;p&gt;This either means that the username and password information in your &lt;code&gt;wp-config.php&lt;/code&gt; file is incorrect or we can't contact the database server at &lt;code&gt;%s&lt;/code&gt;. This could mean your host's database server is down.&lt;/p&gt;
</span><span class="lines">@@ -1048,9 +1039,14 @@
</span><span class="cx">         &lt;li&gt;Are you sure that the database server is running?&lt;/li&gt;
</span><span class="cx"> &lt;/ul&gt;
</span><span class="cx"> &lt;p&gt;If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the &lt;a href='http://wordpress.org/support/'&gt;WordPress Support Forums&lt;/a&gt;.&lt;/p&gt;
</span><del>-&quot;/*/WP_I18N_DB_CONN_ERROR*/, $details['db_host'] ), 'db_connect_fail' );
</del><ins>+&quot;/*/WP_I18N_DB_CONN_ERROR*/, $this-&gt;dbhost ), 'db_connect_fail' );
</ins><span class="cx">                 }
</span><del>-                $this-&gt;select( $details[ 'db_name' ], $this-&gt;$dbhname );
</del><ins>+
+                $this-&gt;set_charset( $this-&gt;dbh );
+
+                $this-&gt;ready = true;
+
+                $this-&gt;select( $this-&gt;dbname, $this-&gt;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' ) &amp;&amp; SAVEQUERIES )
</span><span class="cx">                         $this-&gt;timer_start();
</span><span class="cx"> 
</span><del>-                // use $this-&gt;dbh for read ops, and $this-&gt;dbhwrite for write ops
-                // use $this-&gt;dbhglobal for gloal table ops
-                unset( $dbh );
-                if( defined( 'WP_USE_MULTIPLE_DB' ) &amp;&amp; WP_USE_MULTIPLE_DB ) {
-                        if( $this-&gt;blogs != '' &amp;&amp; preg_match(&quot;/(&quot; . $this-&gt;blogs . &quot;|&quot; . $this-&gt;users . &quot;|&quot; . $this-&gt;usermeta . &quot;|&quot; . $this-&gt;site . &quot;|&quot; . $this-&gt;sitemeta . &quot;|&quot; . $this-&gt;sitecategories . &quot;)/i&quot;,$query) ) {
-                                if( false == isset( $this-&gt;dbhglobal ) ) {
-                                        $this-&gt;db_connect( $query );
-                                }
-                                $dbh =&amp; $this-&gt;dbhglobal;
-                                $this-&gt;last_db_used = &quot;global&quot;;
-                        } elseif ( preg_match(&quot;/^\\s*(alter table|create|insert|delete|update|replace) /i&quot;,$query) ) {
-                                if( false == isset( $this-&gt;dbhwrite ) ) {
-                                        $this-&gt;db_connect( $query );
-                                }
-                                $dbh =&amp; $this-&gt;dbhwrite;
-                                $this-&gt;last_db_used = &quot;write&quot;;
-                        } else {
-                                $dbh =&amp; $this-&gt;dbh;
-                                $this-&gt;last_db_used = &quot;read&quot;;
-                        }
-                } else {
-                        $dbh =&amp; $this-&gt;dbh;
-                        $this-&gt;last_db_used = &quot;other/read&quot;;
-                }
-
-                $this-&gt;result = @mysql_query( $query, $dbh );
</del><ins>+                $this-&gt;result = @mysql_query( $query, $this-&gt;dbh );
</ins><span class="cx">                 $this-&gt;num_queries++;
</span><span class="cx"> 
</span><span class="cx">                 if ( defined( 'SAVEQUERIES' ) &amp;&amp; SAVEQUERIES )
</span><span class="cx">                         $this-&gt;queries[] = array( $query, $this-&gt;timer_stop(), $this-&gt;get_caller() );
</span><span class="cx"> 
</span><span class="cx">                 // If there is an error then take note of it..
</span><del>-                if ( $this-&gt;last_error = mysql_error( $dbh ) ) {
</del><ins>+                if ( $this-&gt;last_error = mysql_error( $this-&gt;dbh ) ) {
</ins><span class="cx">                         $this-&gt;print_error();
</span><span class="cx">                         return false;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 if ( preg_match( &quot;/^\\s*(insert|delete|update|replace|alter) /i&quot;, $query ) ) {
</span><del>-                        $this-&gt;rows_affected = mysql_affected_rows( $dbh );
</del><ins>+                        $this-&gt;rows_affected = mysql_affected_rows( $this-&gt;dbh );
</ins><span class="cx">                         // Take note of the insert_id
</span><span class="cx">                         if ( preg_match( &quot;/^\\s*(insert|replace) /i&quot;, $query ) ) {
</span><del>-                                $this-&gt;insert_id = mysql_insert_id($dbh);
</del><ins>+                                $this-&gt;insert_id = mysql_insert_id($this-&gt;dbh);
</ins><span class="cx">                         }
</span><span class="cx">                         // Return number of rows affected
</span><span class="cx">                         $return_val = $this-&gt;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', '&gt;=' );
</span><ins>+                        case 'set_charset' :
+                                return version_compare($version, '5.0.7', '&gt;=');
</ins><span class="cx">                 };
</span><span class="cx"> 
</span><span class="cx">                 return false;
</span></span></pre>
</div>
</div>

</body>
</html>