Skip to content

Commit de33d35

Browse files
committed
WPDB: When a db.php drop-in is being used, and it doesn't explicitly define itself as connecting to MySQL, skip the character set checks. This ensures that existing drop-ins won't accidentally run checks that they don't support.
See #21212. git-svn-id: https://develop.svn.wordpress.org/trunk@30375 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 92ae13f commit de33d35

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

src/wp-includes/wp-db.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,7 @@ protected function get_table_charset( $table ) {
22112211
$this->col_meta[ $table ] = $columns;
22122212

22132213
foreach ( $columns as $column ) {
2214-
if ( $column->Collation ) {
2214+
if ( ! empty( $column->Collation ) ) {
22152215
list( $charset ) = explode( '_', $column->Collation );
22162216
$charsets[ strtolower( $charset ) ] = true;
22172217
}
@@ -2237,7 +2237,7 @@ protected function get_table_charset( $table ) {
22372237
$charset = key( $charsets );
22382238
} elseif ( 0 === $count ) {
22392239
// No charsets, assume this table can store whatever.
2240-
$charset = 'latin1';
2240+
$charset = false;
22412241
} else {
22422242
// More than one charset. Remove latin1 if present and recalculate.
22432243
unset( $charsets['latin1'] );
@@ -2291,6 +2291,11 @@ protected function get_col_charset( $table, $column ) {
22912291
return $charset;
22922292
}
22932293

2294+
// Skip this entirely if this isn't a MySQL database.
2295+
if ( false === $this->is_mysql ) {
2296+
return false;
2297+
}
2298+
22942299
if ( empty( $this->table_charset[ $table ] ) ) {
22952300
// This primes column information for us.
22962301
$table_charset = $this->get_table_charset( $table );
@@ -2378,13 +2383,12 @@ protected function strip_invalid_text( $data ) {
23782383
foreach ( $data as &$value ) {
23792384
$charset = $value['charset'];
23802385

2381-
// latin1 will happily store anything.
2382-
if ( 'latin1' === $charset ) {
2386+
// Column isn't a string, or is latin1, which will will happily store anything.
2387+
if ( false === $charset || 'latin1' === $charset ) {
23832388
continue;
23842389
}
23852390

2386-
// Column or value isn't a string.
2387-
if ( false === $charset || ! is_string( $value['value'] ) ) {
2391+
if ( ! is_string( $value['value'] ) ) {
23882392
continue;
23892393
}
23902394

tests/phpunit/tests/db/charset.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function test_strip_invalid_text_for_column() {
231231
protected $table_and_column_defs = array(
232232
array(
233233
'definition' => '( a INT, b FLOAT )',
234-
'table_expected' => 'latin1',
234+
'table_expected' => false,
235235
'column_expected' => array( 'a' => false, 'b' => false )
236236
),
237237
array(
@@ -346,6 +346,32 @@ function test_get_column_charset( $drop, $create, $table, $expected_charset ) {
346346
self::$_wpdb->query( $drop );
347347
}
348348

349+
/**
350+
* @dataProvider data_test_get_column_charset
351+
* @ticket 21212
352+
*/
353+
function test_get_column_charset_non_mysql( $drop, $create, $table, $columns ) {
354+
self::$_wpdb->query( $drop );
355+
356+
if ( ! self::$_wpdb->has_cap( 'utf8mb4' ) && preg_match( '/utf8mb[34]/i', $create ) ) {
357+
$this->markTestSkipped( "This version of MySQL doesn't support utf8mb4." );
358+
return;
359+
}
360+
361+
self::$_wpdb->is_mysql = false;
362+
363+
self::$_wpdb->query( $create );
364+
365+
$columns = array_keys( $columns );
366+
foreach ( $columns as $column => $charset ) {
367+
$this->assertEquals( false, self::$_wpdb->get_col_charset( $table, $column ) );
368+
}
369+
370+
self::$_wpdb->query( $drop );
371+
372+
self::$_wpdb->is_mysql = true;
373+
}
374+
349375
/**
350376
* @ticket 21212
351377
*/

0 commit comments

Comments
 (0)