]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/feedwriter/FeedWriter.php
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()
92 if ($this->version
== RSS2
) {
93 header('Content-type: text/xml; charset=UTF-8');
94 // this line prevents Chrome 20 from prompting download
95 // used by Google: https://news.google.com/news/feeds?ned=us&topic=b&output=rss
96 header('X-content-type-options: nosniff');
97 } elseif ($this->version
== JSON
) {
98 header('Content-type: application/json; charset=UTF-8');
99 $this->json
= new stdClass();
100 } elseif ($this->version
== JSONP
) {
101 header('Content-type: application/javascript; charset=UTF-8');
102 $this->json
= new stdClass();
105 $this->printChannels();
108 if ($this->version
== JSON
|| $this->version
== JSONP
) {
109 echo json_encode($this->json
);
114 * Create a new FeedItem.
117 * @return object instance of FeedItem class
119 public function createNewItem()
121 $Item = new FeedItem($this->version
);
126 * Add a FeedItem to the main class
129 * @param object instance of FeedItem class
132 public function addItem($feedItem)
134 $this->items
[] = $feedItem;
137 // Wrapper functions -------------------------------------------------------------------
140 * Set the 'title' channel element
143 * @param srting value of 'title' channel tag
146 public function setTitle($title)
148 $this->setChannelElement('title', $title);
152 * Add a hub to the channel element
158 public function addHub($hub)
160 $this->hubs
[] = $hub;
170 public function setXsl($xsl)
182 public function setSelf($self)
188 * Set the 'description' channel element
191 * @param srting value of 'description' channel tag
194 public function setDescription($desciption)
196 $tag = ($this->version
== ATOM
)? 'subtitle' : 'description';
197 $this->setChannelElement($tag, $desciption);
201 * Set the 'link' channel element
204 * @param srting value of 'link' channel tag
207 public function setLink($link)
209 $this->setChannelElement('link', $link);
213 * Set the 'image' channel element
216 * @param srting title of image
217 * @param srting link url of the imahe
218 * @param srting path url of the image
221 public function setImage($title, $link, $url)
223 $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));
226 // End # public functions ----------------------------------------------
228 // Start # private functions ----------------------------------------------
231 * Prints the xml and rss namespace
236 private function printHead()
238 if ($this->version
== RSS2
)
240 $out = '<?xml version="1.0" encoding="utf-8"?>'."\n";
241 if ($this->xsl
) $out .= '<?xml-stylesheet type="text/xsl" href="'.htmlspecialchars($this->xsl
).'"?>' . PHP_EOL
;
242 $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">' . PHP_EOL
;
245 elseif ($this->version
== JSON
|| $this->version
== JSONP
)
247 $this->json
->rss
= array('@attributes' => array('version' => '2.0'));
252 * Closes the open tags at the end of file
257 private function printTale()
259 if ($this->version
== RSS2
)
261 echo '</channel>',PHP_EOL
,'</rss>';
263 // do nothing for JSON
267 * Creates a single node as xml format
270 * @param string name of the tag
271 * @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format
272 * @param array Attributes(if any) in 'attrName' => 'attrValue' format
273 * @return string formatted xml tag
275 private function makeNode($tagName, $tagContent, $attributes = null)
277 if ($this->version
== RSS2
)
281 if (is_array($attributes))
283 foreach ($attributes as $key => $value)
285 $attrText .= " $key=\"$value\" ";
288 $nodeText .= "<{$tagName}{$attrText}>";
289 if (is_array($tagContent))
291 foreach ($tagContent as $key => $value)
293 $nodeText .= $this->makeNode($key, $value);
298 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);
299 $nodeText .= htmlspecialchars($tagContent);
301 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? "]]></$tagName>" : "</$tagName>";
302 $nodeText .= "</$tagName>";
303 return $nodeText . PHP_EOL
;
305 elseif ($this->version
== JSON
|| $this->version
== JSONP
)
307 $tagName = (string)$tagName;
308 $tagName = strtr($tagName, ':', '_');
310 if (!$tagContent && is_array($attributes) && count($attributes))
312 $node = array('@attributes' => $this->json_keys($attributes));
314 if (is_array($tagContent)) {
315 $node = $this->json_keys($tagContent);
322 return ''; // should not get here
325 private function json_keys(array $array) {
327 foreach ($array as $key => $val) {
328 if (is_string($key)) $key = strtr($key, ':', '_');
329 if (is_array($val)) {
330 $new[$key] = $this->json_keys($val);
339 * @desc Print channels
343 private function printChannels()
346 if ($this->version
== RSS2
) {
347 echo '<channel>' . PHP_EOL
;
349 foreach ($this->hubs
as $hub) {
350 //echo $this->makeNode('link', '', array('rel'=>'hub', 'href'=>$hub, 'xmlns'=>'http://www.w3.org/2005/Atom'));
351 echo '<link rel="hub" href="'.htmlspecialchars($hub).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
354 if (isset($this->self
)) {
355 //echo $this->makeNode('link', '', array('rel'=>'self', 'href'=>$this->self, 'xmlns'=>'http://www.w3.org/2005/Atom'));
356 echo '<link rel="self" href="'.htmlspecialchars($this->self
).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
358 //Print Items of channel
359 foreach ($this->channels
as $key => $value)
361 echo $this->makeNode($key, $value);
363 } elseif ($this->version
== JSON
|| $this->version
== JSONP
) {
364 $this->json
->rss
['channel'] = (object)$this->json_keys($this->channels
);
369 * Prints formatted feed items
374 private function printItems()
376 foreach ($this->items
as $item) {
377 $itemElements = $item->getElements();
379 echo $this->startItem();
381 if ($this->version
== JSON
|| $this->version
== JSONP
) {
382 $json_item = array();
385 foreach ($itemElements as $thisElement) {
386 foreach ($thisElement as $instance) {
387 if ($this->version
== RSS2
) {
388 echo $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
389 } elseif ($this->version
== JSON
|| $this->version
== JSONP
) {
390 $_json_node = $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
391 if (count($thisElement) > 1) {
392 $json_item[strtr($instance['name'], ':', '_')][] = $_json_node;
394 $json_item[strtr($instance['name'], ':', '_')] = $_json_node;
399 echo $this->endItem();
400 if ($this->version
== JSON
|| $this->version
== JSONP
) {
401 if (count($this->items
) > 1) {
402 $this->json
->rss
['channel']->item
[] = $json_item;
404 $this->json
->rss
['channel']->item
= $json_item;
411 * Make the starting tag of channels
416 private function startItem()
418 if ($this->version
== RSS2
)
420 echo '<item>' . PHP_EOL
;
426 * Closes feed item tag
431 private function endItem()
433 if ($this->version
== RSS2
)
435 echo '</item>' . PHP_EOL
;
440 // End # private functions ----------------------------------------------