]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/FeedBuilder.php
58c6bb179a636688a648bab720e43930306d5f1a
[github/shaarli/Shaarli.git] / application / FeedBuilder.php
1 <?php
2
3 /**
4 * FeedBuilder class.
5 *
6 * Used to build ATOM and RSS feeds data.
7 */
8 class FeedBuilder
9 {
10 /**
11 * @var string Constant: RSS feed type.
12 */
13 public static $FEED_RSS = 'rss';
14
15 /**
16 * @var string Constant: ATOM feed type.
17 */
18 public static $FEED_ATOM = 'atom';
19
20 /**
21 * @var string Default language if the locale isn't set.
22 */
23 public static $DEFAULT_LANGUAGE = 'en-en';
24
25 /**
26 * @var int Number of links to display in a feed by default.
27 */
28 public static $DEFAULT_NB_LINKS = 50;
29
30 /**
31 * @var LinkDB instance.
32 */
33 protected $linkDB;
34
35 /**
36 * @var string RSS or ATOM feed.
37 */
38 protected $feedType;
39
40 /**
41 * @var array $_SERVER.
42 */
43 protected $serverInfo;
44
45 /**
46 * @var array $_GET.
47 */
48 protected $userInput;
49
50 /**
51 * @var boolean True if the user is currently logged in, false otherwise.
52 */
53 protected $isLoggedIn;
54
55 /**
56 * @var boolean Use permalinks instead of direct links if true.
57 */
58 protected $usePermalinks;
59
60 /**
61 * @var boolean true to hide dates in feeds.
62 */
63 protected $hideDates;
64
65 /**
66 * @var string PubSub hub URL.
67 */
68 protected $pubsubhubUrl;
69
70 /**
71 * @var string server locale.
72 */
73 protected $locale;
74
75 /**
76 * @var DateTime Latest item date.
77 */
78 protected $latestDate;
79
80 /**
81 * Feed constructor.
82 *
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.
88 */
89 public function __construct($linkDB, $feedType, $serverInfo, $userInput, $isLoggedIn)
90 {
91 $this->linkDB = $linkDB;
92 $this->feedType = $feedType;
93 $this->serverInfo = $serverInfo;
94 $this->userInput = $userInput;
95 $this->isLoggedIn = $isLoggedIn;
96 }
97
98 /**
99 * Build data for feed templates.
100 *
101 * @return array Formatted data for feeds templates.
102 */
103 public function buildData()
104 {
105 // Optionally filter the results:
106 $linksToDisplay = $this->linkDB->filterSearch($this->userInput);
107
108 $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay));
109
110 // Can't use array_keys() because $link is a LinkDB instance and not a real array.
111 $keys = array();
112 foreach ($linksToDisplay as $key => $value) {
113 $keys[] = $key;
114 }
115
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);
120 }
121
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'] = $pageaddr . escape(ltrim($this->serverInfo['REQUEST_URI'], '/'));
128 $data['index_url'] = $pageaddr;
129 $data['usepermalinks'] = $this->usePermalinks === true;
130 $data['links'] = $linkDisplayed;
131
132 return $data;
133 }
134
135 /**
136 * Build a feed item (one per shaare).
137 *
138 * @param array $link Single link array extracted from LinkDB.
139 * @param string $pageaddr Index URL.
140 *
141 * @return array Link array with feed attributes.
142 */
143 protected function buildItem($link, $pageaddr)
144 {
145 $link['guid'] = $pageaddr .'?'. smallHash($link['linkdate']);
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'];
149 }
150 if ($this->usePermalinks === true) {
151 $permalink = '<a href="'. $link['url'] .'" title="Direct link">Direct link</a>';
152 } else {
153 $permalink = '<a href="'. $link['guid'] .'" title="Permalink">Permalink</a>';
154 }
155 $link['description'] = format_description($link['description']) . PHP_EOL .'<br>&#8212; '. $permalink;
156
157 $pubDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
158 $link['pub_iso_date'] = $this->getIsoDate($pubDate);
159
160 // atom:entry elements MUST contain exactly one atom:updated element.
161 if (!empty($link['updated'])) {
162 $upDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['updated']);
163 $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime::ATOM);
164 } else {
165 $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM);;
166 }
167
168 // Save the more recent item.
169 if (empty($this->latestDate) || $this->latestDate < $pubDate) {
170 $this->latestDate = $pubDate;
171 }
172 if (!empty($upDate) && $this->latestDate < $upDate) {
173 $this->latestDate = $upDate;
174 }
175
176 $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
177 uasort($taglist, 'strcasecmp');
178 $link['taglist'] = $taglist;
179
180 return $link;
181 }
182
183 /**
184 * Assign PubSub hub URL.
185 *
186 * @param string $pubsubhubUrl PubSub hub url.
187 */
188 public function setPubsubhubUrl($pubsubhubUrl)
189 {
190 $this->pubsubhubUrl = $pubsubhubUrl;
191 }
192
193 /**
194 * Set this to true to use permalinks instead of direct links.
195 *
196 * @param boolean $usePermalinks true to force permalinks.
197 */
198 public function setUsePermalinks($usePermalinks)
199 {
200 $this->usePermalinks = $usePermalinks;
201 }
202
203 /**
204 * Set this to true to hide timestamps in feeds.
205 *
206 * @param boolean $hideDates true to enable.
207 */
208 public function setHideDates($hideDates)
209 {
210 $this->hideDates = $hideDates;
211 }
212
213 /**
214 * Set the locale. Used to show feed language.
215 *
216 * @param string $locale The locale (eg. 'fr_FR.UTF8').
217 */
218 public function setLocale($locale)
219 {
220 $this->locale = strtolower($locale);
221 }
222
223 /**
224 * Get the language according to the feed type, based on the locale:
225 *
226 * - RSS format: en-us (default: 'en-en').
227 * - ATOM format: fr (default: 'en').
228 *
229 * @return string The language.
230 */
231 public function getTypeLanguage()
232 {
233 // Use the locale do define the language, if available.
234 if (! empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) {
235 $length = ($this->feedType == self::$FEED_RSS) ? 5 : 2;
236 return str_replace('_', '-', substr($this->locale, 0, $length));
237 }
238 return ($this->feedType == self::$FEED_RSS) ? 'en-en' : 'en';
239 }
240
241 /**
242 * Format the latest item date found according to the feed type.
243 *
244 * Return an empty string if invalid DateTime is passed.
245 *
246 * @return string Formatted date.
247 */
248 protected function getLatestDateFormatted()
249 {
250 if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) {
251 return '';
252 }
253
254 $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM;
255 return $this->latestDate->format($type);
256 }
257
258 /**
259 * Get ISO date from DateTime according to feed type.
260 *
261 * @param DateTime $date Date to format.
262 * @param string|bool $format Force format.
263 *
264 * @return string Formatted date.
265 */
266 protected function getIsoDate(DateTime $date, $format = false)
267 {
268 if ($format !== false) {
269 return $date->format($format);
270 }
271 if ($this->feedType == self::$FEED_RSS) {
272 return $date->format(DateTime::RSS);
273
274 }
275 return $date->format(DateTime::ATOM);
276 }
277
278 /**
279 * Returns the number of link to display according to 'nb' user input parameter.
280 *
281 * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
282 * If 'nb' is set to 'all', display all filtered links (max parameter).
283 *
284 * @param int $max maximum number of links to display.
285 *
286 * @return int number of links to display.
287 */
288 public function getNbLinks($max)
289 {
290 if (empty($this->userInput['nb'])) {
291 return self::$DEFAULT_NB_LINKS;
292 }
293
294 if ($this->userInput['nb'] == 'all') {
295 return $max;
296 }
297
298 $intNb = intval($this->userInput['nb']);
299 if (! is_int($intNb) || $intNb == 0) {
300 return self::$DEFAULT_NB_LINKS;
301 }
302
303 return $intNb;
304 }
305 }