Add dependency checking for included files.
[htmlpurifier-web.git] / xhtml-compiler / XHTMLCompiler / DOMFilter / RSSGenerator.php
blob1b951bb1ef96282684d1f92db418493de959cafa
1 <?php
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);
31 /**
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);
50 // setup the RSS feed
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'));
67 // parse data source
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')) {
78 case 'title':
79 $item->appendChild($doc->createElement('title',
80 $title=$element->textContent));
81 break;
82 case 'date':
83 $item->appendChild($doc->createElement('pubDate',
84 $element->textContent));
85 break;
86 case 'body':
87 $item->appendChild($doc->createElement('description',
88 $element->textContent));
89 break;
93 $item_id = $src_item->getAttribute('id');
94 if (!$item_id) {
95 trigger_error("News item '$title' has no ID");
96 continue;
98 $item->appendChild($doc->createElement('link', $path . '#' . $item_id));
102 // save the feed
103 $doc->save($location);
104 chmod($location, 0666);