[BUGFIX] Doctrine: Use compatible charset name for database connection 10/48310/3
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Fri, 27 May 2016 08:23:35 +0000 (10:23 +0200)
committerFrank Naegler <frank.naegler@typo3.org>
Fri, 27 May 2016 12:26:27 +0000 (14:26 +0200)
Use the charset name 'utf8' instead of 'utf-8' for MySQL connections as
some MySQL server versions silently ignore the connection charset name
'utf-8' and work with the global default connection charset instead.
Using 'utf8' as charset name doesn't exhibit this behavior.

Also changes the silent upgrade wizard to only set the default driver
and connection charset if no value has been configured, this avoids
silently changing non-default values for these options.

Change-Id: I94e1e7c557aff890cec357ef9ee069ae9aa052c6
Resolves: #76265
Releases: master
Reviewed-on: https://review.typo3.org/48310
Tested-by: Riccardo De Contardi <erredeco@gmail.com>
Tested-by: Gianluigi Martino <gmartino27@gmail.com>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/install/Classes/Service/SilentConfigurationUpgradeService.php

index 03d8f6e..4f7a64c 100755 (executable)
@@ -106,6 +106,7 @@ class SilentConfigurationUpgradeService
         $this->migrateThumbnailsPngSetting();
         $this->migrateLockSslSetting();
         $this->migrateDatabaseConnectionSettings();
+        $this->migrateDatabaseConnectionCharset();
     }
 
     /**
@@ -660,15 +661,44 @@ class SilentConfigurationUpgradeService
             );
         }
 
-        // Set the utf-8 connection charset by default
-        $confManager->setLocalConfigurationValueByPath('DB/Connections/Default/charset', 'utf-8');
+        // Set the utf-8 connection charset by default if no value has been provided yet
+        try {
+            $confManager->getLocalConfigurationValueByPath('DB/Connections/Default/charset');
+        } catch (\RuntimeException $e) {
+            $confManager->setLocalConfigurationValueByPath('DB/Connections/Default/charset', 'utf8');
+        }
 
-        // Use the mysqli driver by default
-        $confManager->setLocalConfigurationValueByPath('DB/Connections/Default/driver', 'mysqli');
+        // Use the mysqli driver by default if no value has been provided yet
+        try {
+            $confManager->getLocalConfigurationValueByPath('DB/Connections/Default/driver');
+        } catch (\RuntimeException $e) {
+            $confManager->setLocalConfigurationValueByPath('DB/Connections/Default/driver', 'mysqli');
+        }
 
         if (!empty(array_filter($changedSettings))) {
             $confManager->removeLocalConfigurationKeysByPath(array_keys($changedSettings));
             $this->throwRedirectException();
         }
     }
+
+    /**
+     * Migrate the configuration setting DB/Connections/Default/charset to 'utf8' as
+     * 'utf-8' is not supported by all MySQL versions.
+     *
+     * @return void
+     */
+    protected function migrateDatabaseConnectionCharset()
+    {
+        $confManager = $this->configurationManager;
+        try {
+            $driver = $confManager->getLocalConfigurationValueByPath('DB/Connections/Default/driver');
+            $charset = $confManager->getLocalConfigurationValueByPath('DB/Connections/Default/charset');
+            if (in_array($driver, ['mysqli', 'pdo_mysql', 'drizzle_pdo_mysql'], true) && $charset === 'utf-8') {
+                $confManager->setLocalConfigurationValueByPath('DB/Connections/Default/charset', 'utf8');
+                $this->throwRedirectException();
+            }
+        } catch (\RuntimeException $e) {
+            // no incompatible charset configuration found, so nothing needs to be modified
+        }
+    }
 }