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