Improve documentation.
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / RSSFromSVN.php
blobe919fb82abba9693deab11af8512e12c34f14846
1 <?php
3 /**
4 * Generates an RSS feed from a page's associated SVN log
5 */
6 class XHTMLCompiler_DOMFilter_RSSFromSVN extends XHTMLCompiler_DOMFilter
9 protected $name = 'RSSFromSVN';
11 /**
12 * Number of log entries to display
14 protected $limit = 10;
16 /**
17 * Description of changelog to place in RSS feed title
19 protected $description = 'Subversion changelog for %s';
21 /**
22 * Compact title to place in <link> and other places
24 protected $title = 'Changelog for %s';
26 protected $xcAttr = array('rss-from-svn');
28 public function process(DOMDocument $dom, $page, $manager) {
30 $xc = XHTMLCompiler::getInstance();
31 $viewvc_url = $xc->getConf('viewvc_url');
33 // see if we need to make the link ourself
34 $trigger = $this->confiscateAttr($dom->documentElement, $this->ns, 'rss-from-svn');
35 if ($trigger == 'yes') {
36 // alright, set it up ourselves
37 $link = $dom->createElement('link');
38 $link->setAttribute('rel', 'alternate');
39 $link->setAttribute('type', 'application/rss+xml');
40 $link->setAttribute('title', str_replace('%s', $page->getCachePath(), $this->title));
41 $link->setAttribute('href', $page->getPathStem() . '.rss');
42 $head = $this->query('//html:head')->item(0);
43 $head->appendChild($link);
44 } else {
45 // grab the document's links to RSS feeds
46 // link must be marked with xc:rss-from-svn
47 // only one allowed
48 $link = $this->query('//html:link[@xc:rss-from-svn]')->item(0);
49 if (!$link) return; // nothing to do
51 if (!function_exists('svn_log')) {
52 throw new Exception('RSSFromSVN requires the svn extension');
55 $trigger = $this->confiscateAttr($link, $this->ns, 'rss-from-svn');
56 if ($trigger != 'yes') return;
59 $path = $page->getWebPath();
61 $rss = new XHTMLCompiler_RSSFeed(
62 $link->getAttribute('title'),
63 $path,
64 str_replace('%s', $page->getCachePath(), $this->description),
65 $dom->documentElement->getAttribute('xml:lang')
68 // must be accessible by this script, but not necessarily the
69 // general public
70 $url = $page->getSVNHeadURLMunged();
72 if (!$url) {
73 // file is not versioned, fail silently: it might work later
74 return;
77 $logs = svn_log_limit($url, $this->limit, $page);
79 $page_link = $page->getWebPath();
81 date_default_timezone_set('UTC');
82 foreach ($logs as $log) {
83 // link to ViewVC snapshot if possible
84 if ($viewvc_url) {
85 $item_link = $viewvc_url . '/' . $page->getSourcePath() .
86 '?r1=' . ($log['rev'] - 1) . '&r2=' . $log['rev'];
87 } else {
88 $item_link = $page_link . '#svn_r' . $log['rev'];
90 // generate short message (first line) for title
91 $lines = explode("\n", wordwrap($log['msg'], 40));
92 $short_msg = $lines[0];
94 $rss->addItem(
95 $item_link,
96 'Revision ' . $log['rev'] . ': ' . htmlspecialchars($short_msg),
97 $d = gmdate('r', strtotime($log['date'])),
98 htmlspecialchars($log['msg'])
102 $rss->save(
103 $page->normalizePath(
104 $link->getAttribute('href')