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');
90 $pageUrl = page_url($this->container
->environment
);
91 $cache = $this->container
->pageCacheManager
->getCachePage($pageUrl);
93 $cached = $cache->cachedVersion();
94 if (!empty($cached)) {
95 return $response->write($cached);
99 $type = DailyPageHelper
::extractRequestedType($request);
100 $format = DailyPageHelper
::getFormatByType($type);
101 $length = DailyPageHelper
::getRssLengthByType($type);
102 foreach ($this->container
->bookmarkService
->search() as $bookmark) {
103 $day = $bookmark->getCreated()->format($format);
105 // Stop iterating after DAILY_RSS_NB_DAYS entries
106 if (count($days) === $length && !isset($days[$day])) {
110 $days[$day][] = $bookmark;
113 // Build the RSS feed.
114 $indexUrl = escape(index_url($this->container
->environment
));
116 $formatter = $this->container
->formatterFactory
->getFormatter();
117 $formatter->addContextData('index_url', $indexUrl);
121 /** @var Bookmark[] $bookmarks */
122 foreach ($days as $day => $bookmarks) {
123 $dayDateTime = DailyPageHelper
::extractRequestedDateTime($type, (string) $day);
124 $endDateTime = DailyPageHelper
::getEndDateTimeByType($type, $dayDateTime);
126 // We only want the RSS entry to be published when the period is over.
127 if (new DateTime() < $endDateTime) {
131 $dataPerDay[$day] = [
132 'date' => $endDateTime,
133 'date_rss' => $endDateTime->format(DateTime
::RSS
),
134 'date_human' => DailyPageHelper
::getDescriptionByType($type, $dayDateTime),
135 'absolute_url' => $indexUrl . 'daily?' . $type . '=' . $day,
139 foreach ($bookmarks as $key => $bookmark) {
140 $dataPerDay[$day]['links'][$key] = $formatter->format($bookmark);
142 // Make permalink URL absolute
143 if ($bookmark->isNote()) {
144 $dataPerDay[$day]['links'][$key]['url'] = rtrim($indexUrl, '/') . $bookmark->getUrl();
149 $this->assignAllView([
150 'title' => $this->container
->conf
->get('general.title', 'Shaarli'),
151 'index_url' => $indexUrl,
152 'page_url' => $pageUrl,
153 'hide_timestamps' => $this->container
->conf
->get('privacy.hide_timestamps', false),
154 'days' => $dataPerDay,
156 'localizedType' => $this->translateType($type),
159 $rssContent = $this->render(TemplatePage
::DAILY_RSS
);
161 $cache->cache($rssContent);
163 return $response->write($rssContent);
167 * We need to spread the articles on 3 columns.
168 * did not want to use a JavaScript lib like http://masonry.desandro.com/
169 * so I manually spread entries with a simple method: I roughly evaluate the
170 * height of a div according to title and description length.
172 protected function calculateColumns(array $links): array
174 // Entries to display, for each column.
175 $columns = [[], [], []];
176 // Rough estimate of columns fill.
178 foreach ($links as $link) {
179 // Roughly estimate length of entry (by counting characters)
180 // Title: 30 chars = 1 line. 1 line is 30 pixels height.
181 // Description: 836 characters gives roughly 342 pixel height.
182 // This is not perfect, but it's usually OK.
183 $length = strlen($link['title'] ?? '') +
(342 * strlen($link['description'] ?? '')) / 836;
184 if (! empty($link['thumbnail'])) {
185 $length +
= 100; // 1 thumbnails roughly takes 100 pixels height.
187 // Then put in column which is the less filled:
188 $smallest = min($fill); // find smallest value in array.
189 $index = array_search($smallest, $fill); // find index of this smallest value.
190 array_push($columns[$index], $link); // Put entry in this column.
191 $fill[$index] +
= $length;
197 protected function translateType($type): string
200 t('day') => t('Daily'),
201 t('week') => t('Weekly'),
202 t('month') => t('Monthly'),
203 ][t($type)] ?? t('Daily');