1<?php
2/**
3 * XML feed export
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Böhler <dev@aboehler.at>
7 */
8
9if(!defined('DOKU_INC'))    define('DOKU_INC', realpath(dirname(__FILE__) . '/../../../') . '/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
11require_once(DOKU_INC . 'inc/init.php');
12require_once(DOKU_INC . 'inc/common.php');
13require_once(DOKU_INC . 'inc/events.php');
14require_once(DOKU_INC . 'inc/parserutils.php');
15require_once(DOKU_INC . 'inc/feedcreator.class.php');
16require_once(DOKU_INC . 'inc/auth.php');
17require_once(DOKU_INC . 'inc/pageutils.php');
18require_once(DOKU_INC . 'inc/httputils.php');
19
20//close session
21session_write_close();
22
23$id     = $_REQUEST['id'];
24
25$type = $conf['rss_type'];
26
27switch($type) {
28    case 'rss':
29        $type = 'RSS0.91';
30        $mime = 'text/xml';
31        break;
32    case 'rss2':
33        $type = 'RSS2.0';
34        $mime = 'text/xml';
35        break;
36    case 'atom':
37        $type = 'ATOM0.3';
38        $mime = 'application/xml';
39        break;
40    case 'atom1':
41        $type = 'ATOM1.0';
42        $mime = 'application/atom+xml';
43        break;
44    default:
45        $type = 'RSS1.0';
46        $mime = 'application/xml';
47}
48
49// the feed is dynamic - we need a cache for each combo
50// (but most people just use the default feed so it's still effective)
51$cache = getCacheName($id . $_SERVER['REMOTE_USER'], '.feed');
52$cmod  = @filemtime($cache); // 0 if not exists
53if($cmod && (@filemtime(DOKU_CONF . 'local.php') > $cmod
54        || @filemtime(DOKU_CONF . 'dokuwiki.php') > $cmod)
55) {
56    // ignore cache if feed prefs may have changed
57    $cmod = 0;
58}
59
60// check cacheage and deliver if nothing has changed since last
61// time or the update interval has not passed, also handles conditional requests
62header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
63header('Pragma: public');
64header('Content-Type: application/xml; charset=utf-8');
65
66if($cmod && (
67        ($cmod + $conf['rss_update'] > time())
68        || (
69            ($cmod > @filemtime($conf['changelog']))
70        )
71    )) {
72    http_conditionalRequest($cmod);
73    if($conf['allowdebug']) header("X-CacheUsed: $cache");
74    print io_readFile($cache);
75    exit;
76} else {
77    http_conditionalRequest(time());
78}
79
80// create new feed
81$rss = new DokuWikiFeedCreator();
82$rss->title = p_get_metadata($id, 'title', METADATA_DONT_RENDER);
83$rss->title .= ' · ' . $conf['title'];
84$rss->link = DOKU_URL;
85$rss->syndicationURL = DOKU_PLUGIN . 'csstimeline/feed.php';
86$rss->cssStyleSheet = DOKU_URL . 'lib/exe/css.php?s=feed';
87
88$author = p_get_metadata($id, 'creator', METADATA_DONT_RENDER);
89
90$image = new FeedImage();
91$image->title = $conf['title'];
92$image->url = DOKU_URL . "lib/images/favicon.ico";
93$image->link = DOKU_URL;
94$rss->image = $image;
95
96$page = rawWiki($id);// get pages here
97$po = &plugin_load('helper', 'csstimeline');
98preg_match('/'.str_replace('/', '\/', $po->specialPattern).'/si', $page, $matches);
99
100foreach($matches as $match)
101{
102    $data = $po->handleMatch($match);
103    foreach($data['entries'] as $entry)
104    {
105        $item = new FeedItem();
106        $item->title = htmlspecialchars_decode($entry['title']);
107        if($entry['link'])
108            $item->link = htmlspecialchars_decode($entry['link']);
109        else
110            $item->link = wl($id, '', true, '&');
111        $item->description = hsc(strip_tags(htmlspecialchars_decode($entry['description'])));
112        $item->date = date('r', strtotime($entry['date']));
113        $item->author = $author;
114        $rss->addItem($item);
115
116    }
117}
118
119$feed = $rss->createFeed($type, 'utf-8');
120
121// save cachefile
122io_saveFile($cache, $feed);
123
124// finally deliver
125print $feed;
126
127