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
;
15 use dokuwiki\Extension\AuthPlugin
;
16 use dokuwiki\Extension\Event
;
18 if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__
).'/');
19 require_once(DOKU_INC
.'inc/init.php');
22 session_write_close();
25 if(!actionOK('rss')) {
27 echo '<error>RSS feed is disabled.</error>';
32 $opt = rss_parseOptions();
34 // the feed is dynamic - we need a cache for each combo
35 // (but most people just use the default feed so it's still effective)
36 $key = join('', array_values($opt)).'$'.$_SERVER['REMOTE_USER'].'$'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'];
37 $cache = new Cache($key, '.feed');
39 // prepare cache depends
40 $depends['files'] = getConfigFiles('main');
41 $depends['age'] = $conf['rss_update'];
42 $depends['purge'] = $INPUT->bool('purge');
44 // check cacheage and deliver if nothing has changed since last
45 // time or the update interval has not passed, also handles conditional requests
46 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
47 header('Pragma: public');
48 header('Content-Type: application/xml; charset=utf-8');
49 header('X-Robots-Tag: noindex');
50 if($cache->useCache($depends)) {
51 http_conditionalRequest($cache->getTime());
52 if($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
53 print $cache->retrieveCache();
56 http_conditionalRequest(time());
60 $rss = new UniversalFeedCreator();
61 $rss->title
= $conf['title'].(($opt['namespace']) ?
' '.$opt['namespace'] : '');
62 $rss->link
= DOKU_URL
;
63 $rss->syndicationURL
= DOKU_URL
.'feed.php';
64 $rss->cssStyleSheet
= DOKU_URL
.'lib/exe/css.php?s=feed';
66 $image = new FeedImage();
67 $image->title
= $conf['title'];
68 $image->url
= tpl_getMediaFile(array(':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico'), true);
69 $image->link
= DOKU_URL
;
74 'list' => 'rssListNamespace',
75 'search' => 'rssSearch',
76 'recent' => 'rssRecentChanges'
78 if(isset($modes[$opt['feed_mode']])) {
79 $data = $modes[$opt['feed_mode']]($opt);
85 $event = new Event('FEED_MODE_UNKNOWN', $eventData);
86 if($event->advise_before(true)) {
87 echo sprintf('<error>Unknown feed mode %s</error>', hsc($opt['feed_mode']));
90 $event->advise_after();
93 rss_buildItems($rss, $data, $opt);
94 $feed = $rss->createFeed($opt['feed_type']);
97 $cache->storeCache($feed);
102 // ---------------------------------------------------------------- //
105 * Get URL parameters and config options and return an initialized option array
107 * @author Andreas Gohr <andi@splitbrain.org>
109 function rss_parseOptions() {
116 // Basic feed properties
117 // Plugins may probably want to add new values to these
118 // properties for implementing own feeds
120 // One of: list, search, recent
121 'feed_mode' => array('str', 'mode', 'recent'),
122 // One of: diff, page, rev, current
123 'link_to' => array('str', 'linkto', $conf['rss_linkto']),
124 // One of: abstract, diff, htmldiff, html
125 'item_content' => array('str', 'content', $conf['rss_content']),
127 // Special feed properties
128 // These are only used by certain feed_modes
130 // String, used for feed title, in list and rc mode
131 'namespace' => array('str', 'ns', null),
132 // Positive integer, only used in rc mode
133 'items' => array('int', 'num', $conf['recent']),
134 // Boolean, only used in rc mode
135 'show_minor' => array('bool', 'minor', false),
136 // Boolean, only used in rc mode
137 'only_new' => array('bool', 'onlynewpages', false),
138 // String, only used in list mode
139 'sort' => array('str', 'sort', 'natural'),
140 // String, only used in search mode
141 'search_query' => array('str', 'q', null),
142 // One of: pages, media, both
143 'content_type' => array('str', 'view', $conf['rss_media'])
145 ) as $name => $val) {
146 $opt[$name] = $INPUT->{$val[0]}($val[1], $val[2], true);
149 $opt['items'] = max(0, (int) $opt['items']);
150 $opt['show_minor'] = (bool) $opt['show_minor'];
151 $opt['only_new'] = (bool) $opt['only_new'];
152 $opt['sort'] = valid_input_set('sort', array('default' => 'natural', 'date'), $opt);
154 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
156 $type = $INPUT->valid(
158 array( 'rss', 'rss2', 'atom', 'atom1', 'rss1'),
163 $opt['feed_type'] = 'RSS0.91';
164 $opt['mime_type'] = 'text/xml';
167 $opt['feed_type'] = 'RSS2.0';
168 $opt['mime_type'] = 'text/xml';
171 $opt['feed_type'] = 'ATOM0.3';
172 $opt['mime_type'] = 'application/xml';
175 $opt['feed_type'] = 'ATOM1.0';
176 $opt['mime_type'] = 'application/atom+xml';
179 $opt['feed_type'] = 'RSS1.0';
180 $opt['mime_type'] = 'application/xml';
186 Event
::createAndTrigger('FEED_OPTS_POSTPROCESS', $eventData);
191 * Add recent changed pages to a feed object
193 * @author Andreas Gohr <andi@splitbrain.org>
194 * @param FeedCreator $rss the FeedCreator Object
195 * @param array $data the items to add
196 * @param array $opt the feed options
198 function rss_buildItems(&$rss, &$data, $opt) {
201 /* @var AuthPlugin $auth */
209 $event = new Event('FEED_DATA_PROCESS', $eventData);
210 if($event->advise_before(false)) {
211 foreach($data as $ditem) {
212 if(!is_array($ditem)) {
213 // not an array? then only a list of IDs was given
214 $ditem = array('id' => $ditem);
217 $item = new FeedItem();
219 if(!$ditem['media']) {
220 $meta = p_get_metadata($id);
227 $date = $ditem['date'];
228 } elseif ($ditem['media']) {
229 $date = @filemtime
(mediaFN($id));
230 } elseif (file_exists(wikiFN($id))) {
231 $date = @filemtime
(wikiFN($id));
232 } elseif($meta['date']['modified']) {
233 $date = $meta['date']['modified'];
237 if($date) $item->date
= date('r', $date);
240 if($conf['useheading'] && $meta['title']) {
241 $item->title
= $meta['title'];
243 $item->title
= $ditem['id'];
245 if($conf['rss_show_summary'] && !empty($ditem['sum'])) {
246 $item->title
.= ' - '.strip_tags($ditem['sum']);
250 switch($opt['link_to']) {
252 if($ditem['media']) {
253 $item->link
= media_managerURL(
261 $item->link
= wl($id, 'rev='.$date, true, '&');
265 if($ditem['media']) {
266 $item->link
= media_managerURL(
271 'tab_details' => 'history'
275 $item->link
= wl($id, 'do=revisions&rev='.$date, true, '&');
279 if($ditem['media']) {
280 $item->link
= media_managerURL(
287 $item->link
= wl($id, '', true, '&');
292 if($ditem['media']) {
293 $item->link
= media_managerURL(
298 'tab_details' => 'history',
303 $item->link
= wl($id, 'rev='.$date.'&do=diff', true, '&');
308 switch($opt['item_content']) {
311 if($ditem['media']) {
312 $medialog = new MediaChangeLog($id);
313 $revs = $medialog->getRevisions(0, 1);
318 if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)), 300)) {
319 $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime
(mediaFN($id));
320 $src_r = ml($id, $more, true, '&', true);
322 if($rev && $size = media_image_preview_size($id, $rev, new JpegMeta(mediaFN($id, $rev)), 300)) {
323 $more = 'rev='.$rev.'&w='.$size[0].'&h='.$size[1];
324 $src_l = ml($id, $more, true, '&', true);
328 $content = '<table>';
329 $content .= '<tr><th width="50%">'.$rev.'</th>';
330 $content .= '<th width="50%">'.$lang['current'].'</th></tr>';
331 $content .= '<tr align="center"><td><img src="'.$src_l.'" alt="" /></td><td>';
332 $content .= '<img src="'.$src_r.'" alt="'.$id.'" /></td></tr>';
333 $content .= '</table>';
337 require_once(DOKU_INC
.'inc/DifferenceEngine.php');
338 $pagelog = new PageChangeLog($id);
339 $revs = $pagelog->getRevisions(0, 1);
343 $df = new Diff(explode("\n", rawWiki($id, $rev)),
344 explode("\n", rawWiki($id, '')));
346 $df = new Diff(array(''),
347 explode("\n", rawWiki($id, '')));
350 if($opt['item_content'] == 'htmldiff') {
351 // note: no need to escape diff output, TableDiffFormatter provides 'safe' html
352 $tdf = new TableDiffFormatter();
353 $content = '<table>';
354 $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
355 $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
356 $content .= $tdf->format($df);
357 $content .= '</table>';
359 // note: diff output must be escaped, UnifiedDiffFormatter provides plain text
360 $udf = new UnifiedDiffFormatter();
361 $content = "<pre>\n".hsc($udf->format($df))."\n</pre>";
366 if($ditem['media']) {
367 if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) {
368 $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime
(mediaFN($id));
369 $src = ml($id, $more, true, '&', true);
370 $content = '<img src="'.$src.'" alt="'.$id.'" />';
375 if (@filemtime
(wikiFN($id)) === $date) {
376 $content = p_wiki_xhtml($id, '', false);
378 $content = p_wiki_xhtml($id, $date, false);
381 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s', '', $content);
383 // add alignment for images
384 $content = preg_replace('/(<img .*?class="medialeft")/s', '\\1 align="left"', $content);
385 $content = preg_replace('/(<img .*?class="mediaright")/s', '\\1 align="right"', $content);
387 // make URLs work when canonical is not set, regexp instead of rerendering!
388 if(!$conf['canonical']) {
389 $base = preg_quote(DOKU_REL
, '/');
390 $content = preg_replace('/(<a href|<img src)="('.$base.')/s', '$1="'.DOKU_URL
, $content);
397 if($ditem['media']) {
398 if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) {
399 $more = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime
(mediaFN($id));
400 $src = ml($id, $more, true, '&', true);
401 $content = '<img src="'.$src.'" alt="'.$id.'" />';
406 $content = $meta['description']['abstract'];
409 $item->description
= $content; //FIXME a plugin hook here could be senseful
412 # FIXME should the user be pulled from metadata as well?
413 $user = @$ditem['user']; // the @ spares time repeating lookup
415 $item->author
= 'Anonymous';
416 $item->authorEmail
= 'anonymous@undisclosed.example.com';
418 $item->author
= $user;
419 $item->authorEmail
= $user . '@undisclosed.example.com';
421 // get real user name if configured
422 if($conf['useacl'] && $auth) {
423 $userInfo = $auth->getUserData($user);
425 switch($conf['showuseras']) {
427 case 'username_link':
428 $item->author
= $userInfo['name'];
431 $item->author
= $user;
435 $item->author
= $user;
441 if(isset($meta['subject'])) {
442 $item->category
= $meta['subject'];
445 if($cat) $item->category
= $cat;
448 // finally add the item to the feed object, after handing it to registered plugins
455 $evt = new Event('FEED_ITEM_ADD', $evdata);
456 if($evt->advise_before()) {
457 $rss->addItem($item);
459 $evt->advise_after(); // for completeness
462 $event->advise_after();
466 * Add recent changed pages to the feed object
468 * @author Andreas Gohr <andi@splitbrain.org>
470 function rssRecentChanges($opt) {
473 if(!$conf['rss_show_deleted']) $flags +
= RECENTS_SKIP_DELETED
;
474 if(!$opt['show_minor']) $flags +
= RECENTS_SKIP_MINORS
;
475 if($opt['only_new']) $flags +
= RECENTS_ONLY_CREATION
;
476 if($opt['content_type'] == 'media' && $conf['mediarevisions']) $flags +
= RECENTS_MEDIA_CHANGES
;
477 if($opt['content_type'] == 'both' && $conf['mediarevisions']) $flags +
= RECENTS_MEDIA_PAGES_MIXED
;
479 $recents = getRecents(0, $opt['items'], $opt['namespace'], $flags);
484 * Add all pages of a namespace to the feed object
486 * @author Andreas Gohr <andi@splitbrain.org>
488 function rssListNamespace($opt) {
489 require_once(DOKU_INC
.'inc/search.php');
492 $ns = ':'.cleanID($opt['namespace']);
493 $ns = utf8_encodeFN(str_replace(':', '/', $ns));
496 $search_opts = array(
501 search($data, $conf['datadir'], 'search_universal', $search_opts, $ns, $lvl = 1, $opt['sort']);
507 * Add the result of a full text search to the feed object
509 * @author Andreas Gohr <andi@splitbrain.org>
511 function rssSearch($opt) {
512 if(!$opt['search_query']) return array();
514 require_once(DOKU_INC
.'inc/fulltext.php');
515 $data = ft_pageSearch($opt['search_query'], $poswords);
516 $data = array_keys($data);
521 //Setup VIM: ex: et ts=4 :