Database: When parsing the host, leave the port and socket as `null` if they're not defined.

This fixes a change in behaviour introduced by [41629].

The host is set to an empty string when it isn't defined, this continues existing behaviour. In particular, the mysqli library treats a `null` host as being the same as `localhost`, which is not always the intended behaviour.

Props birgire, markjaquith, pento.
Fixes #41722.



git-svn-id: https://develop.svn.wordpress.org/trunk@41820 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2017-10-11 00:09:47 +00:00
parent 32e7a51a01
commit 0e5b75b8be
2 changed files with 11 additions and 10 deletions

View File

@ -1479,7 +1479,7 @@ class wpdb {
$port = null;
$socket = null;
$is_ipv6 = false;
if ( $host_data = $this->parse_db_host( $this->dbhost ) ) {
list( $host, $port, $socket, $is_ipv6 ) = $host_data;
}
@ -1621,9 +1621,10 @@ class wpdb {
return false;
}
$host = '';
foreach ( array( 'host', 'port', 'socket' ) as $component ) {
if ( array_key_exists( $component, $matches ) ) {
$$component = $matches[$component];
if ( ! empty( $matches[ $component ] ) ) {
$$component = $matches[ $component ];
}
}

View File

@ -1208,10 +1208,10 @@ class Tests_DB extends WP_UnitTestCase {
list( $parsed_host, $parsed_port, $parsed_socket, $parsed_is_ipv6 ) = $data;
$this->assertEquals( $host, $parsed_host );
$this->assertEquals( $port, $parsed_port );
$this->assertEquals( $socket, $parsed_socket );
$this->assertEquals( $is_ipv6, $parsed_is_ipv6 );
$this->assertSame( $host, $parsed_host );
$this->assertSame( $port, $parsed_port );
$this->assertSame( $socket, $parsed_socket );
$this->assertSame( $is_ipv6, $parsed_is_ipv6 );
}
}
@ -1220,7 +1220,7 @@ class Tests_DB extends WP_UnitTestCase {
array(
'', // DB_HOST
false, // Expect parse_db_host to bail for this hostname
null, // Parsed host
'', // Parsed host
null, // Parsed port
null, // Parsed socket
false, // is_ipv6
@ -1228,7 +1228,7 @@ class Tests_DB extends WP_UnitTestCase {
array(
':3306',
false,
null,
'',
'3306',
null,
false,
@ -1236,7 +1236,7 @@ class Tests_DB extends WP_UnitTestCase {
array(
':/tmp/mysql.sock',
false,
null,
'',
null,
'/tmp/mysql.sock',
false,