‪TYPO3CMS  11.5
WorkspaceNewPlaceholderRemovalMigration.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 
20 use Psr\Log\LoggerAwareInterface;
21 use Psr\Log\LoggerAwareTrait;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
26 
44 {
45  use LoggerAwareTrait;
46 
47  public function ‪getTitle(): string
48  {
49  return 'Scan for new versioned records of workspaces and migrate the placeholder and versioned records into one record.';
50  }
51 
56  public function ‪hasPotentialUpdateForTable(string $tableName): bool
57  {
58  return BackendUtility::isTableWorkspaceEnabled($tableName);
59  }
60 
61  public function ‪updateTableRow(string $tableName, array $row): array
62  {
63  $versionState = (int)($row['t3ver_state'] ?? 0);
64  if ($versionState === 1) {
65  $versionedRecord = $this->‪fetchVersionedRecord($tableName, (int)$row['uid']);
66  if ($versionedRecord === null) {
67  return $row;
68  }
69  foreach ($versionedRecord as $fieldName => $value) {
70  if (in_array($fieldName, ['uid', 'pid', 'deleted', 't3ver_state', 't3ver_oid'], true)) {
71  continue;
72  }
73  if ($this->‪isMMField($tableName, (string)$fieldName)) {
74  $this->‪transferMMValues($tableName, (string)$fieldName, (int)$versionedRecord['uid'], (int)$row['uid']);
75  continue;
76  }
77  $row[$fieldName] = $value;
78  }
79  } elseif ($versionState === -1) {
80  // Delete this row, as it has no use anymore.
81  // This is safe to do since the uid of the t3ver_state=1 record is always lower than the -1 one,
82  // so the record has been handled already. Rows are always sorted by uid in the row updater.
83  $row['deleted'] = 1;
84  }
85  return $row;
86  }
87 
95  protected function ‪fetchVersionedRecord(string $tableName, int $uid): ?array
96  {
97  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
98  $queryBuilder->getRestrictions()->removeAll();
99  $row = $queryBuilder
100  ->select('*')
101  ->from($tableName)
102  ->where(
103  $queryBuilder->expr()->eq('t3ver_state', $queryBuilder->createNamedParameter(-1, ‪Connection::PARAM_INT)),
104  $queryBuilder->expr()->eq('t3ver_oid', $queryBuilder->createNamedParameter($uid, ‪Connection::PARAM_INT))
105  )
106  ->executeQuery()
107  ->fetchAssociative();
108  return is_array($row) ? $row : null;
109  }
110 
111  protected function ‪isMMField(string $tableName, string $fieldName): bool
112  {
113  $fieldConfig = ‪$GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'] ?? null;
114  if (!is_array($fieldConfig)) {
115  return false;
116  }
117  if (isset($fieldConfig['MM'])) {
118  return true;
119  }
120  return false;
121  }
122 
133  protected function ‪transferMMValues(string $tableName, string $fieldName, int $originalUid, int $newUid): void
134  {
135  $fieldConfig = ‪$GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'] ?? null;
136 
137  $mmTable = $fieldConfig['MM'];
138  $matchMMFields = $fieldConfig['MM_match_fields'] ?? null;
139  $matchMMFieldsMultiple = $fieldConfig['MM_oppositeUsage'] ?? null;
140  $isOnRightSide = is_array($matchMMFieldsMultiple);
141  $relationFieldName = $isOnRightSide ? 'uid_local' : 'uid_foreign';
142  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($mmTable);
143  $queryBuilder->update($mmTable)
144  ->set($relationFieldName, $newUid, true, ‪Connection::PARAM_INT)
145  ->where(
146  $queryBuilder->expr()->eq($relationFieldName, $queryBuilder->createNamedParameter($originalUid, ‪Connection::PARAM_INT))
147  );
148  if ($matchMMFields) {
149  foreach ($matchMMFields as $matchMMFieldName => $matchMMFieldValue) {
150  $queryBuilder->andWhere(
151  $queryBuilder->expr()->eq($matchMMFieldName, $queryBuilder->createNamedParameter($matchMMFieldValue))
152  );
153  }
154  }
155  $queryBuilder->executeStatement();
156  }
157 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Install\Updates\RowUpdater\WorkspaceNewPlaceholderRemovalMigration\fetchVersionedRecord
‪array null fetchVersionedRecord(string $tableName, int $uid)
Definition: WorkspaceNewPlaceholderRemovalMigration.php:95
‪TYPO3\CMS\Install\Updates\RowUpdater\RowUpdaterInterface
Definition: RowUpdaterInterface.php:24
‪TYPO3\CMS\Install\Updates\RowUpdater\WorkspaceNewPlaceholderRemovalMigration\isMMField
‪isMMField(string $tableName, string $fieldName)
Definition: WorkspaceNewPlaceholderRemovalMigration.php:111
‪TYPO3\CMS\Install\Updates\RowUpdater\WorkspaceNewPlaceholderRemovalMigration
Definition: WorkspaceNewPlaceholderRemovalMigration.php:44
‪TYPO3\CMS\Install\Updates\RowUpdater
Definition: L18nDiffsourceToJsonMigration.php:5
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Install\Updates\RowUpdater\WorkspaceNewPlaceholderRemovalMigration\updateTableRow
‪updateTableRow(string $tableName, array $row)
Definition: WorkspaceNewPlaceholderRemovalMigration.php:61
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Install\Updates\RowUpdater\WorkspaceNewPlaceholderRemovalMigration\transferMMValues
‪transferMMValues(string $tableName, string $fieldName, int $originalUid, int $newUid)
Definition: WorkspaceNewPlaceholderRemovalMigration.php:133
‪TYPO3\CMS\Install\Updates\RowUpdater\WorkspaceNewPlaceholderRemovalMigration\getTitle
‪getTitle()
Definition: WorkspaceNewPlaceholderRemovalMigration.php:47
‪TYPO3\CMS\Install\Updates\RowUpdater\WorkspaceNewPlaceholderRemovalMigration\hasPotentialUpdateForTable
‪bool hasPotentialUpdateForTable(string $tableName)
Definition: WorkspaceNewPlaceholderRemovalMigration.php:56