[FEATURE] Check database default charset
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / DatabaseCharsetUpdate.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Move "wizard done" flags to system registry
19 */
20 class DatabaseCharsetUpdate extends AbstractUpdate
21 {
22 /**
23 * @var string
24 */
25 protected $title = 'Set default database charset to utf-8';
26
27 /**
28 * Checks if an update is needed
29 *
30 * @param string &$description The description for the update
31 * @return bool Whether an update is needed (TRUE) or not (FALSE)
32 */
33 public function checkForUpdate(&$description)
34 {
35 $result = false;
36 $description = 'Sets the default database charset to utf-8 to ensure new tables are created with correct charset.
37 WARNING: This will NOT convert any existing data.';
38
39 if($this->isDbalEnabled()) {
40 return $result;
41 }
42 // check if database charset is utf-8
43 $defaultDatabaseCharset = $this->getDefaultDatabaseCharset();
44 // also allow utf8mb4
45 if (substr($defaultDatabaseCharset, 0, 4) !== 'utf8') {
46 $result = true;
47 }
48
49 return $result;
50 }
51
52 /**
53 * Performs the accordant updates.
54 *
55 * @param array &$dbQueries Queries done in this update
56 * @param mixed &$customMessages Custom messages
57 * @return bool Whether everything went smoothly or not
58 */
59 public function performUpdate(array &$dbQueries, &$customMessages)
60 {
61 $result = true;
62 $db = $this->getDatabaseConnection();
63 $query = 'ALTER DATABASE `' . $GLOBALS['TYPO3_CONF_VARS']['DB']['database'] . '` DEFAULT CHARACTER SET utf8';
64 $db->admin_query($query);
65 $dbQueries[] = $query;
66 if ($db->sql_error()) {
67 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($db->sql_error());
68 $result = false;
69 }
70 return $result;
71 }
72
73 /**
74 * Return TRUE if dbal and adodb extension is loaded
75 *
76 * @return bool TRUE if dbal and adodb is loaded
77 */
78 protected function isDbalEnabled()
79 {
80 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('adodb')
81 && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal')
82 ) {
83 return true;
84 }
85 return false;
86 }
87
88 /**
89 * Retrieves the default character set of the database.
90 *
91 * @return string
92 */
93 protected function getDefaultDatabaseCharset()
94 {
95 $db = $this->getDatabaseConnection();
96 $result = $db->admin_query('SHOW VARIABLES LIKE "character_set_database"');
97 $row = $db->sql_fetch_assoc($result);
98
99 $key = $row['Variable_name'];
100 $value = $row['Value'];
101 $databaseCharset = '';
102
103 if ($key == 'character_set_database') {
104 $databaseCharset = $value;
105 }
106
107 return $databaseCharset;
108 }
109 }