[FEATURE] Report wrong database character set 16/47016/4
authorAndreas Wolf <andreas.wolf@typo3.org>
Thu, 3 Mar 2016 17:27:00 +0000 (18:27 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Fri, 4 Mar 2016 15:04:50 +0000 (16:04 +0100)
This adds a new report to the reports module that checks if the MySQL
database uses the right character set.

Change-Id: I5e5d8b4767accb74f3cbfb8eb4c9f4824dfd7eca
Resolves: #74038
Releases: master
Reviewed-on: https://review.typo3.org/47016
Reviewed-by: Jonas Götze <jonnsn@gmail.com>
Tested-by: Jonas Götze <jonnsn@gmail.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/reports/Classes/Report/Status/ConfigurationStatus.php
typo3/sysext/reports/Resources/Private/Language/locallang_reports.xlf

index 3292d5d..cdc9474 100644 (file)
@@ -63,6 +63,9 @@ class ConfigurationStatus implements StatusProviderInterface
             $statuses['createdFilesWorldWritable'] = $this->getCreatedFilesWorldWritableStatus();
             $statuses['createdDirectoriesWorldWritable'] = $this->getCreatedDirectoriesWorldWritableStatus();
         }
+        if ($this->isMysqlUsed()) {
+            $statuses['mysqlDatabaseUsesUtf8'] = $this->getMysqlDatabaseUtf8Status();
+        }
         return $statuses;
     }
 
@@ -256,6 +259,53 @@ class ConfigurationStatus implements StatusProviderInterface
     }
 
     /**
+     * Verifies that MySQL is used.
+     *
+     * @return bool
+     */
+    protected function isMysqlUsed()
+    {
+        return get_class($this->getDatabaseConnection()) == DatabaseConnection::class;
+    }
+
+    /**
+     * Checks the character set of the database and reports an error if it is not utf-8.
+     *
+     * @return ReportStatus
+     */
+    protected function getMysqlDatabaseUtf8Status()
+    {
+        $result = $this->getDatabaseConnection()->admin_query('SHOW VARIABLES LIKE "character_set_database"');
+        $row = $this->getDatabaseConnection()->sql_fetch_assoc($result);
+
+        $key = $row['Variable_name'];
+        $value = $row['Value'];
+
+        $message = '';
+        $severity = ReportStatus::OK;
+        $statusValue = $this->getLanguageService()->getLL('status_ok');
+
+        if ($key !== 'character_set_database') {
+            $message = sprintf($this->getLanguageService()->getLL('status_MysqlDatabaseCharacterSet_CheckFailed'),$key);
+            $severity = ReportStatus::WARNING;
+            $statusValue = $this->getLanguageService()->getLL('status_checkFailed');
+        }
+        // also allow utf8mb4
+        if (substr($value, 0, 4) !== 'utf8') {
+            $message = sprintf($this->getLanguageService()->getLL('status_MysqlDatabaseCharacterSet_Unsupported'),$value);
+            $severity = ReportStatus::ERROR;
+            $statusValue = $this->getLanguageService()->getLL('status_wrongValue');
+        } else {
+            $message = $this->getLanguageService()->getLL('status_MysqlDatabaseCharacterSet_Ok');
+        }
+
+        return GeneralUtility::makeInstance(ReportStatus::class,
+            $this->getLanguageService()->getLL('status_MysqlDatabaseCharacterSet'),
+            $statusValue, $message, $severity
+        );
+    }
+
+    /**
      * Executes admin commands.
      *
      * Currently implemented commands are:
index 7e4d073..ed3a689 100644 (file)
@@ -15,6 +15,9 @@
                        <trans-unit id="status_ok">
                                <source>OK</source>
                        </trans-unit>
+                       <trans-unit id="status_wrongValue">
+                               <source>Wrong value detected</source>
+                       </trans-unit>
                        <trans-unit id="status_insecure">
                                <source>Insecure</source>
                        </trans-unit>
@@ -39,6 +42,9 @@
                        <trans-unit id="status_connectionFailed">
                                <source>Connection Failed</source>
                        </trans-unit>
+                       <trans-unit id="status_checkFailed">
+                               <source>Check Failed</source>
+                       </trans-unit>
                        <trans-unit id="status_updateComplete">
                                <source>Update Complete</source>
                        </trans-unit>
                        <trans-unit id="status_CreatedDirectoryPermissions.writable">
                                <source>Directories created by TYPO3 are configured to be world writable. Depending on your server configuration, this can be a security risk. It is usually better to configure the create mask to not allow writing to directories by "others". A sane default is often '2770' for $GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask']. This can be set in the install tool.</source>
                        </trans-unit>
+                       <trans-unit id="status_MysqlDatabaseCharacterSet">
+                               <source>MySQL Database Character Set</source>
+                       </trans-unit>
+                       <trans-unit id="status_MysqlDatabaseCharacterSet_CheckFailed">
+                               <source>Checking database character set failed, got key "%1$s" instead of "character_set_database"</source>
+                       </trans-unit>
+                       <trans-unit id="status_MysqlDatabaseCharacterSet_Unsupported">
+                               <source>Your database uses character set "%1$s", but only "utf8" is supported with TYPO3.</source>
+                       </trans-unit>
+                       <trans-unit id="status_MysqlDatabaseCharacterSet_Ok">
+                               <source>Your database uses utf-8. All good.</source>
+                       </trans-unit>
                        <trans-unit id="status_trustedHostsPattern">
                                <source>Trusted Hosts Pattern</source>
                        </trans-unit>