diff --git a/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php b/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php --- a/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php +++ b/src/aphront/storage/connection/mysql/AphrontBaseMySQLDatabaseConnection.php @@ -20,7 +20,7 @@ abstract protected function freeResult($result); public function __construct(array $configuration) { - $this->configuration = $configuration; + $this->configuration = $configuration; } public function __clone() { @@ -324,16 +324,32 @@ * can lead to data loss and security problems. */ protected function validateUTF8String($string) { - if (phutil_is_utf8_with_only_bmp_characters($string)) { - return; - } + // TODO: Make this `true` eventually, once we make storage adjustment + // mandatory. For now, you can set it to `true` to test things. + $has_utf8mb4 = false; + + if ($has_utf8mb4) { + if (phutil_is_utf8($string)) { + return; + } - throw new AphrontCharacterSetQueryException( - pht( - 'Attempting to construct a query containing characters outside of '. - 'the Unicode Basic Multilingual Plane. MySQL will silently truncate '. - 'this data if it is inserted into a `utf8` column. Use the `%%B` '. - 'conversion to escape binary strings data.')); + throw new AphrontCharacterSetQueryException( + pht( + 'Attempting to construct a query using a non-utf8 string when '. + 'utf8 is expected. Use the `%%B` conversion to escape binary '. + 'strings data.')); + } else { + if (phutil_is_utf8_with_only_bmp_characters($string)) { + return; + } + + throw new AphrontCharacterSetQueryException( + pht( + 'Attempting to construct a query containing characters outside of '. + 'the Unicode Basic Multilingual Plane. MySQL will silently truncate '. + 'this data if it is inserted into a `utf8` column. Use the `%%B` '. + 'conversion to escape binary strings data.')); + } } } diff --git a/src/aphront/storage/connection/mysql/AphrontMySQLDatabaseConnection.php b/src/aphront/storage/connection/mysql/AphrontMySQLDatabaseConnection.php --- a/src/aphront/storage/connection/mysql/AphrontMySQLDatabaseConnection.php +++ b/src/aphront/storage/connection/mysql/AphrontMySQLDatabaseConnection.php @@ -72,7 +72,10 @@ } } - mysql_set_charset('utf8', $conn); + $ok = mysql_set_charset('utf8mb4', $conn); + if (!$ok) { + mysql_set_charset('utf8', $conn); + } return $conn; } diff --git a/src/aphront/storage/connection/mysql/AphrontMySQLiDatabaseConnection.php b/src/aphront/storage/connection/mysql/AphrontMySQLiDatabaseConnection.php --- a/src/aphront/storage/connection/mysql/AphrontMySQLiDatabaseConnection.php +++ b/src/aphront/storage/connection/mysql/AphrontMySQLiDatabaseConnection.php @@ -67,7 +67,10 @@ "#{$errno}: {$error}.", $errno); } - $conn->set_charset('utf8'); + $ok = $conn->set_charset('utf8mb4'); + if (!$ok) { + $ok = $conn->set_charset('utf8'); + } return $conn; }