TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
DatabaseCharsetUpdate.php
Go to the documentation of this file.
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 use Doctrine\DBAL\DBALException;
20 
25 {
29  protected $title = 'Set default database charset to utf-8';
30 
39  public function checkForUpdate(&$description)
40  {
41  if ($this->isWizardDone() || !$this->isDefaultConnectionMySQL()) {
42  return false;
43  }
44 
45  $result = false;
46  $description = 'Sets the default database charset to utf-8 to'
47  . ' ensure new tables are created with correct charset.
48  WARNING: This will NOT convert any existing data.';
49 
50  // check if database charset is utf-8, also allows utf8mb4
51  if (strpos($this->getDefaultDatabaseCharset(), 'utf8') !== 0) {
52  $result = true;
53  } else {
54  $this->markWizardAsDone();
55  }
56 
57  return $result;
58  }
59 
69  public function performUpdate(array &$dbQueries, &$customMessages)
70  {
71  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
72  ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
73  $sql = 'ALTER DATABASE ' . $connection->quoteIdentifier($connection->getDatabase()) . ' CHARACTER SET utf8';
74 
75  try {
76  $connection->exec($sql);
77  } catch (DBALException $e) {
78  $customMessages = 'SQL-ERROR: ' . htmlspecialchars($e->getPrevious()->getMessage());
79  return false;
80  }
81  $dbQueries[] = $sql;
82  $this->markWizardAsDone();
83 
84  return true;
85  }
86 
94  protected function isDefaultConnectionMySQL(): bool
95  {
96  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
97  ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
98 
99  return strpos($connection->getServerVersion(), 'MySQL') === 0;
100  }
101 
109  protected function getDefaultDatabaseCharset(): string
110  {
111  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
112  ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
113  $queryBuilder = $connection->createQueryBuilder();
114  return (string)$queryBuilder->select('DEFAULT_CHARACTER_SET_NAME')
115  ->from('information_schema.SCHEMATA')
116  ->where(
117  $queryBuilder->expr()->eq(
118  'SCHEMA_NAME',
119  $queryBuilder->createNamedParameter($connection->getDatabase(), \PDO::PARAM_STR)
120  )
121  )
122  ->setMaxResults(1)
123  ->execute()
124  ->fetchColumn();
125  }
126 }
static makeInstance($className,...$constructorArguments)
performUpdate(array &$dbQueries, &$customMessages)