227e56a547f97086e671ac85456ac9a633e0888f
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / RSSFromGit.php
blob227e56a547f97086e671ac85456ac9a633e0888f
1 <?php
3 /**
4 * GEnerates an RSS feed from a page's associated Git log.
5 */
6 class XHTMLCompiler_DOMFilter_RSSFromGit extends XHTMLCompiler_DOMFilter
8 protected $name = 'RSSFromGit';
9 /** Number of log entries to display */
10 protected $limit = 10;
11 /** Description of changelog to place in feed title */
12 protected $description = 'Git changelog for %s';
13 /** Compact title to place in <link> and other places */
14 protected $title = 'Log for %s';
15 protected $xcAttr = array('rss-from-git');
17 public function process(DOMDocument $dom, $page, $manager) {
18 $xc = XHTMLCompiler::getInstance();
19 // implement gitweb/repo.or.cz support
21 $logs = $page->getLog();
23 // see if we need to make the link ourself
24 $trigger = $this->confiscateAttr($dom->documentElement, $this->ns, 'rss-from-git');
25 if ($trigger == 'yes') {
26 // this shows up twice because we have to perform all
27 // confiscations before aborting
28 if (!$logs) return;
29 $link = $dom->createElement('link');
30 $link->setAttribute('rel', 'alternate');
31 $link->setAttribute('type', 'application/rss+xml');
32 $link->setAttribute('title', str_replace('%s', $page->getCachePath(), $this->title));
33 $link->setAttribute('href', $page->getPathStem() . '.rss');
34 $head = $this->query('//html:head')->item(0);
35 $head->appendChild($link);
36 } else {
37 // grab the document's links to RSS feeds
38 // link must be marked with xc:rss-from-svn
39 // only one allowed
40 $link = $this->query('//html:link[@xc:rss-from-svn]')->item(0);
41 if (!$link) return; // nothing to do
43 $trigger = $this->confiscateAttr($link, $this->ns, 'rss-from-svn');
44 if ($trigger != 'yes') return;
45 if (!$logs) return;
48 $path = $page->getWebPath();
50 $rss = new XHTMLCompiler_RSSFeed(
51 $link->getAttribute('title'),
52 $path,
53 str_replace('%s', $page->getCachePath(), $this->description),
54 $dom->documentElement->getAttribute('xml:lang')
57 for ($i = 0; $i < $this->limit && isset($logs[$i]); $i++) {
58 $commit = $logs[$i];
60 // :TODO: link to gitweb commitdiff
61 $item_link = $path . '#git_' . $commit->id;
63 // generate short message (first line) for title\
65 $rss->addItem(
66 $item_link,
67 htmlspecialchars($commit->message),
68 $d = $commit->committedDate->format('r'),
69 // :TODO: Extend phpgit so that we can retrieve full message
70 htmlspecialchars($commit->message)
74 $rss->save(
75 $page->normalizePath(
76 $link->getAttribute('href')