]>
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);
5 define('ATOM', 4, true);
8 * Univarsel Feed Writer class
10 * Genarate RSS2 or JSON (original: RSS 1.0, RSS2.0 and ATOM Feed)
12 * Modified for FiveFilters.org's Full-Text RSS project
13 * to allow for inclusion of hubs, JSON output.
14 * Stripped RSS1 and ATOM support.
16 * @package UnivarselFeedWriter
17 * @author Anis uddin Ahmad <anisniit@gmail.com>
18 * @link http://www.ajaxray.com/projects/rss
22 private $self = null; // self URL - http://feed2.w3.org/docs/warning/MissingAtomSelfLink.html
23 private $hubs = array(); // PubSubHubbub hubs
24 private $channels = array(); // Collection of channel elements
25 private $items = array(); // Collection of items as object of FeedItem class.
26 private $data = array(); // Store some other version wise data
27 private $CDATAEncoding = array(); // The tag names which have to encoded as CDATA
28 private $xsl = null; // stylesheet to render RSS (used by Chrome)
29 private $json = null; // JSON object
31 private $version = null;
36 * @param constant the version constant (RSS2 or JSON).
38 function __construct($version = RSS2
)
40 $this->version
= $version;
42 // Setting default value for assential channel elements
43 $this->channels
['title'] = $version . ' Feed';
44 $this->channels
['link'] = 'http://www.ajaxray.com/blog';
46 //Tag names to encode in CDATA
47 $this->CDATAEncoding
= array('description', 'content:encoded', 'content', 'subtitle', 'summary');
50 public function setFormat($format) {
51 $this->version
= $format;
54 // Start # public functions ---------------------------------------------
57 * Set a channel element
59 * @param srting name of the channel tag
60 * @param string content of the channel tag
63 public function setChannelElement($elementName, $content)
65 $this->channels
[$elementName] = $content ;
69 * Set multiple channel elements from an array. Array elements
70 * should be 'channelName' => 'channelContent' format.
73 * @param array array of channels
76 public function setChannelElementsFromArray($elementArray)
78 if(! is_array($elementArray)) return;
79 foreach ($elementArray as $elementName => $content)
81 $this->setChannelElement($elementName, $content);
86 * Genarate the actual RSS/JSON file
91 public function genarateFeed($withHeaders = true)
94 if ($this->version
== RSS2
) {
95 header('Content-type: text/xml; charset=UTF-8');
96 // this line prevents Chrome 20 from prompting download
97 // used by Google: https://news.google.com/news/feeds?ned=us&topic=b&output=rss
98 header('X-content-type-options: nosniff');
99 } elseif ($this->version
== JSON
) {
100 header('Content-type: application/json; charset=UTF-8');
101 } elseif ($this->version
== JSONP
) {
102 header('Content-type: application/javascript; charset=UTF-8');
106 if ($this->version
== JSON
|| $this->version
== JSONP
) {
107 $this->json
= new stdClass();
112 $this->printChannels();
115 if ($this->version
== JSON
|| $this->version
== JSONP
) {
116 echo json_encode($this->json
);
120 public function &getItems()
126 * Create a new FeedItem.
129 * @return object instance of FeedItem class
131 public function createNewItem()
133 $Item = new FeedItem($this->version
);
138 * Add a FeedItem to the main class
141 * @param object instance of FeedItem class
144 public function addItem($feedItem)
146 $this->items
[] = $feedItem;
149 // Wrapper functions -------------------------------------------------------------------
152 * Set the 'title' channel element
155 * @param srting value of 'title' channel tag
158 public function setTitle($title)
160 $this->setChannelElement('title', $title);
164 * Add a hub to the channel element
170 public function addHub($hub)
172 $this->hubs
[] = $hub;
182 public function setXsl($xsl)
194 public function setSelf($self)
200 * Set the 'description' channel element
203 * @param srting value of 'description' channel tag
206 public function setDescription($description)
208 $tag = ($this->version
== ATOM
)? 'subtitle' : 'description';
209 $this->setChannelElement($tag, $description);
213 * Set the 'link' channel element
216 * @param srting value of 'link' channel tag
219 public function setLink($link)
221 $this->setChannelElement('link', $link);
225 * Set the 'image' channel element
228 * @param srting title of image
229 * @param srting link url of the imahe
230 * @param srting path url of the image
233 public function setImage($title, $link, $url)
235 $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));
238 // End # public functions ----------------------------------------------
240 // Start # private functions ----------------------------------------------
243 * Prints the xml and rss namespace
248 private function printHead()
250 if ($this->version
== RSS2
)
252 $out = '<?xml version="1.0" encoding="utf-8"?>'."\n";
253 if ($this->xsl
) $out .= '<?xml-stylesheet type="text/xsl" href="'.htmlspecialchars($this->xsl
).'"?>' . PHP_EOL
;
254 $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
;
257 elseif ($this->version
== JSON
|| $this->version
== JSONP
)
259 $this->json
->rss
= array('@attributes' => array('version' => '2.0'));
264 * Closes the open tags at the end of file
269 private function printTale()
271 if ($this->version
== RSS2
)
273 echo '</channel>',PHP_EOL
,'</rss>';
275 // do nothing for JSON
279 * Creates a single node as xml format
282 * @param string name of the tag
283 * @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format
284 * @param array Attributes(if any) in 'attrName' => 'attrValue' format
285 * @return string formatted xml tag
287 private function makeNode($tagName, $tagContent, $attributes = null)
289 if ($this->version
== RSS2
)
293 if (is_array($attributes))
295 foreach ($attributes as $key => $value)
297 $attrText .= " $key=\"$value\" ";
300 $nodeText .= "<{$tagName}{$attrText}>";
301 if (is_array($tagContent))
303 foreach ($tagContent as $key => $value)
305 $nodeText .= $this->makeNode($key, $value);
310 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);
311 $nodeText .= htmlspecialchars($tagContent);
313 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? "]]></$tagName>" : "</$tagName>";
314 $nodeText .= "</$tagName>";
315 return $nodeText . PHP_EOL
;
317 elseif ($this->version
== JSON
|| $this->version
== JSONP
)
319 $tagName = (string)$tagName;
320 $tagName = strtr($tagName, ':', '_');
322 if (!$tagContent && is_array($attributes) && count($attributes))
324 $node = array('@attributes' => $this->json_keys($attributes));
326 if (is_array($tagContent)) {
327 $node = $this->json_keys($tagContent);
334 return ''; // should not get here
337 private function json_keys(array $array) {
339 foreach ($array as $key => $val) {
340 if (is_string($key)) $key = strtr($key, ':', '_');
341 if (is_array($val)) {
342 $new[$key] = $this->json_keys($val);
351 * @desc Print channels
355 private function printChannels()
358 if ($this->version
== RSS2
) {
359 echo '<channel>' . PHP_EOL
;
361 foreach ($this->hubs
as $hub) {
362 //echo $this->makeNode('link', '', array('rel'=>'hub', 'href'=>$hub, 'xmlns'=>'http://www.w3.org/2005/Atom'));
363 echo '<link rel="hub" href="'.htmlspecialchars($hub).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
366 if (isset($this->self
)) {
367 //echo $this->makeNode('link', '', array('rel'=>'self', 'href'=>$this->self, 'xmlns'=>'http://www.w3.org/2005/Atom'));
368 echo '<link rel="self" href="'.htmlspecialchars($this->self
).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
370 //Print Items of channel
371 foreach ($this->channels
as $key => $value)
373 echo $this->makeNode($key, $value);
375 } elseif ($this->version
== JSON
|| $this->version
== JSONP
) {
376 $this->json
->rss
['channel'] = (object)$this->json_keys($this->channels
);
381 * Prints formatted feed items
386 private function printItems()
388 foreach ($this->items
as $item) {
389 $itemElements = $item->getElements();
391 echo $this->startItem();
393 if ($this->version
== JSON
|| $this->version
== JSONP
) {
394 $json_item = array();
397 foreach ($itemElements as $thisElement) {
398 foreach ($thisElement as $instance) {
399 if ($this->version
== RSS2
) {
400 echo $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
401 } elseif ($this->version
== JSON
|| $this->version
== JSONP
) {
402 $_json_node = $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
403 if (count($thisElement) > 1) {
404 $json_item[strtr($instance['name'], ':', '_')][] = $_json_node;
406 $json_item[strtr($instance['name'], ':', '_')] = $_json_node;
411 echo $this->endItem();
412 if ($this->version
== JSON
|| $this->version
== JSONP
) {
413 if (count($this->items
) > 1) {
414 $this->json
->rss
['channel']->item
[] = $json_item;
416 $this->json
->rss
['channel']->item
= $json_item;
423 * Make the starting tag of channels
428 private function startItem()
430 if ($this->version
== RSS2
)
432 echo '<item>' . PHP_EOL
;
438 * Closes feed item tag
443 private function endItem()
445 if ($this->version
== RSS2
)
447 echo '</item>' . PHP_EOL
;
452 // End # private functions ----------------------------------------------