diff options
Diffstat (limited to 'application/front/controller/visitor/DailyController.php')
-rw-r--r-- | application/front/controller/visitor/DailyController.php | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/application/front/controller/visitor/DailyController.php b/application/front/controller/visitor/DailyController.php new file mode 100644 index 00000000..728bc2d8 --- /dev/null +++ b/application/front/controller/visitor/DailyController.php | |||
@@ -0,0 +1,205 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
7 | use DateTime; | ||
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; | ||
13 | |||
14 | /** | ||
15 | * Class DailyController | ||
16 | * | ||
17 | * Slim controller used to render the daily page. | ||
18 | */ | ||
19 | class DailyController extends ShaarliVisitorController | ||
20 | { | ||
21 | public static $DAILY_RSS_NB_DAYS = 8; | ||
22 | |||
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 | { | ||
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 | ); | ||
43 | |||
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(); | ||
52 | } | ||
53 | |||
54 | $data = [ | ||
55 | 'linksToDisplay' => $linksToDisplay, | ||
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), | ||
63 | ]; | ||
64 | |||
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); | ||
67 | |||
68 | $data['cols'] = $this->calculateColumns($data['linksToDisplay']); | ||
69 | |||
70 | $this->assignAllView($data); | ||
71 | |||
72 | $mainTitle = $this->container->conf->get('general.title', 'Shaarli'); | ||
73 | $this->assignView( | ||
74 | 'pagetitle', | ||
75 | $data['localizedType'] . ' - ' . $data['dayDesc'] . ' - ' . $mainTitle | ||
76 | ); | ||
77 | |||
78 | return $response->write($this->render(TemplatePage::DAILY)); | ||
79 | } | ||
80 | |||
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 = []; | ||
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); | ||
104 | |||
105 | // Stop iterating after DAILY_RSS_NB_DAYS entries | ||
106 | if (count($days) === $length && !isset($days[$day])) { | ||
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) { | ||
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 | |||
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, | ||
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()) { | ||
144 | $dataPerDay[$day]['links'][$key]['url'] = rtrim($indexUrl, '/') . $bookmark->getUrl(); | ||
145 | } | ||
146 | } | ||
147 | } | ||
148 | |||
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 | ]); | ||
158 | |||
159 | $rssContent = $this->render(TemplatePage::DAILY_RSS); | ||
160 | |||
161 | $cache->cache($rssContent); | ||
162 | |||
163 | return $response->write($rssContent); | ||
164 | } | ||
165 | |||
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 | } | ||
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 | } | ||
205 | } | ||