]>
Commit | Line | Data |
---|---|---|
336a28fa A |
1 | <?php |
2 | ||
efb7d21b | 3 | declare(strict_types=1); |
336a28fa A |
4 | |
5 | namespace Shaarli\Bookmark; | |
6 | ||
efb7d21b | 7 | use DateTime; |
336a28fa | 8 | use Exception; |
fd1ddad9 | 9 | use malkusch\lock\mutex\Mutex; |
336a28fa | 10 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; |
d6e5f04d | 11 | use Shaarli\Bookmark\Exception\DatastoreNotInitializedException; |
336a28fa A |
12 | use Shaarli\Bookmark\Exception\EmptyDataStoreException; |
13 | use Shaarli\Config\ConfigManager; | |
a39acb25 | 14 | use Shaarli\Formatter\BookmarkMarkdownFormatter; |
336a28fa A |
15 | use Shaarli\History; |
16 | use Shaarli\Legacy\LegacyLinkDB; | |
17 | use Shaarli\Legacy\LegacyUpdater; | |
b0428aa9 | 18 | use Shaarli\Render\PageCacheManager; |
336a28fa A |
19 | use Shaarli\Updater\UpdaterUtils; |
20 | ||
21 | /** | |
22 | * Class BookmarksService | |
23 | * | |
24 | * This is the entry point to manipulate the bookmark DB. | |
25 | * It manipulates loads links from a file data store containing all bookmarks. | |
26 | * | |
27 | * It also triggers the legacy format (bookmarks as arrays) migration. | |
28 | */ | |
29 | class BookmarkFileService implements BookmarkServiceInterface | |
30 | { | |
31 | /** @var Bookmark[] instance */ | |
32 | protected $bookmarks; | |
33 | ||
34 | /** @var BookmarkIO instance */ | |
35 | protected $bookmarksIO; | |
36 | ||
37 | /** @var BookmarkFilter */ | |
38 | protected $bookmarkFilter; | |
39 | ||
40 | /** @var ConfigManager instance */ | |
41 | protected $conf; | |
42 | ||
43 | /** @var History instance */ | |
44 | protected $history; | |
45 | ||
b0428aa9 A |
46 | /** @var PageCacheManager instance */ |
47 | protected $pageCacheManager; | |
48 | ||
336a28fa A |
49 | /** @var bool true for logged in users. Default value to retrieve private bookmarks. */ |
50 | protected $isLoggedIn; | |
51 | ||
fd1ddad9 A |
52 | /** @var Mutex */ |
53 | protected $mutex; | |
54 | ||
336a28fa A |
55 | /** |
56 | * @inheritDoc | |
57 | */ | |
efb7d21b | 58 | public function __construct(ConfigManager $conf, History $history, Mutex $mutex, bool $isLoggedIn) |
336a28fa A |
59 | { |
60 | $this->conf = $conf; | |
61 | $this->history = $history; | |
fd1ddad9 | 62 | $this->mutex = $mutex; |
c4d5be53 | 63 | $this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache'), $isLoggedIn); |
fd1ddad9 | 64 | $this->bookmarksIO = new BookmarkIO($this->conf, $this->mutex); |
336a28fa A |
65 | $this->isLoggedIn = $isLoggedIn; |
66 | ||
67 | if (!$this->isLoggedIn && $this->conf->get('privacy.hide_public_links', false)) { | |
68 | $this->bookmarks = []; | |
69 | } else { | |
70 | try { | |
71 | $this->bookmarks = $this->bookmarksIO->read(); | |
d6e5f04d | 72 | } catch (EmptyDataStoreException|DatastoreNotInitializedException $e) { |
336a28fa | 73 | $this->bookmarks = new BookmarkArray(); |
d6e5f04d | 74 | |
c4ad3d4f | 75 | if ($this->isLoggedIn) { |
d6e5f04d A |
76 | // Datastore file does not exists, we initialize it with default bookmarks. |
77 | if ($e instanceof DatastoreNotInitializedException) { | |
78 | $this->initialize(); | |
79 | } else { | |
80 | $this->save(); | |
81 | } | |
336a28fa A |
82 | } |
83 | } | |
84 | ||
85 | if (! $this->bookmarks instanceof BookmarkArray) { | |
86 | $this->migrate(); | |
87 | exit( | |
88 | 'Your data store has been migrated, please reload the page.'. PHP_EOL . | |
89 | 'If this message keeps showing up, please delete data/updates.txt file.' | |
90 | ); | |
91 | } | |
92 | } | |
93 | ||
b3bd8c3e | 94 | $this->bookmarkFilter = new BookmarkFilter($this->bookmarks, $this->conf); |
336a28fa A |
95 | } |
96 | ||
97 | /** | |
98 | * @inheritDoc | |
99 | */ | |
9c04921a | 100 | public function findByHash(string $hash, string $privateKey = null): Bookmark |
336a28fa A |
101 | { |
102 | $bookmark = $this->bookmarkFilter->filter(BookmarkFilter::$FILTER_HASH, $hash); | |
103 | // PHP 7.3 introduced array_key_first() to avoid this hack | |
104 | $first = reset($bookmark); | |
9c04921a A |
105 | if (!$this->isLoggedIn |
106 | && $first->isPrivate() | |
107 | && (empty($privateKey) || $privateKey !== $first->getAdditionalContentEntry('private_key')) | |
108 | ) { | |
156061d4 | 109 | throw new BookmarkNotFoundException(); |
336a28fa A |
110 | } |
111 | ||
1a8ac737 | 112 | return $first; |
336a28fa A |
113 | } |
114 | ||
115 | /** | |
116 | * @inheritDoc | |
117 | */ | |
efb7d21b | 118 | public function findByUrl(string $url): ?Bookmark |
336a28fa A |
119 | { |
120 | return $this->bookmarks->getByUrl($url); | |
121 | } | |
122 | ||
123 | /** | |
124 | * @inheritDoc | |
125 | */ | |
a8e210fa | 126 | public function search( |
efb7d21b A |
127 | array $request = [], |
128 | string $visibility = null, | |
129 | bool $caseSensitive = false, | |
130 | bool $untaggedOnly = false, | |
a8e210fa A |
131 | bool $ignoreSticky = false |
132 | ) { | |
336a28fa A |
133 | if ($visibility === null) { |
134 | $visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC; | |
135 | } | |
136 | ||
137 | // Filter bookmark database according to parameters. | |
efb7d21b A |
138 | $searchTags = isset($request['searchtags']) ? $request['searchtags'] : ''; |
139 | $searchTerm = isset($request['searchterm']) ? $request['searchterm'] : ''; | |
336a28fa | 140 | |
a8e210fa A |
141 | if ($ignoreSticky) { |
142 | $this->bookmarks->reorder('DESC', true); | |
143 | } | |
144 | ||
336a28fa A |
145 | return $this->bookmarkFilter->filter( |
146 | BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT, | |
efb7d21b | 147 | [$searchTags, $searchTerm], |
336a28fa A |
148 | $caseSensitive, |
149 | $visibility, | |
150 | $untaggedOnly | |
151 | ); | |
152 | } | |
153 | ||
154 | /** | |
155 | * @inheritDoc | |
156 | */ | |
efb7d21b | 157 | public function get(int $id, string $visibility = null): Bookmark |
336a28fa A |
158 | { |
159 | if (! isset($this->bookmarks[$id])) { | |
160 | throw new BookmarkNotFoundException(); | |
161 | } | |
162 | ||
163 | if ($visibility === null) { | |
a39acb25 | 164 | $visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC; |
336a28fa A |
165 | } |
166 | ||
167 | $bookmark = $this->bookmarks[$id]; | |
168 | if (($bookmark->isPrivate() && $visibility != 'all' && $visibility != 'private') | |
169 | || (! $bookmark->isPrivate() && $visibility != 'all' && $visibility != 'public') | |
170 | ) { | |
171 | throw new Exception('Unauthorized'); | |
172 | } | |
173 | ||
174 | return $bookmark; | |
175 | } | |
176 | ||
177 | /** | |
178 | * @inheritDoc | |
179 | */ | |
efb7d21b | 180 | public function set(Bookmark $bookmark, bool $save = true): Bookmark |
336a28fa | 181 | { |
d6e5f04d | 182 | if (true !== $this->isLoggedIn) { |
336a28fa A |
183 | throw new Exception(t('You\'re not authorized to alter the datastore')); |
184 | } | |
336a28fa A |
185 | if (! isset($this->bookmarks[$bookmark->getId()])) { |
186 | throw new BookmarkNotFoundException(); | |
187 | } | |
188 | $bookmark->validate(); | |
189 | ||
efb7d21b | 190 | $bookmark->setUpdated(new DateTime()); |
336a28fa A |
191 | $this->bookmarks[$bookmark->getId()] = $bookmark; |
192 | if ($save === true) { | |
193 | $this->save(); | |
194 | $this->history->updateLink($bookmark); | |
195 | } | |
196 | return $this->bookmarks[$bookmark->getId()]; | |
197 | } | |
198 | ||
199 | /** | |
200 | * @inheritDoc | |
201 | */ | |
efb7d21b | 202 | public function add(Bookmark $bookmark, bool $save = true): Bookmark |
336a28fa | 203 | { |
d6e5f04d | 204 | if (true !== $this->isLoggedIn) { |
336a28fa A |
205 | throw new Exception(t('You\'re not authorized to alter the datastore')); |
206 | } | |
efb7d21b | 207 | if (!empty($bookmark->getId())) { |
336a28fa A |
208 | throw new Exception(t('This bookmarks already exists')); |
209 | } | |
210 | $bookmark->setId($this->bookmarks->getNextId()); | |
211 | $bookmark->validate(); | |
212 | ||
213 | $this->bookmarks[$bookmark->getId()] = $bookmark; | |
214 | if ($save === true) { | |
215 | $this->save(); | |
216 | $this->history->addLink($bookmark); | |
217 | } | |
218 | return $this->bookmarks[$bookmark->getId()]; | |
219 | } | |
220 | ||
221 | /** | |
222 | * @inheritDoc | |
223 | */ | |
efb7d21b | 224 | public function addOrSet(Bookmark $bookmark, bool $save = true): Bookmark |
336a28fa | 225 | { |
d6e5f04d | 226 | if (true !== $this->isLoggedIn) { |
336a28fa A |
227 | throw new Exception(t('You\'re not authorized to alter the datastore')); |
228 | } | |
336a28fa A |
229 | if ($bookmark->getId() === null) { |
230 | return $this->add($bookmark, $save); | |
231 | } | |
232 | return $this->set($bookmark, $save); | |
233 | } | |
234 | ||
235 | /** | |
236 | * @inheritDoc | |
237 | */ | |
efb7d21b | 238 | public function remove(Bookmark $bookmark, bool $save = true): void |
336a28fa | 239 | { |
d6e5f04d | 240 | if (true !== $this->isLoggedIn) { |
336a28fa A |
241 | throw new Exception(t('You\'re not authorized to alter the datastore')); |
242 | } | |
336a28fa A |
243 | if (! isset($this->bookmarks[$bookmark->getId()])) { |
244 | throw new BookmarkNotFoundException(); | |
245 | } | |
246 | ||
247 | unset($this->bookmarks[$bookmark->getId()]); | |
248 | if ($save === true) { | |
249 | $this->save(); | |
250 | $this->history->deleteLink($bookmark); | |
251 | } | |
252 | } | |
253 | ||
254 | /** | |
255 | * @inheritDoc | |
256 | */ | |
efb7d21b | 257 | public function exists(int $id, string $visibility = null): bool |
336a28fa A |
258 | { |
259 | if (! isset($this->bookmarks[$id])) { | |
260 | return false; | |
261 | } | |
262 | ||
263 | if ($visibility === null) { | |
264 | $visibility = $this->isLoggedIn ? 'all' : 'public'; | |
265 | } | |
266 | ||
267 | $bookmark = $this->bookmarks[$id]; | |
268 | if (($bookmark->isPrivate() && $visibility != 'all' && $visibility != 'private') | |
269 | || (! $bookmark->isPrivate() && $visibility != 'all' && $visibility != 'public') | |
270 | ) { | |
271 | return false; | |
272 | } | |
273 | ||
274 | return true; | |
275 | } | |
276 | ||
277 | /** | |
278 | * @inheritDoc | |
279 | */ | |
efb7d21b | 280 | public function count(string $visibility = null): int |
336a28fa A |
281 | { |
282 | return count($this->search([], $visibility)); | |
283 | } | |
284 | ||
285 | /** | |
286 | * @inheritDoc | |
287 | */ | |
efb7d21b | 288 | public function save(): void |
336a28fa | 289 | { |
d6e5f04d | 290 | if (true !== $this->isLoggedIn) { |
336a28fa A |
291 | // TODO: raise an Exception instead |
292 | die('You are not authorized to change the database.'); | |
293 | } | |
c4ad3d4f | 294 | |
336a28fa A |
295 | $this->bookmarks->reorder(); |
296 | $this->bookmarksIO->write($this->bookmarks); | |
b0428aa9 | 297 | $this->pageCacheManager->invalidateCaches(); |
336a28fa A |
298 | } |
299 | ||
300 | /** | |
301 | * @inheritDoc | |
302 | */ | |
efb7d21b | 303 | public function bookmarksCountPerTag(array $filteringTags = [], string $visibility = null): array |
336a28fa A |
304 | { |
305 | $bookmarks = $this->search(['searchtags' => $filteringTags], $visibility); | |
306 | $tags = []; | |
307 | $caseMapping = []; | |
308 | foreach ($bookmarks as $bookmark) { | |
309 | foreach ($bookmark->getTags() as $tag) { | |
a39acb25 A |
310 | if (empty($tag) |
311 | || (! $this->isLoggedIn && startsWith($tag, '.')) | |
312 | || $tag === BookmarkMarkdownFormatter::NO_MD_TAG | |
c79473bd | 313 | || in_array($tag, $filteringTags, true) |
a39acb25 | 314 | ) { |
336a28fa A |
315 | continue; |
316 | } | |
a39acb25 | 317 | |
336a28fa A |
318 | // The first case found will be displayed. |
319 | if (!isset($caseMapping[strtolower($tag)])) { | |
320 | $caseMapping[strtolower($tag)] = $tag; | |
321 | $tags[$caseMapping[strtolower($tag)]] = 0; | |
322 | } | |
323 | $tags[$caseMapping[strtolower($tag)]]++; | |
324 | } | |
325 | } | |
326 | ||
327 | /* | |
328 | * Formerly used arsort(), which doesn't define the sort behaviour for equal values. | |
329 | * Also, this function doesn't produce the same result between PHP 5.6 and 7. | |
330 | * | |
331 | * So we now use array_multisort() to sort tags by DESC occurrences, | |
332 | * then ASC alphabetically for equal values. | |
333 | * | |
334 | * @see https://github.com/shaarli/Shaarli/issues/1142 | |
335 | */ | |
336 | $keys = array_keys($tags); | |
337 | $tmpTags = array_combine($keys, $keys); | |
338 | array_multisort($tags, SORT_DESC, $tmpTags, SORT_ASC, $tags); | |
efb7d21b | 339 | |
336a28fa A |
340 | return $tags; |
341 | } | |
342 | ||
343 | /** | |
344 | * @inheritDoc | |
345 | */ | |
36e6d88d A |
346 | public function findByDate( |
347 | \DateTimeInterface $from, | |
348 | \DateTimeInterface $to, | |
349 | ?\DateTimeInterface &$previous, | |
350 | ?\DateTimeInterface &$next | |
351 | ): array { | |
352 | $out = []; | |
353 | $previous = null; | |
354 | $next = null; | |
355 | ||
356 | foreach ($this->search([], null, false, false, true) as $bookmark) { | |
357 | if ($to < $bookmark->getCreated()) { | |
358 | $next = $bookmark->getCreated(); | |
359 | } else if ($from < $bookmark->getCreated() && $to > $bookmark->getCreated()) { | |
360 | $out[] = $bookmark; | |
361 | } else { | |
362 | if ($previous !== null) { | |
363 | break; | |
364 | } | |
365 | $previous = $bookmark->getCreated(); | |
366 | } | |
336a28fa | 367 | } |
336a28fa | 368 | |
36e6d88d | 369 | return $out; |
336a28fa A |
370 | } |
371 | ||
372 | /** | |
373 | * @inheritDoc | |
374 | */ | |
36e6d88d | 375 | public function getLatest(): ?Bookmark |
336a28fa | 376 | { |
36e6d88d A |
377 | foreach ($this->search([], null, false, false, true) as $bookmark) { |
378 | return $bookmark; | |
379 | } | |
27ddfec3 | 380 | |
36e6d88d | 381 | return null; |
336a28fa A |
382 | } |
383 | ||
384 | /** | |
385 | * @inheritDoc | |
386 | */ | |
efb7d21b | 387 | public function initialize(): void |
336a28fa A |
388 | { |
389 | $initializer = new BookmarkInitializer($this); | |
390 | $initializer->initialize(); | |
336a28fa | 391 | |
d6e5f04d A |
392 | if (true === $this->isLoggedIn) { |
393 | $this->save(); | |
394 | } | |
c4ad3d4f A |
395 | } |
396 | ||
336a28fa A |
397 | /** |
398 | * Handles migration to the new database format (BookmarksArray). | |
399 | */ | |
efb7d21b | 400 | protected function migrate(): void |
336a28fa A |
401 | { |
402 | $bookmarkDb = new LegacyLinkDB( | |
403 | $this->conf->get('resource.datastore'), | |
404 | true, | |
405 | false | |
406 | ); | |
407 | $updater = new LegacyUpdater( | |
408 | UpdaterUtils::read_updates_file($this->conf->get('resource.updates')), | |
409 | $bookmarkDb, | |
410 | $this->conf, | |
411 | true | |
412 | ); | |
413 | $newUpdates = $updater->update(); | |
414 | if (! empty($newUpdates)) { | |
415 | UpdaterUtils::write_updates_file( | |
416 | $this->conf->get('resource.updates'), | |
417 | $updater->getDoneUpdates() | |
418 | ); | |
419 | } | |
420 | } | |
421 | } |