]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/History.php
10 * Handle the history file tracing events in Shaarli.
11 * The history is stored as JSON in a file set by 'resource.history' setting.
15 * - datetime: event date, in ISO8601 format.
16 * - id: event item identifier (currently only link IDs).
18 * Available event keys:
20 * - UPDATED: link updated
21 * - DELETED: link deleted
22 * - SETTINGS: the settings have been updated through the UI.
23 * - IMPORT: bulk links import
25 * Note: new events are put at the beginning of the file and history array.
30 * @var string Action key: a new link has been created.
32 const CREATED
= 'CREATED';
35 * @var string Action key: a link has been updated.
37 const UPDATED
= 'UPDATED';
40 * @var string Action key: a link has been deleted.
42 const DELETED
= 'DELETED';
45 * @var string Action key: settings have been updated.
47 const SETTINGS
= 'SETTINGS';
50 * @var string Action key: a bulk import has been processed.
52 const IMPORT
= 'IMPORT';
55 * @var string History file path.
57 protected $historyFilePath;
60 * @var array History data.
65 * @var int History retention time in seconds (1 month).
67 protected $retentionTime = 2678400;
70 * History constructor.
72 * @param string $historyFilePath History file path.
73 * @param int $retentionTime History content retention time in seconds.
75 * @throws Exception if something goes wrong.
77 public function __construct($historyFilePath, $retentionTime = null)
79 $this->historyFilePath
= $historyFilePath;
80 if ($retentionTime !== null) {
81 $this->retentionTime
= $retentionTime;
86 * Initialize: read history file.
88 * Allow lazy loading (don't read the file if it isn't necessary).
90 protected function initialize()
97 * Add Event: new link.
99 * @param array $link Link data.
101 public function addLink($link)
103 $this->addEvent(self
::CREATED
, $link['id']);
107 * Add Event: update existing link.
109 * @param array $link Link data.
111 public function updateLink($link)
113 $this->addEvent(self
::UPDATED
, $link['id']);
117 * Add Event: delete existing link.
119 * @param array $link Link data.
121 public function deleteLink($link)
123 $this->addEvent(self
::DELETED
, $link['id']);
127 * Add Event: settings updated.
129 public function updateSettings()
131 $this->addEvent(self
::SETTINGS
);
135 * Add Event: bulk import.
137 * Note: we don't store links add/update one by one since it can have a huge impact on performances.
139 public function importLinks()
141 $this->addEvent(self
::IMPORT
);
145 * Save a new event and write it in the history file.
147 * @param string $status Event key, should be defined as constant.
148 * @param mixed $id Event item identifier (e.g. link ID).
150 protected function addEvent($status, $id = null)
152 if ($this->history
=== null) {
158 'datetime' => new DateTime(),
159 'id' => $id !== null ? $id : '',
161 $this->history
= array_merge([$item], $this->history
);
166 * Check that the history file is writable.
167 * Create the file if it doesn't exist.
169 * @throws Exception if it isn't writable.
171 protected function check()
173 if (!is_file($this->historyFilePath
)) {
174 FileUtils
::writeFlatDB($this->historyFilePath
, []);
177 if (!is_writable($this->historyFilePath
)) {
178 throw new Exception(t('History file isn\'t readable or writable'));
183 * Read JSON history file.
185 protected function read()
187 $this->history
= FileUtils
::readFlatDB($this->historyFilePath
, []);
188 if ($this->history
=== false) {
189 throw new Exception(t('Could not parse history file'));
194 * Write JSON history file and delete old entries.
196 protected function write()
198 $comparaison = new DateTime('-' . $this->retentionTime
. ' seconds');
199 foreach ($this->history
as $key => $value) {
200 if ($value['datetime'] < $comparaison) {
201 unset($this->history
[$key]);
204 FileUtils
::writeFlatDB($this->historyFilePath
, array_values($this->history
));
212 public function getHistory()
214 if ($this->history
=== null) {
218 return $this->history
;