]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - 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 header('Content-type: text/xml; charset=UTF-8');
93 // this line prevents Chrome 20 from prompting download
94 // used by Google: https://news.google.com/news/feeds?ned=us&topic=b&output=rss
95 header('X-content-type-options: nosniff');
98 $this->printChannels();
101 if ($this->version
== JSON
|| $this->version
== JSONP
) {
102 echo json_encode($this->json
);
107 * Create a new FeedItem.
110 * @return object instance of FeedItem class
112 public function createNewItem()
114 $Item = new FeedItem($this->version
);
119 * Add a FeedItem to the main class
122 * @param object instance of FeedItem class
125 public function addItem($feedItem)
127 $this->items
[] = $feedItem;
130 // Wrapper functions -------------------------------------------------------------------
133 * Set the 'title' channel element
136 * @param srting value of 'title' channel tag
139 public function setTitle($title)
141 $this->setChannelElement('title', $title);
145 * Add a hub to the channel element
151 public function addHub($hub)
153 $this->hubs
[] = $hub;
163 public function setXsl($xsl)
175 public function setSelf($self)
181 * Set the 'description' channel element
184 * @param srting value of 'description' channel tag
187 public function setDescription($description)
189 $this->setChannelElement('description', $description);
193 * Set the 'link' channel element
196 * @param srting value of 'link' channel tag
199 public function setLink($link)
201 $this->setChannelElement('link', $link);
205 * Set the 'image' channel element
208 * @param srting title of image
209 * @param srting link url of the imahe
210 * @param srting path url of the image
213 public function setImage($title, $link, $url)
215 $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));
218 // End # public functions ----------------------------------------------
220 // Start # private functions ----------------------------------------------
223 * Prints the xml and rss namespace
228 private function printHead()
230 if ($this->version
== RSS2
)
232 $out = '<?xml version="1.0" encoding="utf-8"?>'."\n";
233 if ($this->xsl
) $out .= '<?xml-stylesheet type="text/xsl" href="'.htmlspecialchars($this->xsl
).'"?>' . PHP_EOL
;
234 $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">' . PHP_EOL
;
237 elseif ($this->version
== JSON
|| $this->version
== JSONP
)
239 $this->json
->rss
= array('@attributes' => array('version' => '2.0'));
244 * Closes the open tags at the end of file
249 private function printTale()
251 if ($this->version
== RSS2
)
253 echo '</channel>',PHP_EOL
,'</rss>';
255 // do nothing for JSON
259 * Creates a single node as xml format
262 * @param string name of the tag
263 * @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format
264 * @param array Attributes(if any) in 'attrName' => 'attrValue' format
265 * @return string formatted xml tag
267 private function makeNode($tagName, $tagContent, $attributes = null)
269 if ($this->version
== RSS2
)
273 if (is_array($attributes))
275 foreach ($attributes as $key => $value)
277 $attrText .= " $key=\"$value\" ";
280 $nodeText .= "<{$tagName}{$attrText}>";
281 if (is_array($tagContent))
283 foreach ($tagContent as $key => $value)
285 $nodeText .= $this->makeNode($key, $value);
290 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);
291 $nodeText .= htmlspecialchars($tagContent);
293 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? "]]></$tagName>" : "</$tagName>";
294 $nodeText .= "</$tagName>";
295 return $nodeText . PHP_EOL
;
297 elseif ($this->version
== JSON
|| $this->version
== JSONP
)
299 $tagName = (string)$tagName;
300 $tagName = strtr($tagName, ':', '_');
302 if (!$tagContent && is_array($attributes) && count($attributes))
304 $node = array('@attributes' => $this->json_keys($attributes));
306 if (is_array($tagContent)) {
307 $node = $this->json_keys($tagContent);
314 return ''; // should not get here
317 private function json_keys(array $array) {
319 foreach ($array as $key => $val) {
320 if (is_string($key)) $key = strtr($key, ':', '_');
321 if (is_array($val)) {
322 $new[$key] = $this->json_keys($val);
331 * @desc Print channels
335 private function printChannels()
338 if ($this->version
== RSS2
) {
339 echo '<channel>' . PHP_EOL
;
341 foreach ($this->hubs
as $hub) {
342 //echo $this->makeNode('link', '', array('rel'=>'hub', 'href'=>$hub, 'xmlns'=>'http://www.w3.org/2005/Atom'));
343 echo '<link rel="hub" href="'.htmlspecialchars($hub).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
346 if (isset($this->self
)) {
347 //echo $this->makeNode('link', '', array('rel'=>'self', 'href'=>$this->self, 'xmlns'=>'http://www.w3.org/2005/Atom'));
348 echo '<link rel="self" href="'.htmlspecialchars($this->self
).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
350 //Print Items of channel
351 foreach ($this->channels
as $key => $value)
353 echo $this->makeNode($key, $value);
355 } elseif ($this->version
== JSON
|| $this->version
== JSONP
) {
356 $this->json
->rss
['channel'] = (object)$this->json_keys($this->channels
);
361 * Prints formatted feed items
366 private function printItems()
368 foreach ($this->items
as $item) {
369 $itemElements = $item->getElements();
371 echo $this->startItem();
373 if ($this->version
== JSON
|| $this->version
== JSONP
) {
374 $json_item = array();
377 foreach ($itemElements as $thisElement) {
378 foreach ($thisElement as $instance) {
379 if ($this->version
== RSS2
) {
380 echo $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
381 } elseif ($this->version
== JSON
|| $this->version
== JSONP
) {
382 $_json_node = $this->makeNode($instance['name'], $instance['content'], $instance['attributes']);
383 if (count($thisElement) > 1) {
384 $json_item[strtr($instance['name'], ':', '_')][] = $_json_node;
386 $json_item[strtr($instance['name'], ':', '_')] = $_json_node;
391 echo $this->endItem();
392 if ($this->version
== JSON
|| $this->version
== JSONP
) {
393 if (count($this->items
) > 1) {
394 $this->json
->rss
['channel']->item
[] = $json_item;
396 $this->json
->rss
['channel']->item
= $json_item;
403 * Make the starting tag of channels
408 private function startItem()
410 if ($this->version
== RSS2
)
412 echo '<item>' . PHP_EOL
;
418 * Closes feed item tag
423 private function endItem()
425 if ($this->version
== RSS2
)
427 echo '</item>' . PHP_EOL
;
432 // End # private functions ----------------------------------------------