]>
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 // Search for untagged links
101 if (isset($this->userInput
['searchtags']) && empty($this->userInput
['searchtags'])) {
102 $this->userInput
['searchtags'] = false;
105 // Optionally filter the results:
106 $linksToDisplay = $this->linkDB
->filterSearch($this->userInput
);
108 $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay));
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 $linkDisplayed = array();
118 for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++
) {
119 $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr);
122 $data['language'] = $this->getTypeLanguage();
123 $data['last_update'] = $this->getLatestDateFormatted();
124 $data['show_dates'] = !$this->hideDates
|| $this->isLoggedIn
;
125 // Remove leading slash from REQUEST_URI.
126 $data['self_link'] = escape(server_url($this->serverInfo
))
127 . escape($this->serverInfo
['REQUEST_URI']);
128 $data['index_url'] = $pageaddr;
129 $data['usepermalinks'] = $this->usePermalinks
=== true;
130 $data['links'] = $linkDisplayed;
136 * Build a feed item (one per shaare).
138 * @param array $link Single link array extracted from LinkDB.
139 * @param string $pageaddr Index URL.
141 * @return array Link array with feed attributes.
143 protected function buildItem($link, $pageaddr)
145 $link['guid'] = $pageaddr .'?'. $link['shorturl'];
146 // Check for both signs of a note: starting with ? and 7 chars long.
147 if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
148 $link['url'] = $pageaddr . $link['url'];
150 if ($this->usePermalinks
=== true) {
151 $permalink = '<a href="'. $link['url'] .'" title="'. t('Direct link') .'">'. t('Direct link') .'</a>';
153 $permalink = '<a href="'. $link['guid'] .'" title="'. t('Permalink') .'">'. t('Permalink') .'</a>';
155 $link['description'] = format_description($link['description'], '', false, $pageaddr);
156 $link['description'] .= PHP_EOL
.'<br>— '. $permalink;
158 $pubDate = $link['created'];
159 $link['pub_iso_date'] = $this->getIsoDate($pubDate);
161 // atom:entry elements MUST contain exactly one atom:updated element.
162 if (!empty($link['updated'])) {
163 $upDate = $link['updated'];
164 $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime
::ATOM
);
166 $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime
::ATOM
);;
169 // Save the more recent item.
170 if (empty($this->latestDate
) || $this->latestDate
< $pubDate) {
171 $this->latestDate
= $pubDate;
173 if (!empty($upDate) && $this->latestDate
< $upDate) {
174 $this->latestDate
= $upDate;
177 $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
178 uasort($taglist, 'strcasecmp');
179 $link['taglist'] = $taglist;
185 * Set this to true to use permalinks instead of direct links.
187 * @param boolean $usePermalinks true to force permalinks.
189 public function setUsePermalinks($usePermalinks)
191 $this->usePermalinks
= $usePermalinks;
195 * Set this to true to hide timestamps in feeds.
197 * @param boolean $hideDates true to enable.
199 public function setHideDates($hideDates)
201 $this->hideDates
= $hideDates;
205 * Set the locale. Used to show feed language.
207 * @param string $locale The locale (eg. 'fr_FR.UTF8').
209 public function setLocale($locale)
211 $this->locale
= strtolower($locale);
215 * Get the language according to the feed type, based on the locale:
217 * - RSS format: en-us (default: 'en-en').
218 * - ATOM format: fr (default: 'en').
220 * @return string The language.
222 public function getTypeLanguage()
224 // Use the locale do define the language, if available.
225 if (! empty($this->locale
) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale
)) {
226 $length = ($this->feedType
== self
::$FEED_RSS) ? 5 : 2;
227 return str_replace('_', '-', substr($this->locale
, 0, $length));
229 return ($this->feedType
== self
::$FEED_RSS) ? 'en-en' : 'en';
233 * Format the latest item date found according to the feed type.
235 * Return an empty string if invalid DateTime is passed.
237 * @return string Formatted date.
239 protected function getLatestDateFormatted()
241 if (empty($this->latestDate
) || !$this->latestDate
instanceof DateTime
) {
245 $type = ($this->feedType
== self
::$FEED_RSS) ? DateTime
::RSS
: DateTime
::ATOM
;
246 return $this->latestDate
->format($type);
250 * Get ISO date from DateTime according to feed type.
252 * @param DateTime $date Date to format.
253 * @param string|bool $format Force format.
255 * @return string Formatted date.
257 protected function getIsoDate(DateTime
$date, $format = false)
259 if ($format !== false) {
260 return $date->format($format);
262 if ($this->feedType
== self
::$FEED_RSS) {
263 return $date->format(DateTime
::RSS
);
266 return $date->format(DateTime
::ATOM
);
270 * Returns the number of link to display according to 'nb' user input parameter.
272 * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
273 * If 'nb' is set to 'all', display all filtered links (max parameter).
275 * @param int $max maximum number of links to display.
277 * @return int number of links to display.
279 public function getNbLinks($max)
281 if (empty($this->userInput
['nb'])) {
282 return self
::$DEFAULT_NB_LINKS;
285 if ($this->userInput
['nb'] == 'all') {
289 $intNb = intval($this->userInput
['nb']);
290 if (! is_int($intNb) || $intNb == 0) {
291 return self
::$DEFAULT_NB_LINKS;