3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
8 use Shaarli\Bookmark\Bookmark
;
9 use Shaarli\Helper\DailyPageHelper
;
10 use Shaarli\Render\TemplatePage
;
11 use Slim\Http\Request
;
12 use Slim\Http\Response
;
15 * Class DailyController
17 * Slim controller used to render the daily page.
19 class DailyController
extends ShaarliVisitorController
21 public static $DAILY_RSS_NB_DAYS = 8;
24 * Controller displaying all bookmarks published in a single day.
25 * It take a `day` date query parameter (format YYYYMMDD).
27 public function index(Request
$request, Response
$response): Response
29 $type = DailyPageHelper
::extractRequestedType($request);
30 $format = DailyPageHelper
::getFormatByType($type);
31 $latestBookmark = $this->container
->bookmarkService
->getLatest();
32 $dateTime = DailyPageHelper
::extractRequestedDateTime($type, $request->getQueryParam($type), $latestBookmark);
33 $start = DailyPageHelper
::getStartDateTimeByType($type, $dateTime);
34 $end = DailyPageHelper
::getEndDateTimeByType($type, $dateTime);
35 $dailyDesc = DailyPageHelper
::getDescriptionByType($type, $dateTime);
37 $linksToDisplay = $this->container
->bookmarkService
->findByDate(
44 $formatter = $this->container
->formatterFactory
->getFormatter();
45 $formatter->addContextData('base_path', $this->container
->basePath
);
46 // We pre-format some fields for proper output.
47 foreach ($linksToDisplay as $key => $bookmark) {
48 $linksToDisplay[$key] = $formatter->format($bookmark);
49 // This page is a bit specific, we need raw description to calculate the length
50 $linksToDisplay[$key]['formatedDescription'] = $linksToDisplay[$key]['description'];
51 $linksToDisplay[$key]['description'] = $bookmark->getDescription();
55 'linksToDisplay' => $linksToDisplay,
57 'day' => $start->getTimestamp(),
58 'previousday' => $previousDay ? $previousDay->format($format) : '',
59 'nextday' => $nextDay ? $nextDay->format($format) : '',
60 'dayDesc' => $dailyDesc,
62 'localizedType' => $this->translateType($type),
65 // Hooks are called before column construction so that plugins don't have to deal with columns.
66 $this->executePageHooks('render_daily', $data, TemplatePage
::DAILY
);
68 $data['cols'] = $this->calculateColumns($data['linksToDisplay']);
70 $this->assignAllView($data);
72 $mainTitle = $this->container
->conf
->get('general.title', 'Shaarli');
75 $data['localizedType'] . ' - ' . $data['dayDesc'] . ' - ' . $mainTitle
78 return $response->write($this->render(TemplatePage
::DAILY
));
82 * Daily RSS feed: 1 RSS entry per day giving all the bookmarks on that day.
83 * Gives the last 7 days (which have bookmarks).
84 * This RSS feed cannot be filtered and does not trigger plugins yet.
86 public function rss(Request
$request, Response
$response): Response
88 $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8');
89 $type = DailyPageHelper
::extractRequestedType($request);
90 $cacheDuration = DailyPageHelper
::getCacheDatePeriodByType($type);
92 $pageUrl = page_url($this->container
->environment
);
93 $cache = $this->container
->pageCacheManager
->getCachePage($pageUrl, $cacheDuration);
95 $cached = $cache->cachedVersion();
96 if (!empty($cached)) {
97 return $response->write($cached);
101 $format = DailyPageHelper
::getFormatByType($type);
102 $length = DailyPageHelper
::getRssLengthByType($type);
103 foreach ($this->container
->bookmarkService
->search() as $bookmark) {
104 $day = $bookmark->getCreated()->format($format);
106 // Stop iterating after DAILY_RSS_NB_DAYS entries
107 if (count($days) === $length && !isset($days[$day])) {
111 $days[$day][] = $bookmark;
114 // Build the RSS feed.
115 $indexUrl = escape(index_url($this->container
->environment
));
117 $formatter = $this->container
->formatterFactory
->getFormatter();
118 $formatter->addContextData('index_url', $indexUrl);
122 /** @var Bookmark[] $bookmarks */
123 foreach ($days as $day => $bookmarks) {
124 $dayDateTime = DailyPageHelper
::extractRequestedDateTime($type, (string) $day);
125 $endDateTime = DailyPageHelper
::getEndDateTimeByType($type, $dayDateTime);
127 // We only want the RSS entry to be published when the period is over.
128 if (new DateTime() < $endDateTime) {
132 $dataPerDay[$day] = [
133 'date' => $endDateTime,
134 'date_rss' => $endDateTime->format(DateTime
::RSS
),
135 'date_human' => DailyPageHelper
::getDescriptionByType($type, $dayDateTime, false),
136 'absolute_url' => $indexUrl . 'daily?' . $type . '=' . $day,
140 foreach ($bookmarks as $key => $bookmark) {
141 $dataPerDay[$day]['links'][$key] = $formatter->format($bookmark);
143 // Make permalink URL absolute
144 if ($bookmark->isNote()) {
145 $dataPerDay[$day]['links'][$key]['url'] = rtrim($indexUrl, '/') . $bookmark->getUrl();
150 $this->assignAllView([
151 'title' => $this->container
->conf
->get('general.title', 'Shaarli'),
152 'index_url' => $indexUrl,
153 'page_url' => $pageUrl,
154 'hide_timestamps' => $this->container
->conf
->get('privacy.hide_timestamps', false),
155 'days' => $dataPerDay,
157 'localizedType' => $this->translateType($type),
160 $rssContent = $this->render(TemplatePage
::DAILY_RSS
);
162 $cache->cache($rssContent);
164 return $response->write($rssContent);
168 * We need to spread the articles on 3 columns.
169 * did not want to use a JavaScript lib like http://masonry.desandro.com/
170 * so I manually spread entries with a simple method: I roughly evaluate the
171 * height of a div according to title and description length.
173 protected function calculateColumns(array $links): array
175 // Entries to display, for each column.
176 $columns = [[], [], []];
177 // Rough estimate of columns fill.
179 foreach ($links as $link) {
180 // Roughly estimate length of entry (by counting characters)
181 // Title: 30 chars = 1 line. 1 line is 30 pixels height.
182 // Description: 836 characters gives roughly 342 pixel height.
183 // This is not perfect, but it's usually OK.
184 $length = strlen($link['title'] ?? '') +
(342 * strlen($link['description'] ?? '')) / 836;
185 if (! empty($link['thumbnail'])) {
186 $length +
= 100; // 1 thumbnails roughly takes 100 pixels height.
188 // Then put in column which is the less filled:
189 $smallest = min($fill); // find smallest value in array.
190 $index = array_search($smallest, $fill); // find index of this smallest value.
191 array_push($columns[$index], $link); // Put entry in this column.
192 $fill[$index] +
= $length;
198 protected function translateType($type): string
201 t('day') => t('Daily'),
202 t('week') => t('Weekly'),
203 t('month') => t('Monthly'),
204 ][t($type)] ?? t('Daily');