Make WordPress Core

Changeset 12667


Ignore:
Timestamp:
01/08/2010 07:26:00 PM (14 years ago)
Author:
wpmuguru
Message:

db class compatible with WP, MU & multi site - See #11644

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/wp-db.php

    r12573 r12667  
    136136     */
    137137    var $ready = false;
     138    var $blogid = 0;
     139    var $siteid = 0;
     140    var $blogs;
     141    var $signups;
     142    var $site;
     143    var $sitemeta;
     144    var $sitecategories;
     145    var $global_tables = array('blogs', 'signups', 'site', 'sitemeta', 'users', 'usermeta', 'sitecategories', 'registration_log', 'blog_versions');
    138146
    139147    /**
     
    261269     * @var array
    262270     */
    263     var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
     271    var $tables = array('posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
    264272            'postmeta', 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta');
    265273
     
    338346     */
    339347    function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
     348        if( defined( "WP_USE_MULTIPLE_DB" ) && CONSTANT( "WP_USE_MULTIPLE_DB" ) == true )
     349            $this->db_connect();
    340350        return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
    341351    }
     
    360370        if ( WP_DEBUG )
    361371            $this->show_errors();
     372
     373                if( is_multisite() ) {
     374                        $this->charset = 'utf8';
     375                        if( defined( 'DB_COLLATE' ) && constant( 'DB_COLLATE' ) != '' ) {
     376                                $this->collate = constant( 'DB_COLLATE' );
     377                        } else {
     378                                $this->collate = 'utf8_general_ci';
     379                        }
     380                }
    362381
    363382        if ( defined('DB_CHARSET') )
     
    398417        }
    399418
    400         $this->select($dbname);
     419        $this->select($dbname, $this->dbh);
    401420    }
    402421
     
    428447            return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
    429448
    430         $old_prefix = $this->prefix;
    431         $this->prefix = $prefix;
     449                if( is_multisite() ) {
     450                        $old_prefix = '';
     451                } else {
     452                        $old_prefix = $prefix;
     453                }
     454        if( isset( $this->base_prefix ) )
     455            $old_prefix = $this->base_prefix;
     456        $this->base_prefix = $prefix;
     457        foreach ( $this->global_tables as $table )
     458            $this->$table = $prefix . $table;
     459
     460        if ( defined('VHOST') && empty($this->blogid) )
     461            return $old_prefix;
     462
     463        $this->prefix = $this->get_blog_prefix( $this->blogid );
    432464
    433465        foreach ( (array) $this->tables as $table )
     
    443475    }
    444476
     477    function set_blog_id($blog_id, $site_id = '') {
     478        if ( !empty($site_id) )
     479            $this->siteid = $site_id;
     480
     481        $old_blog_id = $this->blogid;
     482        $this->blogid = $blog_id;
     483
     484        $this->prefix = $this->get_blog_prefix( $this->blogid );
     485
     486        foreach ( $this->tables as $table )
     487            $this->$table = $this->prefix . $table;
     488
     489        return $old_blog_id;
     490    }
     491
     492    function get_blog_prefix( $blog_id = '' ) {
     493        if ( $blog_id ) {
     494                        if( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1) ) {
     495                    return $this->prefix;
     496                        } else {
     497                    return $this->base_prefix . $blog_id . '_';
     498                        }
     499        } else {
     500            return $this->base_prefix;
     501        }
     502    }
     503
    445504    /**
    446505     * Selects a database using the current database connection.
     
    454513     * @return null Always null.
    455514     */
    456     function select($db) {
    457         if (!@mysql_select_db($db, $this->dbh)) {
     515    function select($db, &$dbh) {
     516        if (!@mysql_select_db($db, $dbh)) {
    458517            $this->ready = false;
    459518            $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
     
    606665            return false;
    607666
    608         $str = htmlspecialchars($str, ENT_QUOTES);
    609         $query = htmlspecialchars($this->last_query, ENT_QUOTES);
    610 
    611         // If there is an error then take note of it
    612         print "<div id='error'>
    613         <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
    614         <code>$query</code></p>
    615         </div>";
     667                // If there is an error then take note of it
     668                if( is_multisite() ) {
     669                        $msg = "WordPress database error: [$str]\n{$this->query}\n";
     670                        if( defined( 'ERRORLOGFILE' ) )
     671                                error_log( $msg, 3, CONSTANT( 'ERRORLOGFILE' ) );
     672                        if( defined( 'DIEONDBERROR' ) )
     673                                die( $msg );
     674                } else {
     675                        $str = htmlspecialchars($str, ENT_QUOTES);
     676                        $query = htmlspecialchars($this->last_query, ENT_QUOTES);
     677
     678                        print "<div id='error'>
     679                        <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
     680                        <code>$query</code></p>
     681                        </div>";
     682                }
    616683    }
    617684
     
    671738    }
    672739
     740    function db_connect( $query = "SELECT" ) {
     741        global $db_list, $global_db_list;
     742        if( is_array( $db_list ) == false )
     743            return true;
     744
     745        if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
     746            $action = 'global';
     747            $details = $global_db_list[ mt_rand( 0, count( $global_db_list ) -1 ) ];
     748            $this->db_global = $details;
     749        } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
     750            $action = 'write';
     751            $details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ];
     752            $this->db_write = $details;
     753        } else {
     754            $action = '';
     755            $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ];
     756            $this->db_read = $details;
     757        }
     758
     759        $dbhname = "dbh" . $action;
     760        $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
     761        if (!$this->$dbhname ) {
     762            $this->bail("
     763<h1>Error establishing a database connection</h1>
     764<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>
     765<ul>
     766    <li>Are you sure you have the correct username and password?</li>
     767    <li>Are you sure that you have typed the correct hostname?</li>
     768    <li>Are you sure that the database server is running?</li>
     769</ul>
     770<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>
     771");
     772        }
     773        $this->select( $details[ 'db_name' ], $this->$dbhname );
     774    }
     775
    673776    /**
    674777     * Perform a MySQL database query, using current database connection.
     
    703806        if ( defined('SAVEQUERIES') && SAVEQUERIES )
    704807            $this->timer_start();
    705 
    706         $this->result = @mysql_query($query, $this->dbh);
     808       
     809        // use $this->dbh for read ops, and $this->dbhwrite for write ops
     810        // use $this->dbhglobal for gloal table ops
     811        unset( $dbh );
     812        if( defined( "WP_USE_MULTIPLE_DB" ) && CONSTANT( "WP_USE_MULTIPLE_DB" ) == true ) {
     813            if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
     814                if( false == isset( $this->dbhglobal ) ) {
     815                    $this->db_connect( $query );
     816                }
     817                $dbh =& $this->dbhglobal;
     818                $this->last_db_used = "global";
     819            } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
     820                if( false == isset( $this->dbhwrite ) ) {
     821                    $this->db_connect( $query );
     822                }
     823                $dbh =& $this->dbhwrite;
     824                $this->last_db_used = "write";
     825            } else {
     826                $dbh =& $this->dbh;
     827                $this->last_db_used = "read";
     828            }
     829        } else {
     830            $dbh =& $this->dbh;
     831            $this->last_db_used = "other/read";
     832        }
     833
     834        $this->result = @mysql_query($query, $dbh);
    707835        ++$this->num_queries;
    708836
     
    711839
    712840        // If there is an error then take note of it..
    713         if ( $this->last_error = mysql_error($this->dbh) ) {
     841        if ( $this->last_error = mysql_error($dbh) ) {
    714842            $this->print_error();
    715843            return false;
     
    717845
    718846        if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {
    719             $this->rows_affected = mysql_affected_rows($this->dbh);
     847            $this->rows_affected = mysql_affected_rows($dbh);
    720848            // Take note of the insert_id
    721849            if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
    722                 $this->insert_id = mysql_insert_id($this->dbh);
     850                $this->insert_id = mysql_insert_id($dbh);
    723851            }
    724852            // Return number of rows affected
     
    10451173     * @since 2.5.0
    10461174     * @uses $wp_version
    1047      * @uses $required_mysql_version
    10481175     *
    10491176     * @return WP_Error
Note: See TracChangeset for help on using the changeset viewer.