Skip to content

Commit af51a95

Browse files
committed
Resolve the wpdb charset and collation without a connection
With DB_CHARSET 'utf8' and an empty DB_COLLATE, $wpdb->collate stayed empty, because the charset was only initialized before connecting, when wpdb::determine_charset() doesn't resolve it. Tables created with get_charset_collate() then got the MySQL 8-only utf8mb4_0900_ai_ci collation, which fails to import into MariaDB. SQLite stores all text as UTF-8 and the emulated connection always uses utf8mb4, so the charset and collation don't depend on the connection. Resolve them to utf8mb4 with utf8mb4_unicode_520_ci (or a configured UTF-8 collation) in init_charset(). This also applies while the driver connects, when it may reconstruct WordPress tables with wp_get_db_schema(). Reported in Automattic/studio#4737
1 parent a8468b5 commit af51a95

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public function __construct( $dbname ) {
5050
$GLOBALS['wpdb'] = $this;
5151

5252
parent::__construct( '', '', $dbname, '' );
53-
$this->charset = 'utf8mb4';
5453
}
5554

5655
/**
@@ -232,6 +231,41 @@ public function close() {
232231
return true;
233232
}
234233

234+
/**
235+
* Sets $this->charset and $this->collate.
236+
*
237+
* This overrides wpdb::init_charset(). SQLite stores all text as UTF-8, and
238+
* the emulated MySQL connection always uses utf8mb4 (see set_charset()), so
239+
* the charset is always utf8mb4, and only a UTF-8 DB_COLLATE applies.
240+
*
241+
* Unlike in wpdb, the result doesn't depend on the database connection.
242+
* This is important, as the SQLite driver may use the charset and collation
243+
* while connecting (see the constructor).
244+
*
245+
* @see wpdb::init_charset()
246+
*/
247+
public function init_charset() {
248+
$collate = defined( 'DB_COLLATE' ) ? strtolower( DB_COLLATE ) : '';
249+
250+
// Collations of other charsets don't apply to the utf8mb4 connection.
251+
if ( ! preg_match( '/^utf8(mb4)?_/', $collate ) ) {
252+
$collate = '';
253+
}
254+
255+
// Upgrade the collation, as wpdb::determine_charset() does for utf8mb4.
256+
if ( '' === $collate || 'utf8_general_ci' === $collate ) {
257+
$collate = 'utf8mb4_unicode_ci';
258+
} else {
259+
$collate = str_replace( 'utf8_', 'utf8mb4_', $collate );
260+
}
261+
if ( 'utf8mb4_unicode_ci' === $collate ) {
262+
$collate = 'utf8mb4_unicode_520_ci';
263+
}
264+
265+
$this->charset = 'utf8mb4';
266+
$this->collate = $collate;
267+
}
268+
235269
/**
236270
* Determines the best charset and collation to use given a charset and collation.
237271
*
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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->init_charset();
23+
$this->assertSame( 'utf8mb4', $wpdb->charset );
24+
$this->assertSame( 'utf8mb4_unicode_520_ci', $wpdb->collate );
25+
} finally {
26+
$this->assertTrue( $wpdb->check_connection() );
27+
}
28+
}
29+
30+
public function test_table_created_with_charset_collate() {
31+
global $wpdb;
32+
33+
$table = $wpdb->prefix . 'sqlite_charset_test';
34+
$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) );
35+
36+
try {
37+
$wpdb->query( $wpdb->prepare( 'CREATE TABLE %i (id INT, name VARCHAR(20))', $table ) . ' ' . $wpdb->get_charset_collate() );
38+
$this->assertSame( '', $wpdb->last_error );
39+
40+
$this->assertSame(
41+
'utf8mb4_unicode_520_ci',
42+
$wpdb->get_var(
43+
$wpdb->prepare(
44+
'SELECT table_collation FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = %s',
45+
$table
46+
)
47+
)
48+
);
49+
50+
$create_table = $wpdb->get_row( $wpdb->prepare( 'SHOW CREATE TABLE %i', $table ), ARRAY_N )[1];
51+
$this->assertStringEndsWith( 'DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci', $create_table );
52+
} finally {
53+
$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) );
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)