Make WordPress Core

Ticket #21663: 21663.6.diff

File 21663.6.diff, 7.8 KB (added by aaroncampbell, 11 years ago)
  • src/wp-includes/wp-db.php

     
    510510        public $is_mysql = null;
    511511
    512512        /**
     513         * Whether to use mysqli over mysql.
     514         *
     515         * @since 3.8.0
     516         * @access private
     517         * @var bool
     518         */
     519        private $use_mysqli = false;
     520
     521        /**
    513522         * Connects to the database server and selects a database
    514523         *
    515524         * PHP5 style constructor for compatibility with PHP5. Does
     
    530539                if ( WP_DEBUG && WP_DEBUG_DISPLAY )
    531540                        $this->show_errors();
    532541
     542                $this->use_mysqli = ( version_compare( phpversion(), '5.5', '>=' ) && function_exists( 'mysqli_connect' ) );
     543
    533544                $this->init_charset();
    534545
    535546                $this->dbuser = $dbuser;
     
    637648                if ( ! isset( $collate ) )
    638649                        $collate = $this->collate;
    639650                if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) {
    640                         if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
    641                                 mysql_set_charset( $charset, $dbh );
     651                        if ( $this->use_mysqli ) {
     652                                        mysqli_set_charset( $dbh, $charset );
    642653                        } else {
    643                                 $query = $this->prepare( 'SET NAMES %s', $charset );
    644                                 if ( ! empty( $collate ) )
    645                                         $query .= $this->prepare( ' COLLATE %s', $collate );
    646                                 mysql_query( $query, $dbh );
     654                                if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
     655                                        mysql_set_charset( $charset, $dbh );
     656                                } else {
     657                                        $query = $this->prepare( 'SET NAMES %s', $charset );
     658                                        if ( ! empty( $collate ) )
     659                                                $query .= $this->prepare( ' COLLATE %s', $collate );
     660                                        mysql_query( $query, $dbh );
     661                                }
    647662                        }
    648663                }
    649664        }
     
    830845                if ( is_null($dbh) )
    831846                        $dbh = $this->dbh;
    832847
    833                 if ( !@mysql_select_db( $db, $dbh ) ) {
     848                if ( $this->use_mysqli ) {
     849                        $success = @mysqli_select_db( $dbh, $db );
     850                } else {
     851                        $success = @mysql_select_db( $db, $dbh );
     852                }
     853                if ( ! $success ) {
    834854                        $this->ready = false;
    835855                        wp_load_translations_early();
    836856                        $this->bail( sprintf( __( '<h1>Can&#8217;t select database</h1>
     
    866886        }
    867887
    868888        /**
    869          * Real escape, using mysql_real_escape_string()
     889         * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string()
    870890         *
     891         * @see mysqli_real_escape_string()
    871892         * @see mysql_real_escape_string()
    872893         * @since 2.8.0
    873894         * @access private
     
    876897         * @return string escaped
    877898         */
    878899        function _real_escape( $string ) {
    879                 if ( $this->dbh )
    880                         return mysql_real_escape_string( $string, $this->dbh );
     900                if ( $this->dbh ) {
     901                        if ( $this->use_mysqli ) {
     902                                return mysqli_real_escape_string( $this->dbh, $string );
     903                        } else {
     904                                return mysql_real_escape_string( $string, $this->dbh );
     905                        }
     906                }
    881907
    882908                $class = get_class( $this );
    883909                _doing_it_wrong( $class, "$class must set a database connection for use with escaping.", E_USER_NOTICE );
     
    10181044        function print_error( $str = '' ) {
    10191045                global $EZSQL_ERROR;
    10201046
    1021                 if ( !$str )
    1022                         $str = mysql_error( $this->dbh );
     1047                if ( !$str ) {
     1048                        if ( $this->use_mysqli ) {
     1049                                $str = mysqli_error( $this->dbh );
     1050                        } else {
     1051                                $str = mysql_error( $this->dbh );
     1052                        }
     1053                }
    10231054                $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
    10241055
    10251056                if ( $this->suppress_errors )
     
    11221153                $this->rows_affected = $this->num_rows = 0;
    11231154                $this->last_error  = '';
    11241155
    1125                 if ( is_resource( $this->result ) )
    1126                         mysql_free_result( $this->result );
     1156                if ( is_resource( $this->result ) ) {
     1157                        if ( $this->use_mysqli ) {
     1158                                mysqli_free_result( $this->result );
     1159                        } else {
     1160                                mysql_free_result( $this->result );
     1161                        }
     1162                }
    11271163        }
    11281164
    11291165        /**
     
    11381174                $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true;
    11391175                $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0;
    11401176
    1141                 if ( WP_DEBUG ) {
    1142                         $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1177                if ( $this->use_mysqli ) {
     1178                        $this->dbh = mysqli_init();
     1179                        if ( WP_DEBUG ) {
     1180                                mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags );
     1181                        } else {
     1182                                @mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags );
     1183                        }
    11431184                } else {
    1144                         $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1185                        if ( WP_DEBUG ) {
     1186                                $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1187                        } else {
     1188                                $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1189                        }
    11451190                }
    11461191
    11471192                if ( !$this->dbh ) {
     
    12021247                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    12031248                        $this->timer_start();
    12041249
    1205                 $this->result = @mysql_query( $query, $this->dbh );
     1250                if ( $this->use_mysqli ) {
     1251                        $this->result = @mysqli_query( $this->dbh, $query );
     1252                } else {
     1253                        $this->result = @mysql_query( $query, $this->dbh );
     1254                }
    12061255                $this->num_queries++;
    12071256
    12081257                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    12091258                        $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
    12101259
    12111260                // If there is an error then take note of it..
    1212                 if ( $this->last_error = mysql_error( $this->dbh ) ) {
     1261                if ( $this->use_mysqli ) {
     1262                        $this->last_error = mysqli_error( $this->dbh );
     1263                } else {
     1264                        $this->last_error = mysql_error( $this->dbh );
     1265                }
     1266
     1267                if ( $this->last_error ) {
    12131268                        // Clear insert_id on a subsequent failed insert.
    12141269                        if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) )
    12151270                                $this->insert_id = 0;
     
    12211276                if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) {
    12221277                        $return_val = $this->result;
    12231278                } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
    1224                         $this->rows_affected = mysql_affected_rows( $this->dbh );
     1279                        if ( $this->use_mysqli ) {
     1280                                $this->rows_affected = mysqli_affected_rows( $this->dbh );
     1281                        } else {
     1282                                $this->rows_affected = mysql_affected_rows( $this->dbh );
     1283                        }
    12251284                        // Take note of the insert_id
    12261285                        if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
    1227                                 $this->insert_id = mysql_insert_id($this->dbh);
     1286                                if ( $this->use_mysqli ) {
     1287                                        $this->insert_id = mysqli_insert_id( $this->dbh );
     1288                                } else {
     1289                                        $this->insert_id = mysql_insert_id( $this->dbh );
     1290                                }
    12281291                        }
    12291292                        // Return number of rows affected
    12301293                        $return_val = $this->rows_affected;
    12311294                } else {
    12321295                        $num_rows = 0;
    1233                         while ( $row = @mysql_fetch_object( $this->result ) ) {
    1234                                 $this->last_result[$num_rows] = $row;
    1235                                 $num_rows++;
     1296                        if ( $this->use_mysqli ) {
     1297                                while ( $row = @mysqli_fetch_object( $this->result ) ) {
     1298                                        $this->last_result[$num_rows] = $row;
     1299                                        $num_rows++;
     1300                                }
     1301                        } else {
     1302                                while ( $row = @mysql_fetch_object( $this->result ) ) {
     1303                                        $this->last_result[$num_rows] = $row;
     1304                                        $num_rows++;
     1305                                }
    12361306                        }
    12371307
    12381308                        // Log number of rows the query returned
     
    15741644                if ( $this->col_info )
    15751645                        return;
    15761646
    1577                 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
    1578                         $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
     1647                if ( $this->use_mysqli ) {
     1648                        for ( $i = 0; $i < @mysqli_num_fields( $this->result ); $i++ ) {
     1649                                $this->col_info[ $i ] = @mysqli_fetch_field( $this->result, $i );
     1650                        }
     1651                } else {
     1652                        for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
     1653                                $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
     1654                        }
    15791655                }
    15801656        }
    15811657
     
    17471823         * @return false|string false on failure, version number on success
    17481824         */
    17491825        function db_version() {
    1750                 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
     1826                if ( $this->use_mysqli ) {
     1827                        $server_info = mysqli_get_server_info( $this->dbh );
     1828                } else {
     1829                        $server_info = mysql_get_server_info( $this->dbh );
     1830                }
     1831                return preg_replace( '/[^0-9.].*/', '', $server_info );
    17511832        }
    17521833}