‪TYPO3CMS  11.5
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  public const ‪ACTION_STAGECHANGE = 6;
38 
39  public const ‪USER_BACKEND = 'BE';
40  public const ‪USER_FRONTEND = 'FE';
41  public const ‪USER_ANONYMOUS = '';
42 
46  protected ‪$userId;
47 
51  protected ‪$userType;
52 
56  protected ‪$originalUserId;
57 
61  protected ‪$tstamp;
62 
66  protected ‪$workspaceId;
67 
75  public function ‪__construct(string ‪$userType = self::USER_BACKEND, ?int ‪$userId = null, ?int ‪$originalUserId = null, ?int ‪$tstamp = null, int ‪$workspaceId = 0)
76  {
77  $this->userType = ‪$userType;
78  $this->userId = ‪$userId;
79  $this->originalUserId = ‪$originalUserId;
80  $this->tstamp = ‪$tstamp ?: ‪$GLOBALS['EXEC_TIME'];
81  $this->workspaceId = ‪$workspaceId;
82  }
83 
91  public function ‪addRecord(string $table, int $uid, array $payload, ?‪CorrelationId $correlationId = null): string
92  {
93  $data = [
94  'actiontype' => ‪self::ACTION_ADD,
95  'usertype' => ‪$this->userType,
96  'userid' => ‪$this->userId,
97  'originaluserid' => ‪$this->originalUserId,
98  'tablename' => $table,
99  'recuid' => $uid,
100  'tstamp' => ‪$this->tstamp,
101  'history_data' => json_encode($payload),
102  'workspace' => ‪$this->workspaceId,
103  'correlation_id' => (string)$this->‪createCorrelationId($table, $uid, $correlationId),
104  ];
105  $this->‪getDatabaseConnection()->‪insert('sys_history', $data);
106  return $this->‪getDatabaseConnection()->‪lastInsertId('sys_history');
107  }
108 
116  public function ‪modifyRecord(string $table, int $uid, array $payload, ?‪CorrelationId $correlationId = null): string
117  {
118  $data = [
119  'actiontype' => ‪self::ACTION_MODIFY,
120  'usertype' => ‪$this->userType,
121  'userid' => ‪$this->userId,
122  'originaluserid' => ‪$this->originalUserId,
123  'tablename' => $table,
124  'recuid' => $uid,
125  'tstamp' => ‪$this->tstamp,
126  'history_data' => json_encode($payload),
127  'workspace' => ‪$this->workspaceId,
128  'correlation_id' => (string)$this->‪createCorrelationId($table, $uid, $correlationId),
129  ];
130  $this->‪getDatabaseConnection()->‪insert('sys_history', $data);
131  return $this->‪getDatabaseConnection()->‪lastInsertId('sys_history');
132  }
133 
140  public function ‪deleteRecord(string $table, int $uid, ?CorrelationId $correlationId = null): string
141  {
142  $data = [
143  'actiontype' => ‪self::ACTION_DELETE,
144  'usertype' => ‪$this->userType,
145  'userid' => ‪$this->userId,
146  'originaluserid' => ‪$this->originalUserId,
147  'tablename' => $table,
148  'recuid' => $uid,
149  'tstamp' => ‪$this->tstamp,
150  'workspace' => ‪$this->workspaceId,
151  'correlation_id' => (string)$this->‪createCorrelationId($table, $uid, $correlationId),
152  ];
153  $this->‪getDatabaseConnection()->‪insert('sys_history', $data);
154  return $this->‪getDatabaseConnection()->‪lastInsertId('sys_history');
155  }
156 
163  public function ‪undeleteRecord(string $table, int $uid, ?CorrelationId $correlationId = null): string
164  {
165  $data = [
166  'actiontype' => ‪self::ACTION_UNDELETE,
167  'usertype' => ‪$this->userType,
168  'userid' => ‪$this->userId,
169  'originaluserid' => ‪$this->originalUserId,
170  'tablename' => $table,
171  'recuid' => $uid,
172  'tstamp' => ‪$this->tstamp,
173  'workspace' => ‪$this->workspaceId,
174  'correlation_id' => (string)$this->‪createCorrelationId($table, $uid, $correlationId),
175  ];
176  $this->‪getDatabaseConnection()->‪insert('sys_history', $data);
177  return $this->‪getDatabaseConnection()->‪lastInsertId('sys_history');
178  }
179 
187  public function ‪moveRecord(string $table, int $uid, array $payload, ?CorrelationId $correlationId = null): string
188  {
189  $data = [
190  'actiontype' => ‪self::ACTION_MOVE,
191  'usertype' => ‪$this->userType,
192  'userid' => ‪$this->userId,
193  'originaluserid' => ‪$this->originalUserId,
194  'tablename' => $table,
195  'recuid' => $uid,
196  'tstamp' => ‪$this->tstamp,
197  'history_data' => json_encode($payload),
198  'workspace' => ‪$this->workspaceId,
199  'correlation_id' => (string)$this->‪createCorrelationId($table, $uid, $correlationId),
200  ];
201  $this->‪getDatabaseConnection()->‪insert('sys_history', $data);
202  return $this->‪getDatabaseConnection()->‪lastInsertId('sys_history');
203  }
204 
205  public function ‪changeStageForRecord(string $table, int $uid, array $payload, ?‪CorrelationId $correlationId = null): string
206  {
207  $data = [
208  'actiontype' => ‪self::ACTION_STAGECHANGE,
209  'usertype' => ‪$this->userType,
210  'userid' => ‪$this->userId,
211  'originaluserid' => ‪$this->originalUserId,
212  'tablename' => $table,
213  'recuid' => $uid,
214  'tstamp' => ‪$this->tstamp,
215  'history_data' => json_encode($payload),
216  'workspace' => ‪$this->workspaceId,
217  'correlation_id' => (string)$this->‪createCorrelationId($table, $uid, $correlationId),
218  ];
219  $this->‪getDatabaseConnection()->‪insert('sys_history', $data);
220  return $this->‪getDatabaseConnection()->‪lastInsertId('sys_history');
221  }
222 
223  protected function ‪createCorrelationId(string $tableName, int $uid, ?‪CorrelationId $correlationId): ‪CorrelationId
224  {
225  if ($correlationId !== null && $correlationId->‪getSubject() !== null) {
226  return $correlationId;
227  }
228  $subject = md5($tableName . ':' . $uid);
229  return $correlationId !== null ? $correlationId->‪withSubject($subject) : ‪CorrelationId::forSubject($subject);
230  }
231 
235  protected function ‪getDatabaseConnection(): ‪Connection
236  {
237  return GeneralUtility::makeInstance(ConnectionPool::class)
238  ->getConnectionForTable('sys_history');
239  }
240 }
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\$workspaceId
‪int $workspaceId
Definition: RecordHistoryStore.php:61
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\USER_BACKEND
‪const USER_BACKEND
Definition: RecordHistoryStore.php:39
‪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:463
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\$userId
‪int null $userId
Definition: RecordHistoryStore.php:45
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\createCorrelationId
‪createCorrelationId(string $tableName, int $uid, ?CorrelationId $correlationId)
Definition: RecordHistoryStore.php:218
‪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:49
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\ACTION_MODIFY
‪const ACTION_MODIFY
Definition: RecordHistoryStore.php:33
‪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:70
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\modifyRecord
‪string modifyRecord(string $table, int $uid, array $payload, ?CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:111
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\ACTION_STAGECHANGE
‪const ACTION_STAGECHANGE
Definition: RecordHistoryStore.php:37
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\getDatabaseConnection
‪Connection getDatabaseConnection()
Definition: RecordHistoryStore.php:230
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\undeleteRecord
‪string undeleteRecord(string $table, int $uid, ?CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:158
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore
Definition: RecordHistoryStore.php:31
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\deleteRecord
‪string deleteRecord(string $table, int $uid, ?CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:135
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\$originalUserId
‪int null $originalUserId
Definition: RecordHistoryStore.php:53
‪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\moveRecord
‪string moveRecord(string $table, int $uid, array $payload, ?CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:182
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\$tstamp
‪int null $tstamp
Definition: RecordHistoryStore.php:57
‪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:25
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\USER_FRONTEND
‪const USER_FRONTEND
Definition: RecordHistoryStore.php:40
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\changeStageForRecord
‪changeStageForRecord(string $table, int $uid, array $payload, ?CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:200
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\ACTION_ADD
‪const ACTION_ADD
Definition: RecordHistoryStore.php:32
‪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:50
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\USER_ANONYMOUS
‪const USER_ANONYMOUS
Definition: RecordHistoryStore.php:41
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\addRecord
‪string addRecord(string $table, int $uid, array $payload, ?CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:86