Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.0/wp-includes/wp-db.php

    r17079 r15470  
    4949 * @subpackage Database
    5050 * @since 0.71
     51 * @final
    5152 */
    5253class wpdb {
     
    6566     *
    6667     * @access private
    67      * @since 2.5.0
     68     * @since 2.5
    6869     * @var bool
    6970     */
     
    7475     *
    7576     * @see get_last_error()
    76      * @since 2.5.0
     77     * @since 2.5
    7778     * @access private
    7879     * @var string
     
    9293     * Count of rows returned by previous query
    9394     *
    94      * @since 1.2.0
     95     * @since 1.2
    9596     * @access private
    9697     * @var int
     
    456457     * A textual description of the last query/get_row/get_var call
    457458     *
    458      * @since 3.0.0
     459     * @since unknown
    459460     * @access public
    460461     * @var string
     
    476477     */
    477478    function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) {
     479        if( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB )
     480            $this->db_connect();
    478481        return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost );
    479482    }
     
    500503            $this->show_errors();
    501504
    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() ) {
     505        if ( is_multisite() ) {
    530506            $this->charset = 'utf8';
    531507            if ( defined( 'DB_COLLATE' ) && DB_COLLATE )
     
    539515        if ( defined( 'DB_CHARSET' ) )
    540516            $this->charset = DB_CHARSET;
    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 );
     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 );
    560540                $this->real_escape = true;
    561541            } else {
    562                 $query = $this->prepare( 'SET NAMES %s', $charset );
    563                 if ( ! empty( $collate ) )
    564                     $query .= $this->prepare( ' COLLATE %s', $collate );
    565                 mysql_query( $query, $dbh );
     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 );
    566546            }
    567547        }
     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;
    568561    }
    569562
     
    745738     */
    746739    function select( $db, $dbh = null) {
    747         if ( is_null($dbh) )
     740        if ( is_null($dbh) ) 
    748741            $dbh = $this->dbh;
    749742
    750743        if ( !@mysql_select_db( $db, $dbh ) ) {
    751744            $this->ready = false;
    752             $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'<h1>Can&#8217;t select database</h1>
     745            $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'
     746<h1>Can&#8217;t select database</h1>
    753747<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>
    754748<ul>
     
    781775     * @see mysql_real_escape_string()
    782776     * @see addslashes()
    783      * @since 2.8.0
     777     * @since 2.8
    784778     * @access private
    785779     *
     
    797791     * Escape data. Works on arrays.
    798792     *
    799     * @uses wpdb::_escape()
    800     * @uses wpdb::_real_escape()
    801      * @since  2.8.0
     793    * @uses wpdb::_escape()
     794    * @uses wpdb::_real_escape()
     795     * @since  2.8
    802796     * @access private
    803797     *
     
    1002996     * call to this function they can be enabled.
    1003997     *
    1004      * @since 2.5.0
     998     * @since 2.5
    1005999     * @see wpdb::hide_errors()
    10061000     * @param bool $suppress Optional. New value. Defaults to true.
     
    10251019    }
    10261020
    1027     /**
    1028      * Connect to and select database
    1029      *
    1030      * @since 3.0.0
    1031      */
    1032     function db_connect() {
     1021    function db_connect( $query = "SELECT" ) {
    10331022        global $db_list, $global_db_list;
    1034 
    1035         if ( WP_DEBUG ) {
    1036             $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
     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;
    10371034        } else {
    1038             $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
    1039         }
    1040 
    1041         if ( !$this->dbh ) {
     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 ) {
    10421043            $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/"
    10431044<h1>Error establishing a database connection</h1>
     
    10491050</ul>
    10501051<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*/, $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 );
     1052"/*/WP_I18N_DB_CONN_ERROR*/, $details['db_host'] ), 'db_connect_fail' );
     1053        }
     1054        $this->select( $details[ 'db_name' ], $this->$dbhname );
    10661055    }
    10671056
     
    10961085            $this->timer_start();
    10971086
    1098         $this->result = @mysql_query( $query, $this->dbh );
     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 );
    10991113        $this->num_queries++;
    11001114
     
    11031117
    11041118        // If there is an error then take note of it..
    1105         if ( $this->last_error = mysql_error( $this->dbh ) ) {
     1119        if ( $this->last_error = mysql_error( $dbh ) ) {
    11061120            $this->print_error();
    11071121            return false;
     
    11091123
    11101124        if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) {
    1111             $this->rows_affected = mysql_affected_rows( $this->dbh );
     1125            $this->rows_affected = mysql_affected_rows( $dbh );
    11121126            // Take note of the insert_id
    11131127            if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) {
    1114                 $this->insert_id = mysql_insert_id($this->dbh);
     1128                $this->insert_id = mysql_insert_id($dbh);
    11151129            }
    11161130            // Return number of rows affected
     
    15181532     * Determine if a database supports a particular feature
    15191533     *
    1520      * @since 2.7.0
     1534     * @since 2.7
    15211535     * @see   wpdb::db_version()
    15221536     *
     
    15321546            case 'subqueries' :   // @since 2.7
    15331547                return version_compare( $version, '4.1', '>=' );
    1534             case 'set_charset' :
    1535                 return version_compare($version, '5.0.7', '>=');
    15361548        };
    15371549
     
    15651577     * The database version number.
    15661578     *
    1567      * @since 2.7.0
    1568      *
    15691579     * @return false|string false on failure, version number on success
    15701580     */
     
    15741584}
    15751585
     1586if ( ! 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}
    15761594?>
Note: See TracChangeset for help on using the changeset viewer.