3 class XHTMLCompiler_DOMFilter_RSSGenerator
extends XHTMLCompiler_DOMFilter
6 protected $name = 'RSSGenerator';
7 protected $prefix = 'rss';
9 // new namespace defines the following attributes:
10 // for - IDREF to an element where we are to get the RSS info from
12 public function process(DOMDocument
$dom, $page, $manager) {
14 // attempt to find declarations of the namespace
15 $nodes = $this->query(
16 "//attribute::*[namespace-uri() = '".$this->ns
."']"
18 if (!$nodes->length
) return;
20 // grab the document's links to RSS feeds
21 // we require that the link have a href, a title and a type
22 // as well as an rss:for attribute specifying where to grab data
23 $links = $this->query('//html:link[@rss:for]');
25 foreach ($links as $link) {
26 $this->generateRSS($dom, $link, $page);
32 * Generates the RSS feed for a specific link in a document
33 * @param $link <link> DOMElement we're generating feed for
34 * @param $page Page we're generating for
36 protected function generateRSS($dom, $link, $page) {
37 // get data from the document
38 $location = $link->getAttribute('href');
39 $title = $link->getAttribute('title');
40 $path = $page->getWebPath();
42 // remove specialized attributes
43 $id = $this->confiscateAttr($link, $this->ns
, 'for');
44 $description = $this->confiscateAttr($link, $this->ns
, 'description');
46 $lang = $dom->documentElement
->getAttribute('xml:lang');
48 $data_source = $dom->getElementById($id);
51 $doc = new DOMDocument('1.0', 'UTF-8');
52 $doc->formatOutput
= true;
54 $rss = $doc->createElement('rss');
55 $rss->setAttribute('version', '2.0');
56 $doc->appendChild($rss);
58 $channel = $doc->createElement('channel');
59 $rss->appendChild($channel);
61 $channel->appendChild($doc->createElement('title', $title));
62 $channel->appendChild($doc->createElement('link', $path));
63 if ($lang) $channel->appendChild($doc->createElement('language', $lang));
64 if ($description) $channel->appendChild($doc->createElement('description', $description));
65 $channel->appendChild($doc->createElement('generator', 'XHTML Compiler'));
68 foreach ($data_source->childNodes
as $src_item) {
69 if (! $src_item instanceof DOMElement
) continue;
70 if ($src_item->getAttribute('class') !== 'item') continue;
72 $item = $doc->createElement('item');
73 $channel->appendChild($item);
75 foreach ($src_item->childNodes
as $element) {
76 if (! $element instanceof DOMElement
) continue;
77 switch ($element->getAttribute('class')) {
79 $item->appendChild($doc->createElement('title',
80 $title=$element->textContent
));
83 $item->appendChild($doc->createElement('pubDate',
84 $element->textContent
));
87 $item->appendChild($doc->createElement('description',
88 $element->textContent
));
93 $item_id = $src_item->getAttribute('id');
95 trigger_error("News item '$title' has no ID");
98 $item->appendChild($doc->createElement('link', $path . '#' . $item_id));
103 $doc->save($location);
104 chmod($location, 0666);