diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-18 10:01:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-18 10:01:06 +0100 |
commit | 3fb29fdda04ca86e04422d49b86cf646d53c4f9d (patch) | |
tree | adf8512f93f5559ba87d0c9931969ae4ebea7133 /application/feed | |
parent | 796c4c57d085ae4589b53dfe8369ae9ba30ffdaf (diff) | |
parent | e26e2060f5470ce8bf4c5973284bae07b8af170a (diff) | |
download | Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.gz Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.zst Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.zip |
Store bookmarks as PHP objects and add a service layer to retriā¦ (#1307)
Store bookmarks as PHP objects and add a service layer to retrieve them
Diffstat (limited to 'application/feed')
-rw-r--r-- | application/feed/FeedBuilder.php | 78 |
1 files changed, 39 insertions, 39 deletions
diff --git a/application/feed/FeedBuilder.php b/application/feed/FeedBuilder.php index 957c8273..40bd4f15 100644 --- a/application/feed/FeedBuilder.php +++ b/application/feed/FeedBuilder.php | |||
@@ -2,6 +2,9 @@ | |||
2 | namespace Shaarli\Feed; | 2 | namespace Shaarli\Feed; |
3 | 3 | ||
4 | use DateTime; | 4 | use DateTime; |
5 | use Shaarli\Bookmark\Bookmark; | ||
6 | use Shaarli\Bookmark\BookmarkServiceInterface; | ||
7 | use Shaarli\Formatter\BookmarkFormatter; | ||
5 | 8 | ||
6 | /** | 9 | /** |
7 | * FeedBuilder class. | 10 | * FeedBuilder class. |
@@ -26,16 +29,21 @@ class FeedBuilder | |||
26 | public static $DEFAULT_LANGUAGE = 'en-en'; | 29 | public static $DEFAULT_LANGUAGE = 'en-en'; |
27 | 30 | ||
28 | /** | 31 | /** |
29 | * @var int Number of links to display in a feed by default. | 32 | * @var int Number of bookmarks to display in a feed by default. |
30 | */ | 33 | */ |
31 | public static $DEFAULT_NB_LINKS = 50; | 34 | public static $DEFAULT_NB_LINKS = 50; |
32 | 35 | ||
33 | /** | 36 | /** |
34 | * @var \Shaarli\Bookmark\LinkDB instance. | 37 | * @var BookmarkServiceInterface instance. |
35 | */ | 38 | */ |
36 | protected $linkDB; | 39 | protected $linkDB; |
37 | 40 | ||
38 | /** | 41 | /** |
42 | * @var BookmarkFormatter instance. | ||
43 | */ | ||
44 | protected $formatter; | ||
45 | |||
46 | /** | ||
39 | * @var string RSS or ATOM feed. | 47 | * @var string RSS or ATOM feed. |
40 | */ | 48 | */ |
41 | protected $feedType; | 49 | protected $feedType; |
@@ -56,7 +64,7 @@ class FeedBuilder | |||
56 | protected $isLoggedIn; | 64 | protected $isLoggedIn; |
57 | 65 | ||
58 | /** | 66 | /** |
59 | * @var boolean Use permalinks instead of direct links if true. | 67 | * @var boolean Use permalinks instead of direct bookmarks if true. |
60 | */ | 68 | */ |
61 | protected $usePermalinks; | 69 | protected $usePermalinks; |
62 | 70 | ||
@@ -78,16 +86,17 @@ class FeedBuilder | |||
78 | /** | 86 | /** |
79 | * Feed constructor. | 87 | * Feed constructor. |
80 | * | 88 | * |
81 | * @param \Shaarli\Bookmark\LinkDB $linkDB LinkDB instance. | 89 | * @param BookmarkServiceInterface $linkDB LinkDB instance. |
90 | * @param BookmarkFormatter $formatter instance. | ||
82 | * @param string $feedType Type of feed. | 91 | * @param string $feedType Type of feed. |
83 | * @param array $serverInfo $_SERVER. | 92 | * @param array $serverInfo $_SERVER. |
84 | * @param array $userInput $_GET. | 93 | * @param array $userInput $_GET. |
85 | * @param boolean $isLoggedIn True if the user is currently logged in, | 94 | * @param boolean $isLoggedIn True if the user is currently logged in, false otherwise. |
86 | * false otherwise. | ||
87 | */ | 95 | */ |
88 | public function __construct($linkDB, $feedType, $serverInfo, $userInput, $isLoggedIn) | 96 | public function __construct($linkDB, $formatter, $feedType, $serverInfo, $userInput, $isLoggedIn) |
89 | { | 97 | { |
90 | $this->linkDB = $linkDB; | 98 | $this->linkDB = $linkDB; |
99 | $this->formatter = $formatter; | ||
91 | $this->feedType = $feedType; | 100 | $this->feedType = $feedType; |
92 | $this->serverInfo = $serverInfo; | 101 | $this->serverInfo = $serverInfo; |
93 | $this->userInput = $userInput; | 102 | $this->userInput = $userInput; |
@@ -101,13 +110,13 @@ class FeedBuilder | |||
101 | */ | 110 | */ |
102 | public function buildData() | 111 | public function buildData() |
103 | { | 112 | { |
104 | // Search for untagged links | 113 | // Search for untagged bookmarks |
105 | if (isset($this->userInput['searchtags']) && empty($this->userInput['searchtags'])) { | 114 | if (isset($this->userInput['searchtags']) && empty($this->userInput['searchtags'])) { |
106 | $this->userInput['searchtags'] = false; | 115 | $this->userInput['searchtags'] = false; |
107 | } | 116 | } |
108 | 117 | ||
109 | // Optionally filter the results: | 118 | // Optionally filter the results: |
110 | $linksToDisplay = $this->linkDB->filterSearch($this->userInput); | 119 | $linksToDisplay = $this->linkDB->search($this->userInput); |
111 | 120 | ||
112 | $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay)); | 121 | $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay)); |
113 | 122 | ||
@@ -118,6 +127,7 @@ class FeedBuilder | |||
118 | } | 127 | } |
119 | 128 | ||
120 | $pageaddr = escape(index_url($this->serverInfo)); | 129 | $pageaddr = escape(index_url($this->serverInfo)); |
130 | $this->formatter->addContextData('index_url', $pageaddr); | ||
121 | $linkDisplayed = array(); | 131 | $linkDisplayed = array(); |
122 | for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) { | 132 | for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) { |
123 | $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr); | 133 | $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr); |
@@ -139,54 +149,44 @@ class FeedBuilder | |||
139 | /** | 149 | /** |
140 | * Build a feed item (one per shaare). | 150 | * Build a feed item (one per shaare). |
141 | * | 151 | * |
142 | * @param array $link Single link array extracted from LinkDB. | 152 | * @param Bookmark $link Single link array extracted from LinkDB. |
143 | * @param string $pageaddr Index URL. | 153 | * @param string $pageaddr Index URL. |
144 | * | 154 | * |
145 | * @return array Link array with feed attributes. | 155 | * @return array Link array with feed attributes. |
146 | */ | 156 | */ |
147 | protected function buildItem($link, $pageaddr) | 157 | protected function buildItem($link, $pageaddr) |
148 | { | 158 | { |
149 | $link['guid'] = $pageaddr . '?' . $link['shorturl']; | 159 | $data = $this->formatter->format($link); |
150 | // Prepend the root URL for notes | 160 | $data['guid'] = $pageaddr . '?' . $data['shorturl']; |
151 | if (is_note($link['url'])) { | ||
152 | $link['url'] = $pageaddr . $link['url']; | ||
153 | } | ||
154 | if ($this->usePermalinks === true) { | 161 | if ($this->usePermalinks === true) { |
155 | $permalink = '<a href="' . $link['url'] . '" title="' . t('Direct link') . '">' . t('Direct link') . '</a>'; | 162 | $permalink = '<a href="'. $data['url'] .'" title="'. t('Direct link') .'">'. t('Direct link') .'</a>'; |
156 | } else { | 163 | } else { |
157 | $permalink = '<a href="' . $link['guid'] . '" title="' . t('Permalink') . '">' . t('Permalink') . '</a>'; | 164 | $permalink = '<a href="'. $data['guid'] .'" title="'. t('Permalink') .'">'. t('Permalink') .'</a>'; |
158 | } | 165 | } |
159 | $link['description'] = format_description($link['description'], $pageaddr); | 166 | $data['description'] .= PHP_EOL . PHP_EOL . '<br>— ' . $permalink; |
160 | $link['description'] .= PHP_EOL . PHP_EOL . '<br>— ' . $permalink; | ||
161 | 167 | ||
162 | $pubDate = $link['created']; | 168 | $data['pub_iso_date'] = $this->getIsoDate($data['created']); |
163 | $link['pub_iso_date'] = $this->getIsoDate($pubDate); | ||
164 | 169 | ||
165 | // atom:entry elements MUST contain exactly one atom:updated element. | 170 | // atom:entry elements MUST contain exactly one atom:updated element. |
166 | if (!empty($link['updated'])) { | 171 | if (!empty($link->getUpdated())) { |
167 | $upDate = $link['updated']; | 172 | $data['up_iso_date'] = $this->getIsoDate($data['updated'], DateTime::ATOM); |
168 | $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime::ATOM); | ||
169 | } else { | 173 | } else { |
170 | $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM); | 174 | $data['up_iso_date'] = $this->getIsoDate($data['created'], DateTime::ATOM); |
171 | } | 175 | } |
172 | 176 | ||
173 | // Save the more recent item. | 177 | // Save the more recent item. |
174 | if (empty($this->latestDate) || $this->latestDate < $pubDate) { | 178 | if (empty($this->latestDate) || $this->latestDate < $data['created']) { |
175 | $this->latestDate = $pubDate; | 179 | $this->latestDate = $data['created']; |
176 | } | 180 | } |
177 | if (!empty($upDate) && $this->latestDate < $upDate) { | 181 | if (!empty($data['updated']) && $this->latestDate < $data['updated']) { |
178 | $this->latestDate = $upDate; | 182 | $this->latestDate = $data['updated']; |
179 | } | 183 | } |
180 | 184 | ||
181 | $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); | 185 | return $data; |
182 | uasort($taglist, 'strcasecmp'); | ||
183 | $link['taglist'] = $taglist; | ||
184 | |||
185 | return $link; | ||
186 | } | 186 | } |
187 | 187 | ||
188 | /** | 188 | /** |
189 | * Set this to true to use permalinks instead of direct links. | 189 | * Set this to true to use permalinks instead of direct bookmarks. |
190 | * | 190 | * |
191 | * @param boolean $usePermalinks true to force permalinks. | 191 | * @param boolean $usePermalinks true to force permalinks. |
192 | */ | 192 | */ |
@@ -273,11 +273,11 @@ class FeedBuilder | |||
273 | * Returns the number of link to display according to 'nb' user input parameter. | 273 | * Returns the number of link to display according to 'nb' user input parameter. |
274 | * | 274 | * |
275 | * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS. | 275 | * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS. |
276 | * If 'nb' is set to 'all', display all filtered links (max parameter). | 276 | * If 'nb' is set to 'all', display all filtered bookmarks (max parameter). |
277 | * | 277 | * |
278 | * @param int $max maximum number of links to display. | 278 | * @param int $max maximum number of bookmarks to display. |
279 | * | 279 | * |
280 | * @return int number of links to display. | 280 | * @return int number of bookmarks to display. |
281 | */ | 281 | */ |
282 | public function getNbLinks($max) | 282 | public function getNbLinks($max) |
283 | { | 283 | { |