]>
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.
19 * - IMPORT: bulk links import
21 * Note: new events are put at the beginning of the file and history array.
26 * @var string Action key: a new link has been created.
28 const CREATED
= 'CREATED';
31 * @var string Action key: a link has been updated.
33 const UPDATED
= 'UPDATED';
36 * @var string Action key: a link has been deleted.
38 const DELETED
= 'DELETED';
41 * @var string Action key: settings have been updated.
43 const SETTINGS
= 'SETTINGS';
46 * @var string Action key: a bulk import has been processed.
48 const IMPORT
= 'IMPORT';
51 * @var string History file path.
53 protected $historyFilePath;
56 * @var array History data.
61 * @var int History retention time in seconds (1 month).
63 protected $retentionTime = 2678400;
66 * History constructor.
68 * @param string $historyFilePath History file path.
69 * @param int $retentionTime History content rentention time in seconds.
71 * @throws Exception if something goes wrong.
73 public function __construct($historyFilePath, $retentionTime = null)
75 $this->historyFilePath
= $historyFilePath;
76 if ($retentionTime !== null) {
77 $this->retentionTime
= $retentionTime;
82 * Initialize: read history file.
84 * Allow lazy loading (don't read the file if it isn't necessary).
86 protected function initialize()
93 * Add Event: new link.
95 * @param array $link Link data.
97 public function addLink($link)
99 $this->addEvent(self
::CREATED
, $link['id']);
103 * Add Event: update existing link.
105 * @param array $link Link data.
107 public function updateLink($link)
109 $this->addEvent(self
::UPDATED
, $link['id']);
113 * Add Event: delete existing link.
115 * @param array $link Link data.
117 public function deleteLink($link)
119 $this->addEvent(self
::DELETED
, $link['id']);
123 * Add Event: settings updated.
125 public function updateSettings()
127 $this->addEvent(self
::SETTINGS
);
131 * Add Event: bulk import.
133 * Note: we don't store links add/update one by one since it can have a huge impact on performances.
135 public function importLinks()
137 $this->addEvent(self
::IMPORT
);
141 * Save a new event and write it in the history file.
143 * @param string $status Event key, should be defined as constant.
144 * @param mixed $id Event item identifier (e.g. link ID).
146 protected function addEvent($status, $id = null)
148 if ($this->history
=== null) {
154 'datetime' => new DateTime(),
155 'id' => $id !== null ? $id : '',
157 $this->history
= array_merge([$item], $this->history
);
162 * Check that the history file is writable.
163 * Create the file if it doesn't exist.
165 * @throws Exception if it isn't writable.
167 protected function check()
169 if (! is_file($this->historyFilePath
)) {
170 FileUtils
::writeFlatDB($this->historyFilePath
, []);
173 if (! is_writable($this->historyFilePath
)) {
174 throw new Exception(t('History file isn\'t readable or writable'));
179 * Read JSON history file.
181 protected function read()
183 $this->history
= FileUtils
::readFlatDB($this->historyFilePath
, []);
184 if ($this->history
=== false) {
185 throw new Exception(t('Could not parse history file'));
190 * Write JSON history file and delete old entries.
192 protected function write()
194 $comparaison = new DateTime('-'. $this->retentionTime
. ' seconds');
195 foreach ($this->history
as $key => $value) {
196 if ($value['datetime'] < $comparaison) {
197 unset($this->history
[$key]);
200 FileUtils
::writeFlatDB($this->historyFilePath
, array_values($this->history
));
208 public function getHistory()
210 if ($this->history
=== null) {
214 return $this->history
;