‪TYPO3CMS  10.4
RecordHistoryStore.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 
24 
31 {
32  public const ‪ACTION_ADD = 1;
33  public const ‪ACTION_MODIFY = 2;
34  public const ‪ACTION_MOVE = 3;
35  public const ‪ACTION_DELETE = 4;
36  public const ‪ACTION_UNDELETE = 5;
37 
38  public const ‪USER_BACKEND = 'BE';
39  public const ‪USER_FRONTEND = 'FE';
40  public const ‪USER_ANONYMOUS = '';
41 
45  protected ‪$userId;
46 
50  protected ‪$userType;
51 
55  protected ‪$originalUserId;
56 
60  protected ‪$tstamp;
61 
65  protected ‪$workspaceId;
66 
74  public function ‪__construct(string ‪$userType = self::USER_BACKEND, int ‪$userId = null, int ‪$originalUserId = null, int ‪$tstamp = null, int ‪$workspaceId = 0)
75  {
76  $this->userType = ‪$userType;
77  $this->userId = ‪$userId;
78  $this->originalUserId = ‪$originalUserId;
79  $this->tstamp = ‪$tstamp ?: ‪$GLOBALS['EXEC_TIME'];
80  $this->workspaceId = ‪$workspaceId;
81  }
82 
90  public function ‪addRecord(string $table, int $uid, array $payload, ‪CorrelationId $correlationId = null): string
91  {
92  $data = [
93  'actiontype' => ‪self::ACTION_ADD,
94  'usertype' => ‪$this->userType,
95  'userid' => ‪$this->userId,
96  'originaluserid' => ‪$this->originalUserId,
97  'tablename' => $table,
98  'recuid' => $uid,
99  'tstamp' => ‪$this->tstamp,
100  'history_data' => json_encode($payload),
101  'workspace' => ‪$this->workspaceId,
102  'correlation_id' => (string)$this->‪createCorrelationId($table, $uid, $correlationId),
103  ];
104  $this->‪getDatabaseConnection()->‪insert('sys_history', $data);
105  return $this->‪getDatabaseConnection()->‪lastInsertId('sys_history');
106  }
107 
115  public function ‪modifyRecord(string $table, int $uid, array $payload, ‪CorrelationId $correlationId = null): string
116  {
117  $data = [
118  'actiontype' => ‪self::ACTION_MODIFY,
119  'usertype' => ‪$this->userType,
120  'userid' => ‪$this->userId,
121  'originaluserid' => ‪$this->originalUserId,
122  'tablename' => $table,
123  'recuid' => $uid,
124  'tstamp' => ‪$this->tstamp,
125  'history_data' => json_encode($payload),
126  'workspace' => ‪$this->workspaceId,
127  'correlation_id' => (string)$this->‪createCorrelationId($table, $uid, $correlationId),
128  ];
129  $this->‪getDatabaseConnection()->‪insert('sys_history', $data);
130  return $this->‪getDatabaseConnection()->‪lastInsertId('sys_history');
131  }
132 
139  public function ‪deleteRecord(string $table, int $uid, CorrelationId $correlationId = null): string
140  {
141  $data = [
142  'actiontype' => ‪self::ACTION_DELETE,
143  'usertype' => ‪$this->userType,
144  'userid' => ‪$this->userId,
145  'originaluserid' => ‪$this->originalUserId,
146  'tablename' => $table,
147  'recuid' => $uid,
148  'tstamp' => ‪$this->tstamp,
149  'workspace' => ‪$this->workspaceId,
150  'correlation_id' => (string)$this->‪createCorrelationId($table, $uid, $correlationId),
151  ];
152  $this->‪getDatabaseConnection()->‪insert('sys_history', $data);
153  return $this->‪getDatabaseConnection()->‪lastInsertId('sys_history');
154  }
155 
162  public function ‪undeleteRecord(string $table, int $uid, CorrelationId $correlationId = null): string
163  {
164  $data = [
165  'actiontype' => ‪self::ACTION_UNDELETE,
166  'usertype' => ‪$this->userType,
167  'userid' => ‪$this->userId,
168  'originaluserid' => ‪$this->originalUserId,
169  'tablename' => $table,
170  'recuid' => $uid,
171  'tstamp' => ‪$this->tstamp,
172  'workspace' => ‪$this->workspaceId,
173  'correlation_id' => (string)$this->‪createCorrelationId($table, $uid, $correlationId),
174  ];
175  $this->‪getDatabaseConnection()->‪insert('sys_history', $data);
176  return $this->‪getDatabaseConnection()->‪lastInsertId('sys_history');
177  }
178 
186  public function ‪moveRecord(string $table, int $uid, array $payload, CorrelationId $correlationId = null): string
187  {
188  $data = [
189  'actiontype' => ‪self::ACTION_MOVE,
190  'usertype' => ‪$this->userType,
191  'userid' => ‪$this->userId,
192  'originaluserid' => ‪$this->originalUserId,
193  'tablename' => $table,
194  'recuid' => $uid,
195  'tstamp' => ‪$this->tstamp,
196  'history_data' => json_encode($payload),
197  'workspace' => ‪$this->workspaceId,
198  'correlation_id' => (string)$this->‪createCorrelationId($table, $uid, $correlationId),
199  ];
200  $this->‪getDatabaseConnection()->‪insert('sys_history', $data);
201  return $this->‪getDatabaseConnection()->‪lastInsertId('sys_history');
202  }
203 
204  protected function ‪createCorrelationId(string $tableName, int $uid, ?‪CorrelationId $correlationId): ‪CorrelationId
205  {
206  if ($correlationId !== null && $correlationId->‪getSubject() !== null) {
207  return $correlationId;
208  }
209  $subject = md5($tableName . ':' . $uid);
210  return $correlationId !== null ? $correlationId->‪withSubject($subject) : ‪CorrelationId::forSubject($subject);
211  }
212 
216  protected function ‪getDatabaseConnection(): ‪Connection
217  {
218  return GeneralUtility::makeInstance(ConnectionPool::class)
219  ->getConnectionForTable('sys_history');
220  }
221 }
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\modifyRecord
‪string modifyRecord(string $table, int $uid, array $payload, CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:110
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\$workspaceId
‪int $workspaceId
Definition: RecordHistoryStore.php:60
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\USER_BACKEND
‪const USER_BACKEND
Definition: RecordHistoryStore.php:38
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\getSubject
‪string null getSubject()
Definition: CorrelationId.php:144
‪TYPO3\CMS\Core\Database\Connection\lastInsertId
‪string lastInsertId($tableName=null, string $fieldName='uid')
Definition: Connection.php:452
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\$userId
‪int null $userId
Definition: RecordHistoryStore.php:44
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\createCorrelationId
‪createCorrelationId(string $tableName, int $uid, ?CorrelationId $correlationId)
Definition: RecordHistoryStore.php:199
‪TYPO3\CMS\Core\DataHandling\History
Definition: RecordHistoryStore.php:18
‪TYPO3\CMS\Core\Database\Connection\insert
‪int insert($tableName, array $data, array $types=[])
Definition: Connection.php:211
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\$userType
‪string $userType
Definition: RecordHistoryStore.php:48
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\ACTION_MODIFY
‪const ACTION_MODIFY
Definition: RecordHistoryStore.php:33
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\getDatabaseConnection
‪Connection getDatabaseConnection()
Definition: RecordHistoryStore.php:211
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore
Definition: RecordHistoryStore.php:31
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\addRecord
‪string addRecord(string $table, int $uid, array $payload, CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:85
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\$originalUserId
‪int null $originalUserId
Definition: RecordHistoryStore.php:52
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId
Definition: CorrelationId.php:29
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\withSubject
‪withSubject(string $subject)
Definition: CorrelationId.php:113
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\undeleteRecord
‪string undeleteRecord(string $table, int $uid, CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:157
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\deleteRecord
‪string deleteRecord(string $table, int $uid, CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:134
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\$tstamp
‪int null $tstamp
Definition: RecordHistoryStore.php:56
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\ACTION_DELETE
‪const ACTION_DELETE
Definition: RecordHistoryStore.php:35
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\forSubject
‪static forSubject(string $subject, string ... $aspects)
Definition: CorrelationId.php:64
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\USER_FRONTEND
‪const USER_FRONTEND
Definition: RecordHistoryStore.php:39
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\ACTION_ADD
‪const ACTION_ADD
Definition: RecordHistoryStore.php:32
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\__construct
‪__construct(string $userType=self::USER_BACKEND, int $userId=null, int $originalUserId=null, int $tstamp=null, int $workspaceId=0)
Definition: RecordHistoryStore.php:69
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\ACTION_MOVE
‪const ACTION_MOVE
Definition: RecordHistoryStore.php:34
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\ACTION_UNDELETE
‪const ACTION_UNDELETE
Definition: RecordHistoryStore.php:36
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\USER_ANONYMOUS
‪const USER_ANONYMOUS
Definition: RecordHistoryStore.php:40
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\moveRecord
‪string moveRecord(string $table, int $uid, array $payload, CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:181