3 namespace Shaarli\Feed
;
6 use Shaarli\Bookmark\Bookmark
;
7 use Shaarli\Bookmark\BookmarkServiceInterface
;
8 use Shaarli\Formatter\BookmarkFormatter
;
13 * Used to build ATOM and RSS feeds data.
18 * @var string Constant: RSS feed type.
20 public static $FEED_RSS = 'rss';
23 * @var string Constant: ATOM feed type.
25 public static $FEED_ATOM = 'atom';
28 * @var string Default language if the locale isn't set.
30 public static $DEFAULT_LANGUAGE = 'en-en';
33 * @var int Number of bookmarks to display in a feed by default.
35 public static $DEFAULT_NB_LINKS = 50;
38 * @var BookmarkServiceInterface instance.
43 * @var BookmarkFormatter instance.
47 /** @var mixed[] $_SERVER */
48 protected $serverInfo;
51 * @var boolean True if the user is currently logged in, false otherwise.
53 protected $isLoggedIn;
56 * @var boolean Use permalinks instead of direct bookmarks if true.
58 protected $usePermalinks;
61 * @var boolean true to hide dates in feeds.
66 * @var string server locale.
70 * @var DateTime Latest item date.
72 protected $latestDate;
77 * @param BookmarkServiceInterface $linkDB LinkDB instance.
78 * @param BookmarkFormatter $formatter instance.
79 * @param array $serverInfo $_SERVER.
80 * @param boolean $isLoggedIn True if the user is currently logged in, false otherwise.
82 public function __construct($linkDB, $formatter, $serverInfo, $isLoggedIn)
84 $this->linkDB
= $linkDB;
85 $this->formatter
= $formatter;
86 $this->serverInfo
= $serverInfo;
87 $this->isLoggedIn
= $isLoggedIn;
91 * Build data for feed templates.
93 * @param string $feedType Type of feed (RSS/ATOM).
94 * @param array $userInput $_GET.
96 * @return array Formatted data for feeds templates.
98 public function buildData(string $feedType, ?array $userInput)
100 // Search for untagged bookmarks
101 if (isset($this->userInput
['searchtags']) && empty($userInput['searchtags'])) {
102 $userInput['searchtags'] = false;
105 // Optionally filter the results:
106 $linksToDisplay = $this->linkDB
->search($userInput ?? [], null, false, false, true);
108 $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay), $userInput);
110 // Can't use array_keys() because $link is a LinkDB instance and not a real array.
112 foreach ($linksToDisplay as $key => $value) {
116 $pageaddr = escape(index_url($this->serverInfo
));
117 $this->formatter
->addContextData('index_url', $pageaddr);
119 for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++
) {
120 $linkDisplayed[$keys[$i]] = $this->buildItem($feedType, $linksToDisplay[$keys[$i]], $pageaddr);
123 $data['language'] = $this->getTypeLanguage($feedType);
124 $data['last_update'] = $this->getLatestDateFormatted($feedType);
125 $data['show_dates'] = !$this->hideDates
|| $this->isLoggedIn
;
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;
129 $data['index_url
'] = $pageaddr;
130 $data['usepermalinks
'] = $this->usePermalinks === true;
131 $data['links
'] = $linkDisplayed;
137 * Set this to true to use permalinks instead of direct bookmarks.
139 * @param boolean $usePermalinks true to force permalinks.
141 public function setUsePermalinks($usePermalinks)
143 $this->usePermalinks = $usePermalinks;
147 * Set this to true to hide timestamps in feeds.
149 * @param boolean $hideDates true to enable.
151 public function setHideDates($hideDates)
153 $this->hideDates = $hideDates;
157 * Set the locale. Used to show feed language.
159 * @param string $locale The locale (eg. 'fr_FR
.UTF8
').
161 public function setLocale($locale)
163 $this->locale = strtolower($locale);
167 * Build a feed item (one per shaare).
169 * @param string $feedType Type of feed (RSS/ATOM).
170 * @param Bookmark $link Single link array extracted from LinkDB.
171 * @param string $pageaddr Index URL.
173 * @return array Link array with feed attributes.
175 protected function buildItem(string $feedType, $link, $pageaddr)
177 $data = $this->formatter->format($link);
178 $data['guid
'] = rtrim($pageaddr, '/') . '/shaare
/' . $data['shorturl
'];
179 if ($this->usePermalinks === true) {
180 $permalink = '<a href
="' . $data['url'] . '" title
="' . t('Direct link') . '">' . t('Direct link
') . '</a
>';
182 $permalink = '<a href
="' . $data['guid'] . '" title
="' . t('Permalink') . '">' . t('Permalink
') . '</a
>';
184 $data['description
'] .= PHP_EOL . PHP_EOL . '<br
>— ' . $permalink;
186 $data['pub_iso_date'] = $this->getIsoDate($feedType, $data['created']);
188 // atom:entry elements MUST contain exactly one atom:updated element.
189 if (!empty($link->getUpdated())) {
190 $data['up_iso_date'] = $this->getIsoDate($feedType, $data['updated'], DateTime
::ATOM
);
192 $data['up_iso_date'] = $this->getIsoDate($feedType, $data['created'], DateTime
::ATOM
);
195 // Save the more recent item.
196 if (empty($this->latestDate
) || $this->latestDate
< $data['created']) {
197 $this->latestDate
= $data['created'];
199 if (!empty($data['updated']) && $this->latestDate
< $data['updated']) {
200 $this->latestDate
= $data['updated'];
207 * Get the language according to the feed type, based on the locale:
209 * - RSS format: en-us (default: 'en-en').
210 * - ATOM format: fr (default: 'en').
212 * @param string $feedType Type of feed (RSS/ATOM).
214 * @return string The language.
216 protected function getTypeLanguage(string $feedType)
218 // Use the locale do define the language, if available.
219 if (!empty($this->locale
) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale
)) {
220 $length = ($feedType === self
::$FEED_RSS) ? 5 : 2;
221 return str_replace('_', '-', substr($this->locale
, 0, $length));
223 return ($feedType === self
::$FEED_RSS) ? 'en-en' : 'en';
227 * Format the latest item date found according to the feed type.
229 * Return an empty string if invalid DateTime is passed.
231 * @param string $feedType Type of feed (RSS/ATOM).
233 * @return string Formatted date.
235 protected function getLatestDateFormatted(string $feedType)
237 if (empty($this->latestDate
) || !$this->latestDate
instanceof DateTime
) {
241 $type = ($feedType == self
::$FEED_RSS) ? DateTime
::RSS
: DateTime
::ATOM
;
242 return $this->latestDate
->format($type);
246 * Get ISO date from DateTime according to feed type.
248 * @param string $feedType Type of feed (RSS/ATOM).
249 * @param DateTime $date Date to format.
250 * @param string|bool $format Force format.
252 * @return string Formatted date.
254 protected function getIsoDate(string $feedType, DateTime
$date, $format = false)
256 if ($format !== false) {
257 return $date->format($format);
259 if ($feedType == self
::$FEED_RSS) {
260 return $date->format(DateTime
::RSS
);
262 return $date->format(DateTime
::ATOM
);
266 * Returns the number of link to display according to 'nb' user input parameter.
268 * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
269 * If 'nb' is set to 'all', display all filtered bookmarks (max parameter).
271 * @param int $max maximum number of bookmarks to display.
272 * @param array $userInput $_GET.
274 * @return int number of bookmarks to display.
276 protected function getNbLinks($max, ?array $userInput)
278 if (empty($userInput['nb'])) {
279 return self
::$DEFAULT_NB_LINKS;
282 if ($userInput['nb'] == 'all') {
286 $intNb = intval($userInput['nb']);
287 if (!is_int($intNb) || $intNb == 0) {
288 return self
::$DEFAULT_NB_LINKS;