]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/feedwriter/FeedWriter.php
2 define('RSS2', 1, true);
3 define('JSON', 2, true);
4 define('ATOM', 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 } elseif ($this->version
== JSON
) {
95 header('Content-type: application/json; charset=UTF-8');
96 $this->json
= new stdClass();
99 $this->printChannels();
102 if ($this->version
== JSON
) {
103 echo json_encode($this->json
);
108 * Create a new FeedItem.
111 * @return object instance of FeedItem class
113 public function createNewItem()
115 $Item = new FeedItem($this->version
);
120 * Add a FeedItem to the main class
123 * @param object instance of FeedItem class
126 public function addItem($feedItem)
128 $this->items
[] = $feedItem;
131 // Wrapper functions -------------------------------------------------------------------
134 * Set the 'title' channel element
137 * @param srting value of 'title' channel tag
140 public function setTitle($title)
142 $this->setChannelElement('title', $title);
146 * Add a hub to the channel element
152 public function addHub($hub)
154 $this->hubs
[] = $hub;
164 public function setXsl($xsl)
176 public function setSelf($self)
182 * Set the 'description' channel element
185 * @param srting value of 'description' channel tag
188 public function setDescription($desciption)
190 $tag = ($this->version
== ATOM
)? 'subtitle' : 'description';
191 $this->setChannelElement($tag, $desciption);
195 * Set the 'link' channel element
198 * @param srting value of 'link' channel tag
201 public function setLink($link)
203 $this->setChannelElement('link', $link);
207 * Set the 'image' channel element
210 * @param srting title of image
211 * @param srting link url of the imahe
212 * @param srting path url of the image
215 public function setImage($title, $link, $url)
217 $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));
220 // End # public functions ----------------------------------------------
222 // Start # private functions ----------------------------------------------
225 * Prints the xml and rss namespace
230 private function printHead()
232 if ($this->version
== RSS2
)
234 $out = '<?xml version="1.0" encoding="utf-8"?>'."\n";
235 if ($this->xsl
) $out .= '<?xml-stylesheet type="text/xsl" href="'.htmlspecialchars($this->xsl
).'"?>' . PHP_EOL
;
236 $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">' . PHP_EOL
;
239 elseif ($this->version
== JSON
)
241 $this->json
->rss
= array('@attributes' => array('version' => '2.0'));
246 * Closes the open tags at the end of file
251 private function printTale()
253 if ($this->version
== RSS2
)
255 echo '</channel>',PHP_EOL
,'</rss>';
257 // do nothing for JSON
261 * Creates a single node as xml format
264 * @param string name of the tag
265 * @param mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format
266 * @param array Attributes(if any) in 'attrName' => 'attrValue' format
267 * @return string formatted xml tag
269 private function makeNode($tagName, $tagContent, $attributes = null)
271 if ($this->version
== RSS2
)
275 if (is_array($attributes))
277 foreach ($attributes as $key => $value)
279 $attrText .= " $key=\"$value\" ";
282 $nodeText .= "<{$tagName}{$attrText}>";
283 if (is_array($tagContent))
285 foreach ($tagContent as $key => $value)
287 $nodeText .= $this->makeNode($key, $value);
292 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);
293 $nodeText .= htmlspecialchars($tagContent);
295 //$nodeText .= (in_array($tagName, $this->CDATAEncoding))? "]]></$tagName>" : "</$tagName>";
296 $nodeText .= "</$tagName>";
297 return $nodeText . PHP_EOL
;
299 elseif ($this->version
== JSON
)
301 $tagName = (string)$tagName;
302 $tagName = strtr($tagName, ':', '_');
304 if (!$tagContent && is_array($attributes) && count($attributes))
306 $node = array('@attributes' => $this->json_keys($attributes));
308 if (is_array($tagContent)) {
309 $node = $this->json_keys($tagContent);
316 return ''; // should not get here
319 private function json_keys(array $array) {
321 foreach ($array as $key => $val) {
322 if (is_string($key)) $key = strtr($key, ':', '_');
323 if (is_array($val)) {
324 $new[$key] = $this->json_keys($val);
333 * @desc Print channels
337 private function printChannels()
340 switch ($this->version
)
343 echo '<channel>' . PHP_EOL
;
345 foreach ($this->hubs
as $hub) {
346 //echo $this->makeNode('link', '', array('rel'=>'hub', 'href'=>$hub, 'xmlns'=>'http://www.w3.org/2005/Atom'));
347 echo '<link rel="hub" href="'.htmlspecialchars($hub).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
350 if (isset($this->self
)) {
351 //echo $this->makeNode('link', '', array('rel'=>'self', 'href'=>$this->self, 'xmlns'=>'http://www.w3.org/2005/Atom'));
352 echo '<link rel="self" href="'.htmlspecialchars($this->self
).'" xmlns="http://www.w3.org/2005/Atom" />' . PHP_EOL
;
354 //Print Items of channel
355 foreach ($this->channels
as $key => $value)
357 echo $this->makeNode($key, $value);
361 $this->json
->rss
['channel'] = (object)$this->json_keys($this->channels
);
367 * Prints formatted feed items
372 private function printItems()
374 foreach ($this->items
as $item)
376 $thisItems = $item->getElements();
378 echo $this->startItem();
380 if ($this->version
== JSON
) {
381 $json_item = array();
384 foreach ($thisItems as $feedItem )
386 if ($this->version
== RSS2
) {
387 echo $this->makeNode($feedItem['name'], $feedItem['content'], $feedItem['attributes']);
388 } elseif ($this->version
== JSON
) {
389 $json_item[strtr($feedItem['name'], ':', '_')] = $this->makeNode($feedItem['name'], $feedItem['content'], $feedItem['attributes']);
392 echo $this->endItem();
393 if ($this->version
== JSON
) {
394 if (count($this->items
) > 1) {
395 $this->json
->rss
['channel']->item
[] = $json_item;
397 $this->json
->rss
['channel']->item
= $json_item;
404 * Make the starting tag of channels
409 private function startItem()
411 if ($this->version
== RSS2
)
413 echo '<item>' . PHP_EOL
;
419 * Closes feed item tag
424 private function endItem()
426 if ($this->version
== RSS2
)
428 echo '</item>' . PHP_EOL
;
433 // End # private functions ----------------------------------------------