Make WordPress Core

Changeset 27250


Ignore:
Timestamp:
02/25/2014 12:39:28 AM (13 years ago)
Author:
nacin
Message:

Use ext/mysqli in PHP 5.5 or greater. Expect minor explosions.

props aaroncampbell, pento.
see #21663.

Location:
trunk
Files:
2 edited

Legend:

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

    r27249 r27250  
    540540
    541541        /**
     542         * Whether to use mysqli over mysql.
     543         *
     544         * @since 3.9.0
     545         * @access private
     546         * @var bool
     547         */
     548        private $use_mysqli = false;
     549
     550        /**
    542551         * Connects to the database server and selects a database
    543552         *
     
    559568                if ( WP_DEBUG && WP_DEBUG_DISPLAY )
    560569                        $this->show_errors();
     570
     571                $this->use_mysqli = ( version_compare( phpversion(), '5.5', '>=' ) && function_exists( 'mysqli_connect' ) );
    561572
    562573                $this->init_charset();
     
    667678                        $collate = $this->collate;
    668679                if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) {
    669                         if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
    670                                 mysql_set_charset( $charset, $dbh );
     680                        if ( $this->use_mysqli ) {
     681                                if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
     682                                        mysqli_set_charset( $dbh, $charset );
     683                                } else {
     684                                        $query = $this->prepare( 'SET NAMES %s', $charset );
     685                                        if ( ! empty( $collate ) )
     686                                                $query .= $this->prepare( ' COLLATE %s', $collate );
     687                                        mysqli_query( $query, $dbh );
     688                                }
    671689                        } else {
    672                                 $query = $this->prepare( 'SET NAMES %s', $charset );
    673                                 if ( ! empty( $collate ) )
    674                                         $query .= $this->prepare( ' COLLATE %s', $collate );
    675                                 mysql_query( $query, $dbh );
     690                                if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
     691                                        mysql_set_charset( $charset, $dbh );
     692                                } else {
     693                                        $query = $this->prepare( 'SET NAMES %s', $charset );
     694                                        if ( ! empty( $collate ) )
     695                                                $query .= $this->prepare( ' COLLATE %s', $collate );
     696                                        mysql_query( $query, $dbh );
     697                                }
    676698                        }
    677699                }
     
    690712        function set_sql_mode( $modes = array() ) {
    691713                if ( empty( $modes ) ) {
    692                         $res = mysql_query( 'SELECT @@SESSION.sql_mode;', $this->dbh );
     714                        if ( $this->use_mysqli ) {
     715                                $res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' );
     716                        } else {
     717                                $res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh );
     718                        }
     719
    693720                        if ( empty( $res ) ) {
    694721                                return;
    695722                        }
    696723
    697                         $modes_str = mysql_result( $res, 0 );
     724                        if ( $this->use_mysqli ) {
     725                                $modes_array = mysqli_fetch_array( $res );
     726                                if ( empty( $modes_array[0] ) ) {
     727                                        return;
     728                                }
     729                                $modes_str = $modes_array[0];
     730                        } else {
     731                                $modes_str = mysql_result( $res, 0 );
     732                        }
    698733
    699734                        if ( empty( $modes_str ) ) {
     
    725760                $modes_str = implode( ',', $modes );
    726761
    727                 mysql_query( "SET SESSION sql_mode='$modes_str';", $this->dbh );
     762                if ( $this->use_mysqli ) {
     763                        mysqli_query( $this->dbh, "SET SESSION sql_mode='$modes_str'" );
     764                } else {
     765                        mysql_query( "SET SESSION sql_mode='$modes_str'", $this->dbh );
     766                }
    728767        }
    729768
     
    910949                        $dbh = $this->dbh;
    911950
    912                 if ( !@mysql_select_db( $db, $dbh ) ) {
     951                if ( $this->use_mysqli ) {
     952                        $success = @mysqli_select_db( $dbh, $db );
     953                } else {
     954                        $success = @mysql_select_db( $db, $dbh );
     955                }
     956                if ( ! $success ) {
    913957                        $this->ready = false;
    914958                        wp_load_translations_early();
     
    946990
    947991        /**
    948          * Real escape, using mysql_real_escape_string()
    949          *
     992         * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string()
     993         *
     994         * @see mysqli_real_escape_string()
    950995         * @see mysql_real_escape_string()
    951996         * @since 2.8.0
     
    9561001         */
    9571002        function _real_escape( $string ) {
    958                 if ( $this->dbh )
    959                         return mysql_real_escape_string( $string, $this->dbh );
     1003                if ( $this->dbh ) {
     1004                        if ( $this->use_mysqli ) {
     1005                                return mysqli_real_escape_string( $this->dbh, $string );
     1006                        } else {
     1007                                return mysql_real_escape_string( $string, $this->dbh );
     1008                        }
     1009                }
    9601010
    9611011                $class = get_class( $this );
     
    11031153                global $EZSQL_ERROR;
    11041154
    1105                 if ( !$str )
    1106                         $str = mysql_error( $this->dbh );
     1155                if ( !$str ) {
     1156                        if ( $this->use_mysqli ) {
     1157                                $str = mysqli_error( $this->dbh );
     1158                        } else {
     1159                                $str = mysql_error( $this->dbh );
     1160                        }
     1161                }
    11071162                $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
    11081163
     
    12071262                $this->last_error  = '';
    12081263
    1209                 if ( is_resource( $this->result ) )
    1210                         mysql_free_result( $this->result );
     1264                if ( is_resource( $this->result ) ) {
     1265                        if ( $this->use_mysqli ) {
     1266                                mysqli_free_result( $this->result );
     1267                        } else {
     1268                                mysql_free_result( $this->result );
     1269                        }
     1270                }
    12111271        }
    12121272
     
    12291289                $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0;
    12301290
    1231                 if ( WP_DEBUG ) {
    1232                         $error_reporting = false;
    1233                         if ( defined( 'E_DEPRECATED' ) ) {
    1234                                 $error_reporting = error_reporting();
    1235                                 error_reporting( $error_reporting ^ E_DEPRECATED );
    1236                         }
    1237                         $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
    1238                         if ( false !== $error_reporting ) {
    1239                                 error_reporting( $error_reporting );
     1291                if ( $this->use_mysqli ) {
     1292                        $this->dbh = mysqli_init();
     1293
     1294                        // mysqli_real_connect doesn't support the host param including a port or socket
     1295                        // like mysql_connect does. This duplicates how mysql_connect detects a port and/or socket file.
     1296                        $port = null;
     1297                        $socket = null;
     1298                        $host = $this->dbhost;
     1299                        $port_or_socket = strstr( $host, ':' );
     1300                        if ( ! empty( $port_or_socket ) ) {
     1301                                $host = strstr( $host, ':', true );
     1302                                $port_or_socket = substr( $port_or_socket, 1 );
     1303                                if ( 0 !== strpos( $port_or_socket, '/' ) ) {
     1304                                        $port = intval( $port_or_socket );
     1305                                        $maybe_socket = strstr( $port_or_socket, ':' );
     1306                                        if ( ! empty( $maybe_socket ) ) {
     1307                                                $socket = substr( $maybe_socket, 1 );
     1308                                        }
     1309                                } else {
     1310                                        $socket = $port_or_socket;
     1311                                }
     1312                        }
     1313
     1314                        if ( WP_DEBUG ) {
     1315                                mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
     1316                        } else {
     1317                                @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
    12401318                        }
    12411319                } else {
    1242                         $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1320                        if ( WP_DEBUG ) {
     1321                                $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1322                        } else {
     1323                                $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
     1324                        }
    12431325                }
    12441326
     
    12861368         */
    12871369        function check_connection() {
    1288                 if ( @mysql_ping( $this->dbh ) ) {
    1289                         return true;
     1370                if ( $this->use_mysqli ) {
     1371                        if ( @mysqli_ping( $this->dbh ) ) {
     1372                                return true;
     1373                        }
     1374                } else {
     1375                        if ( @mysql_ping( $this->dbh ) ) {
     1376                                return true;
     1377                        }
    12901378                }
    12911379
     
    13691457
    13701458                // MySQL server has gone away, try to reconnect
    1371                 if ( empty( $this->dbh ) || 2006 == mysql_errno( $this->dbh ) ) {
     1459                $mysql_errno = 0;
     1460                if ( ! empty( $this->dbh ) ) {
     1461                        if ( $this->use_mysqli ) {
     1462                                $mysql_errno = mysqli_errno( $this->dbh );
     1463                        } else {
     1464                                $mysql_errno = mysql_errno( $this->dbh );
     1465                        }
     1466                }
     1467
     1468                if ( empty( $this->dbh ) || 2006 == $mysql_errno ) {
    13721469                        if ( $this->check_connection() ) {
    13731470                                $this->_do_query( $query );
     
    13761473
    13771474                // If there is an error then take note of it..
    1378                 if ( $this->last_error = mysql_error( $this->dbh ) ) {
     1475                if ( $this->use_mysqli ) {
     1476                        $this->last_error = mysqli_error( $this->dbh );
     1477                } else {
     1478                        $this->last_error = mysql_error( $this->dbh );
     1479                }
     1480
     1481                if ( $this->last_error ) {
    13791482                        // Clear insert_id on a subsequent failed insert.
    13801483                        if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) )
     
    13881491                        $return_val = $this->result;
    13891492                } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
    1390                         $this->rows_affected = mysql_affected_rows( $this->dbh );
     1493                        if ( $this->use_mysqli ) {
     1494                                $this->rows_affected = mysqli_affected_rows( $this->dbh );
     1495                        } else {
     1496                                $this->rows_affected = mysql_affected_rows( $this->dbh );
     1497                        }
    13911498                        // Take note of the insert_id
    13921499                        if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
    1393                                 $this->insert_id = mysql_insert_id($this->dbh);
     1500                                if ( $this->use_mysqli ) {
     1501                                        $this->insert_id = mysqli_insert_id( $this->dbh );
     1502                                } else {
     1503                                        $this->insert_id = mysql_insert_id( $this->dbh );
     1504                                }
    13941505                        }
    13951506                        // Return number of rows affected
     
    13971508                } else {
    13981509                        $num_rows = 0;
    1399                         while ( $row = @mysql_fetch_object( $this->result ) ) {
    1400                                 $this->last_result[$num_rows] = $row;
    1401                                 $num_rows++;
     1510                        if ( $this->use_mysqli ) {
     1511                                while ( $row = @mysqli_fetch_object( $this->result ) ) {
     1512                                        $this->last_result[$num_rows] = $row;
     1513                                        $num_rows++;
     1514                                }
     1515                        } else {
     1516                                while ( $row = @mysql_fetch_object( $this->result ) ) {
     1517                                        $this->last_result[$num_rows] = $row;
     1518                                        $num_rows++;
     1519                                }
    14021520                        }
    14031521
     
    14261544                }
    14271545
    1428                 $this->result = @mysql_query( $query, $this->dbh );
     1546                if ( $this->use_mysqli ) {
     1547                        $this->result = @mysqli_query( $this->dbh, $query );
     1548                } else {
     1549                        $this->result = @mysql_query( $query, $this->dbh );
     1550                }
    14291551                $this->num_queries++;
    14301552
     
    17641886                        return;
    17651887
    1766                 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
    1767                         $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
     1888                if ( $this->use_mysqli ) {
     1889                        for ( $i = 0; $i < @mysqli_num_fields( $this->result ); $i++ ) {
     1890                                $this->col_info[ $i ] = @mysqli_fetch_field( $this->result, $i );
     1891                        }
     1892                } else {
     1893                        for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
     1894                                $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
     1895                        }
    17681896                }
    17691897        }
     
    19372065         */
    19382066        function db_version() {
    1939                 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
     2067                if ( $this->use_mysqli ) {
     2068                        $server_info = mysqli_get_server_info( $this->dbh );
     2069                } else {
     2070                        $server_info = mysql_get_server_info( $this->dbh );
     2071                }
     2072                return preg_replace( '/[^0-9.].*/', '', $server_info );
    19402073        }
    19412074}
  • trunk/tests/phpunit/tests/db.php

    r27075 r27250  
    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
Note: See TracChangeset for help on using the changeset viewer.