From 4422aac17cacaf5e99d8fa14ed8b06939cc53e52 Mon Sep 17 00:00:00 2001 From: pento Date: Mon, 23 May 2016 05:53:02 +0000 Subject: [PATCH] Database: Obey locale-specific `utf8` collation settings. Some sites prefer to use locale-specific location settings. For example, the Swedish WordPress package use `utf8_swedish_ci`, instead of `utf8_unicode_ci`. When upgrading the connection to `utf8mb4`, we were overriding this to be `utf8mb4_unicode_ci`, instead of maintaining the use of the `_swedish_ci` variant. The locale-specific collations do have extra collation rules just for that language, so it's useful to maintain compatibility. Fixes #32405. git-svn-id: https://develop.svn.wordpress.org/trunk@37521 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/wp-db.php | 9 +++++++-- tests/phpunit/tests/db.php | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php index 6fcc31a7eb..8d9f84c63e 100644 --- a/src/wp-includes/wp-db.php +++ b/src/wp-includes/wp-db.php @@ -757,8 +757,13 @@ class wpdb { $this->charset = 'utf8mb4'; } - if ( 'utf8mb4' === $this->charset && ( ! $this->collate || stripos( $this->collate, 'utf8_' ) === 0 ) ) { - $this->collate = 'utf8mb4_unicode_ci'; + if ( 'utf8mb4' === $this->charset ) { + // _general_ is outdated, so we can upgrade it to _unicode_, instead. + if ( ! $this->collate || 'utf8_general_ci' === $this->collate ) { + $this->collate = 'utf8mb4_unicode_ci'; + } else { + $this->collate = str_replace( 'utf8_', 'utf8mb4_', $this->collate ); + } } } diff --git a/tests/phpunit/tests/db.php b/tests/phpunit/tests/db.php index f49a3e07b6..20ed791b53 100644 --- a/tests/phpunit/tests/db.php +++ b/tests/phpunit/tests/db.php @@ -955,4 +955,25 @@ class Tests_DB extends WP_UnitTestCase { $wpdb->check_connection(); } + + /** + * @ticket 32405 + */ + function test_non_unicode_collations() { + global $wpdb; + + if ( ! $wpdb->has_cap( 'utf8mb4' ) ) { + $this->markTestSkipped( 'This test requires utf8mb4 support' ); + } + + $charset = $wpdb->charset; + $collate = $wpdb->collate; + + $wpdb->init_charset( 'utf8', 'utf8_swedish_ci' ); + + $this->assertSame( 'utf8mb4', $wpdb->charset ); + $this->assertSame( 'utf8mb4_swedish_ci', $wpdb->collate ); + + $wpdb->init_charset( $charset, $collate ); + } }