2 declare(strict_types
= 1);
3 namespace TYPO3\CMS\Core\History
;
6 * This file is part of the TYPO3 CMS project.
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.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
18 use TYPO3\CMS\Core\Database\Connection
;
19 use TYPO3\CMS\Core\Database\ConnectionPool
;
20 use TYPO3\CMS\Core\Utility\GeneralUtility
;
23 * Used to save any history to a record
25 * @internal should only be used by the TYPO3 Core
27 class RecordHistoryStore
30 const ACTION_MODIFY
= 2;
31 const ACTION_MOVE
= 3;
32 const ACTION_DELETE
= 4;
33 const ACTION_UNDELETE
= 5;
35 const USER_BACKEND
= 'BE';
36 const USER_FRONTEND
= 'FE';
37 const USER_ANONYMOUS
= '';
44 protected $originalUserId;
46 protected $workspaceId;
49 * @param int|null $userId
50 * @param string $userType
51 * @param int $originalUserId
53 * @param int $workspaceId
55 public function __construct(string $userType = self
::USER_BACKEND
, int $userId = null, int $originalUserId = null, int $tstamp = null, int $workspaceId = 0)
57 $this->userId
= $userId;
58 $this->userType
= $userType;
59 $this->originalUserId
= $originalUserId;
60 $this->tstamp
= $tstamp ?
: $GLOBALS['EXEC_TIME'];
61 $this->workspaceId
= $workspaceId;
65 * @param string $table
67 * @param array $payload
70 public function addRecord(string $table, int $uid, array $payload): string
73 'actiontype' => self
::ACTION_ADD
,
74 'usertype' => $this->userType
,
75 'userid' => $this->userId
,
76 'originaluserid' => $this->originalUserId
,
77 'tablename' => $table,
79 'tstamp' => $this->tstamp
,
80 'history_data' => json_encode($payload),
81 'workspace' => $this->workspaceId
,
83 $this->getDatabaseConnection()->insert('sys_history', $data);
84 return $this->getDatabaseConnection()->lastInsertId('sys_history');
88 * @param string $table
90 * @param array $payload
93 public function modifyRecord(string $table, int $uid, array $payload): string
96 'actiontype' => self
::ACTION_MODIFY
,
97 'usertype' => $this->userType
,
98 'userid' => $this->userId
,
99 'originaluserid' => $this->originalUserId
,
100 'tablename' => $table,
102 'tstamp' => $this->tstamp
,
103 'history_data' => json_encode($payload),
104 'workspace' => $this->workspaceId
,
106 $this->getDatabaseConnection()->insert('sys_history', $data);
107 return $this->getDatabaseConnection()->lastInsertId('sys_history');
111 * @param string $table
115 public function deleteRecord(string $table, int $uid): string
118 'actiontype' => self
::ACTION_DELETE
,
119 'usertype' => $this->userType
,
120 'userid' => $this->userId
,
121 'originaluserid' => $this->originalUserId
,
122 'tablename' => $table,
124 'tstamp' => $this->tstamp
,
125 'workspace' => $this->workspaceId
,
127 $this->getDatabaseConnection()->insert('sys_history', $data);
128 return $this->getDatabaseConnection()->lastInsertId('sys_history');
132 * @param string $table
136 public function undeleteRecord(string $table, int $uid): string
139 'actiontype' => self
::ACTION_UNDELETE
,
140 'usertype' => $this->userType
,
141 'userid' => $this->userId
,
142 'originaluserid' => $this->originalUserId
,
143 'tablename' => $table,
145 'tstamp' => $this->tstamp
,
146 'workspace' => $this->workspaceId
,
148 $this->getDatabaseConnection()->insert('sys_history', $data);
149 return $this->getDatabaseConnection()->lastInsertId('sys_history');
153 * @param string $table
155 * @param array $payload
158 public function moveRecord(string $table, int $uid, array $payload): string
161 'actiontype' => self
::ACTION_MOVE
,
162 'usertype' => $this->userType
,
163 'userid' => $this->userId
,
164 'originaluserid' => $this->originalUserId
,
165 'tablename' => $table,
167 'tstamp' => $this->tstamp
,
168 'history_data' => json_encode($payload),
169 'workspace' => $this->workspaceId
,
171 $this->getDatabaseConnection()->insert('sys_history', $data);
172 return $this->getDatabaseConnection()->lastInsertId('sys_history');
178 protected function getDatabaseConnection(): Connection
180 return GeneralUtility
::makeInstance(ConnectionPool
::class)
181 ->getConnectionForTable('sys_history');