Ticket #21663: 21663.12.diff
File 21663.12.diff, 11.4 KB (added by , 11 years ago) |
---|
-
src/wp-includes/wp-db.php
530 530 'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES', 'TRADITIONAL' ); 531 531 532 532 /** 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 /** 533 542 * Connects to the database server and selects a database 534 543 * 535 544 * PHP5 style constructor for compatibility with PHP5. Does … … 550 559 if ( WP_DEBUG && WP_DEBUG_DISPLAY ) 551 560 $this->show_errors(); 552 561 562 $this->use_mysqli = ( version_compare( phpversion(), '5.5', '>=' ) && function_exists( 'mysqli_connect' ) ); 563 553 564 $this->init_charset(); 554 565 555 566 $this->dbuser = $dbuser; … … 657 668 if ( ! isset( $collate ) ) 658 669 $collate = $this->collate; 659 670 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 } 662 680 } 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 } 667 689 } 668 690 } 669 691 } … … 680 702 */ 681 703 function set_sql_mode( $modes = array() ) { 682 704 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 684 711 if ( empty( $res ) ) { 685 712 return; 686 713 } 687 714 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 } 689 724 690 725 if ( empty( $modes_str ) ) { 691 726 return; … … 715 750 716 751 $modes_str = implode( ',', $modes ); 717 752 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 } 719 758 } 720 759 721 760 /** … … 900 939 if ( is_null($dbh) ) 901 940 $dbh = $this->dbh; 902 941 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 ) { 904 948 $this->ready = false; 905 949 wp_load_translations_early(); 906 950 $this->bail( sprintf( __( '<h1>Can’t select database</h1> … … 936 980 } 937 981 938 982 /** 939 * Real escape, using mysql _real_escape_string()983 * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string() 940 984 * 985 * @see mysqli_real_escape_string() 941 986 * @see mysql_real_escape_string() 942 987 * @since 2.8.0 943 988 * @access private … … 946 991 * @return string escaped 947 992 */ 948 993 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 } 951 1001 952 1002 $class = get_class( $this ); 953 1003 _doing_it_wrong( $class, "$class must set a database connection for use with escaping.", E_USER_NOTICE ); … … 1093 1143 function print_error( $str = '' ) { 1094 1144 global $EZSQL_ERROR; 1095 1145 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 } 1098 1153 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 1099 1154 1100 1155 if ( $this->suppress_errors ) … … 1197 1252 $this->rows_affected = $this->num_rows = 0; 1198 1253 $this->last_error = ''; 1199 1254 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 } 1202 1262 } 1203 1263 1204 1264 /** … … 1219 1279 $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; 1220 1280 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; 1221 1281 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 } 1227 1303 } 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 ); 1231 1309 } 1232 1310 } 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 } 1234 1316 } 1235 1317 1236 1318 if ( ! $this->dbh && $allow_bail ) { … … 1276 1358 * @return bool True if the connection is up. 1277 1359 */ 1278 1360 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 } 1281 1369 } 1282 1370 1283 1371 $error_reporting = false; … … 1359 1447 $this->_do_query( $query ); 1360 1448 1361 1449 // 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 ) { 1363 1460 if ( $this->check_connection() ) { 1364 1461 $this->_do_query( $query ); 1365 1462 } 1366 1463 } 1367 1464 1368 1465 // 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 ) { 1370 1473 // Clear insert_id on a subsequent failed insert. 1371 1474 if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) 1372 1475 $this->insert_id = 0; … … 1378 1481 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 1379 1482 $return_val = $this->result; 1380 1483 } 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 } 1382 1489 // Take note of the insert_id 1383 1490 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 } 1385 1496 } 1386 1497 // Return number of rows affected 1387 1498 $return_val = $this->rows_affected; 1388 1499 } else { 1389 1500 $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 } 1393 1511 } 1394 1512 1395 1513 // Log number of rows the query returned … … 1416 1534 $this->timer_start(); 1417 1535 } 1418 1536 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 } 1420 1542 $this->num_queries++; 1421 1543 1422 1544 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { … … 1754 1876 if ( $this->col_info ) 1755 1877 return; 1756 1878 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 } 1759 1887 } 1760 1888 } 1761 1889 … … 1927 2055 * @return false|string false on failure, version number on success 1928 2056 */ 1929 2057 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 ); 1931 2064 } 1932 2065 } -
tests/phpunit/tests/db.php
50 50 $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" ); 51 51 $this->assertGreaterThan( 0, $var ); 52 52 53 mysql_close( $wpdb->dbh ); 53 if ( $wpdb->use_mysqli ) { 54 mysqli_close( $wpdb->dbh ); 55 } else { 56 mysql_close( $wpdb->dbh ); 57 } 54 58 unset( $wpdb->dbh ); 55 59 56 60 $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" );