<!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 & 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 & 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( "WP_USE_MULTIPLE_DB" ) && CONSTANT( "WP_USE_MULTIPLE_DB" ) == true )
+                        $this->db_connect();
</ins><span class="cx">                 return $this->__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->show_errors();
</span><span class="cx">
</span><ins>+ if( is_multisite() ) {
+ $this->charset = 'utf8';
+ if( defined( 'DB_COLLATE' ) && constant( 'DB_COLLATE' ) != '' ) {
+ $this->collate = constant( 'DB_COLLATE' );
+ } else {
+ $this->collate = 'utf8_general_ci';
+ }
+ }
+
</ins><span class="cx">                 if ( defined('DB_CHARSET') )
</span><span class="cx">                         $this->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->select($dbname);
</del><ins>+                $this->select($dbname, $this->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->prefix;
-                $this->prefix = $prefix;
</del><ins>+ 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;
</ins><span class="cx">
</span><ins>+                if ( defined('VHOST') && empty($this->blogid) )
+                        return $old_prefix;
+
+                $this->prefix = $this->get_blog_prefix( $this->blogid );
+
</ins><span class="cx">                 foreach ( (array) $this->tables as $table )
</span><span class="cx">                         $this->$table = $this->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->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;
+                }
+        }
+
</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->dbh)) {
</del><ins>+        function select($db, &$dbh) {
+                if (!@mysql_select_db($db, $dbh)) {
</ins><span class="cx">                         $this->ready = false;
</span><span class="cx">                         $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
</span><span class="cx"> <h1>Can&#8217;t select database</h1>
</span><span class="lines">@@ -605,14 +664,22 @@
</span><span class="cx">                 if ( !$this->show_errors )
</span><span class="cx">                         return false;
</span><span class="cx">
</span><del>-                $str = htmlspecialchars($str, ENT_QUOTES);
-                $query = htmlspecialchars($this->last_query, ENT_QUOTES);
</del><ins>+ // 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);
</ins><span class="cx">
</span><del>-                // 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>";
</del><ins>+ print "<div id='error'>
+ <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
+ <code>$query</code></p>
+ </div>";
+ }
</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->last_query = null;
</span><span class="cx">         }
</span><span class="cx">
</span><ins>+        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 );
+        }
+
</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') && SAVEQUERIES )
</span><span class="cx">                         $this->timer_start();
</span><ins>+                
+                // 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";
+                }
</ins><span class="cx">
</span><del>-                $this->result = @mysql_query($query, $this->dbh);
</del><ins>+                $this->result = @mysql_query($query, $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($this->dbh) ) {
</del><ins>+                if ( $this->last_error = mysql_error($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($this->dbh);
</del><ins>+                        $this->rows_affected = mysql_affected_rows($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($this->dbh);
</del><ins>+                                $this->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->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>