From 477d8a4b35cba80af54dc9dac71bf09384032681 Mon Sep 17 00:00:00 2001 From: "Nicholas K. Dionysopoulos" Date: Sat, 13 Jun 2015 21:19:48 +0300 Subject: [PATCH] UTF-8 Multibyte (utf8mb4) support Make the Database Fix feature really convert an existing database to utf8mb4. The kicker? It's not required in any installation or upgrade scenario. It's only required to let people test the pull request of this feature. [sigh] --- .../com_installer/models/database.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/administrator/components/com_installer/models/database.php b/administrator/components/com_installer/models/database.php index 75165013a7b41..e6a4398f82a52 100644 --- a/administrator/components/com_installer/models/database.php +++ b/administrator/components/com_installer/models/database.php @@ -63,6 +63,9 @@ public function fix() $installer = new JoomlaInstallerScript; $installer->deleteUnexistingFiles(); $this->fixDefaultTextFilters(); + + // Finally, make sure the database is converted to utf8mb4 if supported by the server + $this->convertTablesToUtf8mb4(); } /** @@ -255,4 +258,45 @@ public function fixDefaultTextFilters() } } } + + public function convertTablesToUtf8mb4() + { + $db = JFactory::getDbo(); + + // If the database does not have UTF-8 Multibyte (utf8mb4) support we can't do much about it. + if (!$db->hasUTF8mb4Support()) + { + return; + } + + // Get the SQL file to convert the core tables. Yes, this is hardcoded because we have all sorts of index + // conversions and funky things we can't automate in core tables without an actual SQL file. + $serverType = $db->getServerType(); + $fileName = JPATH_ADMINISTRATOR . "/components/com_admin/sql/updates/$serverType/3.5.0-2015-01-01.sql"; + + if (!is_file($fileName)) + { + return; + } + + $fileContents = @file_get_contents($fileName); + $queries = $db->splitSql($fileContents); + + if (empty($queries)) + { + return; + } + + foreach ($queries as $query) + { + try + { + $db->setQuery($query)->execute(); + } + catch (Exception $e) + { + // If the query fails we will go on. It probably means we've already done this conversion. + } + } + } }