6 use Shaarli\Bookmark\Bookmark
;
7 use Shaarli\Helper\FileUtils
;
12 * Handle the history file tracing events in Shaarli.
13 * The history is stored as JSON in a file set by 'resource.history' setting.
17 * - datetime: event date, in ISO8601 format.
18 * - id: event item identifier (currently only link IDs).
20 * Available event keys:
22 * - UPDATED: link updated
23 * - DELETED: link deleted
24 * - SETTINGS: the settings have been updated through the UI.
25 * - IMPORT: bulk bookmarks import
27 * Note: new events are put at the beginning of the file and history array.
32 * @var string Action key: a new link has been created.
34 const CREATED
= 'CREATED';
37 * @var string Action key: a link has been updated.
39 const UPDATED
= 'UPDATED';
42 * @var string Action key: a link has been deleted.
44 const DELETED
= 'DELETED';
47 * @var string Action key: settings have been updated.
49 const SETTINGS
= 'SETTINGS';
52 * @var string Action key: a bulk import has been processed.
54 const IMPORT
= 'IMPORT';
57 * @var string History file path.
59 protected $historyFilePath;
62 * @var array History data.
67 * @var int History retention time in seconds (1 month).
69 protected $retentionTime = 2678400;
72 * History constructor.
74 * @param string $historyFilePath History file path.
75 * @param int $retentionTime History content retention time in seconds.
77 * @throws Exception if something goes wrong.
79 public function __construct($historyFilePath, $retentionTime = null)
81 $this->historyFilePath
= $historyFilePath;
82 if ($retentionTime !== null) {
83 $this->retentionTime
= $retentionTime;
88 * Initialize: read history file.
90 * Allow lazy loading (don't read the file if it isn't necessary).
92 protected function initialize()
99 * Add Event: new link.
101 * @param Bookmark $link Link data.
103 public function addLink($link)
105 $this->addEvent(self
::CREATED
, $link->getId());
109 * Add Event: update existing link.
111 * @param Bookmark $link Link data.
113 public function updateLink($link)
115 $this->addEvent(self
::UPDATED
, $link->getId());
119 * Add Event: delete existing link.
121 * @param Bookmark $link Link data.
123 public function deleteLink($link)
125 $this->addEvent(self
::DELETED
, $link->getId());
129 * Add Event: settings updated.
131 public function updateSettings()
133 $this->addEvent(self
::SETTINGS
);
137 * Add Event: bulk import.
139 * Note: we don't store bookmarks add/update one by one since it can have a huge impact on performances.
141 public function importLinks()
143 $this->addEvent(self
::IMPORT
);
147 * Save a new event and write it in the history file.
149 * @param string $status Event key, should be defined as constant.
150 * @param mixed $id Event item identifier (e.g. link ID).
152 protected function addEvent($status, $id = null)
154 if ($this->history
=== null) {
160 'datetime' => new DateTime(),
161 'id' => $id !== null ? $id : '',
163 $this->history
= array_merge([$item], $this->history
);
168 * Check that the history file is writable.
169 * Create the file if it doesn't exist.
171 * @throws Exception if it isn't writable.
173 protected function check()
175 if (!is_file($this->historyFilePath
)) {
176 FileUtils
::writeFlatDB($this->historyFilePath
, []);
179 if (!is_writable($this->historyFilePath
)) {
180 throw new Exception(t('History file isn\'t readable or writable'));
185 * Read JSON history file.
187 protected function read()
189 $this->history
= FileUtils
::readFlatDB($this->historyFilePath
, []);
190 if ($this->history
=== false) {
191 throw new Exception(t('Could not parse history file'));
196 * Write JSON history file and delete old entries.
198 protected function write()
200 $comparaison = new DateTime('-' . $this->retentionTime
. ' seconds');
201 foreach ($this->history
as $key => $value) {
202 if ($value['datetime'] < $comparaison) {
203 unset($this->history
[$key]);
206 FileUtils
::writeFlatDB($this->historyFilePath
, array_values($this->history
));
214 public function getHistory()
216 if ($this->history
=== null) {
220 return $this->history
;