[!!!][BUGFIX] Separate sys_history from sys_log db entries
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Service / HistoryService.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Service;
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 TYPO3\CMS\Backend\Backend\Avatar\Avatar;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Service for history
23 */
24 class HistoryService implements \TYPO3\CMS\Core\SingletonInterface
25 {
26 /**
27 * @var array
28 */
29 protected $backendUserNames;
30
31 /**
32 * @var array
33 */
34 protected $historyEntries = [];
35
36 /**
37 * @var \TYPO3\CMS\Core\Utility\DiffUtility
38 */
39 protected $differencesObject;
40
41 /**
42 * Creates this object.
43 */
44 public function __construct()
45 {
46 $this->backendUserNames = BackendUtility::getUserNames();
47 }
48
49 /**
50 * Gets the editing history of a record.
51 *
52 * @param string $table Name of the table
53 * @param int $id Uid of the record
54 * @return array Record history entries
55 */
56 public function getHistory($table, $id)
57 {
58 $history = [];
59 $i = 0;
60 foreach ((array)$this->getHistoryEntries($table, $id) as $entry) {
61 if ($i++ > 20) {
62 break;
63 }
64 $history[] = $this->getHistoryEntry($entry);
65 }
66 return $history;
67 }
68
69 /**
70 * Gets the human readable representation of one
71 * record history entry.
72 *
73 * @param array $entry Record history entry
74 * @return array
75 * @see getHistory
76 */
77 protected function getHistoryEntry(array $entry)
78 {
79 if (!empty($entry['action'])) {
80 $differences = $entry['action'];
81 } else {
82 $differences = $this->getDifferences($entry);
83 }
84
85 /** @var Avatar $avatar */
86 $avatar = GeneralUtility::makeInstance(Avatar::class);
87 $beUserRecord = BackendUtility::getRecord('be_users', $entry['userid']);
88
89 return [
90 'datetime' => htmlspecialchars(BackendUtility::datetime($entry['tstamp'])),
91 'user' => htmlspecialchars($this->getUserName($entry['userid'])),
92 'user_avatar' => $avatar->render($beUserRecord),
93 'differences' => $differences
94 ];
95 }
96
97 /**
98 * Gets the differences between two record versions out
99 * of one record history entry.
100 *
101 * @param array $entry Record history entry
102 * @return array
103 */
104 protected function getDifferences(array $entry)
105 {
106 $differences = [];
107 $tableName = $entry['tablename'];
108 if (is_array($entry['newRecord'])) {
109 $fields = array_keys($entry['newRecord']);
110 foreach ($fields as $field) {
111 if (!empty($GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type']) && $GLOBALS['TCA'][$tableName]['columns'][$field]['config']['type'] !== 'passthrough') {
112 // Create diff-result:
113 $fieldDifferences = $this->getDifferencesObject()->makeDiffDisplay(
114 BackendUtility::getProcessedValue($tableName, $field, $entry['oldRecord'][$field], 0, true),
115 BackendUtility::getProcessedValue($tableName, $field, $entry['newRecord'][$field], 0, true)
116 );
117 if (!empty($fieldDifferences)) {
118 $differences[] = [
119 'label' => $this->getLanguageService()->sL((string)BackendUtility::getItemLabel($tableName, $field)),
120 'html' => nl2br(trim($fieldDifferences)),
121 ];
122 }
123 }
124 }
125 }
126 return $differences;
127 }
128
129 /**
130 * Gets the username of a backend user.
131 *
132 * @param string $user
133 * @return string
134 */
135 protected function getUserName($user)
136 {
137 $userName = 'unknown';
138 if (!empty($this->backendUserNames[$user]['username'])) {
139 $userName = $this->backendUserNames[$user]['username'];
140 }
141 return $userName;
142 }
143
144 /**
145 * Gets an instance of the record history of a record.
146 *
147 * @param string $table Name of the table
148 * @param int $id Uid of the record
149 * @return array
150 */
151 protected function getHistoryEntries($table, $id)
152 {
153 if (!isset($this->historyEntries[$table][$id])) {
154 /** @var $historyObject \TYPO3\CMS\Backend\History\RecordHistory */
155 $historyObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\History\RecordHistory::class);
156 $this->historyEntries[$table][$id] = $historyObject->getHistoryDataForRecord($table, $id);
157 }
158 return $this->historyEntries[$table][$id];
159 }
160
161 /**
162 * Gets an instance of the record differences utility.
163 *
164 * @return \TYPO3\CMS\Core\Utility\DiffUtility
165 */
166 protected function getDifferencesObject()
167 {
168 if (!isset($this->differencesObject)) {
169 $this->differencesObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Utility\DiffUtility::class);
170 $this->differencesObject->stripTags = false;
171 }
172 return $this->differencesObject;
173 }
174
175 /**
176 * @return \TYPO3\CMS\Core\Localization\LanguageService
177 */
178 protected function getLanguageService()
179 {
180 return $GLOBALS['LANG'];
181 }
182 }