]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - FeedWriter.php
aa064afba3ca0bd6914ac6e5fd1e3fff2d39ef81
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 } elseif ($this->version
== JSONP
) {
101 header('Content-type: application/javascript; charset=UTF-8');
105 if ($this->version
== JSON
|| $this->version
== JSONP
) {
106 $this->json
= new stdClass();
111 $this->printChannels();
114 if ($this->version
== JSON
|| $this->version
== JSONP
) {
115 echo json_encode($this->json
);
119 public function &getItems()
125 * Create a new FeedItem.
128 * @return object instance of FeedItem class
130 public function createNewItem()
132 $Item = new FeedItem($this->version
);
137 * Add a FeedItem to the main class
140 * @param object instance of FeedItem class
143 public function addItem($feedItem)
145 $this->items
[] = $feedItem;
148 // Wrapper functions -------------------------------------------------------------------
151 * Set the 'title' channel element
154 * @param srting value of 'title' channel tag
157 public function setTitle($title)
159 $this->setChannelElement('title', $title);
163 * Add a hub to the channel element
169 public function addHub($hub)
171 $this->hubs
[] = $hub;
181 public function setXsl($xsl)
193 public function setSelf($self)
199 * Set the 'description' channel element
202 * @param srting value of 'description' channel tag
205 public function setDescription($description)
207 $tag = ($this->version
== ATOM
)? 'subtitle' : 'description';
208 $this->setChannelElement($tag, $description);
212 * Set the 'link' channel element
215 * @param srting value of 'link' channel tag
218 public function setLink($link)
220 $this->setChannelElement('link', $link);
224 * Set the 'image' channel element
227 * @param srting title of image
228 * @param srting link url of the imahe
229 * @param srting path url of the image
232 public function setImage($title, $link, $url)
234 $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));
237 // End # public functions ----------------------------------------------
239 // Start # private functions ----------------------------------------------
242 * Prints the xml and rss namespace
247 private function printHead()
249 if ($this->version
== RSS2
)
251 $out = '<?xml version="1.0" encoding="utf-8"?>'."\n";
252 if ($this->xsl
) $out .= '<?xml-stylesheet type="text/xsl" href="'.htmlspecialchars($this->xsl
).'"?>' . PHP_EOL
;
253 $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
;
256 elseif ($this->version
== JSON
|| $this->version
== JSONP
)
258 $this->json
->rss
= array('@attributes' => array('version' => '2.0'));
263 * Closes the open tags at the end of file
268 private function printTale()
270 if ($this->version
== RSS2
)
272 echo '</channel>',PHP_EOL
,'</rss>';
274 // do nothing for JSON
278 * Creates a single node as xml format
281 * @param string name of the tag
282 * @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format
283 * @param array Attributes(if any) in 'attrName' => 'attrValue' format
284 * @return string formatted xml tag
286 private function makeNode($tagName, $tagContent, $attributes = null)
288 if ($this->version
== RSS2
)
292 if (is_array($attributes))
294 foreach ($attributes as $key => $value)
296 $attrText .= " $key=\"$value\" ";
299 $nodeText .= "<{$tagName}{$attrText}>";
300 if (is_array($tagContent))
302 foreach ($tagContent as $key => $value)
304 $nodeText .= $this->makeNode($key, $value);
309 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);
310 $nodeText .= htmlspecialchars($tagContent);
312 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? "]]></$tagName>" : "</$tagName>";
313 $nodeText .= "</$tagName>";
314 return $nodeText . PHP_EOL
;
316 elseif ($this->version
== JSON
|| $this->version
== JSONP
)
318 $tagName = (string)$tagName;
319 $tagName = strtr($tagName, ':', '_');
321 if (!$tagContent && is_array($attributes) && count($attributes))
323 $node = array('@attributes' => $this->json_keys($attributes));
325 if (is_array($tagContent)) {
326 $node = $this->json_keys($tagContent);
333 return ''; // should not get here
336 private function json_keys(array $array) {
338 foreach ($array as $key => $val) {
339 if (is_string($key)) $key = strtr($key, ':', '_');
340 if (is_array($val)) {
341 $new[$key] = $this->json_keys($val);
350 * @desc Print channels
354 private function printChannels()
357 if ($this->version
== RSS2
) {
358 echo '<channel>' . PHP_EOL
;
360 foreach ($this->hubs
as $hub) {
361 //echo $this->makeNode('link', '', array('rel'=>'hub', 'href'=>$hub, 'xmlns'=>'http://www.w3.org/2005/Atom'));
362 echo '<link rel="hub" href="'.htmlspecialchars($hub).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
365 if (isset($this->self
)) {
366 //echo $this->makeNode('link', '', array('rel'=>'self', 'href'=>$this->self, 'xmlns'=>'http://www.w3.org/2005/Atom'));
367 echo '<link rel="self" href="'.htmlspecialchars($this->self
).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
369 //Print Items of channel
370 foreach ($this->channels
as $key => $value)
372 echo $this->makeNode($key, $value);
374 } elseif ($this->version
== JSON
|| $this->version
== JSONP
) {
375 $this->json
->rss
['channel'] = (object)$this->json_keys($this->channels
);
380 * Prints formatted feed items
385 private function printItems()
387 foreach ($this->items
as $item) {
388 $itemElements = $item->getElements();
390 echo $this->startItem();
392 if ($this->version
== JSON
|| $this->version
== JSONP
) {
393 $json_item = array();
396 foreach ($itemElements as $thisElement) {
397 foreach ($thisElement as $instance) {
398 if ($this->version
== RSS2
) {
399 echo $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
400 } elseif ($this->version
== JSON
|| $this->version
== JSONP
) {
401 $_json_node = $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
402 if (count($thisElement) > 1) {
403 $json_item[strtr($instance['name'], ':', '_')][] = $_json_node;
405 $json_item[strtr($instance['name'], ':', '_')] = $_json_node;
410 echo $this->endItem();
411 if ($this->version
== JSON
|| $this->version
== JSONP
) {
412 if (count($this->items
) > 1) {
413 $this->json
->rss
['channel']->item
[] = $json_item;
415 $this->json
->rss
['channel']->item
= $json_item;
422 * Make the starting tag of channels
427 private function startItem()
429 if ($this->version
== RSS2
)
431 echo '<item>' . PHP_EOL
;
437 * Closes feed item tag
442 private function endItem()
444 if ($this->version
== RSS2
)
446 echo '</item>' . PHP_EOL
;
451 // End # private functions ----------------------------------------------