TYPO3 CMS  TYPO3_8-7
MySqlCheck.php
Go to the documentation of this file.
1 <?php
2 
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 
24 
34 class MySqlCheck implements CheckInterface
35 {
41  protected $minimumMySQLVersion = '5.5.0';
42 
48  protected $incompatibleSqlModes = [
49  'NO_BACKSLASH_ESCAPES'
50  ];
51 
59  public function getStatus(): array
60  {
61  $statusArray = [];
62  $defaultConnection = GeneralUtility::makeInstance(ConnectionPool::class)
63  ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
64  if (strpos($defaultConnection->getServerVersion(), 'MySQL') !== 0) {
65  return $statusArray;
66  }
67  $statusArray[] = $this->checkMysqlVersion($defaultConnection);
68  $statusArray[] = $this->checkInvalidSqlModes($defaultConnection);
69  $statusArray[] = $this->checkMysqlDatabaseUtf8Status($defaultConnection);
70  return $statusArray;
71  }
72 
79  protected function checkInvalidSqlModes($connection)
80  {
81  $detectedIncompatibleSqlModes = $this->getIncompatibleSqlModes($connection);
82  if (!empty($detectedIncompatibleSqlModes)) {
83  $status = new Status\ErrorStatus();
84  $status->setTitle('Incompatible SQL modes found!');
85  $status->setMessage(
86  'Incompatible SQL modes have been detected:' .
87  ' ' . implode(', ', $detectedIncompatibleSqlModes) . '.' .
88  ' The listed modes are not compatible with TYPO3 CMS.' .
89  ' You have to change that setting in your MySQL environment' .
90  ' or in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'setDBinit\']'
91  );
92  } else {
93  $status = new Status\OkStatus();
94  $status->setTitle('No incompatible SQL modes found.');
95  }
96 
97  return $status;
98  }
99 
106  protected function checkMysqlVersion($connection)
107  {
108  preg_match('/MySQL ((\d+\.)*(\d+\.)*\d+)/', $connection->getServerVersion(), $match);
109  $currentMysqlVersion = $match[1];
110  if (version_compare($currentMysqlVersion, $this->minimumMySQLVersion, '<')) {
111  $status = new Status\ErrorStatus();
112  $status->setTitle('MySQL version too low');
113  $status->setMessage(
114  'Your MySQL version ' . $currentMysqlVersion . ' is too old. TYPO3 CMS does not run' .
115  ' with this version. Update to at least MySQL ' . $this->minimumMySQLVersion
116  );
117  } else {
118  $status = new Status\OkStatus();
119  $status->setTitle('MySQL version is fine');
120  }
121 
122  return $status;
123  }
124 
131  protected function checkMysqlDatabaseUtf8Status(Connection $connection)
132  {
134  $queryBuilder = $connection->createQueryBuilder();
135  $defaultDatabaseCharset = (string)$queryBuilder->select('DEFAULT_CHARACTER_SET_NAME')
136  ->from('information_schema.SCHEMATA')
137  ->where(
138  $queryBuilder->expr()->eq(
139  'SCHEMA_NAME',
140  $queryBuilder->createNamedParameter($connection->getDatabase(), \PDO::PARAM_STR)
141  )
142  )
143  ->setMaxResults(1)
144  ->execute()
145  ->fetchColumn();
146  // also allow utf8mb4
147  if (strpos($defaultDatabaseCharset, 'utf8') !== 0) {
148  $status = new Status\ErrorStatus();
149  $status->setTitle('MySQL database character set check failed');
150  $status->setMessage(
151  'Checking database character set failed, got key "'
152  . $defaultDatabaseCharset . '" instead of "utf8" or "utf8mb4"'
153  );
154  } else {
155  $status = new Status\OkStatus();
156  $status->setTitle('Your database uses utf-8. All good.');
157  }
158  return $status;
159  }
160 
167  protected function getIncompatibleSqlModes($connection)
168  {
169  $sqlModes = explode(',', $connection->executeQuery('SELECT @@SESSION.sql_mode;')
170  ->fetch(0)['@@SESSION.sql_mode']);
171  return array_intersect($this->incompatibleSqlModes, $sqlModes);
172  }
173 }
static makeInstance($className,... $constructorArguments)