Various improvements.
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / RSSFromGit.php
blob74014fd82c20f65626a403bafb590550213792c7
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-vc', 'rss-from-git', 'rss-from-svn');
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-vc');
25 if (!$trigger) $trigger = $this->confiscateAttr($dom->documentElement, $this->ns, 'rss-from-git');
26 if ($trigger == 'yes') {
27 // this shows up twice because we have to perform all
28 // confiscations before aborting
29 if (!$logs) return;
30 $link = $dom->createElement('link');
31 $link->setAttribute('rel', 'alternate');
32 $link->setAttribute('type', 'application/rss+xml');
33 $link->setAttribute('title', str_replace('%s', $page->getCachePath(), $this->title));
34 $link->setAttribute('href', $page->getPathStem() . '.rss');
35 $head = $this->query('//html:head')->item(0);
36 $head->appendChild($link);
37 } else {
38 // grab the document's links to RSS feeds
39 // link must be marked with xc:rss-from-git (or
40 // xc:rss-from-svn, for BC reasons)
41 // only one allowed
42 $link = $this->query('//html:link[@xc:rss-from-vc]')->item(0);
43 if (!$link) return; // nothing to do
45 $trigger = $this->confiscateAttr($link, $this->ns, 'rss-from-vc');
46 if ($trigger != 'yes') return;
47 if (!$logs) return;
49 $manager->addDependency(__FILE__);
51 $path = $page->getWebPath();
53 $rss = new XHTMLCompiler_RSSFeed(
54 $link->getAttribute('title'),
55 $path,
56 str_replace('%s', $page->getCachePath(), $this->description),
57 $dom->documentElement->getAttribute('xml:lang')
60 for ($i = 0; $i < $this->limit && isset($logs[$i]); $i++) {
61 $commit = $logs[$i];
63 // :TODO: link to gitweb commitdiff
64 $item_link = $path . '#git_' . $commit->id;
66 // generate short message (first line) for title\
68 $rss->addItem(
69 $item_link,
70 htmlspecialchars($commit->message),
71 $d = $commit->committedDate->format('r'),
72 // :TODO: Extend phpgit so that we can retrieve full message
73 htmlspecialchars($commit->message)
77 $rss->save(
78 $page->normalizePath(
79 $link->getAttribute('href')