TYPO3 CMS  TYPO3_8-7
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 
21 
26 {
30  protected $title = 'Migrates existing fe_session_data into fe_sessions';
31 
39  public function checkForUpdate(&$description)
40  {
41  if ($this->isWizardDone()) {
42  return false;
43  }
44 
45  if (!$this->checkIfTableExists('fe_session_data')) {
46  return false;
47  }
48 
49  $description = 'With the new Session Framwework the session data is stored in fe_sessions.</p>'
50  . ' <b>To avoid that data is truncated, ensure the columns of fe_sessions have been updated.</b></p>'
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  // Check if there is data to migrate
56  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
57  ->getQueryBuilderForTable('fe_session_data');
58  $queryBuilder->getRestrictions()->removeAll();
59  $count = $queryBuilder->count('*')
60  ->from('fe_session_data')
61  ->execute()
62  ->fetchColumn(0);
63 
64  return $count > 0;
65  }
66 
75  public function performUpdate(array &$databaseQueries, &$customMessage)
76  {
77  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('fe_sessions');
78 
79  // Process records that have entries in fe_sessions and fe_session_data
80  $queryBuilder = $connection->createQueryBuilder();
81  $statement = $queryBuilder->select('fe_session_data.hash', 'fe_session_data.content')
82  ->from('fe_sessions')
83  ->join(
84  'fe_sessions',
85  'fe_session_data',
86  'fe_session_data',
87  $queryBuilder->expr()->eq(
88  'fe_sessions.ses_id',
89  $queryBuilder->quoteIdentifier('fe_session_data.hash')
90  )
91  )
92  ->execute();
93  $databaseQueries[] = $queryBuilder->getSQL();
94 
95  $updateQueryBuilder = $connection->createQueryBuilder();
96  $updateQueryBuilder->update('fe_sessions')
97  ->where(
98  $updateQueryBuilder->expr()->eq(
99  'ses_id',
100  $updateQueryBuilder->createPositionalParameter('', \PDO::PARAM_STR)
101  )
102  )
103  ->set('ses_data', $updateQueryBuilder->createPositionalParameter('', \PDO::PARAM_STR), false);
104  $databaseQueries[] = $updateQueryBuilder->getSQL();
105  $updateStatement = $connection->prepare($updateQueryBuilder->getSQL());
106 
107  $connection->beginTransaction();
108  try {
109  while ($row = $statement->fetch()) {
110  $updateStatement->execute([$row['hash'], $row['content']]);
111  }
112  $connection->commit();
113  } catch (DBALException $e) {
114  $connection->rollBack();
115  throw $e;
116  }
117 
118  // Move records from fe_session_data that are not in fe_sessions
119  $queryBuilder = $connection->createQueryBuilder();
120  $selectSQL = $queryBuilder->select('fe_session_data.hash', 'fe_session_data.content', 'fe_session_data.tstamp')
121  ->addSelectLiteral('1')
122  ->from('fe_session_data')
123  ->leftJoin(
124  'fe_session_data',
125  'fe_sessions',
126  'fe_sessions',
127  $queryBuilder->expr()->eq(
128  'fe_session_data.hash',
129  $queryBuilder->quoteIdentifier('fe_sessions.ses_id')
130  )
131  )
132  ->where($queryBuilder->expr()->isNull('fe_sessions.ses_id'))
133  ->getSQL();
134 
135  $insertSQL = sprintf(
136  'INSERT INTO %s(%s, %s, %s, %s) %s',
137  $connection->quoteIdentifier('fe_sessions'),
138  $connection->quoteIdentifier('ses_id'),
139  $connection->quoteIdentifier('ses_data'),
140  $connection->quoteIdentifier('ses_tstamp'),
141  $connection->quoteIdentifier('ses_anonymous'),
142  $selectSQL
143  );
144  $databaseQueries[] = $insertSQL;
145 
146  try {
147  $connection->beginTransaction();
148  $connection->exec($insertSQL);
149  $connection->commit();
150  } catch (DBALException $e) {
151  $connection->rollBack();
152  throw $e;
153  }
154 
155  $this->markWizardAsDone();
156  return true;
157  }
158 }
performUpdate(array &$databaseQueries, &$customMessage)
static makeInstance($className,... $constructorArguments)