<!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>[12667] trunk/wp-includes/wp-db.php: db class compatible with WP,
  MU &amp; multi site - See #11644</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12667">12667</a></dd>
<dt>Author</dt> <dd>wpmuguru</dd>
<dt>Date</dt> <dd>2010-01-08 19:26:00 +0000 (Fri, 08 Jan 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>db class compatible with WP, MU &amp; multi site - See <a href="http://trac.wordpress.org/ticket/11644">#11644</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 (12666 => 12667)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/wp-db.php        2010-01-08 19:10:50 UTC (rev 12666)
+++ trunk/wp-includes/wp-db.php        2010-01-08 19:26:00 UTC (rev 12667)
</span><span class="lines">@@ -135,6 +135,14 @@
</span><span class="cx">          * @var bool
</span><span class="cx">          */
</span><span class="cx">         var $ready = false;
</span><ins>+        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');
</ins><span class="cx"> 
</span><span class="cx">         /**
</span><span class="cx">          * WordPress Posts table
</span><span class="lines">@@ -260,7 +268,7 @@
</span><span class="cx">          * @access private
</span><span class="cx">          * @var array
</span><span class="cx">          */
</span><del>-        var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
</del><ins>+        var $tables = array('posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
</ins><span class="cx">                         'postmeta', 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta');
</span><span class="cx"> 
</span><span class="cx">         /**
</span><span class="lines">@@ -337,6 +345,8 @@
</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><ins>+                if( defined( &quot;WP_USE_MULTIPLE_DB&quot; ) &amp;&amp; CONSTANT( &quot;WP_USE_MULTIPLE_DB&quot; ) == true )
+                        $this-&gt;db_connect();
</ins><span class="cx">                 return $this-&gt;__construct($dbuser, $dbpassword, $dbname, $dbhost);
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -360,6 +370,15 @@
</span><span class="cx">                 if ( WP_DEBUG )
</span><span class="cx">                         $this-&gt;show_errors();
</span><span class="cx"> 
</span><ins>+                if( is_multisite() ) {
+                        $this-&gt;charset = 'utf8';
+                        if( defined( 'DB_COLLATE' ) &amp;&amp; constant( 'DB_COLLATE' ) != '' ) {
+                                $this-&gt;collate = constant( 'DB_COLLATE' );
+                        } else {
+                                $this-&gt;collate = 'utf8_general_ci';
+                        }
+                }
+
</ins><span class="cx">                 if ( defined('DB_CHARSET') )
</span><span class="cx">                         $this-&gt;charset = DB_CHARSET;
</span><span class="cx"> 
</span><span class="lines">@@ -397,7 +416,7 @@
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                $this-&gt;select($dbname);
</del><ins>+                $this-&gt;select($dbname, $this-&gt;dbh);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><span class="lines">@@ -427,9 +446,22 @@
</span><span class="cx">                 if ( preg_match('|[^a-z0-9_]|i', $prefix) )
</span><span class="cx">                         return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
</span><span class="cx"> 
</span><del>-                $old_prefix = $this-&gt;prefix;
-                $this-&gt;prefix = $prefix;
</del><ins>+                if( is_multisite() ) {
+                        $old_prefix = '';
+                } else {
+                        $old_prefix = $prefix;
+                }
+                if( isset( $this-&gt;base_prefix ) )
+                        $old_prefix = $this-&gt;base_prefix;
+                $this-&gt;base_prefix = $prefix;
+                foreach ( $this-&gt;global_tables as $table )
+                        $this-&gt;$table = $prefix . $table;
</ins><span class="cx"> 
</span><ins>+                if ( defined('VHOST') &amp;&amp; empty($this-&gt;blogid) )
+                        return $old_prefix;
+
+                $this-&gt;prefix = $this-&gt;get_blog_prefix( $this-&gt;blogid );
+
</ins><span class="cx">                 foreach ( (array) $this-&gt;tables as $table )
</span><span class="cx">                         $this-&gt;$table = $this-&gt;prefix . $table;
</span><span class="cx"> 
</span><span class="lines">@@ -442,6 +474,33 @@
</span><span class="cx">                 return $old_prefix;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        function set_blog_id($blog_id, $site_id = '') {
+                if ( !empty($site_id) )
+                        $this-&gt;siteid = $site_id;
+
+                $old_blog_id = $this-&gt;blogid;
+                $this-&gt;blogid = $blog_id;
+
+                $this-&gt;prefix = $this-&gt;get_blog_prefix( $this-&gt;blogid );
+
+                foreach ( $this-&gt;tables as $table )
+                        $this-&gt;$table = $this-&gt;prefix . $table;
+
+                return $old_blog_id;
+        }
+
+        function get_blog_prefix( $blog_id = '' ) {
+                if ( $blog_id ) {
+                        if( defined('MULTISITE') &amp;&amp; ( $blog_id == 0 || $blog_id == 1) ) {
+                                return $this-&gt;prefix;
+                        } else {
+                                return $this-&gt;base_prefix . $blog_id . '_';
+                        }
+                } else {
+                        return $this-&gt;base_prefix;
+                }
+        }
+
</ins><span class="cx">         /**
</span><span class="cx">          * Selects a database using the current database connection.
</span><span class="cx">          *
</span><span class="lines">@@ -453,8 +512,8 @@
</span><span class="cx">          * @param string $db MySQL database name
</span><span class="cx">          * @return null Always null.
</span><span class="cx">          */
</span><del>-        function select($db) {
-                if (!@mysql_select_db($db, $this-&gt;dbh)) {
</del><ins>+        function select($db, &amp;$dbh) {
+                if (!@mysql_select_db($db, $dbh)) {
</ins><span class="cx">                         $this-&gt;ready = false;
</span><span class="cx">                         $this-&gt;bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
</span><span class="cx"> &lt;h1&gt;Can&amp;#8217;t select database&lt;/h1&gt;
</span><span class="lines">@@ -605,14 +664,22 @@
</span><span class="cx">                 if ( !$this-&gt;show_errors )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><del>-                $str = htmlspecialchars($str, ENT_QUOTES);
-                $query = htmlspecialchars($this-&gt;last_query, ENT_QUOTES);
</del><ins>+                // If there is an error then take note of it
+                if( is_multisite() ) {
+                        $msg = &quot;WordPress database error: [$str]\n{$this-&gt;query}\n&quot;;
+                        if( defined( 'ERRORLOGFILE' ) )
+                                error_log( $msg, 3, CONSTANT( 'ERRORLOGFILE' ) );
+                        if( defined( 'DIEONDBERROR' ) )
+                                die( $msg );
+                } else {
+                        $str = htmlspecialchars($str, ENT_QUOTES);
+                        $query = htmlspecialchars($this-&gt;last_query, ENT_QUOTES);
</ins><span class="cx"> 
</span><del>-                // If there is an error then take note of it
-                print &quot;&lt;div id='error'&gt;
-                &lt;p class='wpdberror'&gt;&lt;strong&gt;WordPress database error:&lt;/strong&gt; [$str]&lt;br /&gt;
-                &lt;code&gt;$query&lt;/code&gt;&lt;/p&gt;
-                &lt;/div&gt;&quot;;
</del><ins>+                        print &quot;&lt;div id='error'&gt;
+                        &lt;p class='wpdberror'&gt;&lt;strong&gt;WordPress database error:&lt;/strong&gt; [$str]&lt;br /&gt;
+                        &lt;code&gt;$query&lt;/code&gt;&lt;/p&gt;
+                        &lt;/div&gt;&quot;;
+                }
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><span class="lines">@@ -670,6 +737,42 @@
</span><span class="cx">                 $this-&gt;last_query = null;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        function db_connect( $query = &quot;SELECT&quot; ) {
+                global $db_list, $global_db_list;
+                if( is_array( $db_list ) == false )
+                        return true;
+
+                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;
+                }
+
+                $dbhname = &quot;dbh&quot; . $action;
+                $this-&gt;$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
+                if (!$this-&gt;$dbhname ) {
+                        $this-&gt;bail(&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;$dbhost&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;);
+                }
+                $this-&gt;select( $details[ 'db_name' ], $this-&gt;$dbhname );
+        }
+
</ins><span class="cx">         /**
</span><span class="cx">          * Perform a MySQL database query, using current database connection.
</span><span class="cx">          *
</span><span class="lines">@@ -702,24 +805,49 @@
</span><span class="cx">                 // Perform the query via std mysql_query function..
</span><span class="cx">                 if ( defined('SAVEQUERIES') &amp;&amp; SAVEQUERIES )
</span><span class="cx">                         $this-&gt;timer_start();
</span><ins>+                
+                // 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( &quot;WP_USE_MULTIPLE_DB&quot; ) &amp;&amp; CONSTANT( &quot;WP_USE_MULTIPLE_DB&quot; ) == true ) {
+                        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;;
+                }
</ins><span class="cx"> 
</span><del>-                $this-&gt;result = @mysql_query($query, $this-&gt;dbh);
</del><ins>+                $this-&gt;result = @mysql_query($query, $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($this-&gt;dbh) ) {
</del><ins>+                if ( $this-&gt;last_error = mysql_error($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($this-&gt;dbh);
</del><ins>+                        $this-&gt;rows_affected = mysql_affected_rows($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($this-&gt;dbh);
</del><ins>+                                $this-&gt;insert_id = mysql_insert_id($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">@@ -1044,7 +1172,6 @@
</span><span class="cx">          *
</span><span class="cx">          * @since 2.5.0
</span><span class="cx">          * @uses $wp_version
</span><del>-         * @uses $required_mysql_version
</del><span class="cx">          *
</span><span class="cx">          * @return WP_Error
</span><span class="cx">          */
</span></span></pre>
</div>
</div>

</body>
</html>