]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/feedwriter/FeedWriter.php
7775569033cc9e3c1acf81832f7c1b2dc36946dd
2 define('RSS2', 1, true);
3 define('JSON', 2, true);
4 define('JSONP', 3, true);
7 * Univarsel Feed Writer class
9 * Genarate RSS2 or JSON (original: RSS 1.0, RSS2.0 and ATOM Feed)
11 * Modified for FiveFilters.org's Full-Text RSS project
12 * to allow for inclusion of hubs, JSON output.
13 * Stripped RSS1 and ATOM support.
15 * @package UnivarselFeedWriter
16 * @author Anis uddin Ahmad <anisniit@gmail.com>
17 * @link http://www.ajaxray.com/projects/rss
21 private $self = null; // self URL - http://feed2.w3.org/docs/warning/MissingAtomSelfLink.html
22 private $hubs = array(); // PubSubHubbub hubs
23 private $channels = array(); // Collection of channel elements
24 private $items = array(); // Collection of items as object of FeedItem class.
25 private $data = array(); // Store some other version wise data
26 private $CDATAEncoding = array(); // The tag names which have to encoded as CDATA
27 private $xsl = null; // stylesheet to render RSS (used by Chrome)
28 private $json = null; // JSON object
30 private $version = null;
35 * @param constant the version constant (RSS2 or JSON).
37 function __construct($version = RSS2
)
39 $this->version
= $version;
41 // Setting default value for assential channel elements
42 $this->channels
['title'] = $version . ' Feed';
43 $this->channels
['link'] = 'http://www.ajaxray.com/blog';
45 //Tag names to encode in CDATA
46 $this->CDATAEncoding
= array('description', 'content:encoded', 'content', 'subtitle', 'summary');
49 public function setFormat($format) {
50 $this->version
= $format;
53 // Start # public functions ---------------------------------------------
56 * Set a channel element
58 * @param srting name of the channel tag
59 * @param string content of the channel tag
62 public function setChannelElement($elementName, $content)
64 $this->channels
[$elementName] = $content ;
68 * Set multiple channel elements from an array. Array elements
69 * should be 'channelName' => 'channelContent' format.
72 * @param array array of channels
75 public function setChannelElementsFromArray($elementArray)
77 if(! is_array($elementArray)) return;
78 foreach ($elementArray as $elementName => $content)
80 $this->setChannelElement($elementName, $content);
85 * Genarate the actual RSS/JSON file
90 public function genarateFeed($withHeaders = true)
93 if ($this->version
== RSS2
) {
94 header('Content-type: text/xml; charset=UTF-8');
95 // this line prevents Chrome 20 from prompting download
96 // used by Google: https://news.google.com/news/feeds?ned=us&topic=b&output=rss
97 header('X-content-type-options: nosniff');
98 } elseif ($this->version
== JSON
) {
99 header('Content-type: application/json; charset=UTF-8');
100 $this->json
= new stdClass();
101 } elseif ($this->version
== JSONP
) {
102 header('Content-type: application/javascript; charset=UTF-8');
103 $this->json
= new stdClass();
108 $this->printChannels();
111 if ($this->version
== JSON
|| $this->version
== JSONP
) {
112 echo json_encode($this->json
);
116 public function &getItems()
122 * Create a new FeedItem.
125 * @return object instance of FeedItem class
127 public function createNewItem()
129 $Item = new FeedItem($this->version
);
134 * Add a FeedItem to the main class
137 * @param object instance of FeedItem class
140 public function addItem($feedItem)
142 $this->items
[] = $feedItem;
145 // Wrapper functions -------------------------------------------------------------------
148 * Set the 'title' channel element
151 * @param srting value of 'title' channel tag
154 public function setTitle($title)
156 $this->setChannelElement('title', $title);
160 * Add a hub to the channel element
166 public function addHub($hub)
168 $this->hubs
[] = $hub;
178 public function setXsl($xsl)
190 public function setSelf($self)
196 * Set the 'description' channel element
199 * @param srting value of 'description' channel tag
202 public function setDescription($description)
204 $tag = ($this->version
== ATOM
)? 'subtitle' : 'description';
205 $this->setChannelElement($tag, $desciption);
209 * Set the 'link' channel element
212 * @param srting value of 'link' channel tag
215 public function setLink($link)
217 $this->setChannelElement('link', $link);
221 * Set the 'image' channel element
224 * @param srting title of image
225 * @param srting link url of the imahe
226 * @param srting path url of the image
229 public function setImage($title, $link, $url)
231 $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));
234 // End # public functions ----------------------------------------------
236 // Start # private functions ----------------------------------------------
239 * Prints the xml and rss namespace
244 private function printHead()
246 if ($this->version
== RSS2
)
248 $out = '<?xml version="1.0" encoding="utf-8"?>'."\n";
249 if ($this->xsl
) $out .= '<?xml-stylesheet type="text/xsl" href="'.htmlspecialchars($this->xsl
).'"?>' . PHP_EOL
;
250 $out .= '<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">' . PHP_EOL
;
253 elseif ($this->version
== JSON
|| $this->version
== JSONP
)
255 $this->json
->rss
= array('@attributes' => array('version' => '2.0'));
260 * Closes the open tags at the end of file
265 private function printTale()
267 if ($this->version
== RSS2
)
269 echo '</channel>',PHP_EOL
,'</rss>';
271 // do nothing for JSON
275 * Creates a single node as xml format
278 * @param string name of the tag
279 * @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format
280 * @param array Attributes(if any) in 'attrName' => 'attrValue' format
281 * @return string formatted xml tag
283 private function makeNode($tagName, $tagContent, $attributes = null)
285 if ($this->version
== RSS2
)
289 if (is_array($attributes))
291 foreach ($attributes as $key => $value)
293 $attrText .= " $key=\"$value\" ";
296 $nodeText .= "<{$tagName}{$attrText}>";
297 if (is_array($tagContent))
299 foreach ($tagContent as $key => $value)
301 $nodeText .= $this->makeNode($key, $value);
306 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);
307 $nodeText .= htmlspecialchars($tagContent);
309 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? "]]></$tagName>" : "</$tagName>";
310 $nodeText .= "</$tagName>";
311 return $nodeText . PHP_EOL
;
313 elseif ($this->version
== JSON
|| $this->version
== JSONP
)
315 $tagName = (string)$tagName;
316 $tagName = strtr($tagName, ':', '_');
318 if (!$tagContent && is_array($attributes) && count($attributes))
320 $node = array('@attributes' => $this->json_keys($attributes));
322 if (is_array($tagContent)) {
323 $node = $this->json_keys($tagContent);
330 return ''; // should not get here
333 private function json_keys(array $array) {
335 foreach ($array as $key => $val) {
336 if (is_string($key)) $key = strtr($key, ':', '_');
337 if (is_array($val)) {
338 $new[$key] = $this->json_keys($val);
347 * @desc Print channels
351 private function printChannels()
354 if ($this->version
== RSS2
) {
355 echo '<channel>' . PHP_EOL
;
357 foreach ($this->hubs
as $hub) {
358 //echo $this->makeNode('link', '', array('rel'=>'hub', 'href'=>$hub, 'xmlns'=>'http://www.w3.org/2005/Atom'));
359 echo '<link rel="hub" href="'.htmlspecialchars($hub).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
362 if (isset($this->self
)) {
363 //echo $this->makeNode('link', '', array('rel'=>'self', 'href'=>$this->self, 'xmlns'=>'http://www.w3.org/2005/Atom'));
364 echo '<link rel="self" href="'.htmlspecialchars($this->self
).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
366 //Print Items of channel
367 foreach ($this->channels
as $key => $value)
369 echo $this->makeNode($key, $value);
371 } elseif ($this->version
== JSON
|| $this->version
== JSONP
) {
372 $this->json
->rss
['channel'] = (object)$this->json_keys($this->channels
);
377 * Prints formatted feed items
382 private function printItems()
384 foreach ($this->items
as $item) {
385 $itemElements = $item->getElements();
387 echo $this->startItem();
389 if ($this->version
== JSON
|| $this->version
== JSONP
) {
390 $json_item = array();
393 foreach ($itemElements as $thisElement) {
394 foreach ($thisElement as $instance) {
395 if ($this->version
== RSS2
) {
396 echo $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
397 } elseif ($this->version
== JSON
|| $this->version
== JSONP
) {
398 $_json_node = $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
399 if (count($thisElement) > 1) {
400 $json_item[strtr($instance['name'], ':', '_')][] = $_json_node;
402 $json_item[strtr($instance['name'], ':', '_')] = $_json_node;
407 echo $this->endItem();
408 if ($this->version
== JSON
|| $this->version
== JSONP
) {
409 if (count($this->items
) > 1) {
410 $this->json
->rss
['channel']->item
[] = $json_item;
412 $this->json
->rss
['channel']->item
= $json_item;
419 * Make the starting tag of channels
424 private function startItem()
426 if ($this->version
== RSS2
)
428 echo '<item>' . PHP_EOL
;
434 * Closes feed item tag
439 private function endItem()
441 if ($this->version
== RSS2
)
443 echo '</item>' . PHP_EOL
;
448 // End # private functions ----------------------------------------------