2 namespace Shaarli\Feed
;
5 use Shaarli\Bookmark\Bookmark
;
6 use Shaarli\Bookmark\BookmarkServiceInterface
;
7 use Shaarli\Formatter\BookmarkFormatter
;
12 * Used to build ATOM and RSS feeds data.
17 * @var string Constant: RSS feed type.
19 public static $FEED_RSS = 'rss';
22 * @var string Constant: ATOM feed type.
24 public static $FEED_ATOM = 'atom';
27 * @var string Default language if the locale isn't set.
29 public static $DEFAULT_LANGUAGE = 'en-en';
32 * @var int Number of bookmarks to display in a feed by default.
34 public static $DEFAULT_NB_LINKS = 50;
37 * @var BookmarkServiceInterface instance.
42 * @var BookmarkFormatter instance.
46 /** @var mixed[] $_SERVER */
47 protected $serverInfo;
50 * @var boolean True if the user is currently logged in, false otherwise.
52 protected $isLoggedIn;
55 * @var boolean Use permalinks instead of direct bookmarks if true.
57 protected $usePermalinks;
60 * @var boolean true to hide dates in feeds.
65 * @var string server locale.
69 * @var DateTime Latest item date.
71 protected $latestDate;
76 * @param BookmarkServiceInterface $linkDB LinkDB instance.
77 * @param BookmarkFormatter $formatter instance.
78 * @param array $serverInfo $_SERVER.
79 * @param boolean $isLoggedIn True if the user is currently logged in, false otherwise.
81 public function __construct($linkDB, $formatter, $serverInfo, $isLoggedIn)
83 $this->linkDB
= $linkDB;
84 $this->formatter
= $formatter;
85 $this->serverInfo
= $serverInfo;
86 $this->isLoggedIn
= $isLoggedIn;
90 * Build data for feed templates.
92 * @param string $feedType Type of feed (RSS/ATOM).
93 * @param array $userInput $_GET.
95 * @return array Formatted data for feeds templates.
97 public function buildData(string $feedType, ?array $userInput)
99 // Search for untagged bookmarks
100 if (isset($this->userInput
['searchtags']) && empty($userInput['searchtags'])) {
101 $userInput['searchtags'] = false;
104 // Optionally filter the results:
105 $linksToDisplay = $this->linkDB
->search($userInput, null, false, false, true);
107 $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay), $userInput);
109 // Can't use array_keys() because $link is a LinkDB instance and not a real array.
111 foreach ($linksToDisplay as $key => $value) {
115 $pageaddr = escape(index_url($this->serverInfo
));
116 $this->formatter
->addContextData('index_url', $pageaddr);
117 $linkDisplayed = array();
118 for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++
) {
119 $linkDisplayed[$keys[$i]] = $this->buildItem($feedType, $linksToDisplay[$keys[$i]], $pageaddr);
122 $data['language'] = $this->getTypeLanguage($feedType);
123 $data['last_update'] = $this->getLatestDateFormatted($feedType);
124 $data['show_dates'] = !$this->hideDates
|| $this->isLoggedIn
;
125 // Remove leading path from REQUEST_URI (already contained in $pageaddr).
126 $requestUri = preg_replace('#(.*?/)(feed.*)#', '$2', escape($this->serverInfo['REQUEST_URI
']));
127 $data['self_link
'] = $pageaddr . $requestUri;
128 $data['index_url
'] = $pageaddr;
129 $data['usepermalinks
'] = $this->usePermalinks === true;
130 $data['links
'] = $linkDisplayed;
136 * Set this to true to use permalinks instead of direct bookmarks.
138 * @param boolean $usePermalinks true to force permalinks.
140 public function setUsePermalinks($usePermalinks)
142 $this->usePermalinks = $usePermalinks;
146 * Set this to true to hide timestamps in feeds.
148 * @param boolean $hideDates true to enable.
150 public function setHideDates($hideDates)
152 $this->hideDates = $hideDates;
156 * Set the locale. Used to show feed language.
158 * @param string $locale The locale (eg. 'fr_FR
.UTF8
').
160 public function setLocale($locale)
162 $this->locale = strtolower($locale);
166 * Build a feed item (one per shaare).
168 * @param string $feedType Type of feed (RSS/ATOM).
169 * @param Bookmark $link Single link array extracted from LinkDB.
170 * @param string $pageaddr Index URL.
172 * @return array Link array with feed attributes.
174 protected function buildItem(string $feedType, $link, $pageaddr)
176 $data = $this->formatter->format($link);
177 $data['guid
'] = rtrim($pageaddr, '/') . '/shaare
/' . $data['shorturl
'];
178 if ($this->usePermalinks === true) {
179 $permalink = '<a href
="'. $data['url'] .'" title
="'. t('Direct link') .'">'. t('Direct link
') .'</a
>';
181 $permalink = '<a href
="'. $data['guid'] .'" title
="'. t('Permalink') .'">'. t('Permalink
') .'</a
>';
183 $data['description
'] .= PHP_EOL . PHP_EOL . '<br
>— ' . $permalink;
185 $data['pub_iso_date'] = $this->getIsoDate($feedType, $data['created']);
187 // atom:entry elements MUST contain exactly one atom:updated element.
188 if (!empty($link->getUpdated())) {
189 $data['up_iso_date'] = $this->getIsoDate($feedType, $data['updated'], DateTime
::ATOM
);
191 $data['up_iso_date'] = $this->getIsoDate($feedType, $data['created'], DateTime
::ATOM
);
194 // Save the more recent item.
195 if (empty($this->latestDate
) || $this->latestDate
< $data['created']) {
196 $this->latestDate
= $data['created'];
198 if (!empty($data['updated']) && $this->latestDate
< $data['updated']) {
199 $this->latestDate
= $data['updated'];
206 * Get the language according to the feed type, based on the locale:
208 * - RSS format: en-us (default: 'en-en').
209 * - ATOM format: fr (default: 'en').
211 * @param string $feedType Type of feed (RSS/ATOM).
213 * @return string The language.
215 protected function getTypeLanguage(string $feedType)
217 // Use the locale do define the language, if available.
218 if (!empty($this->locale
) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale
)) {
219 $length = ($feedType === self
::$FEED_RSS) ? 5 : 2;
220 return str_replace('_', '-', substr($this->locale
, 0, $length));
222 return ($feedType === self
::$FEED_RSS) ? 'en-en' : 'en';
226 * Format the latest item date found according to the feed type.
228 * Return an empty string if invalid DateTime is passed.
230 * @param string $feedType Type of feed (RSS/ATOM).
232 * @return string Formatted date.
234 protected function getLatestDateFormatted(string $feedType)
236 if (empty($this->latestDate
) || !$this->latestDate
instanceof DateTime
) {
240 $type = ($feedType == self
::$FEED_RSS) ? DateTime
::RSS
: DateTime
::ATOM
;
241 return $this->latestDate
->format($type);
245 * Get ISO date from DateTime according to feed type.
247 * @param string $feedType Type of feed (RSS/ATOM).
248 * @param DateTime $date Date to format.
249 * @param string|bool $format Force format.
251 * @return string Formatted date.
253 protected function getIsoDate(string $feedType, DateTime
$date, $format = false)
255 if ($format !== false) {
256 return $date->format($format);
258 if ($feedType == self
::$FEED_RSS) {
259 return $date->format(DateTime
::RSS
);
261 return $date->format(DateTime
::ATOM
);
265 * Returns the number of link to display according to 'nb' user input parameter.
267 * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
268 * If 'nb' is set to 'all', display all filtered bookmarks (max parameter).
270 * @param int $max maximum number of bookmarks to display.
271 * @param array $userInput $_GET.
273 * @return int number of bookmarks to display.
275 protected function getNbLinks($max, ?array $userInput)
277 if (empty($userInput['nb'])) {
278 return self
::$DEFAULT_NB_LINKS;
281 if ($userInput['nb'] == 'all') {
285 $intNb = intval($userInput['nb']);
286 if (!is_int($intNb) || $intNb == 0) {
287 return self
::$DEFAULT_NB_LINKS;