]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/FeedBuilder.php
50e0983152484a3e9fc53d06dd15f62debe60b7e
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 $searchtags = !empty($this->userInput
['searchtags']) ? escape($this->userInput
['searchtags']) : '';
107 $searchterm = !empty($this->userInput
['searchterm']) ? escape($this->userInput
['searchterm']) : '';
108 if (! empty($searchtags) && ! empty($searchterm)) {
109 $linksToDisplay = $this->linkDB
->filter(
110 LinkFilter
::$FILTER_TAG | LinkFilter
::$FILTER_TEXT,
111 array($searchtags, $searchterm)
114 elseif ($searchtags) {
115 $linksToDisplay = $this->linkDB
->filter(LinkFilter
::$FILTER_TAG, $searchtags);
117 elseif ($searchterm) {
118 $linksToDisplay = $this->linkDB
->filter(LinkFilter
::$FILTER_TEXT, $searchterm);
121 $linksToDisplay = $this->linkDB
;
124 $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay));
126 // Can't use array_keys() because $link is a LinkDB instance and not a real array.
128 foreach ($linksToDisplay as $key => $value) {
132 $pageaddr = escape(index_url($this->serverInfo
));
133 $linkDisplayed = array();
134 for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++
) {
135 $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr);
138 $data['language'] = $this->getTypeLanguage();
139 $data['pubsubhub_url'] = $this->pubsubhubUrl
;
140 $data['last_update'] = $this->getLatestDateFormatted();
141 $data['show_dates'] = !$this->hideDates
|| $this->isLoggedIn
;
142 // Remove leading slash from REQUEST_URI.
143 $data['self_link'] = $pageaddr . escape(ltrim($this->serverInfo
['REQUEST_URI'], '/'));
144 $data['index_url'] = $pageaddr;
145 $data['usepermalinks'] = $this->usePermalinks
=== true;
146 $data['links'] = $linkDisplayed;
152 * Build a feed item (one per shaare).
154 * @param array $link Single link array extracted from LinkDB.
155 * @param string $pageaddr Index URL.
157 * @return array Link array with feed attributes.
159 protected function buildItem($link, $pageaddr)
161 $link['guid'] = $pageaddr .'?'. smallHash($link['linkdate']);
162 // Check for both signs of a note: starting with ? and 7 chars long.
163 if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
164 $link['url'] = $pageaddr . $link['url'];
166 if ($this->usePermalinks
=== true) {
167 $permalink = '<a href="'. $link['url'] .'" title="Direct link">Direct link</a>';
169 $permalink = '<a href="'. $link['guid'] .'" title="Permalink">Permalink</a>';
171 $link['description'] = format_description($link['description']) . PHP_EOL
.'<br>— '. $permalink;
173 $date = DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, $link['linkdate']);
175 if ($this->feedType
== self
::$FEED_RSS) {
176 $link['iso_date'] = $date->format(DateTime
::RSS
);
178 $link['iso_date'] = $date->format(DateTime
::ATOM
);
181 // Save the more recent item.
182 if (empty($this->latestDate
) || $this->latestDate
< $date) {
183 $this->latestDate
= $date;
186 $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
187 uasort($taglist, 'strcasecmp');
188 $link['taglist'] = $taglist;
194 * Assign PubSub hub URL.
196 * @param string $pubsubhubUrl PubSub hub url.
198 public function setPubsubhubUrl($pubsubhubUrl)
200 $this->pubsubhubUrl
= $pubsubhubUrl;
204 * Set this to true to use permalinks instead of direct links.
206 * @param boolean $usePermalinks true to force permalinks.
208 public function setUsePermalinks($usePermalinks)
210 $this->usePermalinks
= $usePermalinks;
214 * Set this to true to hide timestamps in feeds.
216 * @param boolean $hideDates true to enable.
218 public function setHideDates($hideDates)
220 $this->hideDates
= $hideDates;
224 * Set the locale. Used to show feed language.
226 * @param string $locale The locale (eg. 'fr_FR.UTF8').
228 public function setLocale($locale)
230 $this->locale
= strtolower($locale);
234 * Get the language according to the feed type, based on the locale:
236 * - RSS format: en-us (default: 'en-en').
237 * - ATOM format: fr (default: 'en').
239 * @return string The language.
241 public function getTypeLanguage()
243 // Use the locale do define the language, if available.
244 if (! empty($this->locale
) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale
)) {
245 $length = ($this->feedType
== self
::$FEED_RSS) ? 5 : 2;
246 return str_replace('_', '-', substr($this->locale
, 0, $length));
248 return ($this->feedType
== self
::$FEED_RSS) ? 'en-en' : 'en';
252 * Format the latest item date found according to the feed type.
254 * Return an empty string if invalid DateTime is passed.
256 * @return string Formatted date.
258 protected function getLatestDateFormatted()
260 if (empty($this->latestDate
) || !$this->latestDate
instanceof DateTime
) {
264 $type = ($this->feedType
== self
::$FEED_RSS) ? DateTime
::RSS
: DateTime
::ATOM
;
265 return $this->latestDate
->format($type);
269 * Returns the number of link to display according to 'nb' user input parameter.
271 * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
272 * If 'nb' is set to 'all', display all filtered links (max parameter).
274 * @param int $max maximum number of links to display.
276 * @return int number of links to display.
278 public function getNbLinks($max)
280 if (empty($this->userInput
['nb'])) {
281 return self
::$DEFAULT_NB_LINKS;
284 if ($this->userInput
['nb'] == 'all') {
288 $intNb = intval($this->userInput
['nb']);
289 if (! is_int($intNb) || $intNb == 0) {
290 return self
::$DEFAULT_NB_LINKS;