aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-18 17:17:36 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit7b2ba6ef820335df682fbe3dcfaceef3a62cf4a5 (patch)
treeb9d4b744f514fb3d1b45a40f81323983ca31f7ad /application
parentf4929b1188b4bc5e92b925ebc44f5ad40bb1a4ed (diff)
downloadShaarli-7b2ba6ef820335df682fbe3dcfaceef3a62cf4a5.tar.gz
Shaarli-7b2ba6ef820335df682fbe3dcfaceef3a62cf4a5.tar.zst
Shaarli-7b2ba6ef820335df682fbe3dcfaceef3a62cf4a5.zip
RSS/ATOM feeds: process through Slim controller
Diffstat (limited to 'application')
-rw-r--r--application/container/ContainerBuilder.php10
-rw-r--r--application/container/ShaarliContainer.php2
-rw-r--r--application/feed/FeedBuilder.php2
-rw-r--r--application/front/controllers/FeedController.php79
-rw-r--r--application/front/controllers/ShaarliController.php14
5 files changed, 106 insertions, 1 deletions
diff --git a/application/container/ContainerBuilder.php b/application/container/ContainerBuilder.php
index 199f3f67..84406979 100644
--- a/application/container/ContainerBuilder.php
+++ b/application/container/ContainerBuilder.php
@@ -7,6 +7,7 @@ namespace Shaarli\Container;
7use Shaarli\Bookmark\BookmarkFileService; 7use Shaarli\Bookmark\BookmarkFileService;
8use Shaarli\Bookmark\BookmarkServiceInterface; 8use Shaarli\Bookmark\BookmarkServiceInterface;
9use Shaarli\Config\ConfigManager; 9use Shaarli\Config\ConfigManager;
10use Shaarli\Feed\FeedBuilder;
10use Shaarli\Formatter\FormatterFactory; 11use Shaarli\Formatter\FormatterFactory;
11use Shaarli\History; 12use Shaarli\History;
12use Shaarli\Plugin\PluginManager; 13use Shaarli\Plugin\PluginManager;
@@ -100,6 +101,15 @@ class ContainerBuilder
100 ); 101 );
101 }; 102 };
102 103
104 $container['feedBuilder'] = function (ShaarliContainer $container): FeedBuilder {
105 return new FeedBuilder(
106 $container->bookmarkService,
107 $container->formatterFactory->getFormatter(),
108 $container->environment,
109 $container->loginManager->isLoggedIn()
110 );
111 };
112
103 return $container; 113 return $container;
104 } 114 }
105} 115}
diff --git a/application/container/ShaarliContainer.php b/application/container/ShaarliContainer.php
index 3995f669..deb07197 100644
--- a/application/container/ShaarliContainer.php
+++ b/application/container/ShaarliContainer.php
@@ -6,6 +6,7 @@ namespace Shaarli\Container;
6 6
7use Shaarli\Bookmark\BookmarkServiceInterface; 7use Shaarli\Bookmark\BookmarkServiceInterface;
8use Shaarli\Config\ConfigManager; 8use Shaarli\Config\ConfigManager;
9use Shaarli\Feed\FeedBuilder;
9use Shaarli\Formatter\FormatterFactory; 10use Shaarli\Formatter\FormatterFactory;
10use Shaarli\History; 11use Shaarli\History;
11use Shaarli\Plugin\PluginManager; 12use Shaarli\Plugin\PluginManager;
@@ -29,6 +30,7 @@ use Slim\Container;
29 * @property PluginManager $pluginManager 30 * @property PluginManager $pluginManager
30 * @property FormatterFactory $formatterFactory 31 * @property FormatterFactory $formatterFactory
31 * @property PageCacheManager $pageCacheManager 32 * @property PageCacheManager $pageCacheManager
33 * @property FeedBuilder $feedBuilder
32 */ 34 */
33class ShaarliContainer extends Container 35class ShaarliContainer extends Container
34{ 36{
diff --git a/application/feed/FeedBuilder.php b/application/feed/FeedBuilder.php
index bcf27c2c..c97ae1ea 100644
--- a/application/feed/FeedBuilder.php
+++ b/application/feed/FeedBuilder.php
@@ -78,7 +78,7 @@ class FeedBuilder
78 * @param array $serverInfo $_SERVER. 78 * @param array $serverInfo $_SERVER.
79 * @param boolean $isLoggedIn True if the user is currently logged in, false otherwise. 79 * @param boolean $isLoggedIn True if the user is currently logged in, false otherwise.
80 */ 80 */
81 public function __construct($linkDB, $formatter, array $serverInfo, $isLoggedIn) 81 public function __construct($linkDB, $formatter, $serverInfo, $isLoggedIn)
82 { 82 {
83 $this->linkDB = $linkDB; 83 $this->linkDB = $linkDB;
84 $this->formatter = $formatter; 84 $this->formatter = $formatter;
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
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Shaarli\Feed\FeedBuilder;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class FeedController
13 *
14 * Slim controller handling ATOM and RSS feed.
15 *
16 * @package Front\Controller
17 */
18class 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}
diff --git a/application/front/controllers/ShaarliController.php b/application/front/controllers/ShaarliController.php
index 2b828588..0c5d363e 100644
--- a/application/front/controllers/ShaarliController.php
+++ b/application/front/controllers/ShaarliController.php
@@ -30,6 +30,20 @@ abstract class ShaarliController
30 return $this; 30 return $this;
31 } 31 }
32 32
33 /**
34 * Assign variables to RainTPL template through the PageBuilder.
35 *
36 * @param mixed $data Values to assign to the template and their keys
37 */
38 protected function assignAllView(array $data): self
39 {
40 foreach ($data as $key => $value) {
41 $this->assignView($key, $value);
42 }
43
44 return $this;
45 }
46
33 protected function render(string $template): string 47 protected function render(string $template): string
34 { 48 {
35 $this->assignView('linkcount', $this->container->bookmarkService->count(BookmarkFilter::$ALL)); 49 $this->assignView('linkcount', $this->container->bookmarkService->count(BookmarkFilter::$ALL));