]>
Commit | Line | Data |
---|---|---|
82e36802 A |
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 | ||
82e36802 A |
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 | // Optionally filter the results: | |
528a6f8a | 101 | $linksToDisplay = $this->linkDB->filterSearch($this->userInput); |
82e36802 A |
102 | |
103 | $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay)); | |
104 | ||
105 | // Can't use array_keys() because $link is a LinkDB instance and not a real array. | |
106 | $keys = array(); | |
107 | foreach ($linksToDisplay as $key => $value) { | |
108 | $keys[] = $key; | |
109 | } | |
110 | ||
111 | $pageaddr = escape(index_url($this->serverInfo)); | |
112 | $linkDisplayed = array(); | |
113 | for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) { | |
114 | $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr); | |
115 | } | |
116 | ||
117 | $data['language'] = $this->getTypeLanguage(); | |
82e36802 A |
118 | $data['last_update'] = $this->getLatestDateFormatted(); |
119 | $data['show_dates'] = !$this->hideDates || $this->isLoggedIn; | |
120 | // Remove leading slash from REQUEST_URI. | |
44a71809 V |
121 | $data['self_link'] = escape(server_url($this->serverInfo)) |
122 | . escape($this->serverInfo['REQUEST_URI']); | |
82e36802 A |
123 | $data['index_url'] = $pageaddr; |
124 | $data['usepermalinks'] = $this->usePermalinks === true; | |
125 | $data['links'] = $linkDisplayed; | |
126 | ||
127 | return $data; | |
128 | } | |
129 | ||
130 | /** | |
131 | * Build a feed item (one per shaare). | |
132 | * | |
133 | * @param array $link Single link array extracted from LinkDB. | |
134 | * @param string $pageaddr Index URL. | |
135 | * | |
136 | * @return array Link array with feed attributes. | |
137 | */ | |
138 | protected function buildItem($link, $pageaddr) | |
139 | { | |
d592daea | 140 | $link['guid'] = $pageaddr .'?'. $link['shorturl']; |
82e36802 A |
141 | // Check for both signs of a note: starting with ? and 7 chars long. |
142 | if ($link['url'][0] === '?' && strlen($link['url']) === 7) { | |
143 | $link['url'] = $pageaddr . $link['url']; | |
144 | } | |
145 | if ($this->usePermalinks === true) { | |
146 | $permalink = '<a href="'. $link['url'] .'" title="Direct link">Direct link</a>'; | |
147 | } else { | |
148 | $permalink = '<a href="'. $link['guid'] .'" title="Permalink">Permalink</a>'; | |
149 | } | |
fbc28ff1 A |
150 | $link['description'] = format_description($link['description'], '', $pageaddr); |
151 | $link['description'] .= PHP_EOL .'<br>— '. $permalink; | |
82e36802 | 152 | |
01878a75 | 153 | $pubDate = $link['created']; |
c6d876bb | 154 | $link['pub_iso_date'] = $this->getIsoDate($pubDate); |
82e36802 | 155 | |
c6d876bb A |
156 | // atom:entry elements MUST contain exactly one atom:updated element. |
157 | if (!empty($link['updated'])) { | |
01878a75 | 158 | $upDate = $link['updated']; |
c6d876bb | 159 | $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime::ATOM); |
82e36802 | 160 | } else { |
c6d876bb | 161 | $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM);; |
82e36802 A |
162 | } |
163 | ||
164 | // Save the more recent item. | |
c6d876bb A |
165 | if (empty($this->latestDate) || $this->latestDate < $pubDate) { |
166 | $this->latestDate = $pubDate; | |
167 | } | |
168 | if (!empty($upDate) && $this->latestDate < $upDate) { | |
169 | $this->latestDate = $upDate; | |
82e36802 A |
170 | } |
171 | ||
172 | $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); | |
173 | uasort($taglist, 'strcasecmp'); | |
174 | $link['taglist'] = $taglist; | |
175 | ||
176 | return $link; | |
177 | } | |
178 | ||
82e36802 A |
179 | /** |
180 | * Set this to true to use permalinks instead of direct links. | |
181 | * | |
182 | * @param boolean $usePermalinks true to force permalinks. | |
183 | */ | |
184 | public function setUsePermalinks($usePermalinks) | |
185 | { | |
186 | $this->usePermalinks = $usePermalinks; | |
187 | } | |
188 | ||
189 | /** | |
190 | * Set this to true to hide timestamps in feeds. | |
191 | * | |
192 | * @param boolean $hideDates true to enable. | |
193 | */ | |
194 | public function setHideDates($hideDates) | |
195 | { | |
196 | $this->hideDates = $hideDates; | |
197 | } | |
198 | ||
199 | /** | |
200 | * Set the locale. Used to show feed language. | |
201 | * | |
202 | * @param string $locale The locale (eg. 'fr_FR.UTF8'). | |
203 | */ | |
204 | public function setLocale($locale) | |
205 | { | |
206 | $this->locale = strtolower($locale); | |
207 | } | |
208 | ||
209 | /** | |
210 | * Get the language according to the feed type, based on the locale: | |
211 | * | |
212 | * - RSS format: en-us (default: 'en-en'). | |
213 | * - ATOM format: fr (default: 'en'). | |
214 | * | |
215 | * @return string The language. | |
216 | */ | |
217 | public function getTypeLanguage() | |
218 | { | |
219 | // Use the locale do define the language, if available. | |
220 | if (! empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) { | |
221 | $length = ($this->feedType == self::$FEED_RSS) ? 5 : 2; | |
222 | return str_replace('_', '-', substr($this->locale, 0, $length)); | |
223 | } | |
224 | return ($this->feedType == self::$FEED_RSS) ? 'en-en' : 'en'; | |
225 | } | |
226 | ||
227 | /** | |
228 | * Format the latest item date found according to the feed type. | |
229 | * | |
230 | * Return an empty string if invalid DateTime is passed. | |
231 | * | |
232 | * @return string Formatted date. | |
233 | */ | |
234 | protected function getLatestDateFormatted() | |
235 | { | |
236 | if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) { | |
237 | return ''; | |
238 | } | |
239 | ||
240 | $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM; | |
241 | return $this->latestDate->format($type); | |
242 | } | |
243 | ||
c6d876bb A |
244 | /** |
245 | * Get ISO date from DateTime according to feed type. | |
246 | * | |
247 | * @param DateTime $date Date to format. | |
248 | * @param string|bool $format Force format. | |
249 | * | |
250 | * @return string Formatted date. | |
251 | */ | |
252 | protected function getIsoDate(DateTime $date, $format = false) | |
253 | { | |
254 | if ($format !== false) { | |
255 | return $date->format($format); | |
256 | } | |
257 | if ($this->feedType == self::$FEED_RSS) { | |
258 | return $date->format(DateTime::RSS); | |
259 | ||
260 | } | |
261 | return $date->format(DateTime::ATOM); | |
262 | } | |
263 | ||
82e36802 A |
264 | /** |
265 | * Returns the number of link to display according to 'nb' user input parameter. | |
266 | * | |
267 | * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS. | |
268 | * If 'nb' is set to 'all', display all filtered links (max parameter). | |
269 | * | |
270 | * @param int $max maximum number of links to display. | |
271 | * | |
272 | * @return int number of links to display. | |
273 | */ | |
274 | public function getNbLinks($max) | |
275 | { | |
276 | if (empty($this->userInput['nb'])) { | |
277 | return self::$DEFAULT_NB_LINKS; | |
278 | } | |
279 | ||
280 | if ($this->userInput['nb'] == 'all') { | |
281 | return $max; | |
282 | } | |
283 | ||
284 | $intNb = intval($this->userInput['nb']); | |
285 | if (! is_int($intNb) || $intNb == 0) { | |
286 | return self::$DEFAULT_NB_LINKS; | |
287 | } | |
288 | ||
289 | return $intNb; | |
290 | } | |
291 | } |