From a67d926ec3154145aea0563a82b1a5ee570ce27c Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Mon, 24 Oct 2016 23:01:30 +0200
Subject: [PATCH] [BUGFIX] Report module should not alert when all db tables
 are utf8

In case a database default character set is latin1 but all tables
created in the database are utf8, the reports module warns that
the db is configured wrong, which might be the case, but on certain
hosters this is not an excuse. Instead, all TYPO3 tables
are created with a utf8 collation (equiv. to utf8 character set) and
stored correctly, so there is no need to throw a big red box.

The patch extends the utf-8 check to see if all database tables
are utf8 and everything is fine.

Resolves: #78413
Releases: master
Change-Id: I78cc65e4fec58e40f41f36ec6dbf6d46e867246f
Reviewed-on: https://review.typo3.org/50351
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../Report/Status/ConfigurationStatus.php     | 29 ++++++++++++++++---
 .../Private/Language/locallang_reports.xlf    |  6 ++++
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/typo3/sysext/reports/Classes/Report/Status/ConfigurationStatus.php b/typo3/sysext/reports/Classes/Report/Status/ConfigurationStatus.php
index 1cd256a59a3b..74c688be39f4 100644
--- a/typo3/sysext/reports/Classes/Report/Status/ConfigurationStatus.php
+++ b/typo3/sysext/reports/Classes/Report/Status/ConfigurationStatus.php
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Reports\Report\Status;
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Database\Query\QueryBuilder;
 use TYPO3\CMS\Core\Messaging\FlashMessage;
 use TYPO3\CMS\Core\Messaging\FlashMessageService;
 use TYPO3\CMS\Core\Registry;
@@ -288,6 +289,7 @@ class ConfigurationStatus implements StatusProviderInterface
     {
         $connection = GeneralUtility::makeInstance(ConnectionPool::class)
             ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
+        /** @var QueryBuilder $queryBuilder */
         $queryBuilder = $connection->createQueryBuilder();
         $defaultDatabaseCharset = (string)$queryBuilder->select('DEFAULT_CHARACTER_SET_NAME')
             ->from('information_schema.SCHEMATA')
@@ -302,10 +304,29 @@ class ConfigurationStatus implements StatusProviderInterface
         $statusValue = $this->getLanguageService()->getLL('status_ok');
         // also allow utf8mb4
         if (!StringUtility::beginsWith($defaultDatabaseCharset, 'utf8')) {
-            $message = sprintf($this->getLanguageService()
-                ->getLL('status_MysqlDatabaseCharacterSet_Unsupported'), $defaultDatabaseCharset);
-            $severity = ReportStatus::ERROR;
-            $statusValue = $this->getLanguageService()->getLL('status_wrongValue');
+            // If the default character set is e.g. latin1, BUT all tables in the system are UTF-8,
+            // we assume that TYPO3 has the correct charset for adding tables, and everything is fine
+            $nonUtf8TableCollationsFound = $queryBuilder->select('table_collation')
+                ->from('information_schema.tables')
+                ->where(
+                    $queryBuilder->expr()->andX(
+                        $queryBuilder->expr()->eq('table_schema', $queryBuilder->quote($connection->getDatabase())),
+                        $queryBuilder->expr()->notLike('table_collation', $queryBuilder->quote('utf8%'))
+                    )
+                )
+                ->setMaxResults(1)
+                ->execute();
+
+            if ($nonUtf8TableCollationsFound->rowCount() > 0) {
+                $message = sprintf($this->getLanguageService()
+                    ->getLL('status_MysqlDatabaseCharacterSet_Unsupported'), $defaultDatabaseCharset);
+                $severity = ReportStatus::ERROR;
+                $statusValue = $this->getLanguageService()->getLL('status_wrongValue');
+            } else {
+                $message = $this->getLanguageService()->getLL('status_MysqlDatabaseCharacterSet_Info');
+                $severity = ReportStatus::INFO;
+                $statusValue = $this->getLanguageService()->getLL('status_info');
+            }
         } else {
             $message = $this->getLanguageService()->getLL('status_MysqlDatabaseCharacterSet_Ok');
         }
diff --git a/typo3/sysext/reports/Resources/Private/Language/locallang_reports.xlf b/typo3/sysext/reports/Resources/Private/Language/locallang_reports.xlf
index 4a5f656a31fd..a1fc98495a19 100644
--- a/typo3/sysext/reports/Resources/Private/Language/locallang_reports.xlf
+++ b/typo3/sysext/reports/Resources/Private/Language/locallang_reports.xlf
@@ -18,6 +18,9 @@
 			<trans-unit id="status_wrongValue">
 				<source>Wrong value detected</source>
 			</trans-unit>
+			<trans-unit id="status_info">
+				<source>Information</source>
+			</trans-unit>
 			<trans-unit id="status_insecure">
 				<source>Insecure</source>
 			</trans-unit>
@@ -114,6 +117,9 @@
 			<trans-unit id="status_MysqlDatabaseCharacterSet_Ok">
 				<source>Your default database uses utf-8. All good.</source>
 			</trans-unit>
+			<trans-unit id="status_MysqlDatabaseCharacterSet_Info">
+				<source>Your default database uses a different charset, but all tables uses utf-8. All good. But consider fixing your database collation and check the table creation settings.</source>
+			</trans-unit>
 			<trans-unit id="status_trustedHostsPattern">
 				<source>Trusted Hosts Pattern</source>
 			</trans-unit>
-- 
GitLab