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\Feed\FeedCreatorOptions
;
14 use dokuwiki\Cache\Cache
;
15 use dokuwiki\ChangeLog\MediaChangeLog
;
16 use dokuwiki\ChangeLog\PageChangeLog
;
17 use dokuwiki\Extension\AuthPlugin
;
18 use dokuwiki\Extension\Event
;
20 if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__
. '/');
21 require_once(DOKU_INC
. 'inc/init.php');
24 session_write_close();
27 if (!actionOK('rss')) {
29 echo '<error>RSS feed is disabled.</error>';
33 $options = new FeedCreatorOptions();
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)
38 $options->getCacheKey(),
39 $INPUT->server
->str('REMOTE_USER'),
40 $INPUT->server
->str('HTTP_HOST'),
41 $INPUT->server
->str('SERVER_PORT')
43 $cache = new Cache($key, '.feed');
45 // prepare cache depends
46 $depends['files'] = getConfigFiles('main');
47 $depends['age'] = $conf['rss_update'];
48 $depends['purge'] = $INPUT->bool('purge');
50 // check cacheage and deliver if nothing has changed since last
51 // time or the update interval has not passed, also handles conditional requests
52 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
53 header('Pragma: public');
54 header('Content-Type: ' . $options->get('mime_type'));
55 header('X-Robots-Tag: noindex');
56 if ($cache->useCache($depends)) {
57 http_conditionalRequest($cache->getTime());
58 if ($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
59 echo $cache->retrieveCache();
62 http_conditionalRequest(time());
67 $feed = (new \dokuwiki\Feed\
FeedCreator($options))->build();
68 $cache->storeCache($feed);
70 } catch (Exception
$e) {
72 echo '<error>' . hsc($e->getMessage()) . '</error>';
77 // ---------------------------------------------------------------- //
80 * Get URL parameters and config options and return an initialized option array
82 * @author Andreas Gohr <andi@splitbrain.org>
84 function rss_parseOptions()
93 // Basic feed properties
94 // Plugins may probably want to add new values to these
95 // properties for implementing own feeds
97 // One of: list, search, recent
98 'feed_mode' => ['str', 'mode', 'recent'],
99 // One of: diff, page, rev, current
100 'link_to' => ['str', 'linkto', $conf['rss_linkto']],
101 // One of: abstract, diff, htmldiff, html
102 'item_content' => ['str', 'content', $conf['rss_content']],
104 // Special feed properties
105 // These are only used by certain feed_modes
107 // String, used for feed title, in list and rc mode
108 'namespace' => ['str', 'ns', null],
109 // Positive integer, only used in rc mode
110 'items' => ['int', 'num', $conf['recent']],
111 // Boolean, only used in rc mode
112 'show_minor' => ['bool', 'minor', false],
113 // Boolean, only used in rc mode
114 'only_new' => ['bool', 'onlynewpages', false],
115 // String, only used in list mode
116 'sort' => ['str', 'sort', 'natural'],
117 // String, only used in search mode
118 'search_query' => ['str', 'q', null],
119 // One of: pages, media, both
120 'content_type' => ['str', 'view', $conf['rss_media']]
124 $opt[$name] = $INPUT->{$val[0]}($val[1], $val[2], true);
127 $opt['items'] = max(0, (int)$opt['items']);
128 $opt['show_minor'] = (bool)$opt['show_minor'];
129 $opt['only_new'] = (bool)$opt['only_new'];
130 $opt['sort'] = valid_input_set('sort', ['default' => 'natural', 'date'], $opt);
132 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
134 $type = $INPUT->valid(
136 ['rss', 'rss2', 'atom', 'atom1', 'rss1'],
141 $opt['feed_type'] = 'RSS0.91';
142 $opt['mime_type'] = 'text/xml';
145 $opt['feed_type'] = 'RSS2.0';
146 $opt['mime_type'] = 'text/xml';
149 $opt['feed_type'] = 'ATOM0.3';
150 $opt['mime_type'] = 'application/xml';
153 $opt['feed_type'] = 'ATOM1.0';
154 $opt['mime_type'] = 'application/atom+xml';
157 $opt['feed_type'] = 'RSS1.0';
158 $opt['mime_type'] = 'application/xml';
164 Event
::createAndTrigger('FEED_OPTS_POSTPROCESS', $eventData);
169 * Add recent changed pages to a feed object
171 * @param FeedCreator $rss the FeedCreator Object
172 * @param array $data the items to add
173 * @param array $opt the feed options
174 * @author Andreas Gohr <andi@splitbrain.org>
176 function rss_buildItems(&$rss, &$data, $opt)
180 /* @var AuthPlugin $auth */
188 $event = new Event('FEED_DATA_PROCESS', $eventData);
189 if ($event->advise_before(false)) {
190 foreach ($data as $ditem) {
191 if (!is_array($ditem)) {
192 // not an array? then only a list of IDs was given
193 $ditem = ['id' => $ditem];
196 $item = new FeedItem();
198 if (empty($ditem['media'])) {
199 $meta = p_get_metadata($id);
205 if (isset($ditem['date'])) {
206 $date = $ditem['date'];
207 } elseif ($ditem['media']) {
208 $date = @filemtime
(mediaFN($id));
209 } elseif (file_exists(wikiFN($id))) {
210 $date = @filemtime
(wikiFN($id));
211 } elseif ($meta['date']['modified']) {
212 $date = $meta['date']['modified'];
216 if ($date) $item->date
= date('r', $date);
219 if ($conf['useheading'] && $meta['title'] ??
'') {
220 $item->title
= $meta['title'];
222 $item->title
= $ditem['id'];
224 if ($conf['rss_show_summary'] && !empty($ditem['sum'])) {
225 $item->title
.= ' - ' . strip_tags($ditem['sum']);
229 switch ($opt['link_to']) {
231 if (isset($ditem['media'])) {
232 $item->link
= media_managerURL(
242 $item->link
= wl($id, 'rev=' . $date, true, '&');
246 if ($ditem['media']) {
247 $item->link
= media_managerURL(
252 'tab_details' => 'history'
258 $item->link
= wl($id, 'do=revisions&rev=' . $date, true, '&');
262 if ($ditem['media']) {
263 $item->link
= media_managerURL(
272 $item->link
= wl($id, '', true, '&');
277 if ($ditem['media']) {
278 $item->link
= media_managerURL(
283 'tab_details' => 'history',
290 $item->link
= wl($id, 'rev=' . $date . '&do=diff', true, '&');
295 switch ($opt['item_content']) {
298 if ($ditem['media']) {
299 $medialog = new MediaChangeLog($id);
300 $revs = $medialog->getRevisions(0, 1);
305 if ($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)), 300)) {
306 $more = 'w=' . $size[0] . '&h=' . $size[1] . '&t=' . @filemtime
(mediaFN($id));
307 $src_r = ml($id, $more, true, '&', true);
310 $rev && $size = media_image_preview_size(
313 new JpegMeta(mediaFN($id, $rev)),
317 $more = 'rev=' . $rev . '&w=' . $size[0] . '&h=' . $size[1];
318 $src_l = ml($id, $more, true, '&', true);
322 $content = '<table>';
323 $content .= '<tr><th width="50%">' . $rev . '</th>';
324 $content .= '<th width="50%">' . $lang['current'] . '</th></tr>';
325 $content .= '<tr align="center"><td><img src="' . $src_l . '" alt="" /></td><td>';
326 $content .= '<img src="' . $src_r . '" alt="' . $id . '" /></td></tr>';
327 $content .= '</table>';
330 require_once(DOKU_INC
. 'inc/DifferenceEngine.php');
331 $pagelog = new PageChangeLog($id);
332 $revs = $pagelog->getRevisions(0, 1);
337 explode("\n", rawWiki($id, $rev)),
338 explode("\n", rawWiki($id, ''))
343 explode("\n", rawWiki($id, ''))
347 if ($opt['item_content'] == 'htmldiff') {
348 // note: no need to escape diff output, TableDiffFormatter provides 'safe' html
349 $tdf = new TableDiffFormatter();
350 $content = '<table>';
351 $content .= '<tr><th colspan="2" width="50%">' . $rev . '</th>';
352 $content .= '<th colspan="2" width="50%">' . $lang['current'] . '</th></tr>';
353 $content .= $tdf->format($df);
354 $content .= '</table>';
356 // note: diff output must be escaped, UnifiedDiffFormatter provides plain text
357 $udf = new UnifiedDiffFormatter();
358 $content = "<pre>\n" . hsc($udf->format($df)) . "\n</pre>";
363 if ($ditem['media']) {
364 if ($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) {
365 $more = 'w=' . $size[0] . '&h=' . $size[1] . '&t=' . @filemtime
(mediaFN($id));
366 $src = ml($id, $more, true, '&', true);
367 $content = '<img src="' . $src . '" alt="' . $id . '" />';
372 if (@filemtime
(wikiFN($id)) === $date) {
373 $content = p_wiki_xhtml($id, '', false);
375 $content = p_wiki_xhtml($id, $date, false);
378 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s', '', $content);
380 // add alignment for images
381 $content = preg_replace('/(<img .*?class="medialeft")/s', '\\1 align="left"', $content);
382 $content = preg_replace('/(<img .*?class="mediaright")/s', '\\1 align="right"', $content);
384 // make URLs work when canonical is not set, regexp instead of rerendering!
385 if (!$conf['canonical']) {
386 $base = preg_quote(DOKU_REL
, '/');
387 $content = preg_replace(
388 '/(<a href|<img src)="(' . $base . ')/s',
398 if (isset($ditem['media'])) {
399 if ($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) {
400 $more = 'w=' . $size[0] . '&h=' . $size[1] . '&t=' . @filemtime
(mediaFN($id));
401 $src = ml($id, $more, true, '&', true);
402 $content = '<img src="' . $src . '" alt="' . $id . '" />';
407 $content = $meta['description']['abstract'];
410 $item->description
= $content; //FIXME a plugin hook here could be senseful
413 # FIXME should the user be pulled from metadata as well?
414 $user = @$ditem['user']; // the @ spares time repeating lookup
416 $item->author
= 'Anonymous';
417 $item->authorEmail
= 'anonymous@undisclosed.example.com';
419 $item->author
= $user;
420 $item->authorEmail
= $user . '@undisclosed.example.com';
422 // get real user name if configured
423 if ($conf['useacl'] && $auth instanceof AuthPlugin
) {
424 $userInfo = $auth->getUserData($user);
426 switch ($conf['showuseras']) {
428 case 'username_link':
429 $item->author
= $userInfo['name'];
432 $item->author
= $user;
436 $item->author
= $user;
442 if (isset($meta['subject'])) {
443 $item->category
= $meta['subject'];
446 if ($cat) $item->category
= $cat;
449 // finally add the item to the feed object, after handing it to registered plugins
456 $evt = new Event('FEED_ITEM_ADD', $evdata);
457 if ($evt->advise_before()) {
458 $rss->addItem($item);
460 $evt->advise_after(); // for completeness
463 $event->advise_after();
467 * Add recent changed pages to the feed object
469 * @author Andreas Gohr <andi@splitbrain.org>
471 function rssRecentChanges($opt)
475 if (!$conf['rss_show_deleted']) $flags +
= RECENTS_SKIP_DELETED
;
476 if (!$opt['show_minor']) $flags +
= RECENTS_SKIP_MINORS
;
477 if ($opt['only_new']) $flags +
= RECENTS_ONLY_CREATION
;
478 if ($opt['content_type'] == 'media' && $conf['mediarevisions']) $flags +
= RECENTS_MEDIA_CHANGES
;
479 if ($opt['content_type'] == 'both' && $conf['mediarevisions']) $flags +
= RECENTS_MEDIA_PAGES_MIXED
;
481 $recents = getRecents(0, $opt['items'], $opt['namespace'], $flags);
486 * Add all pages of a namespace to the feed object
488 * @author Andreas Gohr <andi@splitbrain.org>
490 function rssListNamespace($opt)
492 require_once(DOKU_INC
. 'inc/search.php');
495 $ns = ':' . cleanID($opt['namespace']);
496 $ns = utf8_encodeFN(str_replace(':', '/', $ns));
504 search($data, $conf['datadir'], 'search_universal', $search_opts, $ns, $lvl = 1, $opt['sort']);
510 * Add the result of a full text search to the feed object
512 * @author Andreas Gohr <andi@splitbrain.org>
514 function rssSearch($opt)
516 if (!$opt['search_query'] ||
!actionOK('search')) return [];
518 require_once(DOKU_INC
. 'inc/fulltext.php');
519 $data = ft_pageSearch($opt['search_query'], $poswords);
520 $data = array_keys($data);
525 //Setup VIM: ex: et ts=4 :