‪TYPO3CMS  ‪main
RecordHistoryRollback.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\EventDispatcher\EventDispatcherInterface;
21 use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
24 use TYPO3\CMS\Backend\Utility\BackendUtility;
28 
29 #[Autoconfigure(public: true)]
31 {
35  protected ‪$eventDispatcher;
36 
37  public function ‪__construct(EventDispatcherInterface ‪$eventDispatcher)
38  {
39  $this->eventDispatcher = ‪$eventDispatcher;
40  }
41 
45  public function ‪performRollback(string $rollbackFields, array $diff, ?‪BackendUserAuthentication $backendUserAuthentication = null): void
46  {
47  $this->eventDispatcher->dispatch(new ‪BeforeHistoryRollbackStartEvent($rollbackFields, $diff, $this, $backendUserAuthentication));
48  $rollbackData = explode(':', $rollbackFields);
49  $rollbackDataCount = count($rollbackData);
50  // PROCESS INSERTS AND DELETES
51  // rewrite inserts and deletes
52  $commandMapArray = [];
53  $data = [];
54  if ($diff['insertsDeletes']) {
55  if ($rollbackDataCount === 1) {
56  // all tables
57  $data = $diff['insertsDeletes'];
58  } elseif ($rollbackDataCount === 2 && $diff['insertsDeletes'][$rollbackFields]) {
59  // one record
60  $data[$rollbackFields] = $diff['insertsDeletes'][$rollbackFields];
61  }
62  if (!empty($data)) {
63  foreach ($data as $key => $action) {
64  $elParts = explode(':', $key);
65  if ((int)$action === 1) {
66  // inserted records should be deleted
67  $commandMapArray[$elParts[0]][$elParts[1]]['delete'] = 1;
68  // When the record is deleted, the contents of the record do not need to be updated
69  unset(
70  $diff['oldData'][$key],
71  $diff['newData'][$key]
72  );
73  } elseif ((int)$action === -1) {
74  // deleted records should be inserted again
75  $commandMapArray[$elParts[0]][$elParts[1]]['undelete'] = 1;
76  }
77  }
78  }
79  }
80  // Writes the data:
81  if ($commandMapArray) {
82  $tce = GeneralUtility::makeInstance(DataHandler::class);
83  $tce->dontProcessTransformations = true;
84  $tce->start([], $commandMapArray, $backendUserAuthentication);
85  $tce->process_cmdmap();
86  unset($tce);
87  }
88  if (!$diff['insertsDeletes']) {
89  // PROCESS CHANGES
90  // create an array for process_datamap
91  $diffModified = [];
92  foreach ($diff['oldData'] as $key => $value) {
93  $splitKey = explode(':', $key);
94  $diffModified[$splitKey[0]][$splitKey[1]] = $value;
95  }
96  if ($rollbackDataCount === 1) {
97  // all tables
98  $data = $diffModified;
99  } elseif ($rollbackDataCount === 2) {
100  // one record
101  $data[$rollbackData[0]][$rollbackData[1]] = $diffModified[$rollbackData[0]][$rollbackData[1]];
102  } elseif ($rollbackDataCount === 3) {
103  // one field in one record
104  $data[$rollbackData[0]][$rollbackData[1]][$rollbackData[2]] = $diffModified[$rollbackData[0]][$rollbackData[1]][$rollbackData[2]];
105  }
106  // Writes the data:
107  $tce = GeneralUtility::makeInstance(DataHandler::class);
108  $tce->dontProcessTransformations = true;
109  $tce->start($data, [], $backendUserAuthentication);
110  $tce->process_datamap();
111  unset($tce);
112  }
113  if (isset($data['pages']) || isset($commandMapArray['pages'])) {
114  BackendUtility::setUpdateSignal('updatePageTree');
115  }
116  $this->eventDispatcher->dispatch(new AfterHistoryRollbackFinishedEvent($rollbackFields, $diff, $data, $this, $backendUserAuthentication));
117  }
118 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:104
‪TYPO3\CMS\Backend\History\RecordHistoryRollback\__construct
‪__construct(EventDispatcherInterface $eventDispatcher)
Definition: RecordHistoryRollback.php:36
‪TYPO3\CMS\Backend\History\RecordHistoryRollback
Definition: RecordHistoryRollback.php:31
‪TYPO3\CMS\Backend\History\RecordHistoryRollback\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: RecordHistoryRollback.php:34
‪TYPO3\CMS\Backend\History
‪TYPO3\CMS\Backend\History\Event\BeforeHistoryRollbackStartEvent
Definition: BeforeHistoryRollbackStartEvent.php:27
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\History\Event\AfterHistoryRollbackFinishedEvent
Definition: AfterHistoryRollbackFinishedEvent.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\History\RecordHistoryRollback\performRollback
‪performRollback(string $rollbackFields, array $diff, ?BackendUserAuthentication $backendUserAuthentication=null)
Definition: RecordHistoryRollback.php:44