Skip to content

Commit

Permalink
Merge pull request #3 from shaarli/master
Browse files Browse the repository at this point in the history
Merge upstream
  • Loading branch information
yude authored Apr 4, 2021
2 parents bf02f8b + 2c2c349 commit 0a47426
Show file tree
Hide file tree
Showing 71 changed files with 2,614 additions and 1,003 deletions.
1 change: 1 addition & 0 deletions application/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ function format_date($date, $time = true, $intl = true)
IntlDateFormatter::LONG,
$time ? IntlDateFormatter::LONG : IntlDateFormatter::NONE
);
$formatter->setTimeZone($date->getTimezone());

return $formatter->format($date);
}
Expand Down
1 change: 1 addition & 0 deletions application/api/ApiMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ protected function setLinkDb($conf)
{
$linkDb = new BookmarkFileService(
$conf,
$this->container->get('pluginManager'),
$this->container->get('history'),
new FlockMutex(fopen(SHAARLI_MUTEX_FILE, 'r'), 2),
true
Expand Down
38 changes: 19 additions & 19 deletions application/api/controllers/Links.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,13 @@ class Links extends ApiController
public function getLinks($request, $response)
{
$private = $request->getParam('visibility');
$bookmarks = $this->bookmarkService->search(
[
'searchtags' => $request->getParam('searchtags', ''),
'searchterm' => $request->getParam('searchterm', ''),
],
$private
);

// Return bookmarks from the {offset}th link, starting from 0.
$offset = $request->getParam('offset');
if (! empty($offset) && ! ctype_digit($offset)) {
throw new ApiBadParametersException('Invalid offset');
}
$offset = ! empty($offset) ? intval($offset) : 0;
if ($offset > count($bookmarks)) {
return $response->withJson([], 200, $this->jsonStyle);
}

// limit parameter is either a number of bookmarks or 'all' for everything.
$limit = $request->getParam('limit');
Expand All @@ -61,23 +51,33 @@ public function getLinks($request, $response)
} elseif (ctype_digit($limit)) {
$limit = intval($limit);
} elseif ($limit === 'all') {
$limit = count($bookmarks);
$limit = null;
} else {
throw new ApiBadParametersException('Invalid limit');
}

$searchResult = $this->bookmarkService->search(
[
'searchtags' => $request->getParam('searchtags', ''),
'searchterm' => $request->getParam('searchterm', ''),
],
$private,
false,
false,
false,
[
'limit' => $limit,
'offset' => $offset,
'allowOutOfBounds' => true,
]
);

// 'environment' is set by Slim and encapsulate $_SERVER.
$indexUrl = index_url($this->ci['environment']);

$out = [];
$index = 0;
foreach ($bookmarks as $bookmark) {
if (count($out) >= $limit) {
break;
}
if ($index++ >= $offset) {
$out[] = ApiUtils::formatLink($bookmark, $indexUrl);
}
foreach ($searchResult->getBookmarks() as $bookmark) {
$out[] = ApiUtils::formatLink($bookmark, $indexUrl);
}

return $response->withJson($out, 200, $this->jsonStyle);
Expand Down
8 changes: 4 additions & 4 deletions application/api/controllers/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ public function putTag($request, $response, $args)
throw new ApiBadParametersException('New tag name is required in the request body');
}

$bookmarks = $this->bookmarkService->search(
$searchResult = $this->bookmarkService->search(
['searchtags' => $args['tagName']],
BookmarkFilter::$ALL,
true
);
foreach ($bookmarks as $bookmark) {
foreach ($searchResult->getBookmarks() as $bookmark) {
$bookmark->renameTag($args['tagName'], $data['name']);
$this->bookmarkService->set($bookmark, false);
$this->history->updateLink($bookmark);
Expand Down Expand Up @@ -157,12 +157,12 @@ public function deleteTag($request, $response, $args)
throw new ApiTagNotFoundException();
}

$bookmarks = $this->bookmarkService->search(
$searchResult = $this->bookmarkService->search(
['searchtags' => $args['tagName']],
BookmarkFilter::$ALL,
true
);
foreach ($bookmarks as $bookmark) {
foreach ($searchResult->getBookmarks() as $bookmark) {
$bookmark->deleteTag($args['tagName']);
$this->bookmarkService->set($bookmark, false);
$this->history->updateLink($bookmark);
Expand Down
42 changes: 30 additions & 12 deletions application/bookmark/BookmarkFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Shaarli\History;
use Shaarli\Legacy\LegacyLinkDB;
use Shaarli\Legacy\LegacyUpdater;
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\PageCacheManager;
use Shaarli\Updater\UpdaterUtils;

Expand All @@ -40,6 +41,9 @@ class BookmarkFileService implements BookmarkServiceInterface
/** @var ConfigManager instance */
protected $conf;

/** @var PluginManager */
protected $pluginManager;

/** @var History instance */
protected $history;

Expand All @@ -55,8 +59,13 @@ class BookmarkFileService implements BookmarkServiceInterface
/**
* @inheritDoc
*/
public function __construct(ConfigManager $conf, History $history, Mutex $mutex, bool $isLoggedIn)
{
public function __construct(
ConfigManager $conf,
PluginManager $pluginManager,
History $history,
Mutex $mutex,
bool $isLoggedIn
) {
$this->conf = $conf;
$this->history = $history;
$this->mutex = $mutex;
Expand All @@ -65,7 +74,7 @@ public function __construct(ConfigManager $conf, History $history, Mutex $mutex,
$this->isLoggedIn = $isLoggedIn;

if (!$this->isLoggedIn && $this->conf->get('privacy.hide_public_links', false)) {
$this->bookmarks = [];
$this->bookmarks = new BookmarkArray();
} else {
try {
$this->bookmarks = $this->bookmarksIO->read();
Expand All @@ -91,7 +100,8 @@ public function __construct(ConfigManager $conf, History $history, Mutex $mutex,
}
}

$this->bookmarkFilter = new BookmarkFilter($this->bookmarks, $this->conf);
$this->pluginManager = $pluginManager;
$this->bookmarkFilter = new BookmarkFilter($this->bookmarks, $this->conf, $this->pluginManager);
}

/**
Expand Down Expand Up @@ -129,8 +139,9 @@ public function search(
string $visibility = null,
bool $caseSensitive = false,
bool $untaggedOnly = false,
bool $ignoreSticky = false
) {
bool $ignoreSticky = false,
array $pagination = []
): SearchResult {
if ($visibility === null) {
$visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC;
}
Expand All @@ -143,13 +154,20 @@ public function search(
$this->bookmarks->reorder('DESC', true);
}

return $this->bookmarkFilter->filter(
$bookmarks = $this->bookmarkFilter->filter(
BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
[$searchTags, $searchTerm],
$caseSensitive,
$visibility,
$untaggedOnly
);

return SearchResult::getSearchResult(
$bookmarks,
$pagination['offset'] ?? 0,
$pagination['limit'] ?? null,
$pagination['allowOutOfBounds'] ?? false
);
}

/**
Expand Down Expand Up @@ -282,7 +300,7 @@ public function exists(int $id, string $visibility = null): bool
*/
public function count(string $visibility = null): int
{
return count($this->search([], $visibility));
return $this->search([], $visibility)->getResultCount();
}

/**
Expand All @@ -305,10 +323,10 @@ public function save(): void
*/
public function bookmarksCountPerTag(array $filteringTags = [], string $visibility = null): array
{
$bookmarks = $this->search(['searchtags' => $filteringTags], $visibility);
$searchResult = $this->search(['searchtags' => $filteringTags], $visibility);
$tags = [];
$caseMapping = [];
foreach ($bookmarks as $bookmark) {
foreach ($searchResult->getBookmarks() as $bookmark) {
foreach ($bookmark->getTags() as $tag) {
if (
empty($tag)
Expand Down Expand Up @@ -357,7 +375,7 @@ public function findByDate(
$previous = null;
$next = null;

foreach ($this->search([], null, false, false, true) as $bookmark) {
foreach ($this->search([], null, false, false, true)->getBookmarks() as $bookmark) {
if ($to < $bookmark->getCreated()) {
$next = $bookmark->getCreated();
} elseif ($from < $bookmark->getCreated() && $to > $bookmark->getCreated()) {
Expand All @@ -378,7 +396,7 @@ public function findByDate(
*/
public function getLatest(): ?Bookmark
{
foreach ($this->search([], null, false, false, true) as $bookmark) {
foreach ($this->search([], null, false, false, true)->getBookmarks() as $bookmark) {
return $bookmark;
}

Expand Down
Loading

0 comments on commit 0a47426

Please sign in to comment.