5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
9 if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__
).'/');
10 require_once(DOKU_INC
.'inc/init.php');
13 session_write_close();
16 $opt = rss_parseOptions();
18 // the feed is dynamic - we need a cache for each combo
19 // (but most people just use the default feed so it's still effective)
20 $cache = getCacheName(join('',array_values($opt)).$_SERVER['REMOTE_USER'],'.feed');
21 $key = join('', array_values($opt)) . $_SERVER['REMOTE_USER'];
22 $cache = new cache($key, '.feed');
24 // prepare cache depends
25 $depends['files'] = getConfigFiles('main');
26 $depends['age'] = $conf['rss_update'];
27 $depends['purge'] = isset($_REQUEST['purge']);
29 // check cacheage and deliver if nothing has changed since last
30 // time or the update interval has not passed, also handles conditional requests
31 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
32 header('Pragma: public');
33 header('Content-Type: application/xml; charset=utf-8');
34 header('X-Robots-Tag: noindex');
35 if($cache->useCache($depends)) {
36 http_conditionalRequest($cache->_time
);
37 if($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
38 print $cache->retrieveCache();
41 http_conditionalRequest(time());
45 $rss = new DokuWikiFeedCreator();
46 $rss->title
= $conf['title'].(($opt['namespace']) ?
' '.$opt['namespace'] : '');
47 $rss->link
= DOKU_URL
;
48 $rss->syndicationURL
= DOKU_URL
.'feed.php';
49 $rss->cssStyleSheet
= DOKU_URL
.'lib/exe/css.php?s=feed';
51 $image = new FeedImage();
52 $image->title
= $conf['title'];
53 $image->url
= DOKU_URL
."lib/images/favicon.ico";
54 $image->link
= DOKU_URL
;
58 $modes = array('list' => 'rssListNamespace',
59 'search' => 'rssSearch',
60 'recent' => 'rssRecentChanges');
61 if (isset($modes[$opt['feed_mode']])) {
62 $data = $modes[$opt['feed_mode']]($opt);
68 $event = new Doku_Event('FEED_MODE_UNKNOWN', $eventData);
69 if ($event->advise_before(true)) {
70 echo sprintf('<error>Unknown feed mode %s</error>', hsc($opt['feed_mode']));
73 $event->advise_after();
76 rss_buildItems($rss, $data, $opt);
77 $feed = $rss->createFeed($opt['feed_type'],'utf-8');
80 $cache->storeCache($feed);
85 // ---------------------------------------------------------------- //
88 * Get URL parameters and config options and return an initialized option array
90 * @author Andreas Gohr <andi@splitbrain.org>
92 function rss_parseOptions(){
98 // Basic feed properties
99 // Plugins may probably want to add new values to these
100 // properties for implementing own feeds
102 // One of: list, search, recent
103 'feed_mode' => array('mode', 'recent'),
104 // One of: diff, page, rev, current
105 'link_to' => array('linkto', $conf['rss_linkto']),
106 // One of: abstract, diff, htmldiff, html
107 'item_content' => array('content', $conf['rss_content']),
109 // Special feed properties
110 // These are only used by certain feed_modes
112 // String, used for feed title, in list and rc mode
113 'namespace' => array('ns', null),
114 // Positive integer, only used in rc mode
115 'items' => array('num', $conf['recent']),
116 // Boolean, only used in rc mode
117 'show_minor' => array('minor', false),
118 // String, only used in search mode
119 'search_query' => array('q', null),
121 ) as $name => $val) {
122 $opt[$name] = (isset($_REQUEST[$val[0]]) && !empty($_REQUEST[$val[0]]))
123 ?
$_REQUEST[$val[0]] : $val[1];
126 $opt['items'] = max(0, (int) $opt['items']);
127 $opt['show_minor'] = (bool) $opt['show_minor'];
129 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
131 $type = valid_input_set('type', array('rss','rss2','atom','atom1','rss1',
132 'default' => $conf['rss_type']),
136 $opt['feed_type'] = 'RSS0.91';
137 $opt['mime_type'] = 'text/xml';
140 $opt['feed_type'] = 'RSS2.0';
141 $opt['mime_type'] = 'text/xml';
144 $opt['feed_type'] = 'ATOM0.3';
145 $opt['mime_type'] = 'application/xml';
148 $opt['feed_type'] = 'ATOM1.0';
149 $opt['mime_type'] = 'application/atom+xml';
152 $opt['feed_type'] = 'RSS1.0';
153 $opt['mime_type'] = 'application/xml';
159 trigger_event('FEED_OPTS_POSTPROCESS', $eventData);
164 * Add recent changed pages to a feed object
166 * @author Andreas Gohr <andi@splitbrain.org>
167 * @param object $rss - the FeedCreator Object
168 * @param array $data - the items to add
169 * @param array $opt - the feed options
171 function rss_buildItems(&$rss,&$data,$opt){
181 $event = new Doku_Event('FEED_DATA_PROCESS', $eventData);
182 if ($event->advise_before(false)){
183 foreach($data as $ditem){
184 if(!is_array($ditem)){
185 // not an array? then only a list of IDs was given
186 $ditem = array( 'id' => $ditem );
189 $item = new FeedItem();
191 $meta = p_get_metadata($id);
195 $date = $ditem['date'];
196 }elseif($meta['date']['modified']){
197 $date = $meta['date']['modified'];
199 $date = @filemtime
(wikiFN($id));
201 if($date) $item->date
= date('r',$date);
204 if($conf['useheading'] && $meta['title']){
205 $item->title
= $meta['title'];
207 $item->title
= $ditem['id'];
209 if($conf['rss_show_summary'] && !empty($ditem['sum'])){
210 $item->title
.= ' - '.strip_tags($ditem['sum']);
214 switch ($opt['link_to']){
216 $item->link
= wl($id,'rev='.$date,true,'&');
219 $item->link
= wl($id,'do=revisions&rev='.$date,true,'&');
222 $item->link
= wl($id, '', true,'&');
226 $item->link
= wl($id,'rev='.$date.'&do=diff',true,'&');
230 switch ($opt['item_content']){
233 require_once(DOKU_INC
.'inc/DifferenceEngine.php');
234 $revs = getRevisions($id, 0, 1);
238 $df = new Diff(explode("\n",htmlspecialchars(rawWiki($id,$rev))),
239 explode("\n",htmlspecialchars(rawWiki($id,''))));
241 $df = new Diff(array(''),
242 explode("\n",htmlspecialchars(rawWiki($id,''))));
245 if($opt['item_content'] == 'htmldiff'){
246 $tdf = new TableDiffFormatter();
247 $content = '<table>';
248 $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
249 $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
250 $content .= $tdf->format($df);
251 $content .= '</table>';
253 $udf = new UnifiedDiffFormatter();
254 $content = "<pre>\n".$udf->format($df)."\n</pre>";
258 $content = p_wiki_xhtml($id,$date,false);
260 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s','',$content);
262 // make URLs work when canonical is not set, regexp instead of rerendering!
263 if(!$conf['canonical']){
264 $base = preg_quote(DOKU_REL
,'/');
265 $content = preg_replace('/(<a href|<img src)="('.$base.')/s','$1="'.DOKU_URL
,$content);
271 $content = $meta['description']['abstract'];
273 $item->description
= $content; //FIXME a plugin hook here could be senseful
276 # FIXME should the user be pulled from metadata as well?
277 $user = @$ditem['user']; // the @ spares time repeating lookup
279 if($user && $conf['useacl'] && $auth){
280 $userInfo = $auth->getUserData($user);
282 switch ($conf['showuseras']){
284 $item->author
= $userInfo['name'];
287 $item->author
= $user;
291 $item->author
= $user;
293 if($userInfo && !$opt['guardmail']){
294 $item->authorEmail
= $userInfo['mail'];
296 //cannot obfuscate because some RSS readers may check validity
297 $item->authorEmail
= $user.'@'.$ditem['ip'];
300 // this happens when no ACL but some Apache auth is used
301 $item->author
= $user;
302 $item->authorEmail
= $user.'@'.$ditem['ip'];
304 $item->authorEmail
= 'anonymous@'.$ditem['ip'];
308 if(isset($meta['subject'])) {
309 $item->category
= $meta['subject'];
312 if($cat) $item->category
= $cat;
315 // finally add the item to the feed object, after handing it to registered plugins
316 $evdata = array('item' => &$item,
320 $evt = new Doku_Event('FEED_ITEM_ADD', $evdata);
321 if ($evt->advise_before()){
322 $rss->addItem($item);
324 $evt->advise_after(); // for completeness
327 $event->advise_after();
332 * Add recent changed pages to the feed object
334 * @author Andreas Gohr <andi@splitbrain.org>
336 function rssRecentChanges($opt){
337 $flags = RECENTS_SKIP_DELETED
;
338 if(!$opt['show_minor']) $flags +
= RECENTS_SKIP_MINORS
;
340 $recents = getRecents(0,$opt['items'],$opt['namespace'],$flags);
345 * Add all pages of a namespace to the feed object
347 * @author Andreas Gohr <andi@splitbrain.org>
349 function rssListNamespace($opt){
350 require_once(DOKU_INC
.'inc/search.php');
353 $ns=':'.cleanID($opt['namespace']);
354 $ns=str_replace(':','/',$ns);
358 search($data,$conf['datadir'],'search_list','',$ns);
364 * Add the result of a full text search to the feed object
366 * @author Andreas Gohr <andi@splitbrain.org>
368 function rssSearch($opt){
369 if(!$opt['search_query']) return;
371 require_once(DOKU_INC
.'inc/fulltext.php');
372 $data = ft_pageSearch($opt['search_query'],$poswords);
373 $data = array_keys($data);
378 //Setup VIM: ex: et ts=4 :