$formatter = $this->container->formatterFactory->getFormatter();
$formatter->addContextData('base_path', $this->container->basePath);
+ $formatter->addContextData('index_url', index_url($this->container->environment));
$searchTags = normalize_spaces($request->getParam('searchtags') ?? '');
- $searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));;
+ $searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));
// Filter bookmarks according search parameters.
$visibility = $this->container->sessionManager->getSessionParameter('visibility');
'searchtags' => $searchTags,
'searchterm' => $searchTerm,
];
- $linksToDisplay = $this->container->bookmarkService->search(
- $search,
- $visibility,
- false,
- !!$this->container->sessionManager->getSessionParameter('untaggedonly')
- ) ?? [];
-
- // ---- Handle paging.
- $keys = [];
- foreach ($linksToDisplay as $key => $value) {
- $keys[] = $key;
- }
-
- $linksPerPage = $this->container->sessionManager->getSessionParameter('LINKS_PER_PAGE', 20) ?: 20;
// Select articles according to paging.
- $pageCount = (int) ceil(count($keys) / $linksPerPage) ?: 1;
- $page = (int) $request->getParam('page') ?? 1;
+ $page = (int) ($request->getParam('page') ?? 1);
$page = $page < 1 ? 1 : $page;
- $page = $page > $pageCount ? $pageCount : $page;
+ $linksPerPage = $this->container->sessionManager->getSessionParameter('LINKS_PER_PAGE', 20) ?: 20;
- // Start index.
- $i = ($page - 1) * $linksPerPage;
- $end = $i + $linksPerPage;
+ $searchResult = $this->container->bookmarkService->search(
+ $search,
+ $visibility,
+ false,
+ !!$this->container->sessionManager->getSessionParameter('untaggedonly'),
+ false,
+ ['offset' => $linksPerPage * ($page - 1), 'limit' => $linksPerPage]
+ ) ?? [];
- $linkDisp = [];
$save = false;
- while ($i < $end && $i < count($keys)) {
- $save = $this->updateThumbnail($linksToDisplay[$keys[$i]], false) || $save;
- $link = $formatter->format($linksToDisplay[$keys[$i]]);
-
- $linkDisp[$keys[$i]] = $link;
- $i++;
+ $links = [];
+ foreach ($searchResult->getBookmarks() as $key => $bookmark) {
+ $save = $this->updateThumbnail($bookmark, false) || $save;
+ $links[$key] = $formatter->format($bookmark);
}
if ($save) {
// Compute paging navigation
$searchtagsUrl = $searchTags === '' ? '' : '&searchtags=' . urlencode($searchTags);
$searchtermUrl = $searchTerm === '' ? '' : '&searchterm=' . urlencode($searchTerm);
+ $page = $searchResult->getPage();
- $previous_page_url = '';
- if ($i !== count($keys)) {
- $previous_page_url = '?page=' . ($page + 1) . $searchtermUrl . $searchtagsUrl;
- }
- $next_page_url = '';
- if ($page > 1) {
- $next_page_url = '?page=' . ($page - 1) . $searchtermUrl . $searchtagsUrl;
- }
+ $previousPageUrl = !$searchResult->isLastPage() ? '?page=' . ($page + 1) . $searchtermUrl . $searchtagsUrl : '';
+ $nextPageUrl = !$searchResult->isFirstPage() ? '?page=' . ($page - 1) . $searchtermUrl . $searchtagsUrl : '';
+
+ $tagsSeparator = $this->container->conf->get('general.tags_separator', ' ');
+ $searchTagsUrlEncoded = array_map('urlencode', tags_str2array($searchTags, $tagsSeparator));
+ $searchTags = !empty($searchTags) ? trim($searchTags, $tagsSeparator) . $tagsSeparator : '';
// Fill all template fields.
$data = array_merge(
$this->initializeTemplateVars(),
[
- 'previous_page_url' => $previous_page_url,
- 'next_page_url' => $next_page_url,
+ 'previous_page_url' => $previousPageUrl,
+ 'next_page_url' => $nextPageUrl,
'page_current' => $page,
- 'page_max' => $pageCount,
- 'result_count' => count($linksToDisplay),
+ 'page_max' => $searchResult->getLastPage(),
+ 'result_count' => $searchResult->getTotalCount(),
'search_term' => escape($searchTerm),
'search_tags' => escape($searchTags),
- 'search_tags_url' => array_map('urlencode', explode(' ', $searchTags)),
+ 'search_tags_url' => $searchTagsUrlEncoded,
'visibility' => $visibility,
- 'links' => $linkDisp,
+ 'links' => $links,
]
);
return '[' . $tag . ']';
};
$data['pagetitle'] .= ! empty($searchTags)
- ? implode(' ', array_map($bracketWrap, preg_split('/\s+/', $searchTags))) . ' '
- : '';
+ ? implode(' ', array_map($bracketWrap, tags_str2array($searchTags, $tagsSeparator))) . ' '
+ : ''
+ ;
$data['pagetitle'] .= '- ';
}
*/
public function permalink(Request $request, Response $response, array $args): Response
{
+ $privateKey = $request->getParam('key');
+
try {
- $bookmark = $this->container->bookmarkService->findByHash($args['hash']);
+ $bookmark = $this->container->bookmarkService->findByHash($args['hash'], $privateKey);
} catch (BookmarkNotFoundException $e) {
$this->assignView('error_message', $e->getMessage());
$formatter = $this->container->formatterFactory->getFormatter();
$formatter->addContextData('base_path', $this->container->basePath);
+ $formatter->addContextData('index_url', index_url($this->container->environment));
$data = array_merge(
$this->initializeTemplateVars(),
[
- 'pagetitle' => $bookmark->getTitle() .' - '. $this->container->conf->get('general.title', 'Shaarli'),
+ 'pagetitle' => $bookmark->getTitle() . ' - ' . $this->container->conf->get('general.title', 'Shaarli'),
'links' => [$formatter->format($bookmark)],
]
);
*/
protected function updateThumbnail(Bookmark $bookmark, bool $writeDatastore = true): bool
{
- // Logged in, thumbnails enabled, not a note, is HTTP
- // and (never retrieved yet or no valid cache file)
- if ($this->container->loginManager->isLoggedIn()
- && $this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
- && false !== $bookmark->getThumbnail()
- && !$bookmark->isNote()
- && (null === $bookmark->getThumbnail() || !is_file($bookmark->getThumbnail()))
- && startsWith(strtolower($bookmark->getUrl()), 'http')
- ) {
- $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl()));
- $this->container->bookmarkService->set($bookmark, $writeDatastore);
-
- return true;
+ if (false === $this->container->loginManager->isLoggedIn()) {
+ return false;
+ }
+
+ // If thumbnail should be updated, we reset it to null
+ if ($bookmark->shouldUpdateThumbnail()) {
+ $bookmark->setThumbnail(null);
+
+ // Requires an update, not async retrieval, thumbnails enabled
+ if (
+ $bookmark->shouldUpdateThumbnail()
+ && true !== $this->container->conf->get('general.enable_async_metadata', true)
+ && $this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
+ ) {
+ $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl()));
+ $this->container->bookmarkService->set($bookmark, $writeDatastore);
+
+ return true;
+ }
}
return false;
'page_max' => '',
'search_tags' => '',
'result_count' => '',
+ 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true)
];
}