]>
Commit | Line | Data |
---|---|---|
82e36802 | 1 | <?php |
53054b2b | 2 | |
dfc650aa V |
3 | namespace Shaarli\Feed; |
4 | ||
5 | use DateTime; | |
cf92b4dd A |
6 | use Shaarli\Bookmark\Bookmark; |
7 | use Shaarli\Bookmark\BookmarkServiceInterface; | |
8 | use Shaarli\Formatter\BookmarkFormatter; | |
82e36802 A |
9 | |
10 | /** | |
11 | * FeedBuilder class. | |
12 | * | |
13 | * Used to build ATOM and RSS feeds data. | |
14 | */ | |
15 | class FeedBuilder | |
16 | { | |
17 | /** | |
18 | * @var string Constant: RSS feed type. | |
19 | */ | |
20 | public static $FEED_RSS = 'rss'; | |
21 | ||
22 | /** | |
23 | * @var string Constant: ATOM feed type. | |
24 | */ | |
25 | public static $FEED_ATOM = 'atom'; | |
26 | ||
27 | /** | |
28 | * @var string Default language if the locale isn't set. | |
29 | */ | |
30 | public static $DEFAULT_LANGUAGE = 'en-en'; | |
31 | ||
32 | /** | |
cf92b4dd | 33 | * @var int Number of bookmarks to display in a feed by default. |
82e36802 A |
34 | */ |
35 | public static $DEFAULT_NB_LINKS = 50; | |
36 | ||
37 | /** | |
cf92b4dd | 38 | * @var BookmarkServiceInterface instance. |
82e36802 A |
39 | */ |
40 | protected $linkDB; | |
41 | ||
cf92b4dd A |
42 | /** |
43 | * @var BookmarkFormatter instance. | |
44 | */ | |
45 | protected $formatter; | |
46 | ||
f4929b11 | 47 | /** @var mixed[] $_SERVER */ |
82e36802 A |
48 | protected $serverInfo; |
49 | ||
82e36802 A |
50 | /** |
51 | * @var boolean True if the user is currently logged in, false otherwise. | |
52 | */ | |
53 | protected $isLoggedIn; | |
54 | ||
55 | /** | |
cf92b4dd | 56 | * @var boolean Use permalinks instead of direct bookmarks if true. |
82e36802 A |
57 | */ |
58 | protected $usePermalinks; | |
59 | ||
60 | /** | |
61 | * @var boolean true to hide dates in feeds. | |
62 | */ | |
63 | protected $hideDates; | |
64 | ||
82e36802 A |
65 | /** |
66 | * @var string server locale. | |
67 | */ | |
68 | protected $locale; | |
82e36802 A |
69 | /** |
70 | * @var DateTime Latest item date. | |
71 | */ | |
72 | protected $latestDate; | |
73 | ||
74 | /** | |
75 | * Feed constructor. | |
76 | * | |
cf92b4dd A |
77 | * @param BookmarkServiceInterface $linkDB LinkDB instance. |
78 | * @param BookmarkFormatter $formatter instance. | |
f24896b2 | 79 | * @param array $serverInfo $_SERVER. |
cf92b4dd | 80 | * @param boolean $isLoggedIn True if the user is currently logged in, false otherwise. |
82e36802 | 81 | */ |
7b2ba6ef | 82 | public function __construct($linkDB, $formatter, $serverInfo, $isLoggedIn) |
82e36802 A |
83 | { |
84 | $this->linkDB = $linkDB; | |
cf92b4dd | 85 | $this->formatter = $formatter; |
82e36802 | 86 | $this->serverInfo = $serverInfo; |
82e36802 A |
87 | $this->isLoggedIn = $isLoggedIn; |
88 | } | |
89 | ||
90 | /** | |
91 | * Build data for feed templates. | |
92 | * | |
f4929b11 A |
93 | * @param string $feedType Type of feed (RSS/ATOM). |
94 | * @param array $userInput $_GET. | |
95 | * | |
82e36802 A |
96 | * @return array Formatted data for feeds templates. |
97 | */ | |
f4929b11 | 98 | public function buildData(string $feedType, ?array $userInput) |
82e36802 | 99 | { |
cf92b4dd | 100 | // Search for untagged bookmarks |
f4929b11 A |
101 | if (isset($this->userInput['searchtags']) && empty($userInput['searchtags'])) { |
102 | $userInput['searchtags'] = false; | |
7d86f40b A |
103 | } |
104 | ||
82e36802 | 105 | // Optionally filter the results: |
efb7d21b | 106 | $linksToDisplay = $this->linkDB->search($userInput ?? [], null, false, false, true); |
82e36802 | 107 | |
f4929b11 | 108 | $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay), $userInput); |
82e36802 A |
109 | |
110 | // Can't use array_keys() because $link is a LinkDB instance and not a real array. | |
53054b2b | 111 | $keys = []; |
82e36802 A |
112 | foreach ($linksToDisplay as $key => $value) { |
113 | $keys[] = $key; | |
114 | } | |
115 | ||
116 | $pageaddr = escape(index_url($this->serverInfo)); | |
cf92b4dd | 117 | $this->formatter->addContextData('index_url', $pageaddr); |
53054b2b | 118 | $linkDisplayed = []; |
82e36802 | 119 | for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) { |
f4929b11 | 120 | $linkDisplayed[$keys[$i]] = $this->buildItem($feedType, $linksToDisplay[$keys[$i]], $pageaddr); |
82e36802 A |
121 | } |
122 | ||
f4929b11 A |
123 | $data['language'] = $this->getTypeLanguage($feedType); |
124 | $data['last_update'] = $this->getLatestDateFormatted($feedType); | |
82e36802 | 125 | $data['show_dates'] = !$this->hideDates || $this->isLoggedIn; |
650a5f09 A |
126 | // Remove leading path from REQUEST_URI (already contained in $pageaddr). |
127 | $requestUri = preg_replace('#(.*?/)(feed.*)#', '$2', escape($this->serverInfo['REQUEST_URI'])); | |
128 | $data['self_link'] = $pageaddr . $requestUri; | |
82e36802 A |
129 | $data['index_url'] = $pageaddr; |
130 | $data['usepermalinks'] = $this->usePermalinks === true; | |
131 | $data['links'] = $linkDisplayed; | |
132 | ||
133 | return $data; | |
134 | } | |
135 | ||
f4929b11 A |
136 | /** |
137 | * Set this to true to use permalinks instead of direct bookmarks. | |
138 | * | |
139 | * @param boolean $usePermalinks true to force permalinks. | |
140 | */ | |
141 | public function setUsePermalinks($usePermalinks) | |
142 | { | |
143 | $this->usePermalinks = $usePermalinks; | |
144 | } | |
145 | ||
146 | /** | |
147 | * Set this to true to hide timestamps in feeds. | |
148 | * | |
149 | * @param boolean $hideDates true to enable. | |
150 | */ | |
151 | public function setHideDates($hideDates) | |
152 | { | |
153 | $this->hideDates = $hideDates; | |
154 | } | |
155 | ||
156 | /** | |
157 | * Set the locale. Used to show feed language. | |
158 | * | |
159 | * @param string $locale The locale (eg. 'fr_FR.UTF8'). | |
160 | */ | |
161 | public function setLocale($locale) | |
162 | { | |
163 | $this->locale = strtolower($locale); | |
164 | } | |
165 | ||
82e36802 A |
166 | /** |
167 | * Build a feed item (one per shaare). | |
168 | * | |
f4929b11 | 169 | * @param string $feedType Type of feed (RSS/ATOM). |
cf92b4dd A |
170 | * @param Bookmark $link Single link array extracted from LinkDB. |
171 | * @param string $pageaddr Index URL. | |
82e36802 A |
172 | * |
173 | * @return array Link array with feed attributes. | |
174 | */ | |
f4929b11 | 175 | protected function buildItem(string $feedType, $link, $pageaddr) |
82e36802 | 176 | { |
cf92b4dd | 177 | $data = $this->formatter->format($link); |
301c7ab1 | 178 | $data['guid'] = rtrim($pageaddr, '/') . '/shaare/' . $data['shorturl']; |
82e36802 | 179 | if ($this->usePermalinks === true) { |
53054b2b | 180 | $permalink = '<a href="' . $data['url'] . '" title="' . t('Direct link') . '">' . t('Direct link') . '</a>'; |
82e36802 | 181 | } else { |
53054b2b | 182 | $permalink = '<a href="' . $data['guid'] . '" title="' . t('Permalink') . '">' . t('Permalink') . '</a>'; |
82e36802 | 183 | } |
cf92b4dd | 184 | $data['description'] .= PHP_EOL . PHP_EOL . '<br>— ' . $permalink; |
82e36802 | 185 | |
f4929b11 | 186 | $data['pub_iso_date'] = $this->getIsoDate($feedType, $data['created']); |
82e36802 | 187 | |
c6d876bb | 188 | // atom:entry elements MUST contain exactly one atom:updated element. |
cf92b4dd | 189 | if (!empty($link->getUpdated())) { |
f4929b11 | 190 | $data['up_iso_date'] = $this->getIsoDate($feedType, $data['updated'], DateTime::ATOM); |
82e36802 | 191 | } else { |
f4929b11 | 192 | $data['up_iso_date'] = $this->getIsoDate($feedType, $data['created'], DateTime::ATOM); |
82e36802 A |
193 | } |
194 | ||
195 | // Save the more recent item. | |
cf92b4dd A |
196 | if (empty($this->latestDate) || $this->latestDate < $data['created']) { |
197 | $this->latestDate = $data['created']; | |
c6d876bb | 198 | } |
cf92b4dd A |
199 | if (!empty($data['updated']) && $this->latestDate < $data['updated']) { |
200 | $this->latestDate = $data['updated']; | |
82e36802 A |
201 | } |
202 | ||
cf92b4dd | 203 | return $data; |
82e36802 A |
204 | } |
205 | ||
82e36802 A |
206 | /** |
207 | * Get the language according to the feed type, based on the locale: | |
208 | * | |
209 | * - RSS format: en-us (default: 'en-en'). | |
210 | * - ATOM format: fr (default: 'en'). | |
211 | * | |
f4929b11 A |
212 | * @param string $feedType Type of feed (RSS/ATOM). |
213 | * | |
82e36802 A |
214 | * @return string The language. |
215 | */ | |
f4929b11 | 216 | protected function getTypeLanguage(string $feedType) |
82e36802 A |
217 | { |
218 | // Use the locale do define the language, if available. | |
dfc650aa | 219 | if (!empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) { |
f4929b11 | 220 | $length = ($feedType === self::$FEED_RSS) ? 5 : 2; |
82e36802 A |
221 | return str_replace('_', '-', substr($this->locale, 0, $length)); |
222 | } | |
f4929b11 | 223 | return ($feedType === self::$FEED_RSS) ? 'en-en' : 'en'; |
82e36802 A |
224 | } |
225 | ||
226 | /** | |
227 | * Format the latest item date found according to the feed type. | |
228 | * | |
229 | * Return an empty string if invalid DateTime is passed. | |
230 | * | |
f4929b11 A |
231 | * @param string $feedType Type of feed (RSS/ATOM). |
232 | * | |
82e36802 A |
233 | * @return string Formatted date. |
234 | */ | |
f4929b11 | 235 | protected function getLatestDateFormatted(string $feedType) |
82e36802 A |
236 | { |
237 | if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) { | |
238 | return ''; | |
239 | } | |
240 | ||
f4929b11 | 241 | $type = ($feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM; |
82e36802 A |
242 | return $this->latestDate->format($type); |
243 | } | |
244 | ||
c6d876bb A |
245 | /** |
246 | * Get ISO date from DateTime according to feed type. | |
247 | * | |
f4929b11 | 248 | * @param string $feedType Type of feed (RSS/ATOM). |
c6d876bb A |
249 | * @param DateTime $date Date to format. |
250 | * @param string|bool $format Force format. | |
251 | * | |
252 | * @return string Formatted date. | |
253 | */ | |
f4929b11 | 254 | protected function getIsoDate(string $feedType, DateTime $date, $format = false) |
c6d876bb A |
255 | { |
256 | if ($format !== false) { | |
257 | return $date->format($format); | |
258 | } | |
f4929b11 | 259 | if ($feedType == self::$FEED_RSS) { |
c6d876bb | 260 | return $date->format(DateTime::RSS); |
c6d876bb A |
261 | } |
262 | return $date->format(DateTime::ATOM); | |
263 | } | |
264 | ||
82e36802 A |
265 | /** |
266 | * Returns the number of link to display according to 'nb' user input parameter. | |
267 | * | |
268 | * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS. | |
cf92b4dd | 269 | * If 'nb' is set to 'all', display all filtered bookmarks (max parameter). |
82e36802 | 270 | * |
f4929b11 A |
271 | * @param int $max maximum number of bookmarks to display. |
272 | * @param array $userInput $_GET. | |
82e36802 | 273 | * |
cf92b4dd | 274 | * @return int number of bookmarks to display. |
82e36802 | 275 | */ |
f4929b11 | 276 | protected function getNbLinks($max, ?array $userInput) |
82e36802 | 277 | { |
f4929b11 | 278 | if (empty($userInput['nb'])) { |
82e36802 A |
279 | return self::$DEFAULT_NB_LINKS; |
280 | } | |
281 | ||
f4929b11 | 282 | if ($userInput['nb'] == 'all') { |
82e36802 A |
283 | return $max; |
284 | } | |
285 | ||
f4929b11 | 286 | $intNb = intval($userInput['nb']); |
dfc650aa | 287 | if (!is_int($intNb) || $intNb == 0) { |
82e36802 A |
288 | return self::$DEFAULT_NB_LINKS; |
289 | } | |
290 | ||
291 | return $intNb; | |
292 | } | |
293 | } |