Make WordPress Core

Ticket #21663: 21663.11.diff

File 21663.11.diff, 10.7 KB (added by pento, 11 years ago)
  • src/wp-includes/wp-db.php

     
    530530                'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES', 'TRADITIONAL' );
    531531
    532532        /**
     533         * Whether to use mysqli over mysql.
     534         *
     535         * @since 3.9.0
     536         * @access private
     537         * @var bool
     538         */
     539        private $use_mysqli = false;
     540
     541        /**
    533542         * Connects to the database server and selects a database
    534543         *
    535544         * PHP5 style constructor for compatibility with PHP5. Does
     
    550559                if ( WP_DEBUG && WP_DEBUG_DISPLAY )
    551560                        $this->show_errors();
    552561
     562                $this->use_mysqli = ( version_compare( phpversion(), '5.5', '>=' ) && function_exists( 'mysqli_connect' ) );
     563
    553564                $this->init_charset();
    554565
    555566                $this->dbuser = $dbuser;
     
    657668                if ( ! isset( $collate ) )
    658669                        $collate = $this->collate;
    659670                if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) {
    660                         if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
    661                                 mysql_set_charset( $charset, $dbh );
     671                        if ( $this->use_mysqli ) {
     672                                if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
     673                                        mysqli_set_charset( $dbh, $charset );
     674                                } else {
     675                                        $query = $this->prepare( 'SET NAMES %s', $charset );
     676                                        if ( ! empty( $collate ) )
     677                                                $query .= $this->prepare( ' COLLATE %s', $collate );
     678                                        mysqli_query( $query, $dbh );
     679                                }
    662680                        } else {
    663                                 $query = $this->prepare( 'SET NAMES %s', $charset );
    664                                 if ( ! empty( $collate ) )
    665                                         $query .= $this->prepare( ' COLLATE %s', $collate );
    666                                 mysql_query( $query, $dbh );
     681                                if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
     682                                        mysql_set_charset( $charset, $dbh );
     683                                } else {
     684                                        $query = $this->prepare( 'SET NAMES %s', $charset );
     685                                        if ( ! empty( $collate ) )
     686                                                $query .= $this->prepare( ' COLLATE %s', $collate );
     687                                        mysql_query( $query, $dbh );
     688                                }
    667689                        }
    668690                }
    669691        }
     
    680702         */
    681703        function set_sql_mode( $modes = array() ) {
    682704                if ( empty( $modes ) ) {
    683                         $res = mysql_query( 'SELECT @@SESSION.sql_mode;', $this->dbh );
     705                        if ( $this->use_mysqli ) {
     706                                $res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' );
     707                        } else {
     708                                $res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh );
     709                        }
     710
    684711                        if ( empty( $res ) ) {
    685712                                return;
    686713                        }
    687714
    688                         $modes_str = mysql_result( $res, 0 );
     715                        if ( $this->use_mysqli ) {
     716                                $modes_array = mysqli_fetch_array( $res );
     717                                if ( empty( $modes_array[0] ) ) {
     718                                        return;
     719                                }
     720                                $modes_str = $modes_array[0];
     721                        } else {
     722                                $modes_str = mysql_result( $res, 0 );
     723                        }
    689724
    690725                        if ( empty( $modes_str ) ) {
    691726                                return;
     
    715750
    716751                $modes_str = implode( ',', $modes );
    717752
    718                 mysql_query( "SET SESSION sql_mode='$modes_str';", $this->dbh );
     753                if ( $this->use_mysqli ) {
     754                        mysqli_query( $this->dbh, "SET SESSION sql_mode='$modes_str'" );
     755                } else {
     756                        mysql_query( "SET SESSION sql_mode='$modes_str'", $this->dbh );
     757                }
    719758        }
    720759
    721760        /**
     
    900939                if ( is_null($dbh) )
    901940                        $dbh = $this->dbh;
    902941
    903                 if ( !@mysql_select_db( $db, $dbh ) ) {
     942                if ( $this->use_mysqli ) {
     943                        $success = @mysqli_select_db( $dbh, $db );
     944                } else {
     945                        $success = @mysql_select_db( $db, $dbh );
     946                }
     947                if ( ! $success ) {
    904948                        $this->ready = false;
    905949                        wp_load_translations_early();
    906950                        $this->bail( sprintf( __( '<h1>Can&#8217;t select database</h1>
     
    936980        }
    937981
    938982        /**
    939          * Real escape, using mysql_real_escape_string()
     983         * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string()
    940984         *
     985         * @see mysqli_real_escape_string()
    941986         * @see mysql_real_escape_string()
    942987         * @since 2.8.0
    943988         * @access private
     
    946991         * @return string escaped
    947992         */
    948993        function _real_escape( $string ) {
    949                 if ( $this->dbh )
    950                         return mysql_real_escape_string( $string, $this->dbh );
     994                if ( $this->dbh ) {
     995                        if ( $this->use_mysqli ) {
     996                                return mysqli_real_escape_string( $this->dbh, $string );
     997                        } else {
     998                                return mysql_real_escape_string( $string, $this->dbh );
     999                        }
     1000                }
    9511001
    9521002                $class = get_class( $this );
    9531003                _doing_it_wrong( $class, "$class must set a database connection for use with escaping.", E_USER_NOTICE );
     
    10931143        function print_error( $str = '' ) {
    10941144                global $EZSQL_ERROR;
    10951145
    1096                 if ( !$str )
    1097                         $str = mysql_error( $this->dbh );
     1146                if ( !$str ) {
     1147                        if ( $this->use_mysqli ) {
     1148                                $str = mysqli_error( $this->dbh );
     1149                        } else {
     1150                                $str = mysql_error( $this->dbh );
     1151                        }
     1152                }
    10981153                $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
    10991154
    11001155                if ( $this->suppress_errors )
     
    11971252                $this->rows_affected = $this->num_rows = 0;
    11981253                $this->last_error  = '';
    11991254
    1200                 if ( is_resource( $this->result ) )
    1201                         mysql_free_result( $this->result );
     1255                if ( is_resource( $this->result ) ) {
     1256                        if ( $this->use_mysqli ) {
     1257                                mysqli_free_result( $this->result );
     1258                        } else {
     1259                                mysql_free_result( $this->result );
     1260                        }
     1261                }
    12021262        }
    12031263
    12041264        /**
     
    12191279                $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true;
    12201280                $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0;
    12211281
    1222                 if ( WP_DEBUG ) {
    1223                         $error_reporting = false;
    1224                         if ( defined( 'E_DEPRECATED' ) ) {
    1225                                 $error_reporting = error_reporting();
    1226                                 error_reporting( $error_reporting ^ E_DEPRECATED );
     1282                if ( $this->use_mysqli ) {
     1283                        $this->dbh = mysqli_init();
     1284                        if ( WP_DEBUG ) {
     1285                                mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags );
     1286                        } else {
     1287                                @mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags );
    12271288                        }
    1228                         $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
    1229                         if ( false !== $error_reporting ) {
    1230                                 error_reporting( $error_reporting );
     1289                } else {
     1290                        if ( WP_DEBUG ) {
     1291                                $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1292                        } else {
     1293                                $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
    12311294                        }
    1232                 } else {
    1233                         $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
    12341295                }
    12351296
    12361297                if ( ! $this->dbh && $allow_bail ) {
     
    12761337         * @return bool True if the connection is up.
    12771338         */
    12781339        function check_connection() {
    1279                 if ( @mysql_ping( $this->dbh ) ) {
    1280                         return true;
     1340                if ( $this->use_mysqli ) {
     1341                        if ( @mysqli_ping( $this->dbh ) ) {
     1342                                return true;
     1343                        }
     1344                } else {
     1345                        if ( @mysql_ping( $this->dbh ) ) {
     1346                                return true;
     1347                        }
    12811348                }
    12821349
    12831350                $error_reporting = false;
     
    13591426                $this->_do_query( $query );
    13601427
    13611428                // MySQL server has gone away, try to reconnect
    1362                 if ( empty( $this->dbh ) || 2006 == mysql_errno( $this->dbh ) ) {
     1429                $mysql_errno = 0;
     1430                if ( ! empty( $this->dbh ) ) {
     1431                        if ( $this->use_mysqli ) {
     1432                                $mysql_errno = mysqli_errno( $this->dbh );
     1433                        } else {
     1434                                $mysql_errno = mysql_errno( $this->dbh );
     1435                        }
     1436                }
     1437
     1438                if ( empty( $this->dbh ) || 2006 == $mysql_errno ) {
    13631439                        if ( $this->check_connection() ) {
    13641440                                $this->_do_query( $query );
    13651441                        }
    13661442                }
    13671443
    13681444                // If there is an error then take note of it..
    1369                 if ( $this->last_error = mysql_error( $this->dbh ) ) {
     1445                if ( $this->use_mysqli ) {
     1446                        $this->last_error = mysqli_error( $this->dbh );
     1447                } else {
     1448                        $this->last_error = mysql_error( $this->dbh );
     1449                }
     1450
     1451                if ( $this->last_error ) {
    13701452                        // Clear insert_id on a subsequent failed insert.
    13711453                        if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) )
    13721454                                $this->insert_id = 0;
     
    13781460                if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) {
    13791461                        $return_val = $this->result;
    13801462                } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
    1381                         $this->rows_affected = mysql_affected_rows( $this->dbh );
     1463                        if ( $this->use_mysqli ) {
     1464                                $this->rows_affected = mysqli_affected_rows( $this->dbh );
     1465                        } else {
     1466                                $this->rows_affected = mysql_affected_rows( $this->dbh );
     1467                        }
    13821468                        // Take note of the insert_id
    13831469                        if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
    1384                                 $this->insert_id = mysql_insert_id($this->dbh);
     1470                                if ( $this->use_mysqli ) {
     1471                                        $this->insert_id = mysqli_insert_id( $this->dbh );
     1472                                } else {
     1473                                        $this->insert_id = mysql_insert_id( $this->dbh );
     1474                                }
    13851475                        }
    13861476                        // Return number of rows affected
    13871477                        $return_val = $this->rows_affected;
    13881478                } else {
    13891479                        $num_rows = 0;
    1390                         while ( $row = @mysql_fetch_object( $this->result ) ) {
    1391                                 $this->last_result[$num_rows] = $row;
    1392                                 $num_rows++;
     1480                        if ( $this->use_mysqli ) {
     1481                                while ( $row = @mysqli_fetch_object( $this->result ) ) {
     1482                                        $this->last_result[$num_rows] = $row;
     1483                                        $num_rows++;
     1484                                }
     1485                        } else {
     1486                                while ( $row = @mysql_fetch_object( $this->result ) ) {
     1487                                        $this->last_result[$num_rows] = $row;
     1488                                        $num_rows++;
     1489                                }
    13931490                        }
    13941491
    13951492                        // Log number of rows the query returned
     
    14161513                        $this->timer_start();
    14171514                }
    14181515
    1419                 $this->result = @mysql_query( $query, $this->dbh );
     1516                if ( $this->use_mysqli ) {
     1517                        $this->result = @mysqli_query( $this->dbh, $query );
     1518                } else {
     1519                        $this->result = @mysql_query( $query, $this->dbh );
     1520                }
    14201521                $this->num_queries++;
    14211522
    14221523                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
     
    17541855                if ( $this->col_info )
    17551856                        return;
    17561857
    1757                 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
    1758                         $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
     1858                if ( $this->use_mysqli ) {
     1859                        for ( $i = 0; $i < @mysqli_num_fields( $this->result ); $i++ ) {
     1860                                $this->col_info[ $i ] = @mysqli_fetch_field( $this->result, $i );
     1861                        }
     1862                } else {
     1863                        for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
     1864                                $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
     1865                        }
    17591866                }
    17601867        }
    17611868
     
    19272034         * @return false|string false on failure, version number on success
    19282035         */
    19292036        function db_version() {
    1930                 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
     2037                if ( $this->use_mysqli ) {
     2038                        $server_info = mysqli_get_server_info( $this->dbh );
     2039                } else {
     2040                        $server_info = mysql_get_server_info( $this->dbh );
     2041                }
     2042                return preg_replace( '/[^0-9.].*/', '', $server_info );
    19312043        }
    19322044}
  • tests/phpunit/tests/db.php

     
    5050                $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" );
    5151                $this->assertGreaterThan( 0, $var );
    5252
    53                 mysql_close( $wpdb->dbh );
     53                if ( $wpdb->use_mysqli ) {
     54                        mysqli_close( $wpdb->dbh );
     55                } else {
     56                        mysql_close( $wpdb->dbh );
     57                }
    5458                unset( $wpdb->dbh );
    5559
    5660                $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" );