xref: /dokuwiki/feed.php (revision 867da04d85140736fab69e0df2be0abb55719386)
1<?php
2
3/**
4 * XML feed export
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Andreas Gohr <andi@splitbrain.org>
8 *
9 * @global array $conf
10 * @global Input $INPUT
11 */
12
13use dokuwiki\Feed\FeedCreator;
14use dokuwiki\Feed\FeedCreatorOptions;
15use dokuwiki\Cache\Cache;
16use dokuwiki\ChangeLog\MediaChangeLog;
17use dokuwiki\ChangeLog\PageChangeLog;
18use dokuwiki\Extension\AuthPlugin;
19use dokuwiki\Extension\Event;
20
21if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/');
22require_once(DOKU_INC . 'inc/init.php');
23
24//close session
25session_write_close();
26
27//feed disabled?
28if (!actionOK('rss')) {
29    http_status(404);
30    echo '<error>RSS feed is disabled.</error>';
31    exit;
32}
33
34$options = new FeedCreatorOptions();
35
36// we only cache the recent cache, but do so based on dynamic parameters
37// other modes are never cached and always created on the fly
38$cache = null;
39$cacheKey = $options->getCacheKey([
40    $INPUT->server->str('REMOTE_USER'),
41    $INPUT->server->str('HTTP_HOST'),
42    $INPUT->server->str('SERVER_PORT'),
43]);
44if ($cacheKey !== null) {
45    $cache = new Cache($cacheKey, '.feed');
46
47    // prepare cache depends
48    $depends['files'] = getConfigFiles('main');
49    $depends['age'] = $conf['rss_update'];
50    $depends['purge'] = $INPUT->bool('purge');
51}
52
53// check cacheage and deliver if nothing has changed since last
54// time or the update interval has not passed, also handles conditional requests
55header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
56header('Pragma: public');
57header('Content-Type: ' . $options->getMimeType());
58header('X-Robots-Tag: noindex');
59if ($cache?->useCache($depends)) {
60    http_conditionalRequest($cache->getTime());
61    if ($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
62    echo $cache->retrieveCache();
63    exit;
64} else {
65    http_conditionalRequest(time());
66}
67
68// create new feed
69try {
70    $feed = (new FeedCreator($options))->build();
71    $cache?->storeCache($feed);
72    echo $feed;
73} catch (Exception $e) {
74    http_status(500);
75    echo '<error>' . hsc($e->getMessage()) . '</error>';
76    exit;
77}
78