6 use Shaarli\Bookmark\Bookmark
;
11 * Handle the history file tracing events in Shaarli.
12 * The history is stored as JSON in a file set by 'resource.history' setting.
16 * - datetime: event date, in ISO8601 format.
17 * - id: event item identifier (currently only link IDs).
19 * Available event keys:
21 * - UPDATED: link updated
22 * - DELETED: link deleted
23 * - SETTINGS: the settings have been updated through the UI.
24 * - IMPORT: bulk bookmarks import
26 * Note: new events are put at the beginning of the file and history array.
31 * @var string Action key: a new link has been created.
33 const CREATED
= 'CREATED';
36 * @var string Action key: a link has been updated.
38 const UPDATED
= 'UPDATED';
41 * @var string Action key: a link has been deleted.
43 const DELETED
= 'DELETED';
46 * @var string Action key: settings have been updated.
48 const SETTINGS
= 'SETTINGS';
51 * @var string Action key: a bulk import has been processed.
53 const IMPORT
= 'IMPORT';
56 * @var string History file path.
58 protected $historyFilePath;
61 * @var array History data.
66 * @var int History retention time in seconds (1 month).
68 protected $retentionTime = 2678400;
71 * History constructor.
73 * @param string $historyFilePath History file path.
74 * @param int $retentionTime History content retention time in seconds.
76 * @throws Exception if something goes wrong.
78 public function __construct($historyFilePath, $retentionTime = null)
80 $this->historyFilePath
= $historyFilePath;
81 if ($retentionTime !== null) {
82 $this->retentionTime
= $retentionTime;
87 * Initialize: read history file.
89 * Allow lazy loading (don't read the file if it isn't necessary).
91 protected function initialize()
98 * Add Event: new link.
100 * @param Bookmark $link Link data.
102 public function addLink($link)
104 $this->addEvent(self
::CREATED
, $link->getId());
108 * Add Event: update existing link.
110 * @param Bookmark $link Link data.
112 public function updateLink($link)
114 $this->addEvent(self
::UPDATED
, $link->getId());
118 * Add Event: delete existing link.
120 * @param Bookmark $link Link data.
122 public function deleteLink($link)
124 $this->addEvent(self
::DELETED
, $link->getId());
128 * Add Event: settings updated.
130 public function updateSettings()
132 $this->addEvent(self
::SETTINGS
);
136 * Add Event: bulk import.
138 * Note: we don't store bookmarks add/update one by one since it can have a huge impact on performances.
140 public function importLinks()
142 $this->addEvent(self
::IMPORT
);
146 * Save a new event and write it in the history file.
148 * @param string $status Event key, should be defined as constant.
149 * @param mixed $id Event item identifier (e.g. link ID).
151 protected function addEvent($status, $id = null)
153 if ($this->history
=== null) {
159 'datetime' => new DateTime(),
160 'id' => $id !== null ? $id : '',
162 $this->history
= array_merge([$item], $this->history
);
167 * Check that the history file is writable.
168 * Create the file if it doesn't exist.
170 * @throws Exception if it isn't writable.
172 protected function check()
174 if (!is_file($this->historyFilePath
)) {
175 FileUtils
::writeFlatDB($this->historyFilePath
, []);
178 if (!is_writable($this->historyFilePath
)) {
179 throw new Exception(t('History file isn\'t readable or writable'));
184 * Read JSON history file.
186 protected function read()
188 $this->history
= FileUtils
::readFlatDB($this->historyFilePath
, []);
189 if ($this->history
=== false) {
190 throw new Exception(t('Could not parse history file'));
195 * Write JSON history file and delete old entries.
197 protected function write()
199 $comparaison = new DateTime('-' . $this->retentionTime
. ' seconds');
200 foreach ($this->history
as $key => $value) {
201 if ($value['datetime'] < $comparaison) {
202 unset($this->history
[$key]);
205 FileUtils
::writeFlatDB($this->historyFilePath
, array_values($this->history
));
213 public function getHistory()
215 if ($this->history
=== null) {
219 return $this->history
;