aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/bookmark/BookmarkFileService.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-08-27 10:27:34 +0200
committerGitHub <noreply@github.com>2020-08-27 10:27:34 +0200
commitaf41d5ab5d2bd3ba64d052c997bc6afa6966a63c (patch)
tree8fad2829c55f94022e359fa8914e11f80a2afc2a /application/bookmark/BookmarkFileService.php
parentb8e3630f2ecd142d397b1b062a346a667bb78595 (diff)
parent0c6fdbe12bbbb336348666b14b82096f24d5858b (diff)
downloadShaarli-af41d5ab5d2bd3ba64d052c997bc6afa6966a63c.tar.gz
Shaarli-af41d5ab5d2bd3ba64d052c997bc6afa6966a63c.tar.zst
Shaarli-af41d5ab5d2bd3ba64d052c997bc6afa6966a63c.zip
Merge pull request #1511 from ArthurHoaro/wip-slim-routing
Diffstat (limited to 'application/bookmark/BookmarkFileService.php')
-rw-r--r--application/bookmark/BookmarkFileService.php38
1 files changed, 28 insertions, 10 deletions
diff --git a/application/bookmark/BookmarkFileService.php b/application/bookmark/BookmarkFileService.php
index 9c59e139..b3a90ed4 100644
--- a/application/bookmark/BookmarkFileService.php
+++ b/application/bookmark/BookmarkFileService.php
@@ -6,12 +6,14 @@ namespace Shaarli\Bookmark;
6 6
7use Exception; 7use Exception;
8use Shaarli\Bookmark\Exception\BookmarkNotFoundException; 8use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
9use Shaarli\Bookmark\Exception\DatastoreNotInitializedException;
9use Shaarli\Bookmark\Exception\EmptyDataStoreException; 10use Shaarli\Bookmark\Exception\EmptyDataStoreException;
10use Shaarli\Config\ConfigManager; 11use Shaarli\Config\ConfigManager;
11use Shaarli\Formatter\BookmarkMarkdownFormatter; 12use Shaarli\Formatter\BookmarkMarkdownFormatter;
12use Shaarli\History; 13use Shaarli\History;
13use Shaarli\Legacy\LegacyLinkDB; 14use Shaarli\Legacy\LegacyLinkDB;
14use Shaarli\Legacy\LegacyUpdater; 15use Shaarli\Legacy\LegacyUpdater;
16use Shaarli\Render\PageCacheManager;
15use Shaarli\Updater\UpdaterUtils; 17use Shaarli\Updater\UpdaterUtils;
16 18
17/** 19/**
@@ -39,6 +41,9 @@ class BookmarkFileService implements BookmarkServiceInterface
39 /** @var History instance */ 41 /** @var History instance */
40 protected $history; 42 protected $history;
41 43
44 /** @var PageCacheManager instance */
45 protected $pageCacheManager;
46
42 /** @var bool true for logged in users. Default value to retrieve private bookmarks. */ 47 /** @var bool true for logged in users. Default value to retrieve private bookmarks. */
43 protected $isLoggedIn; 48 protected $isLoggedIn;
44 49
@@ -49,6 +54,7 @@ class BookmarkFileService implements BookmarkServiceInterface
49 { 54 {
50 $this->conf = $conf; 55 $this->conf = $conf;
51 $this->history = $history; 56 $this->history = $history;
57 $this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache'), $isLoggedIn);
52 $this->bookmarksIO = new BookmarkIO($this->conf); 58 $this->bookmarksIO = new BookmarkIO($this->conf);
53 $this->isLoggedIn = $isLoggedIn; 59 $this->isLoggedIn = $isLoggedIn;
54 60
@@ -57,10 +63,16 @@ class BookmarkFileService implements BookmarkServiceInterface
57 } else { 63 } else {
58 try { 64 try {
59 $this->bookmarks = $this->bookmarksIO->read(); 65 $this->bookmarks = $this->bookmarksIO->read();
60 } catch (EmptyDataStoreException $e) { 66 } catch (EmptyDataStoreException|DatastoreNotInitializedException $e) {
61 $this->bookmarks = new BookmarkArray(); 67 $this->bookmarks = new BookmarkArray();
62 if ($isLoggedIn) { 68
63 $this->save(); 69 if ($this->isLoggedIn) {
70 // Datastore file does not exists, we initialize it with default bookmarks.
71 if ($e instanceof DatastoreNotInitializedException) {
72 $this->initialize();
73 } else {
74 $this->save();
75 }
64 } 76 }
65 } 77 }
66 78
@@ -88,7 +100,7 @@ class BookmarkFileService implements BookmarkServiceInterface
88 throw new Exception('Not authorized'); 100 throw new Exception('Not authorized');
89 } 101 }
90 102
91 return $bookmark; 103 return $first;
92 } 104 }
93 105
94 /** 106 /**
@@ -149,7 +161,7 @@ class BookmarkFileService implements BookmarkServiceInterface
149 */ 161 */
150 public function set($bookmark, $save = true) 162 public function set($bookmark, $save = true)
151 { 163 {
152 if ($this->isLoggedIn !== true) { 164 if (true !== $this->isLoggedIn) {
153 throw new Exception(t('You\'re not authorized to alter the datastore')); 165 throw new Exception(t('You\'re not authorized to alter the datastore'));
154 } 166 }
155 if (! $bookmark instanceof Bookmark) { 167 if (! $bookmark instanceof Bookmark) {
@@ -174,7 +186,7 @@ class BookmarkFileService implements BookmarkServiceInterface
174 */ 186 */
175 public function add($bookmark, $save = true) 187 public function add($bookmark, $save = true)
176 { 188 {
177 if ($this->isLoggedIn !== true) { 189 if (true !== $this->isLoggedIn) {
178 throw new Exception(t('You\'re not authorized to alter the datastore')); 190 throw new Exception(t('You\'re not authorized to alter the datastore'));
179 } 191 }
180 if (! $bookmark instanceof Bookmark) { 192 if (! $bookmark instanceof Bookmark) {
@@ -199,7 +211,7 @@ class BookmarkFileService implements BookmarkServiceInterface
199 */ 211 */
200 public function addOrSet($bookmark, $save = true) 212 public function addOrSet($bookmark, $save = true)
201 { 213 {
202 if ($this->isLoggedIn !== true) { 214 if (true !== $this->isLoggedIn) {
203 throw new Exception(t('You\'re not authorized to alter the datastore')); 215 throw new Exception(t('You\'re not authorized to alter the datastore'));
204 } 216 }
205 if (! $bookmark instanceof Bookmark) { 217 if (! $bookmark instanceof Bookmark) {
@@ -216,7 +228,7 @@ class BookmarkFileService implements BookmarkServiceInterface
216 */ 228 */
217 public function remove($bookmark, $save = true) 229 public function remove($bookmark, $save = true)
218 { 230 {
219 if ($this->isLoggedIn !== true) { 231 if (true !== $this->isLoggedIn) {
220 throw new Exception(t('You\'re not authorized to alter the datastore')); 232 throw new Exception(t('You\'re not authorized to alter the datastore'));
221 } 233 }
222 if (! $bookmark instanceof Bookmark) { 234 if (! $bookmark instanceof Bookmark) {
@@ -269,13 +281,14 @@ class BookmarkFileService implements BookmarkServiceInterface
269 */ 281 */
270 public function save() 282 public function save()
271 { 283 {
272 if (!$this->isLoggedIn) { 284 if (true !== $this->isLoggedIn) {
273 // TODO: raise an Exception instead 285 // TODO: raise an Exception instead
274 die('You are not authorized to change the database.'); 286 die('You are not authorized to change the database.');
275 } 287 }
288
276 $this->bookmarks->reorder(); 289 $this->bookmarks->reorder();
277 $this->bookmarksIO->write($this->bookmarks); 290 $this->bookmarksIO->write($this->bookmarks);
278 invalidateCaches($this->conf->get('resource.page_cache')); 291 $this->pageCacheManager->invalidateCaches();
279 } 292 }
280 293
281 /** 294 /**
@@ -291,6 +304,7 @@ class BookmarkFileService implements BookmarkServiceInterface
291 if (empty($tag) 304 if (empty($tag)
292 || (! $this->isLoggedIn && startsWith($tag, '.')) 305 || (! $this->isLoggedIn && startsWith($tag, '.'))
293 || $tag === BookmarkMarkdownFormatter::NO_MD_TAG 306 || $tag === BookmarkMarkdownFormatter::NO_MD_TAG
307 || in_array($tag, $filteringTags, true)
294 ) { 308 ) {
295 continue; 309 continue;
296 } 310 }
@@ -349,6 +363,10 @@ class BookmarkFileService implements BookmarkServiceInterface
349 { 363 {
350 $initializer = new BookmarkInitializer($this); 364 $initializer = new BookmarkInitializer($this);
351 $initializer->initialize(); 365 $initializer->initialize();
366
367 if (true === $this->isLoggedIn) {
368 $this->save();
369 }
352 } 370 }
353 371
354 /** 372 /**