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;
/**
/** @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;
{
$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;
} 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();
+ }
}
}
throw new Exception('Not authorized');
}
- return $bookmark;
+ return $first;
}
/**
}
if ($visibility === null) {
- $visibility = $this->isLoggedIn ? 'all' : 'public';
+ $visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC;
}
$bookmark = $this->bookmarks[$id];
*/
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) {
*/
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) {
*/
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) {
*/
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) {
*/
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();
}
/**
$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;
{
$initializer = new BookmarkInitializer($this);
$initializer->initialize();
+
+ if (true === $this->isLoggedIn) {
+ $this->save();
+ }
}
/**