‪TYPO3CMS  11.5
MySql.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
25 
38 {
44  protected ‪$minimumMySQLVersion = '5.5.0';
45 
51  protected ‪$incompatibleSqlModes = [
52  'NO_BACKSLASH_ESCAPES',
53  ];
54 
59  protected ‪$databaseCharsetToCheck = [
60  'utf8',
61  'utf8mb3',
62  'utf8mb4',
63  ];
64 
70  'utf8',
71  'utf8mb3',
72  'utf8mb4',
73  ];
74 
82  public function ‪getStatus(): ‪FlashMessageQueue
83  {
84  $defaultConnection = GeneralUtility::makeInstance(ConnectionPool::class)
85  ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
86  if (strpos($defaultConnection->getServerVersion(), 'MySQL') !== 0) {
88  }
89  $this->‪checkMysqlVersion($defaultConnection);
90  $this->‪checkInvalidSqlModes($defaultConnection);
91  $this->‪checkDefaultDatabaseCharset($defaultConnection);
92  $this->‪checkDefaultDatabaseServerCharset($defaultConnection);
93  $this->‪checkDatabaseName($defaultConnection);
95  }
96 
102  protected function ‪checkInvalidSqlModes(‪Connection $connection)
103  {
104  $detectedIncompatibleSqlModes = $this->‪getIncompatibleSqlModes($connection);
105  if (!empty($detectedIncompatibleSqlModes)) {
106  $this->messageQueue->enqueue(new ‪FlashMessage(
107  'Incompatible SQL modes have been detected:'
108  . ' ' . implode(', ', $detectedIncompatibleSqlModes) . '.'
109  . ' The listed modes are not compatible with TYPO3 CMS.'
110  . ' You have to change that setting in your MySQL environment'
111  . ' or in $GLOBALS[\'TYPO3_CONF_VARS\'][\'DB\'][\'Connections\'][\'Default\'][\'initCommands\']',
112  'Incompatible SQL modes found!',
114  ));
115  } else {
116  $this->messageQueue->enqueue(new ‪FlashMessage(
117  '',
118  'No incompatible SQL modes found.'
119  ));
120  }
121  }
122 
128  protected function ‪checkMysqlVersion(‪Connection $connection)
129  {
130  preg_match('/MySQL ((\d+\.)*(\d+\.)*\d+)/', $connection->‪getServerVersion(), $match);
131  $currentMysqlVersion = $match[1];
132  if (version_compare($currentMysqlVersion, $this->minimumMySQLVersion, '<')) {
133  $this->messageQueue->enqueue(new ‪FlashMessage(
134  'Your MySQL version ' . $currentMysqlVersion . ' is too old. TYPO3 CMS does not run'
135  . ' with this version. Update to at least MySQL ' . $this->minimumMySQLVersion,
136  'MySQL version too low',
138  ));
139  } else {
140  $this->messageQueue->enqueue(new ‪FlashMessage(
141  '',
142  'MySQL version is fine'
143  ));
144  }
145  }
146 
152  public function ‪checkDefaultDatabaseCharset(‪Connection $connection): void
153  {
154  $queryBuilder = $connection->‪createQueryBuilder();
155  $defaultDatabaseCharset = (string)$queryBuilder->select('DEFAULT_CHARACTER_SET_NAME')
156  ->from('information_schema.SCHEMATA')
157  ->where(
158  $queryBuilder->expr()->eq(
159  'SCHEMA_NAME',
160  $queryBuilder->createNamedParameter($connection->getDatabase(), Connection::PARAM_STR)
161  )
162  )
163  ->setMaxResults(1)
164  ->executeQuery()
165  ->fetchOne();
166 
167  if (!in_array($defaultDatabaseCharset, $this->databaseCharsetToCheck, true)) {
168  $this->messageQueue->enqueue(new ‪FlashMessage(
169  sprintf(
170  'Checking database character set failed, got key "%s" instead of "%s"',
171  $defaultDatabaseCharset,
172  implode(' or ', $this->databaseCharsetToCheck)
173  ),
174  'MySQL database character set check failed',
176  ));
177  } else {
178  $this->messageQueue->enqueue(new ‪FlashMessage(
179  '',
180  sprintf('MySQL database uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
181  ));
182  }
183  }
184 
191  protected function ‪getIncompatibleSqlModes(‪Connection $connection): array
192  {
193  $sqlModes = explode(',', (string)$connection->executeQuery('SELECT @@SESSION.sql_mode;')->fetchOne());
194  return array_intersect($this->incompatibleSqlModes, $sqlModes);
195  }
196 
202  public function ‪checkDefaultDatabaseServerCharset(‪Connection $connection): void
203  {
204  $defaultServerCharset = $connection->executeQuery('SHOW VARIABLES LIKE \'character_set_server\'')->fetchAssociative();
205 
206  if (!in_array($defaultServerCharset['Value'], $this->databaseServerCharsetToCheck, true)) {
207  $this->messageQueue->enqueue(new ‪FlashMessage(
208  sprintf(
209  'Checking server character set failed, got key "%s" instead of "%s"',
210  $defaultServerCharset['Value'],
211  implode(' or ', $this->databaseServerCharsetToCheck)
212  ),
213  'MySQL database server character set check failed',
215  ));
216  } else {
217  $this->messageQueue->enqueue(new ‪FlashMessage(
218  '',
219  sprintf('MySQL server default uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
220  ));
221  }
222  }
223 
230  public static function ‪isValidDatabaseName(string $databaseName): bool
231  {
232  return strlen($databaseName) <= static::SCHEMA_NAME_MAX_LENGTH && preg_match('/^[\x{0001}-\x{FFFF}]*$/u', $databaseName);
233  }
234 
235  protected function ‪checkDatabaseName(‪Connection $connection): void
236  {
237  if (static::isValidDatabaseName((string)$connection->getDatabase())) {
238  return;
239  }
240 
241  $this->messageQueue->enqueue(
242  new ‪FlashMessage(
243  'The given database name must not be longer than ' . static::SCHEMA_NAME_MAX_LENGTH . ' characters'
244  . ' and consist of the Unicode Basic Multilingual Plane (BMP), except U+0000',
245  'Database name not valid',
247  )
248  );
249  }
250 }
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\$incompatibleSqlModes
‪array $incompatibleSqlModes
Definition: MySql.php:49
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\AbstractPlatform
Definition: AbstractPlatform.php:28
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform
Definition: AbstractPlatform.php:18
‪TYPO3\CMS\Core\Database\Connection\getServerVersion
‪string getServerVersion()
Definition: Connection.php:383
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\isValidDatabaseName
‪static bool isValidDatabaseName(string $databaseName)
Definition: MySql.php:226
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\AbstractPlatform\$messageQueue
‪FlashMessageQueue $messageQueue
Definition: AbstractPlatform.php:31
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\checkDefaultDatabaseServerCharset
‪checkDefaultDatabaseServerCharset(Connection $connection)
Definition: MySql.php:198
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\checkDefaultDatabaseCharset
‪checkDefaultDatabaseCharset(Connection $connection)
Definition: MySql.php:148
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\getStatus
‪FlashMessageQueue getStatus()
Definition: MySql.php:78
‪TYPO3\CMS\Core\Messaging\AbstractMessage\INFO
‪const INFO
Definition: AbstractMessage.php:28
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:26
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\checkDatabaseName
‪checkDatabaseName(Connection $connection)
Definition: MySql.php:231
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\$databaseCharsetToCheck
‪array $databaseCharsetToCheck
Definition: MySql.php:56
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\checkInvalidSqlModes
‪checkInvalidSqlModes(Connection $connection)
Definition: MySql.php:98
‪TYPO3\CMS\Core\Database\Connection\createQueryBuilder
‪TYPO3 CMS Core Database Query QueryBuilder createQueryBuilder()
Definition: Connection.php:117
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\checkMysqlVersion
‪checkMysqlVersion(Connection $connection)
Definition: MySql.php:124
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:29
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql
Definition: MySql.php:38
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\$databaseServerCharsetToCheck
‪array $databaseServerCharsetToCheck
Definition: MySql.php:65
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\$minimumMySQLVersion
‪string $minimumMySQLVersion
Definition: MySql.php:43
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\getIncompatibleSqlModes
‪array getIncompatibleSqlModes(Connection $connection)
Definition: MySql.php:187