Skip to content

Commit f9bdc0f

Browse files
authored
Fix utf8mb4_0900_ai_ci collation in tables created by WordPress (#514)
## Summary With the SQLite driver, `$wpdb->collate` is empty, so `$wpdb->get_charset_collate()` returns `DEFAULT CHARACTER SET utf8mb4` without a `COLLATE` clause. Tables created with it (for example, via `dbDelta()`) therefore use the emulated MySQL 8 default, **`utf8mb4_0900_ai_ci`**. MariaDB doesn't support this collation, so these tables fail to import there, including when pushing a WordPress Studio site to WordPress.com. This PR makes `$wpdb` use the same charset and collation as WordPress on MySQL: - **`$wpdb->charset` is always `utf8mb4`**, matching the emulated connection. `DB_CHARSET` is ignored. - **`$wpdb->collate` is `utf8mb4_unicode_520_ci`**, unless `DB_COLLATE` specifies another UTF-8 collation. For example, `utf8_bin` becomes `utf8mb4_bin`, as in `wpdb::determine_charset()`. - **`utf8mb4_unicode_520_ci` is listed** in `INFORMATION_SCHEMA.COLLATIONS` and `SHOW COLLATION`. ## Why Previously, `WP_SQLite_DB` initialized the charset only before connecting. At that point, `wpdb::determine_charset()` leaves the values unchanged, so the collation stayed empty. The values are now resolved **without a connection**, since SQLite always uses UTF-8 and needs no server capability checks. This also makes them available while connecting, when the driver may reconstruct WordPress tables with `wp_get_db_schema()`. Calling `init_charset()` again after connecting, as `wpdb::db_connect()` does, wouldn't be enough: WordPress Studio may define `DB_CHARSET` in a mu-plugin that loads later. This PR leaves the following unchanged: - **Tables created without a collation** (for example, a plain `CREATE TABLE`) still use `utf8mb4_0900_ai_ci`, the emulated MySQL 8 default. - **Existing tables** keep their recorded collation. - **Column collations** in the information schema remain `utf8mb4_0900_ai_ci`. Exports use only the table collation. Broader charset and collation support is tracked in #456. Alternative to #511. Related to Automattic/studio#4737. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved compatibility with WordPress charset and collation settings by consistently resolving to `utf8mb4` and the appropriate collation, including `utf8mb4_unicode_520_ci`. * Added support for the `utf8mb4_unicode_520_ci` collation in collation metadata. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 12ae705 commit f9bdc0f

8 files changed

Lines changed: 264 additions & 31 deletions

File tree

‎.github/workflows/phpunit-tests.yml‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,24 @@ jobs:
170170
run: cargo build --release
171171
working-directory: packages/php-ext-wp-mysql-parser
172172

173+
# Load the extension via an INI file, so that it also applies to child
174+
# processes, such as PHPUnit tests that run in a separate process.
175+
- name: Enable parser extension
176+
if: matrix.extension
177+
run: |
178+
echo "extension=$GITHUB_WORKSPACE/packages/php-ext-wp-mysql-parser/target/release/libwp_mysql_parser.so" \
179+
| sudo tee "$(php -r 'echo PHP_CONFIG_FILE_SCAN_DIR;')/wp-mysql-parser.ini"
180+
173181
- name: Verify native parser extension
174182
if: matrix.extension
175-
run: php -d extension="$GITHUB_WORKSPACE/packages/php-ext-wp-mysql-parser/target/release/libwp_mysql_parser.so" tests/tools/verify-native-parser-extension.php
183+
run: php tests/tools/verify-native-parser-extension.php
176184
working-directory: packages/mysql-on-sqlite
177185

178186
- name: Run PHPUnit suite with parser extension
179187
if: matrix.extension
180188
env:
181189
WP_SQLITE_REQUIRE_NATIVE_PARSER_EXTENSION: '1'
182-
run: php -d extension="$GITHUB_WORKSPACE/packages/php-ext-wp-mysql-parser/target/release/libwp_mysql_parser.so" ./vendor/bin/phpunit -c ./phpunit.xml.dist
190+
run: php ./vendor/bin/phpunit -c ./phpunit.xml.dist
183191
working-directory: packages/mysql-on-sqlite
184192

185193
- name: Run PHPUnit suite

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ public function get_computed_information_schema_table_definition( string $table_
450450
('utf8_unicode_ci', 'utf8', 192, '', 'Yes', 8, 'PAD SPACE'),
451451
('utf8mb4_bin', 'utf8mb4', 46, '', 'Yes', 1, 'PAD SPACE'),
452452
('utf8mb4_unicode_ci', 'utf8mb4', 224, '', 'Yes', 8, 'PAD SPACE'),
453+
('utf8mb4_unicode_520_ci', 'utf8mb4', 246, '', 'Yes', 8, 'PAD SPACE'),
453454
('utf8mb4_0900_ai_ci', 'utf8mb4', 255, 'Yes', 'Yes', 0, 'NO PAD')
454455
)";
455456
default:

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Metadata_Tests.php‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ public function testInfromationSchemaCollations(): void {
186186
'SORTLEN' => '1',
187187
'PAD_ATTRIBUTE' => 'PAD SPACE',
188188
),
189+
(object) array(
190+
'COLLATION_NAME' => 'utf8mb4_unicode_520_ci',
191+
'CHARACTER_SET_NAME' => 'utf8mb4',
192+
'ID' => '246',
193+
'IS_DEFAULT' => '',
194+
'IS_COMPILED' => 'Yes',
195+
'SORTLEN' => '8',
196+
'PAD_ATTRIBUTE' => 'PAD SPACE',
197+
),
189198
(object) array(
190199
'COLLATION_NAME' => 'utf8mb4_unicode_ci',
191200
'CHARACTER_SET_NAME' => 'utf8mb4',
@@ -516,19 +525,20 @@ public function testShowCollation(): void {
516525
// Simple.
517526
$this->assertQuery( 'SHOW COLLATION' );
518527
$actual = $this->last_result;
519-
$this->assertCount( 7, $actual );
528+
$this->assertCount( 8, $actual );
520529
$this->assertEquals( 'binary', $actual[0]->Collation );
521530
$this->assertEquals( 'utf8_bin', $actual[1]->Collation );
522531
$this->assertEquals( 'utf8_general_ci', $actual[2]->Collation );
523532
$this->assertEquals( 'utf8_unicode_ci', $actual[3]->Collation );
524533
$this->assertEquals( 'utf8mb4_bin', $actual[4]->Collation );
525534
$this->assertEquals( 'utf8mb4_unicode_ci', $actual[5]->Collation );
526-
$this->assertEquals( 'utf8mb4_0900_ai_ci', $actual[6]->Collation );
535+
$this->assertEquals( 'utf8mb4_unicode_520_ci', $actual[6]->Collation );
536+
$this->assertEquals( 'utf8mb4_0900_ai_ci', $actual[7]->Collation );
527537

528538
// With LIKE clause.
529539
$this->assertQuery( "SHOW COLLATION LIKE 'utf8%'" );
530540
$actual = $this->last_result;
531-
$this->assertCount( 6, $actual );
541+
$this->assertCount( 7, $actual );
532542
$this->assertEquals( 'utf8_bin', $actual[0]->Collation );
533543
$this->assertEquals( 'utf8_general_ci', $actual[1]->Collation );
534544
$this->assertEquals( 'utf8_unicode_ci', $actual[2]->Collation );

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4213,7 +4213,7 @@ public function testFoundRowsWithoutSqlCalcFoundRows(): void {
42134213
// SHOW COLLATION
42144214
$this->assertQuery( 'SHOW COLLATION' );
42154215
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4216-
$this->assertSame( '7', $result[0]->{'FOUND_ROWS()'} );
4216+
$this->assertSame( '8', $result[0]->{'FOUND_ROWS()'} );
42174217

42184218
// SHOW DATABASES
42194219
$this->assertQuery( 'SHOW DATABASES' );

‎packages/mysql-on-sqlite/tests/WP_SQLite_DB_Tests.php‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,62 @@ public function __construct( ?WP_MySQL_On_SQLite $driver ) {
162162
$this->expectExceptionMessage( 'Cannot escape data without an active database connection.' );
163163
$wpdb->_real_escape( 'value' );
164164
}
165+
166+
/**
167+
* Each data set runs in a separate process to define its own constants.
168+
*
169+
* @dataProvider dataCharsetConstants
170+
* @runInSeparateProcess
171+
* @preserveGlobalState disabled
172+
*/
173+
public function testInitCharset( array $constants, string $expected_collate ): void {
174+
foreach ( $constants as $name => $value ) {
175+
define( $name, $value );
176+
}
177+
178+
$wpdb = new class() extends WP_SQLite_DB {
179+
public $charset;
180+
public $collate;
181+
182+
public function __construct() {}
183+
};
184+
$wpdb->init_charset();
185+
186+
$this->assertSame( 'utf8mb4', $wpdb->charset );
187+
$this->assertSame( $expected_collate, $wpdb->collate );
188+
}
189+
190+
public static function dataCharsetConstants(): array {
191+
return array(
192+
'no constants' => array( array(), 'utf8mb4_unicode_520_ci' ),
193+
'empty collation' => array( array( 'DB_COLLATE' => '' ), 'utf8mb4_unicode_520_ci' ),
194+
'null collation' => array( array( 'DB_COLLATE' => null ), 'utf8mb4_unicode_520_ci' ),
195+
'utf8 charset' => array(
196+
array(
197+
'DB_CHARSET' => 'utf8',
198+
'DB_COLLATE' => '',
199+
),
200+
'utf8mb4_unicode_520_ci',
201+
),
202+
'non-UTF-8 charset' => array(
203+
array(
204+
'DB_CHARSET' => 'latin1',
205+
'DB_COLLATE' => 'latin1_swedish_ci',
206+
),
207+
'utf8mb4_unicode_520_ci',
208+
),
209+
'non-UTF-8 collation' => array( array( 'DB_COLLATE' => 'latin1_swedish_ci' ), 'utf8mb4_unicode_520_ci' ),
210+
'charset as collation' => array( array( 'DB_COLLATE' => 'utf8mb4' ), 'utf8mb4_unicode_520_ci' ),
211+
'utf8_general_ci' => array( array( 'DB_COLLATE' => 'utf8_general_ci' ), 'utf8mb4_unicode_520_ci' ),
212+
'utf8_unicode_ci' => array( array( 'DB_COLLATE' => 'utf8_unicode_ci' ), 'utf8mb4_unicode_520_ci' ),
213+
'utf8_bin' => array( array( 'DB_COLLATE' => 'utf8_bin' ), 'utf8mb4_bin' ),
214+
'utf8_swedish_ci' => array( array( 'DB_COLLATE' => 'utf8_swedish_ci' ), 'utf8mb4_swedish_ci' ),
215+
'utf8mb3_general_ci' => array( array( 'DB_COLLATE' => 'utf8mb3_general_ci' ), 'utf8mb4_unicode_520_ci' ),
216+
'utf8mb3_bin' => array( array( 'DB_COLLATE' => 'utf8mb3_bin' ), 'utf8mb4_bin' ),
217+
'utf8mb4_unicode_ci' => array( array( 'DB_COLLATE' => 'utf8mb4_unicode_ci' ), 'utf8mb4_unicode_520_ci' ),
218+
'utf8mb4_general_ci' => array( array( 'DB_COLLATE' => 'utf8mb4_general_ci' ), 'utf8mb4_general_ci' ),
219+
'utf8mb4_0900_ai_ci' => array( array( 'DB_COLLATE' => 'utf8mb4_0900_ai_ci' ), 'utf8mb4_0900_ai_ci' ),
220+
'uppercase UTF-8 collation' => array( array( 'DB_COLLATE' => 'UTF8_BIN' ), 'utf8mb4_bin' ),
221+
);
222+
}
165223
}

‎packages/mysql-on-sqlite/tests/tools/verify-native-parser-extension.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ function wp_sqlite_assert_native_parser_delegate( WP_MySQL_Parser $parser, strin
3030
}
3131

3232
$native_property = $reflection->getProperty( 'native' );
33-
$native_property->setAccessible( true );
33+
if ( PHP_VERSION_ID < 80100 ) {
34+
$native_property->setAccessible( true );
35+
}
3436
if ( ! ( $native_property->getValue( $parser ) instanceof WP_MySQL_Native_Parser ) ) {
3537
wp_sqlite_native_parser_verification_fail( $context );
3638
}

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-db.php‎

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ public function __construct( $dbname ) {
4949
*/
5050
$GLOBALS['wpdb'] = $this;
5151

52+
// The SQLite driver may use the charset and collation while connecting.
53+
$this->init_charset();
54+
5255
parent::__construct( '', '', $dbname, '' );
53-
$this->charset = 'utf8mb4';
5456
}
5557

5658
/**
@@ -232,6 +234,36 @@ public function close() {
232234
return true;
233235
}
234236

237+
/**
238+
* Sets $this->charset and $this->collate.
239+
*
240+
* This overrides wpdb::init_charset(). SQLite stores all text as UTF-8, and
241+
* the emulated MySQL connection always uses utf8mb4 (see set_charset()), so
242+
* the charset is always utf8mb4, and only a UTF-8 DB_COLLATE applies.
243+
*
244+
* Unlike in wpdb, the result doesn't depend on the database connection.
245+
* This is important, as the SQLite driver may use the charset and collation
246+
* while connecting (see the constructor).
247+
*
248+
* @see wpdb::init_charset()
249+
*/
250+
public function init_charset() {
251+
$collate = defined( 'DB_COLLATE' ) ? strtolower( (string) DB_COLLATE ) : '';
252+
253+
// MySQL 8.0.30 and newer name the utf8 collations "utf8mb3_*".
254+
$collate = preg_replace( '/^utf8mb3_/', 'utf8_', $collate );
255+
256+
// Collations of other charsets don't apply to the utf8mb4 connection.
257+
if ( ! preg_match( '/^utf8(mb4)?_/', $collate ) ) {
258+
$collate = '';
259+
}
260+
261+
$charset_collate = $this->resolve_charset( 'utf8mb4', $collate );
262+
263+
$this->charset = $charset_collate['charset'];
264+
$this->collate = $charset_collate['collate'];
265+
}
266+
235267
/**
236268
* Determines the best charset and collation to use given a charset and collation.
237269
*
@@ -254,25 +286,7 @@ public function determine_charset( $charset, $collate ) {
254286
return compact( 'charset', 'collate' );
255287
}
256288

257-
if ( 'utf8' === $charset ) {
258-
$charset = 'utf8mb4';
259-
}
260-
261-
if ( 'utf8mb4' === $charset ) {
262-
// _general_ is outdated, so we can upgrade it to _unicode_, instead.
263-
if ( ! $collate || 'utf8_general_ci' === $collate ) {
264-
$collate = 'utf8mb4_unicode_ci';
265-
} else {
266-
$collate = str_replace( 'utf8_', 'utf8mb4_', $collate );
267-
}
268-
}
269-
270-
// _unicode_520_ is a better collation, we should use that when it's available.
271-
if ( $this->has_cap( 'utf8mb4_520' ) && 'utf8mb4_unicode_ci' === $collate ) {
272-
$collate = 'utf8mb4_unicode_520_ci';
273-
}
274-
275-
return compact( 'charset', 'collate' );
289+
return $this->resolve_charset( $charset, $collate );
276290
}
277291

278292
/**
@@ -428,10 +442,6 @@ public function db_connect( $allow_bail = true ) {
428442
);
429443
}
430444

431-
if ( ! isset( $this->charset ) ) {
432-
$this->init_charset();
433-
}
434-
435445
if ( null === $this->dbname || '' === $this->dbname ) {
436446
$this->bail(
437447
'The database name was not set. The SQLite driver requires a database name to be set to emulate MySQL information schema tables.',
@@ -764,6 +774,45 @@ public function db_server_info() {
764774
return $this->dbh->getAttribute( PDO::ATTR_SERVER_VERSION ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
765775
}
766776

777+
/**
778+
* Resolves the charset and collation as wpdb::determine_charset() does.
779+
*
780+
* Unlike in wpdb, this doesn't need a database connection. The emulated
781+
* MySQL server (5.7 or newer) always supports utf8mb4_unicode_520_ci.
782+
*
783+
* @see wpdb::determine_charset()
784+
*
785+
* @param string $charset The character set to check.
786+
* @param string $collate The collation to check.
787+
* @return array {
788+
* The most appropriate character set and collation to use.
789+
*
790+
* @type string $charset Character set.
791+
* @type string $collate Collation.
792+
* }
793+
*/
794+
private function resolve_charset( $charset, $collate ) {
795+
if ( 'utf8' === $charset ) {
796+
$charset = 'utf8mb4';
797+
}
798+
799+
if ( 'utf8mb4' === $charset ) {
800+
// _general_ is outdated, so we can upgrade it to _unicode_, instead.
801+
if ( ! $collate || 'utf8_general_ci' === $collate ) {
802+
$collate = 'utf8mb4_unicode_ci';
803+
} else {
804+
$collate = str_replace( 'utf8_', 'utf8mb4_', $collate );
805+
}
806+
}
807+
808+
// _unicode_520_ is a better collation, we should use that when it's available.
809+
if ( 'utf8mb4_unicode_ci' === $collate ) {
810+
$collate = 'utf8mb4_unicode_520_ci';
811+
}
812+
813+
return compact( 'charset', 'collate' );
814+
}
815+
767816
/**
768817
* Format MySQL-on-SQLite driver error message.
769818
*
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
class WP_SQLite_Database_Integration_Charset_Test extends PHPUnit_Adapter_TestCase {
4+
5+
public function test_charset_and_collation() {
6+
global $wpdb;
7+
8+
$this->assertSame( 'utf8mb4', $wpdb->charset );
9+
$this->assertSame( 'utf8mb4_unicode_520_ci', $wpdb->collate );
10+
$this->assertSame(
11+
'DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci',
12+
$wpdb->get_charset_collate()
13+
);
14+
}
15+
16+
public function test_charset_and_collation_without_connection() {
17+
global $wpdb;
18+
19+
// The SQLite driver uses the charset while connecting (e.g., to reconstruct tables).
20+
$this->assertTrue( $wpdb->close() );
21+
try {
22+
$wpdb->charset = null;
23+
$wpdb->collate = null;
24+
$wpdb->init_charset();
25+
$this->assertSame( 'utf8mb4', $wpdb->charset );
26+
$this->assertSame( 'utf8mb4_unicode_520_ci', $wpdb->collate );
27+
} finally {
28+
$this->assertTrue( $wpdb->check_connection() );
29+
}
30+
}
31+
32+
public function test_wordpress_table_reconstructed_while_connecting() {
33+
global $wpdb;
34+
35+
$table = $wpdb->options;
36+
$driver = $wpdb->get_driver();
37+
$connection = $driver->get_connection();
38+
$this->assertFalse( $driver->inTransaction() );
39+
40+
// Remove the table from the information schema, and make the next connection reconstruct it.
41+
$connection->query(
42+
sprintf(
43+
'DELETE FROM %s WHERE table_name = ?',
44+
$connection->quote_identifier( WP_MySQL_On_SQLite::RESERVED_PREFIX . 'mysql_information_schema_tables' )
45+
),
46+
array( $table )
47+
);
48+
$connection->query(
49+
sprintf(
50+
'DELETE FROM %s WHERE name = ?',
51+
$connection->quote_identifier( WP_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME )
52+
),
53+
array( WP_MySQL_On_SQLite::DRIVER_VERSION_VARIABLE_NAME )
54+
);
55+
56+
$original_wpdb = $wpdb;
57+
$this->assertTrue( $wpdb->close() );
58+
$db = null;
59+
try {
60+
// As in the SQLite drop-in, the constructor connects and reconstructs the table.
61+
$db = new WP_SQLite_DB( DB_NAME );
62+
$collation = $db->get_var(
63+
$db->prepare(
64+
'SELECT table_collation FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = %s',
65+
$table
66+
)
67+
);
68+
} finally {
69+
if ( $db ) {
70+
$db->close();
71+
}
72+
$GLOBALS['wpdb'] = $original_wpdb;
73+
$this->assertTrue( $original_wpdb->check_connection() );
74+
}
75+
76+
$this->assertSame( 'utf8mb4_unicode_520_ci', $collation );
77+
}
78+
79+
public function test_table_created_with_charset_collate() {
80+
global $wpdb;
81+
82+
$table = $wpdb->prefix . 'sqlite_charset_test';
83+
$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) );
84+
85+
try {
86+
$wpdb->query( $wpdb->prepare( 'CREATE TABLE %i (id INT, name VARCHAR(20))', $table ) . ' ' . $wpdb->get_charset_collate() );
87+
$this->assertSame( '', $wpdb->last_error );
88+
89+
$this->assertSame(
90+
'utf8mb4_unicode_520_ci',
91+
$wpdb->get_var(
92+
$wpdb->prepare(
93+
'SELECT table_collation FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = %s',
94+
$table
95+
)
96+
)
97+
);
98+
99+
$create_table = $wpdb->get_row( $wpdb->prepare( 'SHOW CREATE TABLE %i', $table ), ARRAY_N )[1];
100+
$this->assertStringEndsWith( 'DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci', $create_table );
101+
} finally {
102+
$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) );
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)