]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/libraries/feedwriter/FeedWriter.php
Merge pull request #634 from wallabag/dev
[github/wallabag/wallabag.git] / inc / 3rdparty / libraries / feedwriter / FeedWriter.php
CommitLineData
42c80841
NL
1<?php\r
2define('RSS2', 1, true);\r
3define('JSON', 2, true);\r
4define('JSONP', 3, true);\r
5\r
6 /**\r
7 * Univarsel Feed Writer class\r
8 *\r
9 * Genarate RSS2 or JSON (original: RSS 1.0, RSS2.0 and ATOM Feed)\r
10 *\r
11 * Modified for FiveFilters.org's Full-Text RSS project\r
182faf26 12 * to allow for inclusion of hubs, JSON output.\r
42c80841 13 * Stripped RSS1 and ATOM support.\r
182faf26 14 *\r
42c80841
NL
15 * @package UnivarselFeedWriter\r
16 * @author Anis uddin Ahmad <anisniit@gmail.com>\r
17 * @link http://www.ajaxray.com/projects/rss\r
18 */\r
19 class FeedWriter\r
20 {\r
7a873ef1
NL
21 private $self = null; // self URL - http://feed2.w3.org/docs/warning/MissingAtomSelfLink.html\r
22 private $hubs = array(); // PubSubHubbub hubs\r
23 private $channels = array(); // Collection of channel elements\r
24 private $items = array(); // Collection of items as object of FeedItem class.\r
25 private $data = array(); // Store some other version wise data\r
26 private $CDATAEncoding = array(); // The tag names which have to encoded as CDATA\r
27 private $xsl = null; // stylesheet to render RSS (used by Chrome)\r
28 private $json = null; // JSON object\r
29\r
30 private $version = null;\r
31\r
32 /**\r
33 * Constructor\r
34 *\r
35 * @param constant the version constant (RSS2 or JSON).\r
36 */\r
37 function __construct($version = RSS2)\r
38 {\r
39 $this->version = $version;\r
40\r
41 // Setting default value for assential channel elements\r
42 $this->channels['title'] = $version . ' Feed';\r
43 $this->channels['link'] = 'http://www.ajaxray.com/blog';\r
44\r
45 //Tag names to encode in CDATA\r
46 $this->CDATAEncoding = array('description', 'content:encoded', 'content', 'subtitle', 'summary');\r
47 }\r
48\r
49 public function setFormat($format) {\r
50 $this->version = $format;\r
51 }\r
52\r
53 // Start # public functions ---------------------------------------------\r
54\r
55 /**\r
56 * Set a channel element\r
57 * @access public\r
58 * @param srting name of the channel tag\r
59 * @param string content of the channel tag\r
60 * @return void\r
61 */\r
62 public function setChannelElement($elementName, $content)\r
63 {\r
64 $this->channels[$elementName] = $content ;\r
65 }\r
66\r
67 /**\r
68 * Set multiple channel elements from an array. Array elements\r
69 * should be 'channelName' => 'channelContent' format.\r
70 *\r
71 * @access public\r
72 * @param array array of channels\r
73 * @return void\r
74 */\r
75 public function setChannelElementsFromArray($elementArray)\r
76 {\r
77 if(! is_array($elementArray)) return;\r
78 foreach ($elementArray as $elementName => $content)\r
79 {\r
80 $this->setChannelElement($elementName, $content);\r
81 }\r
82 }\r
83\r
84 /**\r
85 * Genarate the actual RSS/JSON file\r
86 *\r
87 * @access public\r
88 * @return void\r
89 */\r
90 public function genarateFeed()\r
91 {\r
92 if ($this->version == RSS2) {\r
93// header('Content-type: text/xml; charset=UTF-8');\r
94 // this line prevents Chrome 20 from prompting download\r
95 // used by Google: https://news.google.com/news/feeds?ned=us&topic=b&output=rss\r
96// header('X-content-type-options: nosniff');\r
97 } elseif ($this->version == JSON) {\r
98// header('Content-type: application/json; charset=UTF-8');\r
99 $this->json = new stdClass();\r
100 } elseif ($this->version == JSONP) {\r
101// header('Content-type: application/javascript; charset=UTF-8');\r
102 $this->json = new stdClass();\r
103 }\r
104 $this->printHead();\r
105 $this->printChannels();\r
106 $this->printItems();\r
107 $this->printTale();\r
108 if ($this->version == JSON || $this->version == JSONP) {\r
109 echo json_encode($this->json);\r
110 }\r
111 }\r
112\r
113 /**\r
114 * Create a new FeedItem.\r
115 *\r
116 * @access public\r
117 * @return object instance of FeedItem class\r
118 */\r
119 public function createNewItem()\r
120 {\r
121 $Item = new FeedItem($this->version);\r
122 return $Item;\r
123 }\r
124\r
125 /**\r
126 * Add a FeedItem to the main class\r
127 *\r
128 * @access public\r
129 * @param object instance of FeedItem class\r
130 * @return void\r
131 */\r
132 public function addItem($feedItem)\r
133 {\r
134 $this->items[] = $feedItem;\r
135 }\r
136\r
137 // Wrapper functions -------------------------------------------------------------------\r
138\r
139 /**\r
140 * Set the 'title' channel element\r
141 *\r
142 * @access public\r
143 * @param srting value of 'title' channel tag\r
144 * @return void\r
145 */\r
146 public function setTitle($title)\r
147 {\r
148 $this->setChannelElement('title', $title);\r
149 }\r
150\r
151 /**\r
152 * Add a hub to the channel element\r
153 *\r
154 * @access public\r
155 * @param string URL\r
156 * @return void\r
157 */\r
158 public function addHub($hub)\r
159 {\r
160 $this->hubs[] = $hub;\r
161 }\r
162\r
163 /**\r
164 * Set XSL URL\r
165 *\r
166 * @access public\r
167 * @param string URL\r
168 * @return void\r
169 */\r
170 public function setXsl($xsl)\r
171 {\r
172 $this->xsl = $xsl;\r
173 }\r
174\r
175 /**\r
176 * Set self URL\r
177 *\r
178 * @access public\r
179 * @param string URL\r
180 * @return void\r
181 */\r
182 public function setSelf($self)\r
183 {\r
184 $this->self = $self;\r
185 }\r
186\r
187 /**\r
188 * Set the 'description' channel element\r
189 *\r
190 * @access public\r
191 * @param srting value of 'description' channel tag\r
192 * @return void\r
193 */\r
194 public function setDescription($description)\r
195 {\r
196 $this->setChannelElement('description', $description);\r
197 }\r
198\r
199 /**\r
200 * Set the 'link' channel element\r
201 *\r
202 * @access public\r
203 * @param srting value of 'link' channel tag\r
204 * @return void\r
205 */\r
206 public function setLink($link)\r
207 {\r
208 $this->setChannelElement('link', $link);\r
209 }\r
210\r
211 /**\r
212 * Set the 'image' channel element\r
213 *\r
214 * @access public\r
215 * @param srting title of image\r
216 * @param srting link url of the imahe\r
217 * @param srting path url of the image\r
218 * @return void\r
219 */\r
220 public function setImage($title, $link, $url)\r
221 {\r
222 $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));\r
223 }\r
224\r
225 // End # public functions ----------------------------------------------\r
226\r
227 // Start # private functions ----------------------------------------------\r
228\r
229 /**\r
230 * Prints the xml and rss namespace\r
231 *\r
232 * @access private\r
233 * @return void\r
234 */\r
235 private function printHead()\r
236 {\r
237 if ($this->version == RSS2)\r
238 {\r
239 $out = '<?xml version="1.0" encoding="utf-8"?>'."\n";\r
240 if ($this->xsl) $out .= '<?xml-stylesheet type="text/xsl" href="'.htmlspecialchars($this->xsl).'"?>' . PHP_EOL;\r
241 $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">' . PHP_EOL;\r
242 echo $out;\r
243 }\r
244 elseif ($this->version == JSON || $this->version == JSONP)\r
245 {\r
246 $this->json->rss = array('@attributes' => array('version' => '2.0'));\r
247 }\r
248 }\r
249\r
250 /**\r
251 * Closes the open tags at the end of file\r
252 *\r
253 * @access private\r
254 * @return void\r
255 */\r
256 private function printTale()\r
257 {\r
258 if ($this->version == RSS2)\r
259 {\r
260 echo '</channel>',PHP_EOL,'</rss>';\r
261 }\r
262 // do nothing for JSON\r
263 }\r
264\r
265 /**\r
266 * Creates a single node as xml format\r
267 *\r
268 * @access private\r
269 * @param string name of the tag\r
270 * @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format\r
271 * @param array Attributes(if any) in 'attrName' => 'attrValue' format\r
272 * @return string formatted xml tag\r
273 */\r
274 private function makeNode($tagName, $tagContent, $attributes = null)\r
275 {\r
276 if ($this->version == RSS2)\r
277 {\r
278 $nodeText = '';\r
279 $attrText = '';\r
280 if (is_array($attributes))\r
281 {\r
282 foreach ($attributes as $key => $value)\r
283 {\r
284 $attrText .= " $key=\"$value\" ";\r
285 }\r
286 }\r
287 $nodeText .= "<{$tagName}{$attrText}>";\r
288 if (is_array($tagContent))\r
289 {\r
290 foreach ($tagContent as $key => $value)\r
291 {\r
292 $nodeText .= $this->makeNode($key, $value);\r
293 }\r
294 }\r
295 else\r
296 {\r
297 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);\r
298 $nodeText .= htmlspecialchars($tagContent);\r
299 }\r
300 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? "]]></$tagName>" : "</$tagName>";\r
301 $nodeText .= "</$tagName>";\r
302 return $nodeText . PHP_EOL;\r
303 }\r
304 elseif ($this->version == JSON || $this->version == JSONP)\r
305 {\r
306 $tagName = (string)$tagName;\r
307 $tagName = strtr($tagName, ':', '_');\r
308 $node = null;\r
309 if (!$tagContent && is_array($attributes) && count($attributes))\r
310 {\r
311 $node = array('@attributes' => $this->json_keys($attributes));\r
312 } else {\r
313 if (is_array($tagContent)) {\r
314 $node = $this->json_keys($tagContent);\r
315 } else {\r
316 $node = $tagContent;\r
317 }\r
318 }\r
319 return $node;\r
320 }\r
321 return ''; // should not get here\r
322 }\r
323\r
324 private function json_keys(array $array) {\r
325 $new = array();\r
326 foreach ($array as $key => $val) {\r
327 if (is_string($key)) $key = strtr($key, ':', '_');\r
328 if (is_array($val)) {\r
329 $new[$key] = $this->json_keys($val);\r
330 } else {\r
331 $new[$key] = $val;\r
332 }\r
333 }\r
334 return $new;\r
335 }\r
336\r
337 /**\r
338 * @desc Print channels\r
339 * @access private\r
340 * @return void\r
341 */\r
342 private function printChannels()\r
343 {\r
344 //Start channel tag\r
345 if ($this->version == RSS2) {\r
346 echo '<channel>' . PHP_EOL;\r
347 // add hubs\r
348 foreach ($this->hubs as $hub) {\r
349 //echo $this->makeNode('link', '', array('rel'=>'hub', 'href'=>$hub, 'xmlns'=>'http://www.w3.org/2005/Atom'));\r
350 echo '<link rel="hub" href="'.htmlspecialchars($hub).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL;\r
351 }\r
352 // add self\r
353 if (isset($this->self)) {\r
354 //echo $this->makeNode('link', '', array('rel'=>'self', 'href'=>$this->self, 'xmlns'=>'http://www.w3.org/2005/Atom'));\r
355 echo '<link rel="self" href="'.htmlspecialchars($this->self).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL;\r
356 }\r
357 //Print Items of channel\r
358 foreach ($this->channels as $key => $value)\r
359 {\r
360 echo $this->makeNode($key, $value);\r
361 }\r
362 } elseif ($this->version == JSON || $this->version == JSONP) {\r
363 $this->json->rss['channel'] = (object)$this->json_keys($this->channels);\r
364 }\r
365 }\r
366\r
367 /**\r
368 * Prints formatted feed items\r
369 *\r
370 * @access private\r
371 * @return void\r
372 */\r
373 private function printItems()\r
374 {\r
375 foreach ($this->items as $item) {\r
376 $itemElements = $item->getElements();\r
377\r
378 echo $this->startItem();\r
379\r
380 if ($this->version == JSON || $this->version == JSONP) {\r
381 $json_item = array();\r
382 }\r
383\r
384 foreach ($itemElements as $thisElement) {\r
385 foreach ($thisElement as $instance) {\r
386 if ($this->version == RSS2) {\r
387 echo $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);\r
388 } elseif ($this->version == JSON || $this->version == JSONP) {\r
389 $_json_node = $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);\r
390 if (count($thisElement) > 1) {\r
391 $json_item[strtr($instance['name'], ':', '_')][] = $_json_node;\r
392 } else {\r
393 $json_item[strtr($instance['name'], ':', '_')] = $_json_node;\r
394 }\r
395 }\r
396 }\r
397 }\r
398 echo $this->endItem();\r
399 if ($this->version == JSON || $this->version == JSONP) {\r
400 if (count($this->items) > 1) {\r
401 $this->json->rss['channel']->item[] = $json_item;\r
402 } else {\r
403 $this->json->rss['channel']->item = $json_item;\r
404 }\r
405 }\r
406 }\r
407 }\r
408\r
409 /**\r
410 * Make the starting tag of channels\r
411 *\r
412 * @access private\r
413 * @return void\r
414 */\r
415 private function startItem()\r
416 {\r
417 if ($this->version == RSS2)\r
418 {\r
419 echo '<item>' . PHP_EOL;\r
420 }\r
421 // nothing for JSON\r
422 }\r
423\r
424 /**\r
425 * Closes feed item tag\r
426 *\r
427 * @access private\r
428 * @return void\r
429 */\r
430 private function endItem()\r
431 {\r
432 if ($this->version == RSS2)\r
433 {\r
434 echo '</item>' . PHP_EOL;\r
435 }\r
436 // nothing for JSON\r
437 }\r
438\r
439 // End # private functions ----------------------------------------------\r
42c80841 440 }