diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-05-17 14:16:32 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | c4d5be53c2ae503c00da3cfe6b28d0ce9d2ca7f5 (patch) | |
tree | 2aa6b156d45da7a1bb3cfe0b6e8622030fddb990 /application | |
parent | e3d28be9673a9f8404ff907b8191209729ad690c (diff) | |
download | Shaarli-c4d5be53c2ae503c00da3cfe6b28d0ce9d2ca7f5.tar.gz Shaarli-c4d5be53c2ae503c00da3cfe6b28d0ce9d2ca7f5.tar.zst Shaarli-c4d5be53c2ae503c00da3cfe6b28d0ce9d2ca7f5.zip |
Process Daily RSS feed through Slim controller
The daily RSS template has been entirely rewritten to handle the whole feed through the template engine.
Diffstat (limited to 'application')
-rw-r--r-- | application/bookmark/Bookmark.php | 15 | ||||
-rw-r--r-- | application/bookmark/BookmarkFileService.php | 2 | ||||
-rw-r--r-- | application/container/ContainerBuilder.php | 5 | ||||
-rw-r--r-- | application/front/controllers/DailyController.php | 74 | ||||
-rw-r--r-- | application/http/HttpUtils.php | 15 | ||||
-rw-r--r-- | application/legacy/LegacyLinkDB.php | 2 | ||||
-rw-r--r-- | application/render/PageCacheManager.php | 17 |
7 files changed, 115 insertions, 15 deletions
diff --git a/application/bookmark/Bookmark.php b/application/bookmark/Bookmark.php index 83ddab82..90ff5b16 100644 --- a/application/bookmark/Bookmark.php +++ b/application/bookmark/Bookmark.php | |||
@@ -3,6 +3,7 @@ | |||
3 | namespace Shaarli\Bookmark; | 3 | namespace Shaarli\Bookmark; |
4 | 4 | ||
5 | use DateTime; | 5 | use DateTime; |
6 | use DateTimeInterface; | ||
6 | use Shaarli\Bookmark\Exception\InvalidBookmarkException; | 7 | use Shaarli\Bookmark\Exception\InvalidBookmarkException; |
7 | 8 | ||
8 | /** | 9 | /** |
@@ -42,10 +43,10 @@ class Bookmark | |||
42 | /** @var bool Set to true if the bookmark is set as sticky */ | 43 | /** @var bool Set to true if the bookmark is set as sticky */ |
43 | protected $sticky; | 44 | protected $sticky; |
44 | 45 | ||
45 | /** @var DateTime Creation datetime */ | 46 | /** @var DateTimeInterface Creation datetime */ |
46 | protected $created; | 47 | protected $created; |
47 | 48 | ||
48 | /** @var DateTime Update datetime */ | 49 | /** @var DateTimeInterface datetime */ |
49 | protected $updated; | 50 | protected $updated; |
50 | 51 | ||
51 | /** @var bool True if the bookmark can only be seen while logged in */ | 52 | /** @var bool True if the bookmark can only be seen while logged in */ |
@@ -100,7 +101,7 @@ class Bookmark | |||
100 | || ! is_int($this->id) | 101 | || ! is_int($this->id) |
101 | || empty($this->shortUrl) | 102 | || empty($this->shortUrl) |
102 | || empty($this->created) | 103 | || empty($this->created) |
103 | || ! $this->created instanceof DateTime | 104 | || ! $this->created instanceof DateTimeInterface |
104 | ) { | 105 | ) { |
105 | throw new InvalidBookmarkException($this); | 106 | throw new InvalidBookmarkException($this); |
106 | } | 107 | } |
@@ -188,7 +189,7 @@ class Bookmark | |||
188 | /** | 189 | /** |
189 | * Get the Created. | 190 | * Get the Created. |
190 | * | 191 | * |
191 | * @return DateTime | 192 | * @return DateTimeInterface |
192 | */ | 193 | */ |
193 | public function getCreated() | 194 | public function getCreated() |
194 | { | 195 | { |
@@ -198,7 +199,7 @@ class Bookmark | |||
198 | /** | 199 | /** |
199 | * Get the Updated. | 200 | * Get the Updated. |
200 | * | 201 | * |
201 | * @return DateTime | 202 | * @return DateTimeInterface |
202 | */ | 203 | */ |
203 | public function getUpdated() | 204 | public function getUpdated() |
204 | { | 205 | { |
@@ -270,7 +271,7 @@ class Bookmark | |||
270 | * Set the Created. | 271 | * Set the Created. |
271 | * Note: you shouldn't set this manually except for special cases (like bookmark import) | 272 | * Note: you shouldn't set this manually except for special cases (like bookmark import) |
272 | * | 273 | * |
273 | * @param DateTime $created | 274 | * @param DateTimeInterface $created |
274 | * | 275 | * |
275 | * @return Bookmark | 276 | * @return Bookmark |
276 | */ | 277 | */ |
@@ -284,7 +285,7 @@ class Bookmark | |||
284 | /** | 285 | /** |
285 | * Set the Updated. | 286 | * Set the Updated. |
286 | * | 287 | * |
287 | * @param DateTime $updated | 288 | * @param DateTimeInterface $updated |
288 | * | 289 | * |
289 | * @return Bookmark | 290 | * @return Bookmark |
290 | */ | 291 | */ |
diff --git a/application/bookmark/BookmarkFileService.php b/application/bookmark/BookmarkFileService.php index 3b3812af..7439d8d8 100644 --- a/application/bookmark/BookmarkFileService.php +++ b/application/bookmark/BookmarkFileService.php | |||
@@ -53,7 +53,7 @@ class BookmarkFileService implements BookmarkServiceInterface | |||
53 | { | 53 | { |
54 | $this->conf = $conf; | 54 | $this->conf = $conf; |
55 | $this->history = $history; | 55 | $this->history = $history; |
56 | $this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache')); | 56 | $this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache'), $isLoggedIn); |
57 | $this->bookmarksIO = new BookmarkIO($this->conf); | 57 | $this->bookmarksIO = new BookmarkIO($this->conf); |
58 | $this->isLoggedIn = $isLoggedIn; | 58 | $this->isLoggedIn = $isLoggedIn; |
59 | 59 | ||
diff --git a/application/container/ContainerBuilder.php b/application/container/ContainerBuilder.php index c5c4a2c3..199f3f67 100644 --- a/application/container/ContainerBuilder.php +++ b/application/container/ContainerBuilder.php | |||
@@ -94,7 +94,10 @@ class ContainerBuilder | |||
94 | }; | 94 | }; |
95 | 95 | ||
96 | $container['pageCacheManager'] = function (ShaarliContainer $container): PageCacheManager { | 96 | $container['pageCacheManager'] = function (ShaarliContainer $container): PageCacheManager { |
97 | return new PageCacheManager($container->conf->get('resource.page_cache')); | 97 | return new PageCacheManager( |
98 | $container->conf->get('resource.page_cache'), | ||
99 | $container->loginManager->isLoggedIn() | ||
100 | ); | ||
98 | }; | 101 | }; |
99 | 102 | ||
100 | return $container; | 103 | return $container; |
diff --git a/application/front/controllers/DailyController.php b/application/front/controllers/DailyController.php index 271c0ee2..4a0735aa 100644 --- a/application/front/controllers/DailyController.php +++ b/application/front/controllers/DailyController.php | |||
@@ -5,6 +5,7 @@ declare(strict_types=1); | |||
5 | namespace Shaarli\Front\Controller; | 5 | namespace Shaarli\Front\Controller; |
6 | 6 | ||
7 | use DateTime; | 7 | use DateTime; |
8 | use DateTimeImmutable; | ||
8 | use Shaarli\Bookmark\Bookmark; | 9 | use Shaarli\Bookmark\Bookmark; |
9 | use Slim\Http\Request; | 10 | use Slim\Http\Request; |
10 | use Slim\Http\Response; | 11 | use Slim\Http\Response; |
@@ -18,6 +19,8 @@ use Slim\Http\Response; | |||
18 | */ | 19 | */ |
19 | class DailyController extends ShaarliController | 20 | class DailyController extends ShaarliController |
20 | { | 21 | { |
22 | public static $DAILY_RSS_NB_DAYS = 8; | ||
23 | |||
21 | /** | 24 | /** |
22 | * Controller displaying all bookmarks published in a single day. | 25 | * Controller displaying all bookmarks published in a single day. |
23 | * It take a `day` date query parameter (format YYYYMMDD). | 26 | * It take a `day` date query parameter (format YYYYMMDD). |
@@ -88,6 +91,77 @@ class DailyController extends ShaarliController | |||
88 | } | 91 | } |
89 | 92 | ||
90 | /** | 93 | /** |
94 | * Daily RSS feed: 1 RSS entry per day giving all the bookmarks on that day. | ||
95 | * Gives the last 7 days (which have bookmarks). | ||
96 | * This RSS feed cannot be filtered and does not trigger plugins yet. | ||
97 | */ | ||
98 | public function rss(Request $request, Response $response): Response | ||
99 | { | ||
100 | $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8'); | ||
101 | |||
102 | $pageUrl = page_url($this->container->environment); | ||
103 | $cache = $this->container->pageCacheManager->getCachePage($pageUrl); | ||
104 | |||
105 | $cached = $cache->cachedVersion(); | ||
106 | if (!empty($cached)) { | ||
107 | return $response->write($cached); | ||
108 | } | ||
109 | |||
110 | $days = []; | ||
111 | foreach ($this->container->bookmarkService->search() as $bookmark) { | ||
112 | $day = $bookmark->getCreated()->format('Ymd'); | ||
113 | |||
114 | // Stop iterating after DAILY_RSS_NB_DAYS entries | ||
115 | if (count($days) === static::$DAILY_RSS_NB_DAYS && !isset($days[$day])) { | ||
116 | break; | ||
117 | } | ||
118 | |||
119 | $days[$day][] = $bookmark; | ||
120 | } | ||
121 | |||
122 | // Build the RSS feed. | ||
123 | $indexUrl = escape(index_url($this->container->environment)); | ||
124 | |||
125 | $formatter = $this->container->formatterFactory->getFormatter(); | ||
126 | $formatter->addContextData('index_url', $indexUrl); | ||
127 | |||
128 | $dataPerDay = []; | ||
129 | |||
130 | /** @var Bookmark[] $bookmarks */ | ||
131 | foreach ($days as $day => $bookmarks) { | ||
132 | $dayDatetime = DateTimeImmutable::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000'); | ||
133 | $dataPerDay[$day] = [ | ||
134 | 'date' => $dayDatetime, | ||
135 | 'date_rss' => $dayDatetime->format(DateTime::RSS), | ||
136 | 'date_human' => format_date($dayDatetime, false, true), | ||
137 | 'absolute_url' => $indexUrl . '/daily?day=' . $day, | ||
138 | 'links' => [], | ||
139 | ]; | ||
140 | |||
141 | foreach ($bookmarks as $key => $bookmark) { | ||
142 | $dataPerDay[$day]['links'][$key] = $formatter->format($bookmark); | ||
143 | |||
144 | // Make permalink URL absolute | ||
145 | if ($bookmark->isNote()) { | ||
146 | $dataPerDay[$day]['links'][$key]['url'] = $indexUrl . $bookmark->getUrl(); | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | |||
151 | $this->assignView('title', $this->container->conf->get('general.title', 'Shaarli')); | ||
152 | $this->assignView('index_url', $indexUrl); | ||
153 | $this->assignView('page_url', $pageUrl); | ||
154 | $this->assignView('hide_timestamps', $this->container->conf->get('privacy.hide_timestamps', false)); | ||
155 | $this->assignView('days', $dataPerDay); | ||
156 | |||
157 | $rssContent = $this->render('dailyrss'); | ||
158 | |||
159 | $cache->cache($rssContent); | ||
160 | |||
161 | return $response->write($rssContent); | ||
162 | } | ||
163 | |||
164 | /** | ||
91 | * We need to spread the articles on 3 columns. | 165 | * We need to spread the articles on 3 columns. |
92 | * did not want to use a JavaScript lib like http://masonry.desandro.com/ | 166 | * did not want to use a JavaScript lib like http://masonry.desandro.com/ |
93 | * so I manually spread entries with a simple method: I roughly evaluate the | 167 | * so I manually spread entries with a simple method: I roughly evaluate the |
diff --git a/application/http/HttpUtils.php b/application/http/HttpUtils.php index 2ea9195d..f00c4336 100644 --- a/application/http/HttpUtils.php +++ b/application/http/HttpUtils.php | |||
@@ -369,7 +369,7 @@ function server_url($server) | |||
369 | */ | 369 | */ |
370 | function index_url($server) | 370 | function index_url($server) |
371 | { | 371 | { |
372 | $scriptname = $server['SCRIPT_NAME']; | 372 | $scriptname = $server['SCRIPT_NAME'] ?? ''; |
373 | if (endsWith($scriptname, 'index.php')) { | 373 | if (endsWith($scriptname, 'index.php')) { |
374 | $scriptname = substr($scriptname, 0, -9); | 374 | $scriptname = substr($scriptname, 0, -9); |
375 | } | 375 | } |
@@ -377,7 +377,7 @@ function index_url($server) | |||
377 | } | 377 | } |
378 | 378 | ||
379 | /** | 379 | /** |
380 | * Returns the absolute URL of the current script, with the query | 380 | * Returns the absolute URL of the current script, with current route and query |
381 | * | 381 | * |
382 | * If the resource is "index.php", then it is removed (for better-looking URLs) | 382 | * If the resource is "index.php", then it is removed (for better-looking URLs) |
383 | * | 383 | * |
@@ -387,10 +387,17 @@ function index_url($server) | |||
387 | */ | 387 | */ |
388 | function page_url($server) | 388 | function page_url($server) |
389 | { | 389 | { |
390 | $scriptname = $server['SCRIPT_NAME'] ?? ''; | ||
391 | if (endsWith($scriptname, 'index.php')) { | ||
392 | $scriptname = substr($scriptname, 0, -9); | ||
393 | } | ||
394 | |||
395 | $route = ltrim($server['REQUEST_URI'] ?? '', $scriptname); | ||
390 | if (! empty($server['QUERY_STRING'])) { | 396 | if (! empty($server['QUERY_STRING'])) { |
391 | return index_url($server).'?'.$server['QUERY_STRING']; | 397 | return index_url($server) . $route . '?' . $server['QUERY_STRING']; |
392 | } | 398 | } |
393 | return index_url($server); | 399 | |
400 | return index_url($server) . $route; | ||
394 | } | 401 | } |
395 | 402 | ||
396 | /** | 403 | /** |
diff --git a/application/legacy/LegacyLinkDB.php b/application/legacy/LegacyLinkDB.php index 947005ad..7bf76fd4 100644 --- a/application/legacy/LegacyLinkDB.php +++ b/application/legacy/LegacyLinkDB.php | |||
@@ -353,7 +353,7 @@ You use the community supported version of the original Shaarli project, by Seba | |||
353 | 353 | ||
354 | $this->write(); | 354 | $this->write(); |
355 | 355 | ||
356 | $pageCacheManager = new PageCacheManager($pageCacheDir); | 356 | $pageCacheManager = new PageCacheManager($pageCacheDir, $this->loggedIn); |
357 | $pageCacheManager->invalidateCaches(); | 357 | $pageCacheManager->invalidateCaches(); |
358 | } | 358 | } |
359 | 359 | ||
diff --git a/application/render/PageCacheManager.php b/application/render/PageCacheManager.php index bd91fe0d..97805c35 100644 --- a/application/render/PageCacheManager.php +++ b/application/render/PageCacheManager.php | |||
@@ -2,6 +2,8 @@ | |||
2 | 2 | ||
3 | namespace Shaarli\Render; | 3 | namespace Shaarli\Render; |
4 | 4 | ||
5 | use Shaarli\Feed\CachedPage; | ||
6 | |||
5 | /** | 7 | /** |
6 | * Cache utilities | 8 | * Cache utilities |
7 | */ | 9 | */ |
@@ -10,9 +12,13 @@ class PageCacheManager | |||
10 | /** @var string Cache directory */ | 12 | /** @var string Cache directory */ |
11 | protected $pageCacheDir; | 13 | protected $pageCacheDir; |
12 | 14 | ||
13 | public function __construct(string $pageCacheDir) | 15 | /** @var bool */ |
16 | protected $isLoggedIn; | ||
17 | |||
18 | public function __construct(string $pageCacheDir, bool $isLoggedIn) | ||
14 | { | 19 | { |
15 | $this->pageCacheDir = $pageCacheDir; | 20 | $this->pageCacheDir = $pageCacheDir; |
21 | $this->isLoggedIn = $isLoggedIn; | ||
16 | } | 22 | } |
17 | 23 | ||
18 | /** | 24 | /** |
@@ -42,4 +48,13 @@ class PageCacheManager | |||
42 | // Purge page cache shared by sessions. | 48 | // Purge page cache shared by sessions. |
43 | $this->purgeCachedPages(); | 49 | $this->purgeCachedPages(); |
44 | } | 50 | } |
51 | |||
52 | public function getCachePage(string $pageUrl): CachedPage | ||
53 | { | ||
54 | return new CachedPage( | ||
55 | $this->pageCacheDir, | ||
56 | $pageUrl, | ||
57 | false === $this->isLoggedIn | ||
58 | ); | ||
59 | } | ||
45 | } | 60 | } |