]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/feedwriter/FeedWriter.php
2 // RSS 0.90 Officially obsoleted by 1.0
3 // RSS 0.91, 0.92, 0.93 and 0.94 Officially obsoleted by 2.0
4 // So, define constants for RSS 1.0, RSS 2.0 and ATOM
6 define('RSS1', 'RSS 1.0', true);
7 define('RSS2', 'RSS 2.0', true);
8 define('ATOM', 'ATOM', true);
11 * Univarsel Feed Writer class
13 * Genarate RSS 1.0, RSS2.0 and ATOM Feed
15 * @package UnivarselFeedWriter
16 * @author Anis uddin Ahmad <anisniit@gmail.com>
17 * @link http://www.ajaxray.com/projects/rss
21 private $channels = array(); // Collection of channel elements
22 private $items = array(); // Collection of items as object of FeedItem class.
23 private $data = array(); // Store some other version wise data
24 private $CDATAEncoding = array(); // The tag names which have to encoded as CDATA
26 private $version = null;
31 * @param constant the version constant (RSS1/RSS2/ATOM).
33 function __construct($version = RSS2
)
35 $this->version
= $version;
37 // Setting default value for assential channel elements
38 $this->channels
['title'] = $version . ' Feed';
39 $this->channels
['link'] = 'http://www.ajaxray.com/blog';
41 //Tag names to encode in CDATA
42 $this->CDATAEncoding
= array('description', 'content:encoded', 'summary');
45 // Start # public functions ---------------------------------------------
48 * Set a channel element
50 * @param srting name of the channel tag
51 * @param string content of the channel tag
54 public function setChannelElement($elementName, $content)
56 $this->channels
[$elementName] = $content ;
60 * Set multiple channel elements from an array. Array elements
61 * should be 'channelName' => 'channelContent' format.
64 * @param array array of channels
67 public function setChannelElementsFromArray($elementArray)
69 if(! is_array($elementArray)) return;
70 foreach ($elementArray as $elementName => $content)
72 $this->setChannelElement($elementName, $content);
77 * Genarate the actual RSS/ATOM file
82 public function genarateFeed()
84 header("Content-type: text/xml");
87 $this->printChannels();
93 * Create a new FeedItem.
96 * @return object instance of FeedItem class
98 public function createNewItem()
100 $Item = new FeedItem($this->version
);
105 * Add a FeedItem to the main class
108 * @param object instance of FeedItem class
111 public function addItem($feedItem)
113 $this->items
[] = $feedItem;
117 // Wrapper functions -------------------------------------------------------------------
120 * Set the 'title' channel element
123 * @param srting value of 'title' channel tag
126 public function setTitle($title)
128 $this->setChannelElement('title', $title);
132 * Set the 'description' channel element
135 * @param srting value of 'description' channel tag
138 public function setDescription($desciption)
140 $this->setChannelElement('description', $desciption);
144 * Set the 'link' channel element
147 * @param srting value of 'link' channel tag
150 public function setLink($link)
152 $this->setChannelElement('link', $link);
156 * Set the 'image' channel element
159 * @param srting title of image
160 * @param srting link url of the imahe
161 * @param srting path url of the image
164 public function setImage($title, $link, $url)
166 $this->setChannelElement('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));
170 * Set the 'about' channel element. Only for RSS 1.0
173 * @param srting value of 'about' channel tag
176 public function setChannelAbout($url)
178 $this->data
['ChannelAbout'] = $url;
183 * @author Anis uddin Ahmad <admin@ajaxray.com>
184 * @param string an optional prefix
185 * @return string the formated uuid
187 public static function uuid($key = null, $prefix = '')
189 $key = ($key == null)? uniqid(rand()) : $key;
191 $uuid = substr($chars,0,8) . '-';
192 $uuid .= substr($chars,8,4) . '-';
193 $uuid .= substr($chars,12,4) . '-';
194 $uuid .= substr($chars,16,4) . '-';
195 $uuid .= substr($chars,20,12);
197 return $prefix . $uuid;
199 // End # public functions ----------------------------------------------
201 // Start # private functions ----------------------------------------------
204 * Prints the xml and rss namespace
209 private function printHead()
211 $out = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
213 if($this->version
== RSS2
)
215 $out .= '<rss version="2.0"
216 xmlns:content="http://purl.org/rss/1.0/modules/content/"
217 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
220 elseif($this->version
== RSS1
)
223 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
224 xmlns="http://purl.org/rss/1.0/"
225 xmlns:dc="http://purl.org/dc/elements/1.1/"
228 else if($this->version
== ATOM
)
230 $out .= '<feed xmlns="http://www.w3.org/2005/Atom">' . PHP_EOL
;;
236 * Closes the open tags at the end of file
241 private function printTale()
243 if($this->version
== RSS2
)
245 echo '</channel>' . PHP_EOL
. '</rss>';
247 elseif($this->version
== RSS1
)
251 else if($this->version
== ATOM
)
259 * Creates a single node as xml format
262 * @param srting 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)
272 if(is_array($attributes))
274 foreach ($attributes as $key => $value)
276 $attrText .= " $key=\"$value\" ";
280 if(is_array($tagContent) && $this->version
== RSS1
)
282 $attrText = ' rdf:parseType="Resource"';
286 $attrText .= (in_array($tagName, $this->CDATAEncoding
) && $this->version
== ATOM
)? ' type="html" ' : '';
287 $nodeText .= (in_array($tagName, $this->CDATAEncoding
))? "<{$tagName}{$attrText}><![CDATA[" : "<{$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);
301 $nodeText .= (in_array($tagName, $this->CDATAEncoding
))? "]]></$tagName>" : "</$tagName>";
303 return $nodeText . PHP_EOL
;
307 * @desc Print channels
311 private function printChannels()
314 switch ($this->version
)
317 echo '<channel>' . PHP_EOL
;
320 echo (isset($this->data
['ChannelAbout']))? "<channel rdf:about=\"{$this->data['ChannelAbout']}\">" : "<channel rdf:about=\"{$this->channels['link']}\">";
324 //Print Items of channel
325 foreach ($this->channels
as $key => $value)
327 if($this->version
== ATOM
&& $key == 'link')
329 // ATOM prints link element as href attribute
330 echo $this->makeNode($key,'',array('href'=>$value));
331 //Add the id for ATOM
332 echo $this->makeNode('id',self
::uuid($value,'urn:uuid:'));
336 echo $this->makeNode($key, $value);
341 //RSS 1.0 have special tag <rdf:Seq> with channel
342 if($this->version
== RSS1
)
344 echo "<items>" . PHP_EOL
. "<rdf:Seq>" . PHP_EOL
;
345 foreach ($this->items
as $item)
347 $thisItems = $item->getElements();
348 echo "<rdf:li resource=\"{$thisItems['link']['content']}\"/>" . PHP_EOL
;
350 echo "</rdf:Seq>" . PHP_EOL
. "</items>" . PHP_EOL
. "</channel>" . PHP_EOL
;
355 * Prints formatted feed items
360 private function printItems()
362 foreach ($this->items
as $item)
364 $thisItems = $item->getElements();
366 //the argument is printed as rdf:about attribute of item in rss 1.0
367 echo $this->startItem($thisItems['link']['content']);
369 foreach ($thisItems as $feedItem )
371 echo $this->makeNode($feedItem['name'], $feedItem['content'], $feedItem['attributes']);
373 echo $this->endItem();
378 * Make the starting tag of channels
381 * @param srting The vale of about tag which is used for only RSS 1.0
384 private function startItem($about = false)
386 if($this->version
== RSS2
)
388 echo '<item>' . PHP_EOL
;
390 elseif($this->version
== RSS1
)
394 echo "<item rdf:about=\"$about\">" . PHP_EOL
;
398 die('link element is not set .\n It\'s required for RSS 1.0 to be used as about attribute of item');
401 else if($this->version
== ATOM
)
403 echo "<entry>" . PHP_EOL
;
408 * Closes feed item tag
413 private function endItem()
415 if($this->version
== RSS2
|| $this->version
== RSS1
)
417 echo '</item>' . PHP_EOL
;
419 else if($this->version
== ATOM
)
421 echo "</entry>" . PHP_EOL
;
427 // End # private functions ----------------------------------------------
429 } // end of class FeedWriter
432 function __autoload($class_name)
434 require_once $class_name . '.php';