diff options
Diffstat (limited to 'application/front/controllers/FeedController.php')
-rw-r--r-- | application/front/controllers/FeedController.php | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/application/front/controllers/FeedController.php b/application/front/controllers/FeedController.php new file mode 100644 index 00000000..78d826d9 --- /dev/null +++ b/application/front/controllers/FeedController.php | |||
@@ -0,0 +1,79 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller; | ||
6 | |||
7 | use Shaarli\Feed\FeedBuilder; | ||
8 | use Slim\Http\Request; | ||
9 | use Slim\Http\Response; | ||
10 | |||
11 | /** | ||
12 | * Class FeedController | ||
13 | * | ||
14 | * Slim controller handling ATOM and RSS feed. | ||
15 | * | ||
16 | * @package Front\Controller | ||
17 | */ | ||
18 | class FeedController extends ShaarliController | ||
19 | { | ||
20 | public function atom(Request $request, Response $response): Response | ||
21 | { | ||
22 | return $this->processRequest(FeedBuilder::$FEED_ATOM, $request, $response); | ||
23 | } | ||
24 | |||
25 | public function rss(Request $request, Response $response): Response | ||
26 | { | ||
27 | return $this->processRequest(FeedBuilder::$FEED_RSS, $request, $response); | ||
28 | } | ||
29 | |||
30 | protected function processRequest(string $feedType, Request $request, Response $response): Response | ||
31 | { | ||
32 | $response = $response->withHeader('Content-Type', 'application/'. $feedType .'+xml; charset=utf-8'); | ||
33 | |||
34 | $pageUrl = page_url($this->container->environment); | ||
35 | $cache = $this->container->pageCacheManager->getCachePage($pageUrl); | ||
36 | |||
37 | $cached = $cache->cachedVersion(); | ||
38 | if (!empty($cached)) { | ||
39 | return $response->write($cached); | ||
40 | } | ||
41 | |||
42 | // Generate data. | ||
43 | $this->container->feedBuilder->setLocale(strtolower(setlocale(LC_COLLATE, 0))); | ||
44 | $this->container->feedBuilder->setHideDates($this->container->conf->get('privacy.hide_timestamps', false)); | ||
45 | $this->container->feedBuilder->setUsePermalinks( | ||
46 | null !== $request->getParam('permalinks') || !$this->container->conf->get('feed.rss_permalinks') | ||
47 | ); | ||
48 | |||
49 | $data = $this->container->feedBuilder->buildData($feedType, $request->getParams()); | ||
50 | |||
51 | $this->executeHooks($data, $feedType); | ||
52 | $this->assignAllView($data); | ||
53 | |||
54 | $content = $this->render('feed.'. $feedType); | ||
55 | |||
56 | $cache->cache($content); | ||
57 | |||
58 | return $response->write($content); | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * @param mixed[] $data Template data | ||
63 | * | ||
64 | * @return mixed[] Template data after active plugins hook execution. | ||
65 | */ | ||
66 | protected function executeHooks(array $data, string $feedType): array | ||
67 | { | ||
68 | $this->container->pluginManager->executeHooks( | ||
69 | 'render_feed', | ||
70 | $data, | ||
71 | [ | ||
72 | 'loggedin' => $this->container->loginManager->isLoggedIn(), | ||
73 | 'target' => $feedType, | ||
74 | ] | ||
75 | ); | ||
76 | |||
77 | return $data; | ||
78 | } | ||
79 | } | ||