‪TYPO3CMS  10.4
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  'utf8mb4',
62  ];
63 
69  'utf8',
70  'utf8mb4',
71  ];
72 
80  public function ‪getStatus(): ‪FlashMessageQueue
81  {
82  $defaultConnection = GeneralUtility::makeInstance(ConnectionPool::class)
83  ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
84  if (strpos($defaultConnection->getServerVersion(), 'MySQL') !== 0) {
86  }
87  $this->‪checkMysqlVersion($defaultConnection);
88  $this->‪checkInvalidSqlModes($defaultConnection);
89  $this->‪checkDefaultDatabaseCharset($defaultConnection);
90  $this->‪checkDefaultDatabaseServerCharset($defaultConnection);
91  $this->‪checkDatabaseName($defaultConnection);
93  }
94 
100  protected function ‪checkInvalidSqlModes(‪Connection $connection)
101  {
102  $detectedIncompatibleSqlModes = $this->‪getIncompatibleSqlModes($connection);
103  if (!empty($detectedIncompatibleSqlModes)) {
104  $this->messageQueue->enqueue(new ‪FlashMessage(
105  'Incompatible SQL modes have been detected:'
106  . ' ' . implode(', ', $detectedIncompatibleSqlModes) . '.'
107  . ' The listed modes are not compatible with TYPO3 CMS.'
108  . ' You have to change that setting in your MySQL environment'
109  . ' or in $GLOBALS[\'TYPO3_CONF_VARS\'][\'DB\'][\'Connections\'][\'Default\'][\'initCommands\']',
110  'Incompatible SQL modes found!',
112  ));
113  } else {
114  $this->messageQueue->enqueue(new ‪FlashMessage(
115  '',
116  'No incompatible SQL modes found.'
117  ));
118  }
119  }
120 
126  protected function ‪checkMysqlVersion(‪Connection $connection)
127  {
128  preg_match('/MySQL ((\d+\.)*(\d+\.)*\d+)/', $connection->‪getServerVersion(), $match);
129  $currentMysqlVersion = $match[1];
130  if (version_compare($currentMysqlVersion, $this->minimumMySQLVersion, '<')) {
131  $this->messageQueue->enqueue(new ‪FlashMessage(
132  'Your MySQL version ' . $currentMysqlVersion . ' is too old. TYPO3 CMS does not run'
133  . ' with this version. Update to at least MySQL ' . $this->minimumMySQLVersion,
134  'MySQL version too low',
136  ));
137  } else {
138  $this->messageQueue->enqueue(new ‪FlashMessage(
139  '',
140  'MySQL version is fine'
141  ));
142  }
143  }
144 
150  public function ‪checkDefaultDatabaseCharset(‪Connection $connection): void
151  {
152  $queryBuilder = $connection->‪createQueryBuilder();
153  $defaultDatabaseCharset = (string)$queryBuilder->‪select('DEFAULT_CHARACTER_SET_NAME')
154  ->‪from('information_schema.SCHEMATA')
155  ->‪where(
156  $queryBuilder->expr()->eq(
157  'SCHEMA_NAME',
158  $queryBuilder->createNamedParameter($connection->getDatabase(), \PDO::PARAM_STR)
159  )
160  )
161  ->setMaxResults(1)
162  ->‪execute()
163  ->fetchColumn();
164 
165  if (!in_array($defaultDatabaseCharset, $this->databaseCharsetToCheck, true)) {
166  $this->messageQueue->enqueue(new ‪FlashMessage(
167  sprintf(
168  'Checking database character set failed, got key "%s" instead of "%s"',
169  $defaultDatabaseCharset,
170  implode(' or ', $this->databaseCharsetToCheck)
171  ),
172  'MySQL database character set check failed',
174  ));
175  } else {
176  $this->messageQueue->enqueue(new ‪FlashMessage(
177  '',
178  sprintf('MySQL database uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
179  ));
180  }
181  }
182 
189  protected function ‪getIncompatibleSqlModes(‪Connection $connection): array
190  {
191  $sqlModes = explode(',', $connection->executeQuery('SELECT @@SESSION.sql_mode;')
192  ->fetch(0)['@@SESSION.sql_mode']);
193  return array_intersect($this->incompatibleSqlModes, $sqlModes);
194  }
195 
201  public function ‪checkDefaultDatabaseServerCharset(‪Connection $connection): void
202  {
203  $defaultServerCharset = $connection->executeQuery('SHOW VARIABLES LIKE \'character_set_server\'')->fetch();
204 
205  if (!in_array($defaultServerCharset['Value'], $this->databaseServerCharsetToCheck, true)) {
206  $this->messageQueue->enqueue(new ‪FlashMessage(
207  sprintf(
208  'Checking server character set failed, got key "%s" instead of "%s"',
209  $defaultServerCharset['Value'],
210  implode(' or ', $this->databaseServerCharsetToCheck)
211  ),
212  'MySQL database server character set check failed',
214  ));
215  } else {
216  $this->messageQueue->enqueue(new ‪FlashMessage(
217  '',
218  sprintf('MySQL server default uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
219  ));
220  }
221  }
222 
229  public static function ‪isValidDatabaseName(string $databaseName): bool
230  {
231  return strlen($databaseName) <= static::SCHEMA_NAME_MAX_LENGTH && preg_match('/^[\x{0001}-\x{FFFF}]*$/u', $databaseName);
232  }
233 
234  protected function ‪checkDatabaseName(‪Connection $connection): void
235  {
236  if (static::isValidDatabaseName($connection->getDatabase())) {
237  return;
238  }
239 
240  $this->messageQueue->enqueue(
241  new ‪FlashMessage(
242  'The given database name must not be longer than ' . static::SCHEMA_NAME_MAX_LENGTH . ' characters'
243  . ' and consist of the Unicode Basic Multilingual Plane (BMP), except U+0000',
244  'Database name not valid',
246  )
247  );
248  }
249 }
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\select
‪QueryBuilder select(string ... $selects)
Definition: QueryBuilder.php:416
‪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\Query\QueryBuilder\from
‪QueryBuilder from(string $from, string $alias=null)
Definition: QueryBuilder.php:531
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\execute
‪Statement Doctrine DBAL ForwardCompatibility Result Doctrine DBAL Driver ResultStatement int execute()
Definition: QueryBuilder.php:204
‪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:225
‪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:197
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\checkDefaultDatabaseCharset
‪checkDefaultDatabaseCharset(Connection $connection)
Definition: MySql.php:146
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\getStatus
‪FlashMessageQueue getStatus()
Definition: MySql.php:76
‪TYPO3\CMS\Core\Messaging\AbstractMessage\INFO
‪const INFO
Definition: AbstractMessage.php:28
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\checkDatabaseName
‪checkDatabaseName(Connection $connection)
Definition: MySql.php:230
‪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:96
‪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:46
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\MySql\checkMysqlVersion
‪checkMysqlVersion(Connection $connection)
Definition: MySql.php:122
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:29
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\where
‪QueryBuilder where(... $predicates)
Definition: QueryBuilder.php:677
‪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:64
‪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:185