]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/FeedBuilder.php
6 * Used to build ATOM and RSS feeds data.
11 * @var string Constant: RSS feed type.
13 public static $FEED_RSS = 'rss';
16 * @var string Constant: ATOM feed type.
18 public static $FEED_ATOM = 'atom';
21 * @var string Default language if the locale isn't set.
23 public static $DEFAULT_LANGUAGE = 'en-en';
26 * @var int Number of links to display in a feed by default.
28 public static $DEFAULT_NB_LINKS = 50;
31 * @var LinkDB instance.
36 * @var string RSS or ATOM feed.
41 * @var array $_SERVER.
43 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 links if true.
58 protected $usePermalinks;
61 * @var boolean true to hide dates in feeds.
66 * @var string server locale.
71 * @var DateTime Latest item date.
73 protected $latestDate;
78 * @param LinkDB $linkDB LinkDB instance.
79 * @param string $feedType Type of feed.
80 * @param array $serverInfo $_SERVER.
81 * @param array $userInput $_GET.
82 * @param boolean $isLoggedIn True if the user is currently logged in, false otherwise.
84 public function __construct($linkDB, $feedType, $serverInfo, $userInput, $isLoggedIn)
86 $this->linkDB
= $linkDB;
87 $this->feedType
= $feedType;
88 $this->serverInfo
= $serverInfo;
89 $this->userInput
= $userInput;
90 $this->isLoggedIn
= $isLoggedIn;
94 * Build data for feed templates.
96 * @return array Formatted data for feeds templates.
98 public function buildData()
100 // Optionally filter the results:
101 $linksToDisplay = $this->linkDB
->filterSearch($this->userInput
);
103 $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay));
105 // Can't use array_keys() because $link is a LinkDB instance and not a real array.
107 foreach ($linksToDisplay as $key => $value) {
111 $pageaddr = escape(index_url($this->serverInfo
));
112 $linkDisplayed = array();
113 for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++
) {
114 $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr);
117 $data['language'] = $this->getTypeLanguage();
118 $data['last_update'] = $this->getLatestDateFormatted();
119 $data['show_dates'] = !$this->hideDates
|| $this->isLoggedIn
;
120 // Remove leading slash from REQUEST_URI.
121 $data['self_link'] = escape(server_url($this->serverInfo
))
122 . escape($this->serverInfo
['REQUEST_URI']);
123 $data['index_url'] = $pageaddr;
124 $data['usepermalinks'] = $this->usePermalinks
=== true;
125 $data['links'] = $linkDisplayed;
131 * Build a feed item (one per shaare).
133 * @param array $link Single link array extracted from LinkDB.
134 * @param string $pageaddr Index URL.
136 * @return array Link array with feed attributes.
138 protected function buildItem($link, $pageaddr)
140 $link['guid'] = $pageaddr .'?'. $link['shorturl'];
141 // Check for both signs of a note: starting with ? and 7 chars long.
142 if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
143 $link['url'] = $pageaddr . $link['url'];
145 if ($this->usePermalinks
=== true) {
146 $permalink = '<a href="'. $link['url'] .'" title="Direct link">Direct link</a>';
148 $permalink = '<a href="'. $link['guid'] .'" title="Permalink">Permalink</a>';
150 $link['description'] = format_description($link['description'], '', $pageaddr);
151 $link['description'] .= PHP_EOL
.'<br>— '. $permalink;
153 $pubDate = $link['created'];
154 $link['pub_iso_date'] = $this->getIsoDate($pubDate);
156 // atom:entry elements MUST contain exactly one atom:updated element.
157 if (!empty($link['updated'])) {
158 $upDate = $link['updated'];
159 $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime
::ATOM
);
161 $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime
::ATOM
);;
164 // Save the more recent item.
165 if (empty($this->latestDate
) || $this->latestDate
< $pubDate) {
166 $this->latestDate
= $pubDate;
168 if (!empty($upDate) && $this->latestDate
< $upDate) {
169 $this->latestDate
= $upDate;
172 $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
173 uasort($taglist, 'strcasecmp');
174 $link['taglist'] = $taglist;
180 * Set this to true to use permalinks instead of direct links.
182 * @param boolean $usePermalinks true to force permalinks.
184 public function setUsePermalinks($usePermalinks)
186 $this->usePermalinks
= $usePermalinks;
190 * Set this to true to hide timestamps in feeds.
192 * @param boolean $hideDates true to enable.
194 public function setHideDates($hideDates)
196 $this->hideDates
= $hideDates;
200 * Set the locale. Used to show feed language.
202 * @param string $locale The locale (eg. 'fr_FR.UTF8').
204 public function setLocale($locale)
206 $this->locale
= strtolower($locale);
210 * Get the language according to the feed type, based on the locale:
212 * - RSS format: en-us (default: 'en-en').
213 * - ATOM format: fr (default: 'en').
215 * @return string The language.
217 public function getTypeLanguage()
219 // Use the locale do define the language, if available.
220 if (! empty($this->locale
) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale
)) {
221 $length = ($this->feedType
== self
::$FEED_RSS) ? 5 : 2;
222 return str_replace('_', '-', substr($this->locale
, 0, $length));
224 return ($this->feedType
== self
::$FEED_RSS) ? 'en-en' : 'en';
228 * Format the latest item date found according to the feed type.
230 * Return an empty string if invalid DateTime is passed.
232 * @return string Formatted date.
234 protected function getLatestDateFormatted()
236 if (empty($this->latestDate
) || !$this->latestDate
instanceof DateTime
) {
240 $type = ($this->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 DateTime $date Date to format.
248 * @param string|bool $format Force format.
250 * @return string Formatted date.
252 protected function getIsoDate(DateTime
$date, $format = false)
254 if ($format !== false) {
255 return $date->format($format);
257 if ($this->feedType
== self
::$FEED_RSS) {
258 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 links (max parameter).
270 * @param int $max maximum number of links to display.
272 * @return int number of links to display.
274 public function getNbLinks($max)
276 if (empty($this->userInput
['nb'])) {
277 return self
::$DEFAULT_NB_LINKS;
280 if ($this->userInput
['nb'] == 'all') {
284 $intNb = intval($this->userInput
['nb']);
285 if (! is_int($intNb) || $intNb == 0) {
286 return self
::$DEFAULT_NB_LINKS;