Make WordPress Core

Ticket #21663: 21663.12.diff

File 21663.12.diff, 11.4 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
     1285                        // mysqli_real_connect doesn't support the host param including a port or socket
     1286                        // like mysql_connect does. This duplicates how mysql_connect detects a port and/or socket file.
     1287                        $port = null;
     1288                        $socket = null;
     1289                        $host = $this->dbhost;
     1290                        $port_or_socket = strstr( $host, ':' );
     1291                        if ( ! empty( $port_or_socket ) ) {
     1292                                $host = strstr( $host, ':', true );
     1293                                $port_or_socket = substr( $port_or_socket, 1 );
     1294                                if ( 0 !== strpos( $port_or_socket, '/' ) ) {
     1295                                        $port = intval( $port_or_socket );
     1296                                        $maybe_socket = strstr( $port_or_socket, ':' );
     1297                                        if ( ! empty( $maybe_socket ) ) {
     1298                                                $socket = substr( $maybe_socket, 1 );
     1299                                        }
     1300                                } else {
     1301                                        $socket = $port_or_socket;
     1302                                }
    12271303                        }
    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 );
     1304
     1305                        if ( WP_DEBUG ) {
     1306                                mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
     1307                        } else {
     1308                                @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
    12311309                        }
    12321310                } else {
    1233                         $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1311                        if ( WP_DEBUG ) {
     1312                                $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1313                        } else {
     1314                                $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1315                        }
    12341316                }
    12351317
    12361318                if ( ! $this->dbh && $allow_bail ) {
     
    12761358         * @return bool True if the connection is up.
    12771359         */
    12781360        function check_connection() {
    1279                 if ( @mysql_ping( $this->dbh ) ) {
    1280                         return true;
     1361                if ( $this->use_mysqli ) {
     1362                        if ( @mysqli_ping( $this->dbh ) ) {
     1363                                return true;
     1364                        }
     1365                } else {
     1366                        if ( @mysql_ping( $this->dbh ) ) {
     1367                                return true;
     1368                        }
    12811369                }
    12821370
    12831371                $error_reporting = false;
     
    13591447                $this->_do_query( $query );
    13601448
    13611449                // MySQL server has gone away, try to reconnect
    1362                 if ( empty( $this->dbh ) || 2006 == mysql_errno( $this->dbh ) ) {
     1450                $mysql_errno = 0;
     1451                if ( ! empty( $this->dbh ) ) {
     1452                        if ( $this->use_mysqli ) {
     1453                                $mysql_errno = mysqli_errno( $this->dbh );
     1454                        } else {
     1455                                $mysql_errno = mysql_errno( $this->dbh );
     1456                        }
     1457                }
     1458
     1459                if ( empty( $this->dbh ) || 2006 == $mysql_errno ) {
    13631460                        if ( $this->check_connection() ) {
    13641461                                $this->_do_query( $query );
    13651462                        }
    13661463                }
    13671464
    13681465                // If there is an error then take note of it..
    1369                 if ( $this->last_error = mysql_error( $this->dbh ) ) {
     1466                if ( $this->use_mysqli ) {
     1467                        $this->last_error = mysqli_error( $this->dbh );
     1468                } else {
     1469                        $this->last_error = mysql_error( $this->dbh );
     1470                }
     1471
     1472                if ( $this->last_error ) {
    13701473                        // Clear insert_id on a subsequent failed insert.
    13711474                        if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) )
    13721475                                $this->insert_id = 0;
     
    13781481                if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) {
    13791482                        $return_val = $this->result;
    13801483                } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
    1381                         $this->rows_affected = mysql_affected_rows( $this->dbh );
     1484                        if ( $this->use_mysqli ) {
     1485                                $this->rows_affected = mysqli_affected_rows( $this->dbh );
     1486                        } else {
     1487                                $this->rows_affected = mysql_affected_rows( $this->dbh );
     1488                        }
    13821489                        // Take note of the insert_id
    13831490                        if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
    1384                                 $this->insert_id = mysql_insert_id($this->dbh);
     1491                                if ( $this->use_mysqli ) {
     1492                                        $this->insert_id = mysqli_insert_id( $this->dbh );
     1493                                } else {
     1494                                        $this->insert_id = mysql_insert_id( $this->dbh );
     1495                                }
    13851496                        }
    13861497                        // Return number of rows affected
    13871498                        $return_val = $this->rows_affected;
    13881499                } else {
    13891500                        $num_rows = 0;
    1390                         while ( $row = @mysql_fetch_object( $this->result ) ) {
    1391                                 $this->last_result[$num_rows] = $row;
    1392                                 $num_rows++;
     1501                        if ( $this->use_mysqli ) {
     1502                                while ( $row = @mysqli_fetch_object( $this->result ) ) {
     1503                                        $this->last_result[$num_rows] = $row;
     1504                                        $num_rows++;
     1505                                }
     1506                        } else {
     1507                                while ( $row = @mysql_fetch_object( $this->result ) ) {
     1508                                        $this->last_result[$num_rows] = $row;
     1509                                        $num_rows++;
     1510                                }
    13931511                        }
    13941512
    13951513                        // Log number of rows the query returned
     
    14161534                        $this->timer_start();
    14171535                }
    14181536
    1419                 $this->result = @mysql_query( $query, $this->dbh );
     1537                if ( $this->use_mysqli ) {
     1538                        $this->result = @mysqli_query( $this->dbh, $query );
     1539                } else {
     1540                        $this->result = @mysql_query( $query, $this->dbh );
     1541                }
    14201542                $this->num_queries++;
    14211543
    14221544                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
     
    17541876                if ( $this->col_info )
    17551877                        return;
    17561878
    1757                 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
    1758                         $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
     1879                if ( $this->use_mysqli ) {
     1880                        for ( $i = 0; $i < @mysqli_num_fields( $this->result ); $i++ ) {
     1881                                $this->col_info[ $i ] = @mysqli_fetch_field( $this->result, $i );
     1882                        }
     1883                } else {
     1884                        for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
     1885                                $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
     1886                        }
    17591887                }
    17601888        }
    17611889
     
    19272055         * @return false|string false on failure, version number on success
    19282056         */
    19292057        function db_version() {
    1930                 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
     2058                if ( $this->use_mysqli ) {
     2059                        $server_info = mysqli_get_server_info( $this->dbh );
     2060                } else {
     2061                        $server_info = mysql_get_server_info( $this->dbh );
     2062                }
     2063                return preg_replace( '/[^0-9.].*/', '', $server_info );
    19312064        }
    19322065}
  • 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" );