‪TYPO3CMS  ‪main
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 
73  public function ‪__construct(string ‪$userType = self::USER_BACKEND, ?int ‪$userId = null, ?int ‪$originalUserId = null, ?int ‪$tstamp = null, int ‪$workspaceId = 0)
74  {
75  $this->userType = ‪$userType;
76  $this->userId = ‪$userId;
77  $this->originalUserId = ‪$originalUserId;
78  $this->tstamp = ‪$tstamp ?: ‪$GLOBALS['EXEC_TIME'];
79  $this->workspaceId = ‪$workspaceId;
80  }
81 
85  public function ‪addRecord(string $table, int ‪$uid, array $payload, ?‪CorrelationId $correlationId = null): string
86  {
87  $data = [
88  'actiontype' => ‪self::ACTION_ADD,
89  'usertype' => ‪$this->userType,
90  'userid' => ‪$this->userId,
91  'originaluserid' => ‪$this->originalUserId,
92  'tablename' => $table,
93  'recuid' => ‪$uid,
94  'tstamp' => ‪$this->tstamp,
95  'history_data' => json_encode($payload),
96  'workspace' => ‪$this->workspaceId,
97  'correlation_id' => (string)$this->‪createCorrelationId($table, ‪$uid, $correlationId),
98  ];
99  $this->‪getDatabaseConnection()->insert('sys_history', $data);
100  return $this->‪getDatabaseConnection()->lastInsertId();
101  }
102 
106  public function ‪modifyRecord(string $table, int ‪$uid, array $payload, ?‪CorrelationId $correlationId = null): string
107  {
108  $data = [
109  'actiontype' => ‪self::ACTION_MODIFY,
110  'usertype' => ‪$this->userType,
111  'userid' => ‪$this->userId,
112  'originaluserid' => ‪$this->originalUserId,
113  'tablename' => $table,
114  'recuid' => ‪$uid,
115  'tstamp' => ‪$this->tstamp,
116  'history_data' => json_encode($payload),
117  'workspace' => ‪$this->workspaceId,
118  'correlation_id' => (string)$this->‪createCorrelationId($table, ‪$uid, $correlationId),
119  ];
120  $this->‪getDatabaseConnection()->insert('sys_history', $data);
121  return $this->‪getDatabaseConnection()->lastInsertId();
122  }
123 
127  public function ‪deleteRecord(string $table, int ‪$uid, ?‪CorrelationId $correlationId = null): string
128  {
129  $data = [
130  'actiontype' => ‪self::ACTION_DELETE,
131  'usertype' => ‪$this->userType,
132  'userid' => ‪$this->userId,
133  'originaluserid' => ‪$this->originalUserId,
134  'tablename' => $table,
135  'recuid' => ‪$uid,
136  'tstamp' => ‪$this->tstamp,
137  'workspace' => ‪$this->workspaceId,
138  'correlation_id' => (string)$this->‪createCorrelationId($table, ‪$uid, $correlationId),
139  ];
140  $this->‪getDatabaseConnection()->insert('sys_history', $data);
141  return $this->‪getDatabaseConnection()->lastInsertId();
142  }
143 
147  public function ‪undeleteRecord(string $table, int ‪$uid, ?‪CorrelationId $correlationId = null): string
148  {
149  $data = [
150  'actiontype' => ‪self::ACTION_UNDELETE,
151  'usertype' => ‪$this->userType,
152  'userid' => ‪$this->userId,
153  'originaluserid' => ‪$this->originalUserId,
154  'tablename' => $table,
155  'recuid' => ‪$uid,
156  'tstamp' => ‪$this->tstamp,
157  'workspace' => ‪$this->workspaceId,
158  'correlation_id' => (string)$this->‪createCorrelationId($table, ‪$uid, $correlationId),
159  ];
160  $this->‪getDatabaseConnection()->insert('sys_history', $data);
161  return $this->‪getDatabaseConnection()->lastInsertId();
162  }
163 
167  public function ‪moveRecord(string $table, int ‪$uid, array $payload, ?‪CorrelationId $correlationId = null): string
168  {
169  $data = [
170  'actiontype' => ‪self::ACTION_MOVE,
171  'usertype' => ‪$this->userType,
172  'userid' => ‪$this->userId,
173  'originaluserid' => ‪$this->originalUserId,
174  'tablename' => $table,
175  'recuid' => ‪$uid,
176  'tstamp' => ‪$this->tstamp,
177  'history_data' => json_encode($payload),
178  'workspace' => ‪$this->workspaceId,
179  'correlation_id' => (string)$this->‪createCorrelationId($table, ‪$uid, $correlationId),
180  ];
181  $this->‪getDatabaseConnection()->insert('sys_history', $data);
182  return $this->‪getDatabaseConnection()->lastInsertId();
183  }
184 
185  public function ‪changeStageForRecord(string $table, int ‪$uid, array $payload, ?‪CorrelationId $correlationId = null): string
186  {
187  $data = [
188  'actiontype' => ‪self::ACTION_STAGECHANGE,
189  'usertype' => ‪$this->userType,
190  'userid' => ‪$this->userId,
191  'originaluserid' => ‪$this->originalUserId,
192  'tablename' => $table,
193  'recuid' => ‪$uid,
194  'tstamp' => ‪$this->tstamp,
195  'history_data' => json_encode($payload),
196  'workspace' => ‪$this->workspaceId,
197  'correlation_id' => (string)$this->‪createCorrelationId($table, ‪$uid, $correlationId),
198  ];
199  $this->‪getDatabaseConnection()->insert('sys_history', $data);
200  return $this->‪getDatabaseConnection()->lastInsertId();
201  }
202 
203  protected function ‪createCorrelationId(string $tableName, int ‪$uid, ?‪CorrelationId $correlationId): ‪CorrelationId
204  {
205  if ($correlationId !== null && $correlationId->‪getSubject() !== null) {
206  return $correlationId;
207  }
208  $subject = md5($tableName . ':' . ‪$uid);
209  return $correlationId !== null ? $correlationId->‪withSubject($subject) : ‪CorrelationId::forSubject($subject);
210  }
211 
212  protected function ‪getDatabaseConnection(): ‪Connection
213  {
214  return GeneralUtility::makeInstance(ConnectionPool::class)
215  ->getConnectionForTable('sys_history');
216  }
217 }
‪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\History\RecordHistoryStore\$userId
‪int null $userId
Definition: RecordHistoryStore.php:45
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\addRecord
‪addRecord(string $table, int $uid, array $payload, ?CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:80
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\moveRecord
‪moveRecord(string $table, int $uid, array $payload, ?CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:162
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\createCorrelationId
‪createCorrelationId(string $tableName, int $uid, ?CorrelationId $correlationId)
Definition: RecordHistoryStore.php:198
‪TYPO3\CMS\Core\DataHandling\History
Definition: RecordHistoryStore.php:18
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\getSubject
‪getSubject()
Definition: CorrelationId.php:116
‪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:68
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\modifyRecord
‪modifyRecord(string $table, int $uid, array $payload, ?CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:101
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\ACTION_STAGECHANGE
‪const ACTION_STAGECHANGE
Definition: RecordHistoryStore.php:37
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\deleteRecord
‪deleteRecord(string $table, int $uid, ?CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:122
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore
Definition: RecordHistoryStore.php:31
‪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:91
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\$tstamp
‪int null $tstamp
Definition: RecordHistoryStore.php:57
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\undeleteRecord
‪undeleteRecord(string $table, int $uid, ?CorrelationId $correlationId=null)
Definition: RecordHistoryStore.php:142
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪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:49
‪$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:180
‪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:52
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\USER_ANONYMOUS
‪const USER_ANONYMOUS
Definition: RecordHistoryStore.php:41
‪TYPO3\CMS\Core\DataHandling\History\RecordHistoryStore\getDatabaseConnection
‪getDatabaseConnection()
Definition: RecordHistoryStore.php:207