WordPress.org

Make WordPress Core

Changeset 15537


Ignore:
Timestamp:
08/26/2010 06:34:18 PM (8 years ago)
Author:
ryan
Message:

Remove WP_USE_MULTIPLE_DB. Introduce init_charset() and set_charset(). fixes #14672

File:
1 edited

Legend:

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

    r15471 r15537  
    477477     */
    478478    function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) {
    479         if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB )
    480             $this->db_connect();
    481479        return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost );
    482480    }
     
    503501            $this->show_errors();
    504502
    505         if ( is_multisite() ) {
     503        $this->init_charset();
     504
     505        $this->dbuser = $dbuser;
     506        $this->dbpassword = $dbpassword;
     507        $this->dbname = $dbname;
     508        $this->dbhost = $dbhost;
     509
     510        $this->db_connect();
     511    }
     512
     513    /**
     514     * PHP5 style destructor and will run when database object is destroyed.
     515     *
     516     * @see wpdb::__construct()
     517     * @since 2.0.8
     518     * @return bool true
     519     */
     520    function __destruct() {
     521        return true;
     522    }
     523
     524    /**
     525     * Set $this->charset and $this->collate
     526     */
     527    function init_charset() {
     528        if ( function_exists('is_multisite') && is_multisite() ) {
    506529            $this->charset = 'utf8';
    507530            if ( defined( 'DB_COLLATE' ) && DB_COLLATE )
     
    515538        if ( defined( 'DB_CHARSET' ) )
    516539            $this->charset = DB_CHARSET;
    517 
    518         $this->dbuser = $dbuser;
    519 
    520         $this->dbh = @mysql_connect( $dbhost, $dbuser, $dbpassword, true );
    521         if ( !$this->dbh ) {
    522             $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/"
    523 <h1>Error establishing a database connection</h1>
    524 <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>
    525 <ul>
    526     <li>Are you sure you have the correct username and password?</li>
    527     <li>Are you sure that you have typed the correct hostname?</li>
    528     <li>Are you sure that the database server is running?</li>
    529 </ul>
    530 <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>
    531 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost ), 'db_connect_fail' );
    532             return;
    533         }
    534 
    535         $this->ready = true;
    536 
    537         if ( $this->has_cap( 'collation' ) && !empty( $this->charset ) ) {
    538             if ( function_exists( 'mysql_set_charset' ) ) {
    539                 mysql_set_charset( $this->charset, $this->dbh );
     540    }
     541
     542    /**
     543     * Sets the connection's character set.
     544     *
     545     * @param resource $dbh     The resource given by mysql_connect
     546     * @param string   $charset The character set (optional)
     547     * @param string   $collate The collation (optional)
     548     */
     549    function set_charset($dbh, $charset = null, $collate = null) {
     550        if ( !isset($charset) )
     551            $charset = $this->charset;
     552        if ( !isset($collate) )
     553            $collate = $this->collate;
     554        if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) {
     555            if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) {
     556                mysql_set_charset( $charset, $dbh );
    540557                $this->real_escape = true;
    541558            } else {
    542                 $query = $this->prepare( 'SET NAMES %s', $this->charset );
    543                 if ( ! empty( $this->collate ) )
    544                     $query .= $this->prepare( ' COLLATE %s', $this->collate );
    545                 $this->query( $query );
     559                $query = $this->prepare( 'SET NAMES %s', $charset );
     560                if ( ! empty( $collate ) )
     561                    $query .= $this->prepare( ' COLLATE %s', $collate );
     562                mysql_query( $query, $dbh );
    546563            }
    547564        }
    548 
    549         $this->select( $dbname, $this->dbh );
    550     }
    551 
    552     /**
    553      * PHP5 style destructor and will run when database object is destroyed.
    554      *
    555      * @see wpdb::__construct()
    556      * @since 2.0.8
    557      * @return bool true
    558      */
    559     function __destruct() {
    560         return true;
    561565    }
    562566
     
    10181022    }
    10191023
    1020     function db_connect( $query = "SELECT" ) {
     1024    /**
     1025     * Connect to and select database
     1026     */
     1027    function db_connect() {
    10211028        global $db_list, $global_db_list;
    1022         if ( ! is_array( $db_list ) )
    1023             return true;
    1024 
    1025         if ( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
    1026             $action = 'global';
    1027             $details = $global_db_list[ mt_rand( 0, count( $global_db_list ) -1 ) ];
    1028             $this->db_global = $details;
    1029         } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
    1030             $action = 'write';
    1031             $details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ];
    1032             $this->db_write = $details;
    1033         } else {
    1034             $action = '';
    1035             $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ];
    1036             $this->db_read = $details;
    1037         }
    1038 
    1039         $dbhname = "dbh" . $action;
    1040         $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
    1041         if (!$this->$dbhname ) {
     1029
     1030        $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
     1031
     1032        if ( !$this->dbh ) {
    10421033            $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/"
    10431034<h1>Error establishing a database connection</h1>
     
    10491040</ul>
    10501041<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>
    1051 "/*/WP_I18N_DB_CONN_ERROR*/, $details['db_host'] ), 'db_connect_fail' );
    1052         }
    1053         $this->select( $details[ 'db_name' ], $this->$dbhname );
     1042"/*/WP_I18N_DB_CONN_ERROR*/, $this->dbhost ), 'db_connect_fail' );
     1043        }
     1044
     1045        $this->set_charset( $this->dbh );
     1046
     1047        $this->ready = true;
     1048
     1049        $this->select( $this->dbname, $this->dbh );
    10541050    }
    10551051
     
    10841080            $this->timer_start();
    10851081
    1086         // use $this->dbh for read ops, and $this->dbhwrite for write ops
    1087         // use $this->dbhglobal for gloal table ops
    1088         unset( $dbh );
    1089         if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) {
    1090             if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
    1091                 if( false == isset( $this->dbhglobal ) ) {
    1092                     $this->db_connect( $query );
    1093                 }
    1094                 $dbh =& $this->dbhglobal;
    1095                 $this->last_db_used = "global";
    1096             } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
    1097                 if( false == isset( $this->dbhwrite ) ) {
    1098                     $this->db_connect( $query );
    1099                 }
    1100                 $dbh =& $this->dbhwrite;
    1101                 $this->last_db_used = "write";
    1102             } else {
    1103                 $dbh =& $this->dbh;
    1104                 $this->last_db_used = "read";
    1105             }
    1106         } else {
    1107             $dbh =& $this->dbh;
    1108             $this->last_db_used = "other/read";
    1109         }
    1110 
    1111         $this->result = @mysql_query( $query, $dbh );
     1082        $this->result = @mysql_query( $query, $this->dbh );
    11121083        $this->num_queries++;
    11131084
     
    11161087
    11171088        // If there is an error then take note of it..
    1118         if ( $this->last_error = mysql_error( $dbh ) ) {
     1089        if ( $this->last_error = mysql_error( $this->dbh ) ) {
    11191090            $this->print_error();
    11201091            return false;
     
    11221093
    11231094        if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) {
    1124             $this->rows_affected = mysql_affected_rows( $dbh );
     1095            $this->rows_affected = mysql_affected_rows( $this->dbh );
    11251096            // Take note of the insert_id
    11261097            if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) {
    1127                 $this->insert_id = mysql_insert_id($dbh);
     1098                $this->insert_id = mysql_insert_id($this->dbh);
    11281099            }
    11291100            // Return number of rows affected
     
    15451516            case 'subqueries' :   // @since 2.7
    15461517                return version_compare( $version, '4.1', '>=' );
     1518            case 'set_charset' :
     1519                return version_compare($version, '5.0.7', '>=');
    15471520        };
    15481521
Note: See TracChangeset for help on using the changeset viewer.