Remove trailing ?>
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / RSSGenerator.php
blob19e4010b5d72b4205d999e256c6651994228135d
1 <?php
3 /**
4 * Generates an RSS feed from a specially designed HTML container indicated
5 * by rss:for.
6 */
7 class XHTMLCompiler_DOMFilter_RSSGenerator extends XHTMLCompiler_DOMFilter
10 protected $name = 'RSSGenerator';
11 protected $prefix = 'rss';
13 // new namespace defines the following attributes:
14 // for - IDREF to an element where we are to get the RSS info from
16 public function process(DOMDocument $dom, $page, $manager) {
18 // attempt to find declarations of the namespace
19 $nodes = $this->query(
20 "//attribute::*[namespace-uri() = '".$this->ns."']"
22 if (!$nodes->length) return;
24 // grab the document's links to RSS feeds
25 // we require that the link have a href, a title and a type
26 // as well as an rss:for attribute specifying where to grab data
27 $links = $this->query('//html:link[@rss:for]');
29 foreach ($links as $link) {
30 $this->generateRSS($dom, $link, $page);
35 /**
36 * Generates the RSS feed for a specific link in a document
37 * @param $link <link> DOMElement we're generating feed for
38 * @param $page Page we're generating for
40 protected function generateRSS($dom, $link, $page) {
42 // retrieve web-path of the page
43 $path = $page->getWebPath();
45 // generate RSS template
46 $rss = new XHTMLCompiler_RSSFeed(
47 $link->getAttribute('title'),
48 $path,
49 $this->confiscateAttr($link, $this->ns, 'description'),
50 $lang = $dom->documentElement->getAttribute('xml:lang')
53 // retrieve data source
54 $id = $this->confiscateAttr($link, $this->ns, 'for');
55 $data_source = $dom->getElementById($id);
57 // parse data source, add news items
58 foreach ($data_source->childNodes as $src_item) {
59 if (! $src_item instanceof DOMElement) continue;
60 if ($src_item->getAttribute('class') !== 'item') continue;
62 $title = $date = $body = '';
63 foreach ($src_item->childNodes as $element) {
64 if (! $element instanceof DOMElement) continue;
65 $var = $element->getAttribute('class');
66 if ($var == 'title' || $var == 'date' || $var == 'body') {
67 $$var = $element->textContent;
71 // determine the article link, based off anchors
72 $item_id = $src_item->getAttribute('id');
73 if (!$item_id) {
74 trigger_error("News item '$title' has no ID");
75 continue;
77 $article_link = $path . '#' . $item_id;
79 $rss->addItem($article_link, $title, $date, $body);
83 // save the feed
84 $rss->save(
85 $page->normalizePath(
86 $link->getAttribute('href')