]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/feed/FeedBuilder.php
2 namespace Shaarli\Feed
;
9 * Used to build ATOM and RSS feeds data.
14 * @var string Constant: RSS feed type.
16 public static $FEED_RSS = 'rss';
19 * @var string Constant: ATOM feed type.
21 public static $FEED_ATOM = 'atom';
24 * @var string Default language if the locale isn't set.
26 public static $DEFAULT_LANGUAGE = 'en-en';
29 * @var int Number of links to display in a feed by default.
31 public static $DEFAULT_NB_LINKS = 50;
34 * @var \Shaarli\Bookmark\LinkDB instance.
39 * @var string RSS or ATOM feed.
46 protected $serverInfo;
54 * @var boolean True if the user is currently logged in, false otherwise.
56 protected $isLoggedIn;
59 * @var boolean Use permalinks instead of direct links if true.
61 protected $usePermalinks;
64 * @var boolean true to hide dates in feeds.
69 * @var string server locale.
74 * @var DateTime Latest item date.
76 protected $latestDate;
81 * @param \Shaarli\Bookmark\LinkDB $linkDB LinkDB instance.
82 * @param string $feedType Type of feed.
83 * @param array $serverInfo $_SERVER.
84 * @param array $userInput $_GET.
85 * @param boolean $isLoggedIn True if the user is currently logged in,
88 public function __construct($linkDB, $feedType, $serverInfo, $userInput, $isLoggedIn)
90 $this->linkDB
= $linkDB;
91 $this->feedType
= $feedType;
92 $this->serverInfo
= $serverInfo;
93 $this->userInput
= $userInput;
94 $this->isLoggedIn
= $isLoggedIn;
98 * Build data for feed templates.
100 * @return array Formatted data for feeds templates.
102 public function buildData()
104 // Search for untagged links
105 if (isset($this->userInput
['searchtags']) && empty($this->userInput
['searchtags'])) {
106 $this->userInput
['searchtags'] = false;
109 // Optionally filter the results:
110 $linksToDisplay = $this->linkDB
->filterSearch($this->userInput
);
112 $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay));
114 // Can't use array_keys() because $link is a LinkDB instance and not a real array.
116 foreach ($linksToDisplay as $key => $value) {
120 $pageaddr = escape(index_url($this->serverInfo
));
121 $linkDisplayed = array();
122 for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++
) {
123 $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr);
126 $data['language'] = $this->getTypeLanguage();
127 $data['last_update'] = $this->getLatestDateFormatted();
128 $data['show_dates'] = !$this->hideDates
|| $this->isLoggedIn
;
129 // Remove leading slash from REQUEST_URI.
130 $data['self_link'] = escape(server_url($this->serverInfo
))
131 . escape($this->serverInfo
['REQUEST_URI']);
132 $data['index_url'] = $pageaddr;
133 $data['usepermalinks'] = $this->usePermalinks
=== true;
134 $data['links'] = $linkDisplayed;
140 * Build a feed item (one per shaare).
142 * @param array $link Single link array extracted from LinkDB.
143 * @param string $pageaddr Index URL.
145 * @return array Link array with feed attributes.
147 protected function buildItem($link, $pageaddr)
149 $link['guid'] = $pageaddr . '?' . $link['shorturl'];
150 // Prepend the root URL for notes
151 if (is_note($link['url'])) {
152 $link['url'] = $pageaddr . $link['url'];
154 if ($this->usePermalinks
=== true) {
155 $permalink = '<a href="' . $link['url'] . '" title="' . t('Direct link') . '">' . t('Direct link') . '</a>';
157 $permalink = '<a href="' . $link['guid'] . '" title="' . t('Permalink') . '">' . t('Permalink') . '</a>';
159 $link['description'] = format_description($link['description'], $pageaddr);
160 $link['description'] .= PHP_EOL
. PHP_EOL
. '<br>— ' . $permalink;
162 $pubDate = $link['created'];
163 $link['pub_iso_date'] = $this->getIsoDate($pubDate);
165 // atom:entry elements MUST contain exactly one atom:updated element.
166 if (!empty($link['updated'])) {
167 $upDate = $link['updated'];
168 $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime
::ATOM
);
170 $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime
::ATOM
);
173 // Save the more recent item.
174 if (empty($this->latestDate
) || $this->latestDate
< $pubDate) {
175 $this->latestDate
= $pubDate;
177 if (!empty($upDate) && $this->latestDate
< $upDate) {
178 $this->latestDate
= $upDate;
181 $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
182 uasort($taglist, 'strcasecmp');
183 $link['taglist'] = $taglist;
189 * Set this to true to use permalinks instead of direct links.
191 * @param boolean $usePermalinks true to force permalinks.
193 public function setUsePermalinks($usePermalinks)
195 $this->usePermalinks
= $usePermalinks;
199 * Set this to true to hide timestamps in feeds.
201 * @param boolean $hideDates true to enable.
203 public function setHideDates($hideDates)
205 $this->hideDates
= $hideDates;
209 * Set the locale. Used to show feed language.
211 * @param string $locale The locale (eg. 'fr_FR.UTF8').
213 public function setLocale($locale)
215 $this->locale
= strtolower($locale);
219 * Get the language according to the feed type, based on the locale:
221 * - RSS format: en-us (default: 'en-en').
222 * - ATOM format: fr (default: 'en').
224 * @return string The language.
226 public function getTypeLanguage()
228 // Use the locale do define the language, if available.
229 if (!empty($this->locale
) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale
)) {
230 $length = ($this->feedType
=== self
::$FEED_RSS) ? 5 : 2;
231 return str_replace('_', '-', substr($this->locale
, 0, $length));
233 return ($this->feedType
=== self
::$FEED_RSS) ? 'en-en' : 'en';
237 * Format the latest item date found according to the feed type.
239 * Return an empty string if invalid DateTime is passed.
241 * @return string Formatted date.
243 protected function getLatestDateFormatted()
245 if (empty($this->latestDate
) || !$this->latestDate
instanceof DateTime
) {
249 $type = ($this->feedType
== self
::$FEED_RSS) ? DateTime
::RSS
: DateTime
::ATOM
;
250 return $this->latestDate
->format($type);
254 * Get ISO date from DateTime according to feed type.
256 * @param DateTime $date Date to format.
257 * @param string|bool $format Force format.
259 * @return string Formatted date.
261 protected function getIsoDate(DateTime
$date, $format = false)
263 if ($format !== false) {
264 return $date->format($format);
266 if ($this->feedType
== self
::$FEED_RSS) {
267 return $date->format(DateTime
::RSS
);
269 return $date->format(DateTime
::ATOM
);
273 * Returns the number of link to display according to 'nb' user input parameter.
275 * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
276 * If 'nb' is set to 'all', display all filtered links (max parameter).
278 * @param int $max maximum number of links to display.
280 * @return int number of links to display.
282 public function getNbLinks($max)
284 if (empty($this->userInput
['nb'])) {
285 return self
::$DEFAULT_NB_LINKS;
288 if ($this->userInput
['nb'] == 'all') {
292 $intNb = intval($this->userInput
['nb']);
293 if (!is_int($intNb) || $intNb == 0) {
294 return self
::$DEFAULT_NB_LINKS;