]>
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;
76 * Initialize: read history file.
78 * Allow lazy loading (don't read the file if it isn't necessary).
80 protected function initialize()
87 * Add Event: new link.
89 * @param array $link Link data.
91 public function addLink($link)
93 $this->addEvent(self
::CREATED
, $link['id']);
97 * Add Event: update existing link.
99 * @param array $link Link data.
101 public function updateLink($link)
103 $this->addEvent(self
::UPDATED
, $link['id']);
107 * Add Event: delete existing link.
109 * @param array $link Link data.
111 public function deleteLink($link)
113 $this->addEvent(self
::DELETED
, $link['id']);
117 * Add Event: settings updated.
119 public function updateSettings()
121 $this->addEvent(self
::SETTINGS
);
125 * Save a new event and write it in the history file.
127 * @param string $status Event key, should be defined as constant.
128 * @param mixed $id Event item identifier (e.g. link ID).
130 protected function addEvent($status, $id = null)
132 if ($this->history
=== null) {
138 'datetime' => new DateTime(),
139 'id' => $id !== null ? $id : '',
141 $this->history
= array_merge([$item], $this->history
);
146 * Check that the history file is writable.
147 * Create the file if it doesn't exist.
149 * @throws Exception if it isn't writable.
151 protected function check()
153 if (! is_file($this->historyFilePath
)) {
154 FileUtils
::writeFlatDB($this->historyFilePath
, []);
157 if (! is_writable($this->historyFilePath
)) {
158 throw new Exception('History file isn\'t readable or writable');
163 * Read JSON history file.
165 protected function read()
167 $this->history
= FileUtils
::readFlatDB($this->historyFilePath
, []);
168 if ($this->history
=== false) {
169 throw new Exception('Could not parse history file');
174 * Write JSON history file and delete old entries.
176 protected function write()
178 $comparaison = new DateTime('-'. $this->retentionTime
. ' seconds');
179 foreach ($this->history
as $key => $value) {
180 if ($value['datetime'] < $comparaison) {
181 unset($this->history
[$key]);
184 FileUtils
::writeFlatDB($this->historyFilePath
, array_values($this->history
));
192 public function getHistory()
194 if ($this->history
=== null) {
198 return $this->history
;