]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/History.php
6 * Handle the history file tracing events in Shaarli.
7 * The history is stored as JSON in a file set by 'resource.history' setting.
11 * - datetime: event date, in ISO8601 format.
12 * - id: event item identifier (currently only link IDs).
14 * Available event keys:
16 * - UPDATED: link updated
17 * - DELETED: link deleted
18 * - SETTINGS: the settings have been updated through the UI.
20 * Note: new events are put at the beginning of the file and history array.
25 * @var string Action key: a new link has been created.
27 const CREATED
= 'CREATED';
30 * @var string Action key: a link has been updated.
32 const UPDATED
= 'UPDATED';
35 * @var string Action key: a link has been deleted.
37 const DELETED
= 'DELETED';
40 * @var string Action key: settings have been updated.
42 const SETTINGS
= 'SETTINGS';
45 * @var string History file path.
47 protected $historyFilePath;
50 * @var array History data.
55 * @var int History retention time in seconds (1 month).
57 protected $retentionTime = 2678400;
60 * History constructor.
62 * @param string $historyFilePath History file path.
63 * @param int $retentionTime History content rentention time in seconds.
65 * @throws Exception if something goes wrong.
67 public function __construct($historyFilePath, $retentionTime = null)
69 $this->historyFilePath
= $historyFilePath;
70 if ($retentionTime !== null) {
71 $this->retentionTime
= $retentionTime;
78 * Add Event: new link.
80 * @param array $link Link data.
82 public function addLink($link)
84 $this->addEvent(self
::CREATED
, $link['id']);
88 * Add Event: update existing link.
90 * @param array $link Link data.
92 public function updateLink($link)
94 $this->addEvent(self
::UPDATED
, $link['id']);
98 * Add Event: delete existing link.
100 * @param array $link Link data.
102 public function deleteLink($link)
104 $this->addEvent(self
::DELETED
, $link['id']);
108 * Add Event: settings updated.
110 public function updateSettings()
112 $this->addEvent(self
::SETTINGS
);
116 * Save a new event and write it in the history file.
118 * @param string $status Event key, should be defined as constant.
119 * @param mixed $id Event item identifier (e.g. link ID).
121 protected function addEvent($status, $id = null)
125 'datetime' => (new DateTime())->format(DateTime
::ATOM
),
126 'id' => $id !== null ? $id : '',
128 $this->history
= array_merge([$item], $this->history
);
133 * Check that the history file is writable.
134 * Create the file if it doesn't exist.
136 * @throws Exception if it isn't writable.
138 protected function check()
140 if (! is_file($this->historyFilePath
)) {
141 FileUtils
::writeFlatDB($this->historyFilePath
, []);
144 if (! is_writable($this->historyFilePath
)) {
145 throw new Exception('History file isn\'t readable or writable');
150 * Read JSON history file.
152 protected function read()
154 $this->history
= FileUtils
::readFlatDB($this->historyFilePath
, []);
155 if ($this->history
=== false) {
156 throw new Exception('Could not parse history file');
161 * Write JSON history file and delete old entries.
163 protected function write()
165 $comparaison = new DateTime('-'. $this->retentionTime
. ' seconds');
166 foreach ($this->history
as $key => $value) {
167 if (DateTime
::createFromFormat(DateTime
::ATOM
, $value['datetime']) < $comparaison) {
168 unset($this->history
[$key]);
171 FileUtils
::writeFlatDB($this->historyFilePath
, array_values($this->history
));
179 public function getHistory()
181 return $this->history
;