X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2Fbookmark%2FBookmarkFileService.php;h=c9ec260930d159f8603f1f5c796fbec5612c08c2;hb=2785d85e0a7e952fe2de349659b36091fd5f1d51;hp=a56cc92b4067cc4f3fa0f0491e8c912404c9bfe4;hpb=3fb29fdda04ca86e04422d49b86cf646d53c4f9d;p=github%2Fshaarli%2FShaarli.git diff --git a/application/bookmark/BookmarkFileService.php b/application/bookmark/BookmarkFileService.php index a56cc92b..c9ec2609 100644 --- a/application/bookmark/BookmarkFileService.php +++ b/application/bookmark/BookmarkFileService.php @@ -6,11 +6,14 @@ namespace Shaarli\Bookmark; use Exception; use Shaarli\Bookmark\Exception\BookmarkNotFoundException; +use Shaarli\Bookmark\Exception\DatastoreNotInitializedException; use Shaarli\Bookmark\Exception\EmptyDataStoreException; use Shaarli\Config\ConfigManager; +use Shaarli\Formatter\BookmarkMarkdownFormatter; use Shaarli\History; use Shaarli\Legacy\LegacyLinkDB; use Shaarli\Legacy\LegacyUpdater; +use Shaarli\Render\PageCacheManager; use Shaarli\Updater\UpdaterUtils; /** @@ -38,6 +41,9 @@ class BookmarkFileService implements BookmarkServiceInterface /** @var History instance */ protected $history; + /** @var PageCacheManager instance */ + protected $pageCacheManager; + /** @var bool true for logged in users. Default value to retrieve private bookmarks. */ protected $isLoggedIn; @@ -48,6 +54,7 @@ class BookmarkFileService implements BookmarkServiceInterface { $this->conf = $conf; $this->history = $history; + $this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache'), $isLoggedIn); $this->bookmarksIO = new BookmarkIO($this->conf); $this->isLoggedIn = $isLoggedIn; @@ -56,10 +63,16 @@ class BookmarkFileService implements BookmarkServiceInterface } else { try { $this->bookmarks = $this->bookmarksIO->read(); - } catch (EmptyDataStoreException $e) { + } catch (EmptyDataStoreException|DatastoreNotInitializedException $e) { $this->bookmarks = new BookmarkArray(); - if ($isLoggedIn) { - $this->save(); + + if ($this->isLoggedIn) { + // Datastore file does not exists, we initialize it with default bookmarks. + if ($e instanceof DatastoreNotInitializedException) { + $this->initialize(); + } else { + $this->save(); + } } } @@ -87,7 +100,7 @@ class BookmarkFileService implements BookmarkServiceInterface throw new Exception('Not authorized'); } - return $bookmark; + return $first; } /** @@ -101,8 +114,13 @@ class BookmarkFileService implements BookmarkServiceInterface /** * @inheritDoc */ - public function search($request = [], $visibility = null, $caseSensitive = false, $untaggedOnly = false) - { + public function search( + $request = [], + $visibility = null, + $caseSensitive = false, + $untaggedOnly = false, + bool $ignoreSticky = false + ) { if ($visibility === null) { $visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC; } @@ -111,6 +129,10 @@ class BookmarkFileService implements BookmarkServiceInterface $searchtags = isset($request['searchtags']) ? $request['searchtags'] : ''; $searchterm = isset($request['searchterm']) ? $request['searchterm'] : ''; + if ($ignoreSticky) { + $this->bookmarks->reorder('DESC', true); + } + return $this->bookmarkFilter->filter( BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT, [$searchtags, $searchterm], @@ -130,7 +152,7 @@ class BookmarkFileService implements BookmarkServiceInterface } if ($visibility === null) { - $visibility = $this->isLoggedIn ? 'all' : 'public'; + $visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC; } $bookmark = $this->bookmarks[$id]; @@ -148,7 +170,7 @@ class BookmarkFileService implements BookmarkServiceInterface */ public function set($bookmark, $save = true) { - if ($this->isLoggedIn !== true) { + if (true !== $this->isLoggedIn) { throw new Exception(t('You\'re not authorized to alter the datastore')); } if (! $bookmark instanceof Bookmark) { @@ -173,7 +195,7 @@ class BookmarkFileService implements BookmarkServiceInterface */ public function add($bookmark, $save = true) { - if ($this->isLoggedIn !== true) { + if (true !== $this->isLoggedIn) { throw new Exception(t('You\'re not authorized to alter the datastore')); } if (! $bookmark instanceof Bookmark) { @@ -198,7 +220,7 @@ class BookmarkFileService implements BookmarkServiceInterface */ public function addOrSet($bookmark, $save = true) { - if ($this->isLoggedIn !== true) { + if (true !== $this->isLoggedIn) { throw new Exception(t('You\'re not authorized to alter the datastore')); } if (! $bookmark instanceof Bookmark) { @@ -215,7 +237,7 @@ class BookmarkFileService implements BookmarkServiceInterface */ public function remove($bookmark, $save = true) { - if ($this->isLoggedIn !== true) { + if (true !== $this->isLoggedIn) { throw new Exception(t('You\'re not authorized to alter the datastore')); } if (! $bookmark instanceof Bookmark) { @@ -268,13 +290,14 @@ class BookmarkFileService implements BookmarkServiceInterface */ public function save() { - if (!$this->isLoggedIn) { + if (true !== $this->isLoggedIn) { // TODO: raise an Exception instead die('You are not authorized to change the database.'); } + $this->bookmarks->reorder(); $this->bookmarksIO->write($this->bookmarks); - invalidateCaches($this->conf->get('resource.page_cache')); + $this->pageCacheManager->invalidateCaches(); } /** @@ -287,9 +310,14 @@ class BookmarkFileService implements BookmarkServiceInterface $caseMapping = []; foreach ($bookmarks as $bookmark) { foreach ($bookmark->getTags() as $tag) { - if (empty($tag) || (! $this->isLoggedIn && startsWith($tag, '.'))) { + if (empty($tag) + || (! $this->isLoggedIn && startsWith($tag, '.')) + || $tag === BookmarkMarkdownFormatter::NO_MD_TAG + || in_array($tag, $filteringTags, true) + ) { continue; } + // The first case found will be displayed. if (!isset($caseMapping[strtolower($tag)])) { $caseMapping[strtolower($tag)] = $tag; @@ -334,7 +362,9 @@ class BookmarkFileService implements BookmarkServiceInterface */ public function filterDay($request) { - return $this->bookmarkFilter->filter(BookmarkFilter::$FILTER_DAY, $request); + $visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC; + + return $this->bookmarkFilter->filter(BookmarkFilter::$FILTER_DAY, $request, false, $visibility); } /** @@ -344,6 +374,10 @@ class BookmarkFileService implements BookmarkServiceInterface { $initializer = new BookmarkInitializer($this); $initializer->initialize(); + + if (true === $this->isLoggedIn) { + $this->save(); + } } /**