Make WordPress Core


Ignore:
File:
1 edited

Legend:

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

    r15470 r17079  
    4949 * @subpackage Database
    5050 * @since 0.71
    51  * @final
    5251 */
    5352class wpdb {
     
    6665     *
    6766     * @access private
    68      * @since 2.5
     67     * @since 2.5.0
    6968     * @var bool
    7069     */
     
    7574     *
    7675     * @see get_last_error()
    77      * @since 2.5
     76     * @since 2.5.0
    7877     * @access private
    7978     * @var string
     
    9392     * Count of rows returned by previous query
    9493     *
    95      * @since 1.2
     94     * @since 1.2.0
    9695     * @access private
    9796     * @var int
     
    457456     * A textual description of the last query/get_row/get_var call
    458457     *
    459      * @since unknown
     458     * @since 3.0.0
    460459     * @access public
    461460     * @var string
     
    477476     */
    478477    function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) {
    479         if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB )
    480             $this->db_connect();
    481478        return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost );
    482479    }
     
    503500            $this->show_errors();
    504501
    505         if ( is_multisite() ) {
     502        $this->init_charset();
     503
     504        $this->dbuser = $dbuser;
     505        $this->dbpassword = $dbpassword;
     506        $this->dbname = $dbname;
     507        $this->dbhost = $dbhost;
     508
     509        $this->db_connect();
     510    }
     511
     512    /**
     513     * PHP5 style destructor and will run when database object is destroyed.
     514     *
     515     * @see wpdb::__construct()
     516     * @since 2.0.8
     517     * @return bool true
     518     */
     519    function __destruct() {
     520        return true;
     521    }
     522
     523    /**
     524     * Set $this->charset and $this->collate
     525     *
     526     * @since 3.1.0
     527     */
     528    function init_charset() {
     529        if ( function_exists('is_multisite') && is_multisite() ) {
    506530            $this->charset = 'utf8';
    507531            if ( defined( 'DB_COLLATE' ) && DB_COLLATE )
     
    515539        if ( defined( 'DB_CHARSET' ) )
    516540            $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 );
     541    }
     542
     543    /**
     544     * Sets the connection's character set.
     545     *
     546     * @since 3.1.0
     547     *
     548     * @param resource $dbh     The resource given by mysql_connect
     549     * @param string   $charset The character set (optional)
     550     * @param string   $collate The collation (optional)
     551     */
     552    function set_charset($dbh, $charset = null, $collate = null) {
     553        if ( !isset($charset) )
     554            $charset = $this->charset;
     555        if ( !isset($collate) )
     556            $collate = $this->collate;
     557        if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) {
     558            if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) {
     559                mysql_set_charset( $charset, $dbh );
    540560                $this->real_escape = true;
    541561            } 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 );
     562                $query = $this->prepare( 'SET NAMES %s', $charset );
     563                if ( ! empty( $collate ) )
     564                    $query .= $this->prepare( ' COLLATE %s', $collate );
     565                mysql_query( $query, $dbh );
    546566            }
    547567        }
    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;
    561568    }
    562569
     
    738745     */
    739746    function select( $db, $dbh = null) {
    740         if ( is_null($dbh) ) 
     747        if ( is_null($dbh) )
    741748            $dbh = $this->dbh;
    742749
    743750        if ( !@mysql_select_db( $db, $dbh ) ) {
    744751            $this->ready = false;
    745             $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'
    746 <h1>Can&#8217;t select database</h1>
     752            $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'<h1>Can&#8217;t select database</h1>
    747753<p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>%1$s</code> database.</p>
    748754<ul>
     
    775781     * @see mysql_real_escape_string()
    776782     * @see addslashes()
    777      * @since 2.8
     783     * @since 2.8.0
    778784     * @access private
    779785     *
     
    791797     * Escape data. Works on arrays.
    792798     *
    793     * @uses wpdb::_escape()
    794     * @uses wpdb::_real_escape()
    795      * @since  2.8
     799    * @uses wpdb::_escape()
     800    * @uses wpdb::_real_escape()
     801     * @since  2.8.0
    796802     * @access private
    797803     *
     
    9961002     * call to this function they can be enabled.
    9971003     *
    998      * @since 2.5
     1004     * @since 2.5.0
    9991005     * @see wpdb::hide_errors()
    10001006     * @param bool $suppress Optional. New value. Defaults to true.
     
    10191025    }
    10201026
    1021     function db_connect( $query = "SELECT" ) {
     1027    /**
     1028     * Connect to and select database
     1029     *
     1030     * @since 3.0.0
     1031     */
     1032    function db_connect() {
    10221033        global $db_list, $global_db_list;
    1023         if ( ! is_array( $db_list ) )
    1024             return true;
    1025 
    1026         if ( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
    1027             $action = 'global';
    1028             $details = $global_db_list[ mt_rand( 0, count( $global_db_list ) -1 ) ];
    1029             $this->db_global = $details;
    1030         } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
    1031             $action = 'write';
    1032             $details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ];
    1033             $this->db_write = $details;
     1034
     1035        if ( WP_DEBUG ) {
     1036            $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
    10341037        } else {
    1035             $action = '';
    1036             $details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ];
    1037             $this->db_read = $details;
    1038         }
    1039 
    1040         $dbhname = "dbh" . $action;
    1041         $this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
    1042         if (!$this->$dbhname ) {
     1038            $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
     1039        }
     1040
     1041        if ( !$this->dbh ) {
    10431042            $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/"
    10441043<h1>Error establishing a database connection</h1>
     
    10501049</ul>
    10511050<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>
    1052 "/*/WP_I18N_DB_CONN_ERROR*/, $details['db_host'] ), 'db_connect_fail' );
    1053         }
    1054         $this->select( $details[ 'db_name' ], $this->$dbhname );
     1051"/*/WP_I18N_DB_CONN_ERROR*/, $this->dbhost ), 'db_connect_fail' );
     1052
     1053            // If show errors is disabled then we need to die anyway as we don't have a working DB connection
     1054            // unless we're trying to test the initial connection, in which case setup-config.php will handle.
     1055            if ( defined( 'WP_SETUP_CONFIG' ) )
     1056                return;
     1057
     1058            die();
     1059        }
     1060
     1061        $this->set_charset( $this->dbh );
     1062
     1063        $this->ready = true;
     1064
     1065        $this->select( $this->dbname, $this->dbh );
    10551066    }
    10561067
     
    10851096            $this->timer_start();
    10861097
    1087         // use $this->dbh for read ops, and $this->dbhwrite for write ops
    1088         // use $this->dbhglobal for gloal table ops
    1089         unset( $dbh );
    1090         if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) {
    1091             if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
    1092                 if( false == isset( $this->dbhglobal ) ) {
    1093                     $this->db_connect( $query );
    1094                 }
    1095                 $dbh =& $this->dbhglobal;
    1096                 $this->last_db_used = "global";
    1097             } elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
    1098                 if( false == isset( $this->dbhwrite ) ) {
    1099                     $this->db_connect( $query );
    1100                 }
    1101                 $dbh =& $this->dbhwrite;
    1102                 $this->last_db_used = "write";
    1103             } else {
    1104                 $dbh =& $this->dbh;
    1105                 $this->last_db_used = "read";
    1106             }
    1107         } else {
    1108             $dbh =& $this->dbh;
    1109             $this->last_db_used = "other/read";
    1110         }
    1111 
    1112         $this->result = @mysql_query( $query, $dbh );
     1098        $this->result = @mysql_query( $query, $this->dbh );
    11131099        $this->num_queries++;
    11141100
     
    11171103
    11181104        // If there is an error then take note of it..
    1119         if ( $this->last_error = mysql_error( $dbh ) ) {
     1105        if ( $this->last_error = mysql_error( $this->dbh ) ) {
    11201106            $this->print_error();
    11211107            return false;
     
    11231109
    11241110        if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) {
    1125             $this->rows_affected = mysql_affected_rows( $dbh );
     1111            $this->rows_affected = mysql_affected_rows( $this->dbh );
    11261112            // Take note of the insert_id
    11271113            if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) {
    1128                 $this->insert_id = mysql_insert_id($dbh);
     1114                $this->insert_id = mysql_insert_id($this->dbh);
    11291115            }
    11301116            // Return number of rows affected
     
    15321518     * Determine if a database supports a particular feature
    15331519     *
    1534      * @since 2.7
     1520     * @since 2.7.0
    15351521     * @see   wpdb::db_version()
    15361522     *
     
    15461532            case 'subqueries' :   // @since 2.7
    15471533                return version_compare( $version, '4.1', '>=' );
     1534            case 'set_charset' :
     1535                return version_compare($version, '5.0.7', '>=');
    15481536        };
    15491537
     
    15771565     * The database version number.
    15781566     *
     1567     * @since 2.7.0
     1568     *
    15791569     * @return false|string false on failure, version number on success
    15801570     */
     
    15841574}
    15851575
    1586 if ( ! isset( $wpdb ) ) {
    1587     /**
    1588      * WordPress Database Object, if it isn't set already in wp-content/db.php
    1589      * @global object $wpdb Creates a new wpdb object based on wp-config.php Constants for the database
    1590      * @since 0.71
    1591      */
    1592     $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
    1593 }
    15941576?>
Note: See TracChangeset for help on using the changeset viewer.