‪TYPO3CMS  9.5
MigrateFeSessionDataUpdate.php
Go to the documentation of this file.
1 <?php
2 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 
18 use Doctrine\DBAL\DBALException;
21 
27 {
31  public function ‪getIdentifier(): string
32  {
33  return 'migrateFeSessionDataUpdate';
34  }
35 
39  public function ‪getTitle(): string
40  {
41  return 'Migrates existing fe_session_data into fe_sessions';
42  }
43 
47  public function ‪getDescription(): string
48  {
49  return 'With the new Session Framwework the session data is stored in fe_sessions.'
50  . ' To avoid that data is truncated, ensure the columns of fe_sessions have been updated.'
51  . ' This wizard migrates the existing data from fe_session_data into fe_sessions.'
52  . ' Existing entries in fe_sessions having an entry in fe_session_data are updated.'
53  . ' Entries in fe_session_data not found in fe_sessions are inserted with ses_anonymous = true';
54  }
55 
61  public function ‪updateNecessary(): bool
62  {
63  if (!$this->‪checkIfTableExists('fe_session_data')) {
64  return false;
65  }
66  // Check if there is data to migrate
67  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
68  ->getQueryBuilderForTable('fe_session_data');
69  $queryBuilder->getRestrictions()->removeAll();
70  $count = $queryBuilder->count('*')
71  ->from('fe_session_data')
72  ->execute()
73  ->fetchColumn(0);
74 
75  return $count > 0;
76  }
77 
81  public function ‪getPrerequisites(): array
82  {
83  return [
84  DatabaseUpdatedPrerequisite::class
85  ];
86  }
87 
93  public function ‪executeUpdate(): bool
94  {
95  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('fe_sessions');
96 
97  // Process records that have entries in fe_sessions and fe_session_data
98  $queryBuilder = $connection->createQueryBuilder();
99  $statement = $queryBuilder->select('fe_session_data.hash', 'fe_session_data.content')
100  ->from('fe_sessions')
101  ->join(
102  'fe_sessions',
103  'fe_session_data',
104  'fe_session_data',
105  $queryBuilder->expr()->eq(
106  'fe_sessions.ses_id',
107  $queryBuilder->quoteIdentifier('fe_session_data.hash')
108  )
109  )
110  ->execute();
111 
112  $updateQueryBuilder = $connection->createQueryBuilder();
113  $updateQueryBuilder->update('fe_sessions')
114  ->where(
115  $updateQueryBuilder->expr()->eq(
116  'ses_id',
117  $updateQueryBuilder->createPositionalParameter('', \PDO::PARAM_STR)
118  )
119  )
120  ->set('ses_data', $updateQueryBuilder->createPositionalParameter('', \PDO::PARAM_STR), false);
121  $updateStatement = $connection->prepare($updateQueryBuilder->getSQL());
122 
123  $connection->beginTransaction();
124  try {
125  while ($row = $statement->fetch()) {
126  $updateStatement->execute([$row['hash'], $row['content']]);
127  }
128  $connection->commit();
129  } catch (DBALException $e) {
130  $connection->rollBack();
131  throw $e;
132  }
133 
134  // Move records from fe_session_data that are not in fe_sessions
135  $queryBuilder = $connection->createQueryBuilder();
136  $selectSQL = $queryBuilder->select('fe_session_data.hash', 'fe_session_data.content', 'fe_session_data.tstamp')
137  ->addSelectLiteral('1')
138  ->from('fe_session_data')
139  ->leftJoin(
140  'fe_session_data',
141  'fe_sessions',
142  'fe_sessions',
143  $queryBuilder->expr()->eq(
144  'fe_session_data.hash',
145  $queryBuilder->quoteIdentifier('fe_sessions.ses_id')
146  )
147  )
148  ->where($queryBuilder->expr()->isNull('fe_sessions.ses_id'))
149  ->getSQL();
150 
151  $insertSQL = sprintf(
152  'INSERT INTO %s(%s, %s, %s, %s) %s',
153  $connection->quoteIdentifier('fe_sessions'),
154  $connection->quoteIdentifier('ses_id'),
155  $connection->quoteIdentifier('ses_data'),
156  $connection->quoteIdentifier('ses_tstamp'),
157  $connection->quoteIdentifier('ses_anonymous'),
158  $selectSQL
159  );
160 
161  try {
162  $connection->beginTransaction();
163  $connection->exec($insertSQL);
164  $connection->commit();
165  } catch (DBALException $e) {
166  $connection->rollBack();
167  throw $e;
168  }
169 
170  return true;
171  }
172 
179  protected function ‪checkIfTableExists($table): bool
180  {
181  $tableExists = GeneralUtility::makeInstance(ConnectionPool::class)
182  ->getConnectionForTable($table)
183  ->getSchemaManager()
184  ->tablesExist([$table]);
185 
186  return $tableExists;
187  }
188 }
‪TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate\getDescription
‪string getDescription()
Definition: MigrateFeSessionDataUpdate.php:47
‪TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate\executeUpdate
‪bool executeUpdate()
Definition: MigrateFeSessionDataUpdate.php:93
‪TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate\getIdentifier
‪string getIdentifier()
Definition: MigrateFeSessionDataUpdate.php:31
‪TYPO3\CMS\Install\Updates
Definition: AbstractDownloadExtensionUpdate.php:3
‪TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate\getPrerequisites
‪string[] getPrerequisites()
Definition: MigrateFeSessionDataUpdate.php:81
‪TYPO3\CMS\Install\Updates\UpgradeWizardInterface
Definition: UpgradeWizardInterface.php:22
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate
Definition: MigrateFeSessionDataUpdate.php:27
‪TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate\updateNecessary
‪bool updateNecessary()
Definition: MigrateFeSessionDataUpdate.php:61
‪TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate\getTitle
‪string getTitle()
Definition: MigrateFeSessionDataUpdate.php:39
‪TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate\checkIfTableExists
‪bool checkIfTableExists($table)
Definition: MigrateFeSessionDataUpdate.php:179