6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author Andreas Gohr <andi@splitbrain.org>
10 * @global Input $INPUT
13 use dokuwiki\Cache\Cache
;
14 use dokuwiki\ChangeLog\MediaChangeLog
;
15 use dokuwiki\ChangeLog\PageChangeLog
;
16 use dokuwiki\Extension\AuthPlugin
;
17 use dokuwiki\Extension\Event
;
19 if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__
. '/');
20 require_once(DOKU_INC
. 'inc/init.php');
23 session_write_close();
26 if (!actionOK('rss')) {
28 echo '<error>RSS feed is disabled.</error>';
33 $opt = rss_parseOptions();
35 // the feed is dynamic - we need a cache for each combo
36 // (but most people just use the default feed so it's still effective)
37 $key = implode('', array_values($opt)) . '$' . $INPUT->server
->str('REMOTE_USER')
38 . '$' . $INPUT->server
->str('HTTP_HOST') . $INPUT->server
->str('SERVER_PORT');
39 $cache = new Cache($key, '.feed');
41 // prepare cache depends
42 $depends['files'] = getConfigFiles('main');
43 $depends['age'] = $conf['rss_update'];
44 $depends['purge'] = $INPUT->bool('purge');
46 // check cacheage and deliver if nothing has changed since last
47 // time or the update interval has not passed, also handles conditional requests
48 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
49 header('Pragma: public');
50 header('Content-Type: application/xml; charset=utf-8');
51 header('X-Robots-Tag: noindex');
52 if ($cache->useCache($depends)) {
53 http_conditionalRequest($cache->getTime());
54 if ($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
55 print $cache->retrieveCache();
58 http_conditionalRequest(time());
62 $rss = new UniversalFeedCreator();
63 $rss->title
= $conf['title'] . (($opt['namespace']) ?
' ' . $opt['namespace'] : '');
64 $rss->link
= DOKU_URL
;
65 $rss->syndicationURL
= DOKU_URL
. 'feed.php';
66 $rss->cssStyleSheet
= DOKU_URL
. 'lib/exe/css.php?s=feed';
68 $image = new FeedImage();
69 $image->title
= $conf['title'];
70 $image->url
= tpl_getMediaFile([':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico'], true);
71 $image->link
= DOKU_URL
;
76 'list' => 'rssListNamespace',
77 'search' => 'rssSearch',
78 'recent' => 'rssRecentChanges'
81 if (isset($modes[$opt['feed_mode']])) {
82 $data = $modes[$opt['feed_mode']]($opt);
88 $event = new Event('FEED_MODE_UNKNOWN', $eventData);
89 if ($event->advise_before(true)) {
90 echo sprintf('<error>Unknown feed mode %s</error>', hsc($opt['feed_mode']));
93 $event->advise_after();
96 rss_buildItems($rss, $data, $opt);
97 $feed = $rss->createFeed($opt['feed_type']);
100 $cache->storeCache($feed);
105 // ---------------------------------------------------------------- //
108 * Get URL parameters and config options and return an initialized option array
110 * @author Andreas Gohr <andi@splitbrain.org>
112 function rss_parseOptions()
121 // Basic feed properties
122 // Plugins may probably want to add new values to these
123 // properties for implementing own feeds
125 // One of: list, search, recent
126 'feed_mode' => ['str', 'mode', 'recent'],
127 // One of: diff, page, rev, current
128 'link_to' => ['str', 'linkto', $conf['rss_linkto']],
129 // One of: abstract, diff, htmldiff, html
130 'item_content' => ['str', 'content', $conf['rss_content']],
132 // Special feed properties
133 // These are only used by certain feed_modes
135 // String, used for feed title, in list and rc mode
136 'namespace' => ['str', 'ns', null],
137 // Positive integer, only used in rc mode
138 'items' => ['int', 'num', $conf['recent']],
139 // Boolean, only used in rc mode
140 'show_minor' => ['bool', 'minor', false],
141 // Boolean, only used in rc mode
142 'only_new' => ['bool', 'onlynewpages', false],
143 // String, only used in list mode
144 'sort' => ['str', 'sort', 'natural'],
145 // String, only used in search mode
146 'search_query' => ['str', 'q', null],
147 // One of: pages, media, both
148 'content_type' => ['str', 'view', $conf['rss_media']]
152 $opt[$name] = $INPUT->{$val[0]}($val[1], $val[2], true);
155 $opt['items'] = max(0, (int) $opt['items']);
156 $opt['show_minor'] = (bool) $opt['show_minor'];
157 $opt['only_new'] = (bool) $opt['only_new'];
158 $opt['sort'] = valid_input_set('sort', ['default' => 'natural', 'date'], $opt);
160 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
162 $type = $INPUT->valid(
164 ['rss', 'rss2', 'atom', 'atom1', 'rss1'],
169 $opt['feed_type'] = 'RSS0.91';
170 $opt['mime_type'] = 'text/xml';
173 $opt['feed_type'] = 'RSS2.0';
174 $opt['mime_type'] = 'text/xml';
177 $opt['feed_type'] = 'ATOM0.3';
178 $opt['mime_type'] = 'application/xml';
181 $opt['feed_type'] = 'ATOM1.0';
182 $opt['mime_type'] = 'application/atom+xml';
185 $opt['feed_type'] = 'RSS1.0';
186 $opt['mime_type'] = 'application/xml';
192 Event
::createAndTrigger('FEED_OPTS_POSTPROCESS', $eventData);
197 * Add recent changed pages to a feed object
199 * @param FeedCreator $rss the FeedCreator Object
200 * @param array $data the items to add
201 * @param array $opt the feed options
202 * @author Andreas Gohr <andi@splitbrain.org>
204 function rss_buildItems(&$rss, &$data, $opt)
208 /* @var AuthPlugin $auth */
216 $event = new Event('FEED_DATA_PROCESS', $eventData);
217 if ($event->advise_before(false)) {
218 foreach ($data as $ditem) {
219 if (!is_array($ditem)) {
220 // not an array? then only a list of IDs was given
221 $ditem = ['id' => $ditem];
224 $item = new FeedItem();
226 if (empty($ditem['media'])) {
227 $meta = p_get_metadata($id);
233 if (isset($ditem['date'])) {
234 $date = $ditem['date'];
235 } elseif ($ditem['media']) {
236 $date = @filemtime
(mediaFN($id));
237 } elseif (file_exists(wikiFN($id))) {
238 $date = @filemtime
(wikiFN($id));
239 } elseif ($meta['date']['modified']) {
240 $date = $meta['date']['modified'];
244 if ($date) $item->date
= date('r', $date);
247 if ($conf['useheading'] && $meta['title'] ??
'') {
248 $item->title
= $meta['title'];
250 $item->title
= $ditem['id'];
252 if ($conf['rss_show_summary'] && !empty($ditem['sum'])) {
253 $item->title
.= ' - ' . strip_tags($ditem['sum']);
257 switch ($opt['link_to']) {
259 if (isset($ditem['media'])) {
260 $item->link
= media_managerURL(
270 $item->link
= wl($id, 'rev=' . $date, true, '&');
274 if ($ditem['media']) {
275 $item->link
= media_managerURL(
280 'tab_details' => 'history'
286 $item->link
= wl($id, 'do=revisions&rev=' . $date, true, '&');
290 if ($ditem['media']) {
291 $item->link
= media_managerURL(
300 $item->link
= wl($id, '', true, '&');
305 if ($ditem['media']) {
306 $item->link
= media_managerURL(
311 'tab_details' => 'history',
318 $item->link
= wl($id, 'rev=' . $date . '&do=diff', true, '&');
323 switch ($opt['item_content']) {
326 if ($ditem['media']) {
327 $medialog = new MediaChangeLog($id);
328 $revs = $medialog->getRevisions(0, 1);
333 if ($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)), 300)) {
334 $more = 'w=' . $size[0] . '&h=' . $size[1] . '&t=' . @filemtime
(mediaFN($id));
335 $src_r = ml($id, $more, true, '&', true);
337 if ($rev && $size = media_image_preview_size($id, $rev, new JpegMeta(mediaFN($id, $rev)),
339 $more = 'rev=' . $rev . '&w=' . $size[0] . '&h=' . $size[1];
340 $src_l = ml($id, $more, true, '&', true);
344 $content = '<table>';
345 $content .= '<tr><th width="50%">' . $rev . '</th>';
346 $content .= '<th width="50%">' . $lang['current'] . '</th></tr>';
347 $content .= '<tr align="center"><td><img src="' . $src_l . '" alt="" /></td><td>';
348 $content .= '<img src="' . $src_r . '" alt="' . $id . '" /></td></tr>';
349 $content .= '</table>';
352 require_once(DOKU_INC
. 'inc/DifferenceEngine.php');
353 $pagelog = new PageChangeLog($id);
354 $revs = $pagelog->getRevisions(0, 1);
359 explode("\n", rawWiki($id, $rev)),
360 explode("\n", rawWiki($id, ''))
365 explode("\n", rawWiki($id, ''))
369 if ($opt['item_content'] == 'htmldiff') {
370 // note: no need to escape diff output, TableDiffFormatter provides 'safe' html
371 $tdf = new TableDiffFormatter();
372 $content = '<table>';
373 $content .= '<tr><th colspan="2" width="50%">' . $rev . '</th>';
374 $content .= '<th colspan="2" width="50%">' . $lang['current'] . '</th></tr>';
375 $content .= $tdf->format($df);
376 $content .= '</table>';
378 // note: diff output must be escaped, UnifiedDiffFormatter provides plain text
379 $udf = new UnifiedDiffFormatter();
380 $content = "<pre>\n" . hsc($udf->format($df)) . "\n</pre>";
385 if ($ditem['media']) {
386 if ($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) {
387 $more = 'w=' . $size[0] . '&h=' . $size[1] . '&t=' . @filemtime
(mediaFN($id));
388 $src = ml($id, $more, true, '&', true);
389 $content = '<img src="' . $src . '" alt="' . $id . '" />';
394 if (@filemtime
(wikiFN($id)) === $date) {
395 $content = p_wiki_xhtml($id, '', false);
397 $content = p_wiki_xhtml($id, $date, false);
400 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s', '', $content);
402 // add alignment for images
403 $content = preg_replace('/(<img .*?class="medialeft")/s', '\\1 align="left"', $content);
404 $content = preg_replace('/(<img .*?class="mediaright")/s', '\\1 align="right"', $content);
406 // make URLs work when canonical is not set, regexp instead of rerendering!
407 if (!$conf['canonical']) {
408 $base = preg_quote(DOKU_REL
, '/');
409 $content = preg_replace(
410 '/(<a href|<img src)="(' . $base . ')/s', '$1="' . DOKU_URL
,
419 if (isset($ditem['media'])) {
420 if ($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) {
421 $more = 'w=' . $size[0] . '&h=' . $size[1] . '&t=' . @filemtime
(mediaFN($id));
422 $src = ml($id, $more, true, '&', true);
423 $content = '<img src="' . $src . '" alt="' . $id . '" />';
428 $content = $meta['description']['abstract'];
431 $item->description
= $content; //FIXME a plugin hook here could be senseful
434 # FIXME should the user be pulled from metadata as well?
435 $user = @$ditem['user']; // the @ spares time repeating lookup
437 $item->author
= 'Anonymous';
438 $item->authorEmail
= 'anonymous@undisclosed.example.com';
440 $item->author
= $user;
441 $item->authorEmail
= $user . '@undisclosed.example.com';
443 // get real user name if configured
444 if ($conf['useacl'] && $auth) {
445 $userInfo = $auth->getUserData($user);
447 switch ($conf['showuseras']) {
449 case 'username_link':
450 $item->author
= $userInfo['name'];
453 $item->author
= $user;
457 $item->author
= $user;
463 if (isset($meta['subject'])) {
464 $item->category
= $meta['subject'];
467 if ($cat) $item->category
= $cat;
470 // finally add the item to the feed object, after handing it to registered plugins
477 $evt = new Event('FEED_ITEM_ADD', $evdata);
478 if ($evt->advise_before()) {
479 $rss->addItem($item);
481 $evt->advise_after(); // for completeness
484 $event->advise_after();
488 * Add recent changed pages to the feed object
490 * @author Andreas Gohr <andi@splitbrain.org>
492 function rssRecentChanges($opt)
496 if (!$conf['rss_show_deleted']) $flags +
= RECENTS_SKIP_DELETED
;
497 if (!$opt['show_minor']) $flags +
= RECENTS_SKIP_MINORS
;
498 if ($opt['only_new']) $flags +
= RECENTS_ONLY_CREATION
;
499 if ($opt['content_type'] == 'media' && $conf['mediarevisions']) $flags +
= RECENTS_MEDIA_CHANGES
;
500 if ($opt['content_type'] == 'both' && $conf['mediarevisions']) $flags +
= RECENTS_MEDIA_PAGES_MIXED
;
502 $recents = getRecents(0, $opt['items'], $opt['namespace'], $flags);
507 * Add all pages of a namespace to the feed object
509 * @author Andreas Gohr <andi@splitbrain.org>
511 function rssListNamespace($opt)
513 require_once(DOKU_INC
. 'inc/search.php');
516 $ns = ':' . cleanID($opt['namespace']);
517 $ns = utf8_encodeFN(str_replace(':', '/', $ns));
525 search($data, $conf['datadir'], 'search_universal', $search_opts, $ns, $lvl = 1, $opt['sort']);
531 * Add the result of a full text search to the feed object
533 * @author Andreas Gohr <andi@splitbrain.org>
535 function rssSearch($opt)
537 if (!$opt['search_query'] ||
!actionOK('search')) return [];
539 require_once(DOKU_INC
. 'inc/fulltext.php');
540 $data = ft_pageSearch($opt['search_query'], $poswords);
541 $data = array_keys($data);
546 //Setup VIM: ex: et ts=4 :