]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Shaarli; | |
4 | ||
5 | use DateTime; | |
6 | use Exception; | |
7 | use Shaarli\Bookmark\Bookmark; | |
8 | use Shaarli\Helper\FileUtils; | |
9 | ||
10 | /** | |
11 | * Class History | |
12 | * | |
13 | * Handle the history file tracing events in Shaarli. | |
14 | * The history is stored as JSON in a file set by 'resource.history' setting. | |
15 | * | |
16 | * Available data: | |
17 | * - event: event key | |
18 | * - datetime: event date, in ISO8601 format. | |
19 | * - id: event item identifier (currently only link IDs). | |
20 | * | |
21 | * Available event keys: | |
22 | * - CREATED: new link | |
23 | * - UPDATED: link updated | |
24 | * - DELETED: link deleted | |
25 | * - SETTINGS: the settings have been updated through the UI. | |
26 | * - IMPORT: bulk bookmarks import | |
27 | * | |
28 | * Note: new events are put at the beginning of the file and history array. | |
29 | */ | |
30 | class History | |
31 | { | |
32 | /** | |
33 | * @var string Action key: a new link has been created. | |
34 | */ | |
35 | public const CREATED = 'CREATED'; | |
36 | ||
37 | /** | |
38 | * @var string Action key: a link has been updated. | |
39 | */ | |
40 | public const UPDATED = 'UPDATED'; | |
41 | ||
42 | /** | |
43 | * @var string Action key: a link has been deleted. | |
44 | */ | |
45 | public const DELETED = 'DELETED'; | |
46 | ||
47 | /** | |
48 | * @var string Action key: settings have been updated. | |
49 | */ | |
50 | public const SETTINGS = 'SETTINGS'; | |
51 | ||
52 | /** | |
53 | * @var string Action key: a bulk import has been processed. | |
54 | */ | |
55 | public const IMPORT = 'IMPORT'; | |
56 | ||
57 | /** | |
58 | * @var string History file path. | |
59 | */ | |
60 | protected $historyFilePath; | |
61 | ||
62 | /** | |
63 | * @var array History data. | |
64 | */ | |
65 | protected $history; | |
66 | ||
67 | /** | |
68 | * @var int History retention time in seconds (1 month). | |
69 | */ | |
70 | protected $retentionTime = 2678400; | |
71 | ||
72 | /** | |
73 | * History constructor. | |
74 | * | |
75 | * @param string $historyFilePath History file path. | |
76 | * @param int $retentionTime History content retention time in seconds. | |
77 | * | |
78 | * @throws Exception if something goes wrong. | |
79 | */ | |
80 | public function __construct($historyFilePath, $retentionTime = null) | |
81 | { | |
82 | $this->historyFilePath = $historyFilePath; | |
83 | if ($retentionTime !== null) { | |
84 | $this->retentionTime = $retentionTime; | |
85 | } | |
86 | } | |
87 | ||
88 | /** | |
89 | * Initialize: read history file. | |
90 | * | |
91 | * Allow lazy loading (don't read the file if it isn't necessary). | |
92 | */ | |
93 | protected function initialize() | |
94 | { | |
95 | $this->check(); | |
96 | $this->read(); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Add Event: new link. | |
101 | * | |
102 | * @param Bookmark $link Link data. | |
103 | */ | |
104 | public function addLink($link) | |
105 | { | |
106 | $this->addEvent(self::CREATED, $link->getId()); | |
107 | } | |
108 | ||
109 | /** | |
110 | * Add Event: update existing link. | |
111 | * | |
112 | * @param Bookmark $link Link data. | |
113 | */ | |
114 | public function updateLink($link) | |
115 | { | |
116 | $this->addEvent(self::UPDATED, $link->getId()); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Add Event: delete existing link. | |
121 | * | |
122 | * @param Bookmark $link Link data. | |
123 | */ | |
124 | public function deleteLink($link) | |
125 | { | |
126 | $this->addEvent(self::DELETED, $link->getId()); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Add Event: settings updated. | |
131 | */ | |
132 | public function updateSettings() | |
133 | { | |
134 | $this->addEvent(self::SETTINGS); | |
135 | } | |
136 | ||
137 | /** | |
138 | * Add Event: bulk import. | |
139 | * | |
140 | * Note: we don't store bookmarks add/update one by one since it can have a huge impact on performances. | |
141 | */ | |
142 | public function importLinks() | |
143 | { | |
144 | $this->addEvent(self::IMPORT); | |
145 | } | |
146 | ||
147 | /** | |
148 | * Save a new event and write it in the history file. | |
149 | * | |
150 | * @param string $status Event key, should be defined as constant. | |
151 | * @param mixed $id Event item identifier (e.g. link ID). | |
152 | */ | |
153 | protected function addEvent($status, $id = null) | |
154 | { | |
155 | if ($this->history === null) { | |
156 | $this->initialize(); | |
157 | } | |
158 | ||
159 | $item = [ | |
160 | 'event' => $status, | |
161 | 'datetime' => new DateTime(), | |
162 | 'id' => $id !== null ? $id : '', | |
163 | ]; | |
164 | $this->history = array_merge([$item], $this->history); | |
165 | $this->write(); | |
166 | } | |
167 | ||
168 | /** | |
169 | * Check that the history file is writable. | |
170 | * Create the file if it doesn't exist. | |
171 | * | |
172 | * @throws Exception if it isn't writable. | |
173 | */ | |
174 | protected function check() | |
175 | { | |
176 | if (!is_file($this->historyFilePath)) { | |
177 | FileUtils::writeFlatDB($this->historyFilePath, []); | |
178 | } | |
179 | ||
180 | if (!is_writable($this->historyFilePath)) { | |
181 | throw new Exception(t('History file isn\'t readable or writable')); | |
182 | } | |
183 | } | |
184 | ||
185 | /** | |
186 | * Read JSON history file. | |
187 | */ | |
188 | protected function read() | |
189 | { | |
190 | $this->history = FileUtils::readFlatDB($this->historyFilePath, []); | |
191 | if ($this->history === false) { | |
192 | throw new Exception(t('Could not parse history file')); | |
193 | } | |
194 | } | |
195 | ||
196 | /** | |
197 | * Write JSON history file and delete old entries. | |
198 | */ | |
199 | protected function write() | |
200 | { | |
201 | $comparaison = new DateTime('-' . $this->retentionTime . ' seconds'); | |
202 | foreach ($this->history as $key => $value) { | |
203 | if ($value['datetime'] < $comparaison) { | |
204 | unset($this->history[$key]); | |
205 | } | |
206 | } | |
207 | FileUtils::writeFlatDB($this->historyFilePath, array_values($this->history)); | |
208 | } | |
209 | ||
210 | /** | |
211 | * Get the History. | |
212 | * | |
213 | * @return array | |
214 | */ | |
215 | public function getHistory() | |
216 | { | |
217 | if ($this->history === null) { | |
218 | $this->initialize(); | |
219 | } | |
220 | ||
221 | return $this->history; | |
222 | } | |
223 | } |