]>
Commit | Line | Data |
---|---|---|
69e29ff6 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
2899ebb5 | 5 | namespace Shaarli\Front\Controller\Visitor; |
69e29ff6 A |
6 | |
7 | use DateTime; | |
8 | use Shaarli\Bookmark\Bookmark; | |
36e6d88d | 9 | use Shaarli\Helper\DailyPageHelper; |
1a8ac737 | 10 | use Shaarli\Render\TemplatePage; |
69e29ff6 A |
11 | use Slim\Http\Request; |
12 | use Slim\Http\Response; | |
13 | ||
14 | /** | |
15 | * Class DailyController | |
16 | * | |
17 | * Slim controller used to render the daily page. | |
69e29ff6 | 18 | */ |
2899ebb5 | 19 | class DailyController extends ShaarliVisitorController |
69e29ff6 | 20 | { |
c4d5be53 A |
21 | public static $DAILY_RSS_NB_DAYS = 8; |
22 | ||
69e29ff6 A |
23 | /** |
24 | * Controller displaying all bookmarks published in a single day. | |
25 | * It take a `day` date query parameter (format YYYYMMDD). | |
26 | */ | |
27 | public function index(Request $request, Response $response): Response | |
28 | { | |
36e6d88d A |
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); | |
36 | ||
37 | $linksToDisplay = $this->container->bookmarkService->findByDate( | |
38 | $start, | |
39 | $end, | |
40 | $previousDay, | |
41 | $nextDay | |
42 | ); | |
69e29ff6 A |
43 | |
44 | $formatter = $this->container->formatterFactory->getFormatter(); | |
301c7ab1 | 45 | $formatter->addContextData('base_path', $this->container->basePath); |
69e29ff6 A |
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(); | |
52 | } | |
53 | ||
69e29ff6 A |
54 | $data = [ |
55 | 'linksToDisplay' => $linksToDisplay, | |
36e6d88d A |
56 | 'dayDate' => $start, |
57 | 'day' => $start->getTimestamp(), | |
58 | 'previousday' => $previousDay ? $previousDay->format($format) : '', | |
59 | 'nextday' => $nextDay ? $nextDay->format($format) : '', | |
60 | 'dayDesc' => $dailyDesc, | |
61 | 'type' => $type, | |
62 | 'localizedType' => $this->translateType($type), | |
69e29ff6 A |
63 | ]; |
64 | ||
65 | // Hooks are called before column construction so that plugins don't have to deal with columns. | |
9fbc4229 | 66 | $this->executePageHooks('render_daily', $data, TemplatePage::DAILY); |
69e29ff6 A |
67 | |
68 | $data['cols'] = $this->calculateColumns($data['linksToDisplay']); | |
69 | ||
9fbc4229 | 70 | $this->assignAllView($data); |
69e29ff6 A |
71 | |
72 | $mainTitle = $this->container->conf->get('general.title', 'Shaarli'); | |
73 | $this->assignView( | |
74 | 'pagetitle', | |
36e6d88d | 75 | $data['localizedType'] . ' - ' . $data['dayDesc'] . ' - ' . $mainTitle |
69e29ff6 A |
76 | ); |
77 | ||
1a8ac737 | 78 | return $response->write($this->render(TemplatePage::DAILY)); |
69e29ff6 A |
79 | } |
80 | ||
c4d5be53 A |
81 | /** |
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. | |
85 | */ | |
86 | public function rss(Request $request, Response $response): Response | |
87 | { | |
88 | $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8'); | |
89 | ||
90 | $pageUrl = page_url($this->container->environment); | |
91 | $cache = $this->container->pageCacheManager->getCachePage($pageUrl); | |
92 | ||
93 | $cached = $cache->cachedVersion(); | |
94 | if (!empty($cached)) { | |
95 | return $response->write($cached); | |
96 | } | |
97 | ||
98 | $days = []; | |
36e6d88d A |
99 | $type = DailyPageHelper::extractRequestedType($request); |
100 | $format = DailyPageHelper::getFormatByType($type); | |
101 | $length = DailyPageHelper::getRssLengthByType($type); | |
c4d5be53 | 102 | foreach ($this->container->bookmarkService->search() as $bookmark) { |
36e6d88d | 103 | $day = $bookmark->getCreated()->format($format); |
c4d5be53 A |
104 | |
105 | // Stop iterating after DAILY_RSS_NB_DAYS entries | |
36e6d88d | 106 | if (count($days) === $length && !isset($days[$day])) { |
c4d5be53 A |
107 | break; |
108 | } | |
109 | ||
110 | $days[$day][] = $bookmark; | |
111 | } | |
112 | ||
113 | // Build the RSS feed. | |
114 | $indexUrl = escape(index_url($this->container->environment)); | |
115 | ||
116 | $formatter = $this->container->formatterFactory->getFormatter(); | |
117 | $formatter->addContextData('index_url', $indexUrl); | |
118 | ||
119 | $dataPerDay = []; | |
120 | ||
121 | /** @var Bookmark[] $bookmarks */ | |
122 | foreach ($days as $day => $bookmarks) { | |
36e6d88d A |
123 | $dayDateTime = DailyPageHelper::extractRequestedDateTime($type, (string) $day); |
124 | $endDateTime = DailyPageHelper::getEndDateTimeByType($type, $dayDateTime); | |
125 | ||
126 | // We only want the RSS entry to be published when the period is over. | |
127 | if (new DateTime() < $endDateTime) { | |
128 | continue; | |
129 | } | |
130 | ||
c4d5be53 | 131 | $dataPerDay[$day] = [ |
36e6d88d A |
132 | 'date' => $endDateTime, |
133 | 'date_rss' => $endDateTime->format(DateTime::RSS), | |
134 | 'date_human' => DailyPageHelper::getDescriptionByType($type, $dayDateTime), | |
53054b2b | 135 | 'absolute_url' => $indexUrl . 'daily?' . $type . '=' . $day, |
c4d5be53 A |
136 | 'links' => [], |
137 | ]; | |
138 | ||
139 | foreach ($bookmarks as $key => $bookmark) { | |
140 | $dataPerDay[$day]['links'][$key] = $formatter->format($bookmark); | |
141 | ||
142 | // Make permalink URL absolute | |
143 | if ($bookmark->isNote()) { | |
36e6d88d | 144 | $dataPerDay[$day]['links'][$key]['url'] = rtrim($indexUrl, '/') . $bookmark->getUrl(); |
c4d5be53 A |
145 | } |
146 | } | |
147 | } | |
148 | ||
36e6d88d A |
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, | |
155 | 'type' => $type, | |
156 | 'localizedType' => $this->translateType($type), | |
157 | ]); | |
c4d5be53 | 158 | |
1a8ac737 | 159 | $rssContent = $this->render(TemplatePage::DAILY_RSS); |
c4d5be53 A |
160 | |
161 | $cache->cache($rssContent); | |
162 | ||
163 | return $response->write($rssContent); | |
164 | } | |
165 | ||
69e29ff6 A |
166 | /** |
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. | |
171 | */ | |
172 | protected function calculateColumns(array $links): array | |
173 | { | |
174 | // Entries to display, for each column. | |
175 | $columns = [[], [], []]; | |
176 | // Rough estimate of columns fill. | |
177 | $fill = [0, 0, 0]; | |
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. | |
186 | } | |
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; | |
192 | } | |
193 | ||
194 | return $columns; | |
195 | } | |
36e6d88d A |
196 | |
197 | protected function translateType($type): string | |
198 | { | |
199 | return [ | |
200 | t('day') => t('Daily'), | |
201 | t('week') => t('Weekly'), | |
202 | t('month') => t('Monthly'), | |
203 | ][t($type)] ?? t('Daily'); | |
204 | } | |
69e29ff6 | 205 | } |