]>
Commit | Line | Data |
---|---|---|
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 server locale. | |
67 | */ | |
68 | protected $locale; | |
69 | ||
70 | /** | |
71 | * @var DateTime Latest item date. | |
72 | */ | |
73 | protected $latestDate; | |
74 | ||
75 | /** | |
76 | * Feed constructor. | |
77 | * | |
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. | |
83 | */ | |
84 | public function __construct($linkDB, $feedType, $serverInfo, $userInput, $isLoggedIn) | |
85 | { | |
86 | $this->linkDB = $linkDB; | |
87 | $this->feedType = $feedType; | |
88 | $this->serverInfo = $serverInfo; | |
89 | $this->userInput = $userInput; | |
90 | $this->isLoggedIn = $isLoggedIn; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Build data for feed templates. | |
95 | * | |
96 | * @return array Formatted data for feeds templates. | |
97 | */ | |
98 | public function buildData() | |
99 | { | |
100 | // Search for untagged links | |
101 | if (isset($this->userInput['searchtags']) && empty($this->userInput['searchtags'])) { | |
102 | $this->userInput['searchtags'] = false; | |
103 | } | |
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['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; | |
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 .'?'. $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']; | |
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'], '', $pageaddr); | |
156 | $link['description'] .= PHP_EOL .'<br>— '. $permalink; | |
157 | ||
158 | $pubDate = $link['created']; | |
159 | $link['pub_iso_date'] = $this->getIsoDate($pubDate); | |
160 | ||
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); | |
165 | } else { | |
166 | $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM);; | |
167 | } | |
168 | ||
169 | // Save the more recent item. | |
170 | if (empty($this->latestDate) || $this->latestDate < $pubDate) { | |
171 | $this->latestDate = $pubDate; | |
172 | } | |
173 | if (!empty($upDate) && $this->latestDate < $upDate) { | |
174 | $this->latestDate = $upDate; | |
175 | } | |
176 | ||
177 | $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); | |
178 | uasort($taglist, 'strcasecmp'); | |
179 | $link['taglist'] = $taglist; | |
180 | ||
181 | return $link; | |
182 | } | |
183 | ||
184 | /** | |
185 | * Set this to true to use permalinks instead of direct links. | |
186 | * | |
187 | * @param boolean $usePermalinks true to force permalinks. | |
188 | */ | |
189 | public function setUsePermalinks($usePermalinks) | |
190 | { | |
191 | $this->usePermalinks = $usePermalinks; | |
192 | } | |
193 | ||
194 | /** | |
195 | * Set this to true to hide timestamps in feeds. | |
196 | * | |
197 | * @param boolean $hideDates true to enable. | |
198 | */ | |
199 | public function setHideDates($hideDates) | |
200 | { | |
201 | $this->hideDates = $hideDates; | |
202 | } | |
203 | ||
204 | /** | |
205 | * Set the locale. Used to show feed language. | |
206 | * | |
207 | * @param string $locale The locale (eg. 'fr_FR.UTF8'). | |
208 | */ | |
209 | public function setLocale($locale) | |
210 | { | |
211 | $this->locale = strtolower($locale); | |
212 | } | |
213 | ||
214 | /** | |
215 | * Get the language according to the feed type, based on the locale: | |
216 | * | |
217 | * - RSS format: en-us (default: 'en-en'). | |
218 | * - ATOM format: fr (default: 'en'). | |
219 | * | |
220 | * @return string The language. | |
221 | */ | |
222 | public function getTypeLanguage() | |
223 | { | |
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)); | |
228 | } | |
229 | return ($this->feedType == self::$FEED_RSS) ? 'en-en' : 'en'; | |
230 | } | |
231 | ||
232 | /** | |
233 | * Format the latest item date found according to the feed type. | |
234 | * | |
235 | * Return an empty string if invalid DateTime is passed. | |
236 | * | |
237 | * @return string Formatted date. | |
238 | */ | |
239 | protected function getLatestDateFormatted() | |
240 | { | |
241 | if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) { | |
242 | return ''; | |
243 | } | |
244 | ||
245 | $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM; | |
246 | return $this->latestDate->format($type); | |
247 | } | |
248 | ||
249 | /** | |
250 | * Get ISO date from DateTime according to feed type. | |
251 | * | |
252 | * @param DateTime $date Date to format. | |
253 | * @param string|bool $format Force format. | |
254 | * | |
255 | * @return string Formatted date. | |
256 | */ | |
257 | protected function getIsoDate(DateTime $date, $format = false) | |
258 | { | |
259 | if ($format !== false) { | |
260 | return $date->format($format); | |
261 | } | |
262 | if ($this->feedType == self::$FEED_RSS) { | |
263 | return $date->format(DateTime::RSS); | |
264 | ||
265 | } | |
266 | return $date->format(DateTime::ATOM); | |
267 | } | |
268 | ||
269 | /** | |
270 | * Returns the number of link to display according to 'nb' user input parameter. | |
271 | * | |
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). | |
274 | * | |
275 | * @param int $max maximum number of links to display. | |
276 | * | |
277 | * @return int number of links to display. | |
278 | */ | |
279 | public function getNbLinks($max) | |
280 | { | |
281 | if (empty($this->userInput['nb'])) { | |
282 | return self::$DEFAULT_NB_LINKS; | |
283 | } | |
284 | ||
285 | if ($this->userInput['nb'] == 'all') { | |
286 | return $max; | |
287 | } | |
288 | ||
289 | $intNb = intval($this->userInput['nb']); | |
290 | if (! is_int($intNb) || $intNb == 0) { | |
291 | return self::$DEFAULT_NB_LINKS; | |
292 | } | |
293 | ||
294 | return $intNb; | |
295 | } | |
296 | } |