Remove trailing ?>
[xhtml-compiler.git] / XHTMLCompiler / RSSFeed.php
blob088d821cc8ae280f03b6e97112e86f7329b64764
1 <?php
3 /**
4 * Representation of an RSS feed, convenience wrapper for quick building
5 */
6 class XHTMLCompiler_RSSFeed
9 /** Internal DOM that represents the feed */
10 protected $doc;
12 /** Convenient reference to channel node in $this->doc */
13 protected $channel;
15 /**
16 * @param $title Title of the feed
17 * @param $path Fully-formed webpath to feed
18 * @param $description Optional description of feed
19 * @param $lang Optional language of feed
21 public function __construct($title, $path, $description = null, $lang = null) {
22 $this->doc = new DOMDocument('1.0', 'UTF-8');
23 $this->doc->formatOutput = true;
25 $rss = $this->doc->createElement('rss');
26 $rss->setAttribute('version', '2.0');
27 $this->doc->appendChild($rss);
29 $channel = $this->doc->createElement('channel');
30 $rss->appendChild($channel);
32 $channel->appendChild($this->doc->createElement('title', $title));
33 $channel->appendChild($this->doc->createElement('link', $path));
34 if ($lang) {
35 $channel->appendChild(
36 $this->doc->createElement('language', $lang)
39 if ($description) {
40 $channel->appendChild(
41 $this->doc->createElement('description', $description)
44 $channel->appendChild(
45 $this->doc->createElement('generator', 'XHTML Compiler')
48 $this->channel = $channel;
51 /**
52 * Adds a news item to the RSS feed
53 * @param $link Link to relevant article
54 * @param $title Title of the news item
55 * @param $date Date of the article
56 * @param $body Short description of article
58 public function addItem($link, $title, $date, $body) {
59 $item = $this->doc->createElement('item');
60 $this->channel->appendChild($item);
62 $body = preg_replace("/\s+/", ' ', $body);
64 if ($title) $item->appendChild($this->doc->createElement('title', htmlspecialchars($title)));
65 $item->appendChild($this->doc->createElement('pubDate', $date));
66 $item->appendChild($this->doc->createElement('description', htmlspecialchars($body)));
67 $item->appendChild($this->doc->createElement('link', htmlspecialchars($link)));
70 /**
71 * Saves RSS feed to path
72 * @param $path Path to save feed to
74 public function save($path) {
75 $this->doc->save($path);
76 chmod($path, 0644);