]>
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 PubSub hub URL.
68 protected $pubsubhubUrl;
71 * @var string server locale.
76 * @var DateTime Latest item date.
78 protected $latestDate;
83 * @param LinkDB $linkDB LinkDB instance.
84 * @param string $feedType Type of feed.
85 * @param array $serverInfo $_SERVER.
86 * @param array $userInput $_GET.
87 * @param boolean $isLoggedIn True if the user is currently logged in, false otherwise.
89 public function __construct($linkDB, $feedType, $serverInfo, $userInput, $isLoggedIn)
91 $this->linkDB
= $linkDB;
92 $this->feedType
= $feedType;
93 $this->serverInfo
= $serverInfo;
94 $this->userInput
= $userInput;
95 $this->isLoggedIn
= $isLoggedIn;
99 * Build data for feed templates.
101 * @return array Formatted data for feeds templates.
103 public function buildData()
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['pubsubhub_url'] = $this->pubsubhubUrl
;
124 $data['last_update'] = $this->getLatestDateFormatted();
125 $data['show_dates'] = !$this->hideDates
|| $this->isLoggedIn
;
126 // Remove leading slash from REQUEST_URI.
127 $data['self_link'] = escape(server_url($this->serverInfo
))
128 . escape($this->serverInfo
['REQUEST_URI']);
129 $data['index_url'] = $pageaddr;
130 $data['usepermalinks'] = $this->usePermalinks
=== true;
131 $data['links'] = $linkDisplayed;
137 * Build a feed item (one per shaare).
139 * @param array $link Single link array extracted from LinkDB.
140 * @param string $pageaddr Index URL.
142 * @return array Link array with feed attributes.
144 protected function buildItem($link, $pageaddr)
146 $link['guid'] = $pageaddr .'?'. smallHash($link['linkdate']);
147 // Check for both signs of a note: starting with ? and 7 chars long.
148 if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
149 $link['url'] = $pageaddr . $link['url'];
151 if ($this->usePermalinks
=== true) {
152 $permalink = '<a href="'. $link['url'] .'" title="Direct link">Direct link</a>';
154 $permalink = '<a href="'. $link['guid'] .'" title="Permalink">Permalink</a>';
156 $link['description'] = format_description($link['description'], '', $pageaddr);
157 $link['description'] .= PHP_EOL
.'<br>— '. $permalink;
159 $pubDate = DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, $link['linkdate']);
160 $link['pub_iso_date'] = $this->getIsoDate($pubDate);
162 // atom:entry elements MUST contain exactly one atom:updated element.
163 if (!empty($link['updated'])) {
164 $upDate = DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, $link['updated']);
165 $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime
::ATOM
);
167 $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime
::ATOM
);;
170 // Save the more recent item.
171 if (empty($this->latestDate
) || $this->latestDate
< $pubDate) {
172 $this->latestDate
= $pubDate;
174 if (!empty($upDate) && $this->latestDate
< $upDate) {
175 $this->latestDate
= $upDate;
178 $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
179 uasort($taglist, 'strcasecmp');
180 $link['taglist'] = $taglist;
186 * Assign PubSub hub URL.
188 * @param string $pubsubhubUrl PubSub hub url.
190 public function setPubsubhubUrl($pubsubhubUrl)
192 $this->pubsubhubUrl
= $pubsubhubUrl;
196 * Set this to true to use permalinks instead of direct links.
198 * @param boolean $usePermalinks true to force permalinks.
200 public function setUsePermalinks($usePermalinks)
202 $this->usePermalinks
= $usePermalinks;
206 * Set this to true to hide timestamps in feeds.
208 * @param boolean $hideDates true to enable.
210 public function setHideDates($hideDates)
212 $this->hideDates
= $hideDates;
216 * Set the locale. Used to show feed language.
218 * @param string $locale The locale (eg. 'fr_FR.UTF8').
220 public function setLocale($locale)
222 $this->locale
= strtolower($locale);
226 * Get the language according to the feed type, based on the locale:
228 * - RSS format: en-us (default: 'en-en').
229 * - ATOM format: fr (default: 'en').
231 * @return string The language.
233 public function getTypeLanguage()
235 // Use the locale do define the language, if available.
236 if (! empty($this->locale
) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale
)) {
237 $length = ($this->feedType
== self
::$FEED_RSS) ? 5 : 2;
238 return str_replace('_', '-', substr($this->locale
, 0, $length));
240 return ($this->feedType
== self
::$FEED_RSS) ? 'en-en' : 'en';
244 * Format the latest item date found according to the feed type.
246 * Return an empty string if invalid DateTime is passed.
248 * @return string Formatted date.
250 protected function getLatestDateFormatted()
252 if (empty($this->latestDate
) || !$this->latestDate
instanceof DateTime
) {
256 $type = ($this->feedType
== self
::$FEED_RSS) ? DateTime
::RSS
: DateTime
::ATOM
;
257 return $this->latestDate
->format($type);
261 * Get ISO date from DateTime according to feed type.
263 * @param DateTime $date Date to format.
264 * @param string|bool $format Force format.
266 * @return string Formatted date.
268 protected function getIsoDate(DateTime
$date, $format = false)
270 if ($format !== false) {
271 return $date->format($format);
273 if ($this->feedType
== self
::$FEED_RSS) {
274 return $date->format(DateTime
::RSS
);
277 return $date->format(DateTime
::ATOM
);
281 * Returns the number of link to display according to 'nb' user input parameter.
283 * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
284 * If 'nb' is set to 'all', display all filtered links (max parameter).
286 * @param int $max maximum number of links to display.
288 * @return int number of links to display.
290 public function getNbLinks($max)
292 if (empty($this->userInput
['nb'])) {
293 return self
::$DEFAULT_NB_LINKS;
296 if ($this->userInput
['nb'] == 'all') {
300 $intNb = intval($this->userInput
['nb']);
301 if (! is_int($intNb) || $intNb == 0) {
302 return self
::$DEFAULT_NB_LINKS;