5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
12 use dokuwiki\Cache\Cache
;
13 use dokuwiki\ChangeLog\MediaChangeLog
;
14 use dokuwiki\ChangeLog\PageChangeLog
;
16 if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__
).'/');
17 require_once(DOKU_INC
.'inc/init.php');
20 session_write_close();
23 if(!actionOK('rss')) {
25 echo '<error>RSS feed is disabled.</error>';
30 $opt = rss_parseOptions();
32 // the feed is dynamic - we need a cache for each combo
33 // (but most people just use the default feed so it's still effective)
34 $key = join('', array_values($opt)).'$'.$_SERVER['REMOTE_USER'].'$'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'];
35 $cache = new Cache($key, '.feed');
37 // prepare cache depends
38 $depends['files'] = getConfigFiles('main');
39 $depends['age'] = $conf['rss_update'];
40 $depends['purge'] = $INPUT->bool('purge');
42 // check cacheage and deliver if nothing has changed since last
43 // time or the update interval has not passed, also handles conditional requests
44 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
45 header('Pragma: public');
46 header('Content-Type: application/xml; charset=utf-8');
47 header('X-Robots-Tag: noindex');
48 if($cache->useCache($depends)) {
49 http_conditionalRequest($cache->getTime());
50 if($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
51 print $cache->retrieveCache();
54 http_conditionalRequest(time());
58 $rss = new UniversalFeedCreator();
59 $rss->title
= $conf['title'].(($opt['namespace']) ?
' '.$opt['namespace'] : '');
60 $rss->link
= DOKU_URL
;
61 $rss->syndicationURL
= DOKU_URL
.'feed.php';
62 $rss->cssStyleSheet
= DOKU_URL
.'lib/exe/css.php?s=feed';
64 $image = new FeedImage();
65 $image->title
= $conf['title'];
66 $image->url
= tpl_getMediaFile(array(':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico'), true);
67 $image->link
= DOKU_URL
;
72 'list' => 'rssListNamespace',
73 'search' => 'rssSearch',
74 'recent' => 'rssRecentChanges'
76 if(isset($modes[$opt['feed_mode']])) {
77 $data = $modes[$opt['feed_mode']]($opt);
83 $event = new Doku_Event('FEED_MODE_UNKNOWN', $eventData);
84 if($event->advise_before(true)) {
85 echo sprintf('<error>Unknown feed mode %s</error>', hsc($opt['feed_mode']));
88 $event->advise_after();
91 rss_buildItems($rss, $data, $opt);
92 $feed = $rss->createFeed($opt['feed_type']);
95 $cache->storeCache($feed);
100 // ---------------------------------------------------------------- //
103 * Get URL parameters and config options and return an initialized option array
105 * @author Andreas Gohr <andi@splitbrain.org>
107 function rss_parseOptions() {
114 // Basic feed properties
115 // Plugins may probably want to add new values to these
116 // properties for implementing own feeds
118 // One of: list, search, recent
119 'feed_mode' => array('str', 'mode', 'recent'),
120 // One of: diff, page, rev, current
121 'link_to' => array('str', 'linkto', $conf['rss_linkto']),
122 // One of: abstract, diff, htmldiff, html
123 'item_content' => array('str', 'content', $conf['rss_content']),
125 // Special feed properties
126 // These are only used by certain feed_modes
128 // String, used for feed title, in list and rc mode
129 'namespace' => array('str', 'ns', null),
130 // Positive integer, only used in rc mode
131 'items' => array('int', 'num', $conf['recent']),
132 // Boolean, only used in rc mode
133 'show_minor' => array('bool', 'minor', false),
134 // String, only used in list mode
135 'sort' => array('str', 'sort', 'natural'),
136 // String, only used in search mode
137 'search_query' => array('str', 'q', null),
138 // One of: pages, media, both
139 'content_type' => array('str', 'view', $conf['rss_media'])
141 ) as $name => $val) {
142 $opt[$name] = $INPUT->{$val[0]}($val[1], $val[2], true);
145 $opt['items'] = max(0, (int) $opt['items']);
146 $opt['show_minor'] = (bool) $opt['show_minor'];
147 $opt['sort'] = valid_input_set('sort', array('default' => 'natural', 'date'), $opt);
149 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
151 $type = $INPUT->valid(
153 array( 'rss', 'rss2', 'atom', 'atom1', 'rss1'),
158 $opt['feed_type'] = 'RSS0.91';
159 $opt['mime_type'] = 'text/xml';
162 $opt['feed_type'] = 'RSS2.0';
163 $opt['mime_type'] = 'text/xml';
166 $opt['feed_type'] = 'ATOM0.3';
167 $opt['mime_type'] = 'application/xml';
170 $opt['feed_type'] = 'ATOM1.0';
171 $opt['mime_type'] = 'application/atom+xml';
174 $opt['feed_type'] = 'RSS1.0';
175 $opt['mime_type'] = 'application/xml';
181 trigger_event('FEED_OPTS_POSTPROCESS', $eventData);
186 * Add recent changed pages to a feed object
188 * @author Andreas Gohr <andi@splitbrain.org>
189 * @param FeedCreator $rss the FeedCreator Object
190 * @param array $data the items to add
191 * @param array $opt the feed options
193 function rss_buildItems(&$rss, &$data, $opt) {
196 /* @var DokuWiki_Auth_Plugin $auth */
204 $event = new Doku_Event('FEED_DATA_PROCESS', $eventData);
205 if($event->advise_before(false)) {
206 foreach($data as $ditem) {
207 if(!is_array($ditem)) {
208 // not an array? then only a list of IDs was given
209 $ditem = array('id' => $ditem);
212 $item = new FeedItem();
214 if(!$ditem['media']) {
215 $meta = p_get_metadata($id);
222 $date = $ditem['date'];
223 } elseif ($ditem['media']) {
224 $date = @filemtime
(mediaFN($id));
225 } elseif (file_exists(wikiFN($id))) {
226 $date = @filemtime
(wikiFN($id));
227 } elseif($meta['date']['modified']) {
228 $date = $meta['date']['modified'];
232 if($date) $item->date
= date('r', $date);
235 if($conf['useheading'] && $meta['title']) {
236 $item->title
= $meta['title'];
238 $item->title
= $ditem['id'];
240 if($conf['rss_show_summary'] && !empty($ditem['sum'])) {
241 $item->title
.= ' - '.strip_tags($ditem['sum']);
245 switch($opt['link_to']) {
247 if($ditem['media']) {
248 $item->link
= media_managerURL(
256 $item->link
= wl($id, 'rev='.$date, true, '&');
260 if($ditem['media']) {
261 $item->link
= media_managerURL(
266 'tab_details' => 'history'
270 $item->link
= wl($id, 'do=revisions&rev='.$date, true, '&');
274 if($ditem['media']) {
275 $item->link
= media_managerURL(
282 $item->link
= wl($id, '', true, '&');
287 if($ditem['media']) {
288 $item->link
= media_managerURL(
293 'tab_details' => 'history',
298 $item->link
= wl($id, 'rev='.$date.'&do=diff', true, '&');
303 switch($opt['item_content']) {
306 if($ditem['media']) {
307 $medialog = new MediaChangeLog($id);
308 $revs = $medialog->getRevisions(0, 1);
313 if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)), 300)) {
314 $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime
(mediaFN($id));
315 $src_r = ml($id, $more, true, '&', true);
317 if($rev && $size = media_image_preview_size($id, $rev, new JpegMeta(mediaFN($id, $rev)), 300)) {
318 $more = 'rev='.$rev.'&w='.$size[0].'&h='.$size[1];
319 $src_l = ml($id, $more, true, '&', true);
323 $content = '<table>';
324 $content .= '<tr><th width="50%">'.$rev.'</th>';
325 $content .= '<th width="50%">'.$lang['current'].'</th></tr>';
326 $content .= '<tr align="center"><td><img src="'.$src_l.'" alt="" /></td><td>';
327 $content .= '<img src="'.$src_r.'" alt="'.$id.'" /></td></tr>';
328 $content .= '</table>';
332 require_once(DOKU_INC
.'inc/DifferenceEngine.php');
333 $pagelog = new PageChangeLog($id);
334 $revs = $pagelog->getRevisions(0, 1);
338 $df = new Diff(explode("\n", rawWiki($id, $rev)),
339 explode("\n", rawWiki($id, '')));
341 $df = new Diff(array(''),
342 explode("\n", rawWiki($id, '')));
345 if($opt['item_content'] == 'htmldiff') {
346 // note: no need to escape diff output, TableDiffFormatter provides 'safe' html
347 $tdf = new TableDiffFormatter();
348 $content = '<table>';
349 $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
350 $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
351 $content .= $tdf->format($df);
352 $content .= '</table>';
354 // note: diff output must be escaped, UnifiedDiffFormatter provides plain text
355 $udf = new UnifiedDiffFormatter();
356 $content = "<pre>\n".hsc($udf->format($df))."\n</pre>";
361 if($ditem['media']) {
362 if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) {
363 $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime
(mediaFN($id));
364 $src = ml($id, $more, true, '&', true);
365 $content = '<img src="'.$src.'" alt="'.$id.'" />';
370 if (@filemtime
(wikiFN($id)) === $date) {
371 $content = p_wiki_xhtml($id, '', false);
373 $content = p_wiki_xhtml($id, $date, false);
376 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s', '', $content);
378 // add alignment for images
379 $content = preg_replace('/(<img .*?class="medialeft")/s', '\\1 align="left"', $content);
380 $content = preg_replace('/(<img .*?class="mediaright")/s', '\\1 align="right"', $content);
382 // make URLs work when canonical is not set, regexp instead of rerendering!
383 if(!$conf['canonical']) {
384 $base = preg_quote(DOKU_REL
, '/');
385 $content = preg_replace('/(<a href|<img src)="('.$base.')/s', '$1="'.DOKU_URL
, $content);
392 if($ditem['media']) {
393 if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) {
394 $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime
(mediaFN($id));
395 $src = ml($id, $more, true, '&', true);
396 $content = '<img src="'.$src.'" alt="'.$id.'" />';
401 $content = $meta['description']['abstract'];
404 $item->description
= $content; //FIXME a plugin hook here could be senseful
407 # FIXME should the user be pulled from metadata as well?
408 $user = @$ditem['user']; // the @ spares time repeating lookup
410 $item->author
= 'Anonymous';
411 $item->authorEmail
= 'anonymous@undisclosed.example.com';
413 $item->author
= $user;
414 $item->authorEmail
= $user . '@undisclosed.example.com';
416 // get real user name if configured
417 if($conf['useacl'] && $auth) {
418 $userInfo = $auth->getUserData($user);
420 switch($conf['showuseras']) {
422 case 'username_link':
423 $item->author
= $userInfo['name'];
426 $item->author
= $user;
430 $item->author
= $user;
436 if(isset($meta['subject'])) {
437 $item->category
= $meta['subject'];
440 if($cat) $item->category
= $cat;
443 // finally add the item to the feed object, after handing it to registered plugins
450 $evt = new Doku_Event('FEED_ITEM_ADD', $evdata);
451 if($evt->advise_before()) {
452 $rss->addItem($item);
454 $evt->advise_after(); // for completeness
457 $event->advise_after();
461 * Add recent changed pages to the feed object
463 * @author Andreas Gohr <andi@splitbrain.org>
465 function rssRecentChanges($opt) {
467 $flags = RECENTS_SKIP_DELETED
;
468 if(!$opt['show_minor']) $flags +
= RECENTS_SKIP_MINORS
;
469 if($opt['content_type'] == 'media' && $conf['mediarevisions']) $flags +
= RECENTS_MEDIA_CHANGES
;
470 if($opt['content_type'] == 'both' && $conf['mediarevisions']) $flags +
= RECENTS_MEDIA_PAGES_MIXED
;
472 $recents = getRecents(0, $opt['items'], $opt['namespace'], $flags);
477 * Add all pages of a namespace to the feed object
479 * @author Andreas Gohr <andi@splitbrain.org>
481 function rssListNamespace($opt) {
482 require_once(DOKU_INC
.'inc/search.php');
485 $ns = ':'.cleanID($opt['namespace']);
486 $ns = utf8_encodeFN(str_replace(':', '/', $ns));
489 $search_opts = array(
494 search($data, $conf['datadir'], 'search_universal', $search_opts, $ns, $lvl = 1, $opt['sort']);
500 * Add the result of a full text search to the feed object
502 * @author Andreas Gohr <andi@splitbrain.org>
504 function rssSearch($opt) {
505 if(!$opt['search_query']) return array();
507 require_once(DOKU_INC
.'inc/fulltext.php');
508 $data = ft_pageSearch($opt['search_query'], $poswords);
509 $data = array_keys($data);
514 //Setup VIM: ex: et ts=4 :