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');
11 require_once(DOKU_INC
.'inc/common.php');
12 require_once(DOKU_INC
.'inc/events.php');
13 require_once(DOKU_INC
.'inc/parserutils.php');
14 require_once(DOKU_INC
.'inc/feedcreator.class.php');
15 require_once(DOKU_INC
.'inc/auth.php');
16 require_once(DOKU_INC
.'inc/pageutils.php');
17 require_once(DOKU_INC
.'inc/httputils.php');
20 session_write_close();
23 $opt = rss_parseOptions();
25 // the feed is dynamic - we need a cache for each combo
26 // (but most people just use the default feed so it's still effective)
27 $cache = getCacheName(join('',array_values($opt)).$_SERVER['REMOTE_USER'],'.feed');
28 $cmod = @filemtime
($cache); // 0 if not exists
29 if ($cmod && (@filemtime
(DOKU_CONF
.'local.php')>$cmod || @filemtime
(DOKU_CONF
.'dokuwiki.php')>$cmod)) {
30 // ignore cache if feed prefs may have changed
34 // check cacheage and deliver if nothing has changed since last
35 // time or the update interval has not passed, also handles conditional requests
36 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
37 header('Pragma: public');
38 header('Content-Type: application/xml; charset=utf-8');
39 header('X-Robots-Tag: noindex');
40 if($cmod && (($cmod+
$conf['rss_update']>time()) ||
($cmod>@filemtime
($conf['changelog'])))){
41 http_conditionalRequest($cmod);
42 if($conf['allowdebug']) header("X-CacheUsed: $cache");
43 print io_readFile($cache);
46 http_conditionalRequest(time());
50 $rss = new DokuWikiFeedCreator();
51 $rss->title
= $conf['title'].(($opt['namespace']) ?
' '.$opt['namespace'] : '');
52 $rss->link
= DOKU_URL
;
53 $rss->syndicationURL
= DOKU_URL
.'feed.php';
54 $rss->cssStyleSheet
= DOKU_URL
.'lib/exe/css.php?s=feed';
56 $image = new FeedImage();
57 $image->title
= $conf['title'];
58 $image->url
= DOKU_URL
."lib/images/favicon.ico";
59 $image->link
= DOKU_URL
;
62 if($opt['feed_mode'] == 'list'){
63 rssListNamespace($rss,$opt);
64 }elseif($opt['feed_mode'] == 'search'){
67 rssRecentChanges($rss,$opt);
70 $feed = $rss->createFeed($opt['feed_type'],'utf-8');
73 io_saveFile($cache,$feed);
78 // ---------------------------------------------------------------- //
81 * Get URL parameters and config options and return a initialized option array
83 * @author Andreas Gohr <andi@splitbrain.org>
85 function rss_parseOptions(){
88 $opt['items'] = (int) $_REQUEST['num'];
89 $opt['feed_type'] = $_REQUEST['type'];
90 $opt['feed_mode'] = $_REQUEST['mode'];
91 $opt['show_minor'] = $_REQUEST['minor'];
92 $opt['namespace'] = $_REQUEST['ns'];
93 $opt['link_to'] = $_REQUEST['linkto'];
94 $opt['item_content'] = $_REQUEST['content'];
95 $opt['search_query'] = $_REQUEST['q'];
97 if(!$opt['feed_type']) $opt['feed_type'] = $conf['rss_type'];
98 if(!$opt['item_content']) $opt['item_content'] = $conf['rss_content'];
99 if(!$opt['link_to']) $opt['link_to'] = $conf['rss_linkto'];
100 if(!$opt['items']) $opt['items'] = $conf['recent'];
101 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
103 switch ($opt['feed_type']){
105 $opt['feed_type'] = 'RSS0.91';
106 $opt['mime_type'] = 'text/xml';
109 $opt['feed_type'] = 'RSS2.0';
110 $opt['mime_type'] = 'text/xml';
113 $opt['feed_type'] = 'ATOM0.3';
114 $opt['mime_type'] = 'application/xml';
117 $opt['feed_type'] = 'ATOM1.0';
118 $opt['mime_type'] = 'application/atom+xml';
121 $opt['feed_type'] = 'RSS1.0';
122 $opt['mime_type'] = 'application/xml';
128 * Add recent changed pages to a feed object
130 * @author Andreas Gohr <andi@splitbrain.org>
131 * @param object $rss - the FeedCreator Object
132 * @param array $data - the items to add
133 * @param array $opt - the feed options
135 function rss_buildItems(&$rss,&$data,$opt){
140 foreach($data as $ditem){
141 if(!is_array($ditem)){
142 // not an array? then only a list of IDs was given
143 $ditem = array( 'id' => $ditem );
146 $item = new FeedItem();
148 $meta = p_get_metadata($id);
152 $date = $ditem['date'];
153 }elseif($meta['date']['modified']){
154 $date = $meta['date']['modified'];
156 $date = @filemtime
(wikiFN($id));
158 if($date) $item->date
= date('r',$date);
161 if($conf['useheading'] && $meta['title']){
162 $item->title
= $meta['title'];
164 $item->title
= $ditem['id'];
166 if($conf['rss_show_summary'] && !empty($ditem['sum'])){
167 $item->title
.= ' - '.strip_tags($ditem['sum']);
171 switch ($opt['link_to']){
173 $item->link
= wl($id,'rev='.$date,true,'&');
176 $item->link
= wl($id,'do=revisions&rev='.$date,true,'&');
179 $item->link
= wl($id, '', true,'&');
183 $item->link
= wl($id,'rev='.$date.'&do=diff',true,'&');
187 switch ($opt['item_content']){
190 require_once(DOKU_INC
.'inc/DifferenceEngine.php');
191 $revs = getRevisions($id, 0, 1);
195 $df = new Diff(explode("\n",htmlspecialchars(rawWiki($id,$rev))),
196 explode("\n",htmlspecialchars(rawWiki($id,''))));
198 $df = new Diff(array(''),
199 explode("\n",htmlspecialchars(rawWiki($id,''))));
202 if($opt['item_content'] == 'htmldiff'){
203 $tdf = new TableDiffFormatter();
204 $content = '<table>';
205 $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
206 $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
207 $content .= $tdf->format($df);
208 $content .= '</table>';
210 $udf = new UnifiedDiffFormatter();
211 $content = "<pre>\n".$udf->format($df)."\n</pre>";
215 $content = p_wiki_xhtml($id,$date,false);
217 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s','',$content);
219 // make URLs work when canonical is not set, regexp instead of rerendering!
220 if(!$conf['canonical']){
221 $base = preg_quote(DOKU_REL
,'/');
222 $content = preg_replace('/(<a href|<img src)="('.$base.')/s','$1="'.DOKU_URL
,$content);
228 $content = $meta['description']['abstract'];
230 $item->description
= $content; //FIXME a plugin hook here could be senseful
234 # FIXME should the user be pulled from metadata as well?
236 $user = @$ditem['user']; // the @ spares time repeating lookup
238 if($user && $conf['useacl'] && $auth){
239 $userInfo = $auth->getUserData($user);
240 $item->author
= $userInfo['name'];
241 if($userInfo && !$opt['guardmail']){
242 $item->authorEmail
= $userInfo['mail'];
244 //cannot obfuscate because some RSS readers may check validity
245 $item->authorEmail
= $user.'@'.$recent['ip'];
248 // this happens when no ACL but some Apache auth is used
249 $item->author
= $user;
250 $item->authorEmail
= $user.'@'.$recent['ip'];
252 $item->authorEmail
= 'anonymous@'.$recent['ip'];
256 if($meta['subject']){
257 $item->category
= $meta['subject'];
260 if($cat) $item->category
= $cat;
263 // finally add the item to the feed object, after handing it to registered plugins
264 $evdata = array('item' => &$item,
268 $evt = new Doku_Event('FEED_ITEM_ADD', $evdata);
269 if ($evt->advise_before()){
270 $rss->addItem($item);
272 $evt->advise_after(); // for completeness
278 * Add recent changed pages to the feed object
280 * @author Andreas Gohr <andi@splitbrain.org>
282 function rssRecentChanges(&$rss,$opt){
286 $flags = RECENTS_SKIP_DELETED
;
287 if(!$opt['show_minor']) $flags +
= RECENTS_SKIP_MINORS
;
289 $recents = getRecents(0,$opt['items'],$opt['namespace'],$flags);
291 rss_buildItems($rss,$recents,$opt);
295 * Add all pages of a namespace to the feed object
297 * @author Andreas Gohr <andi@splitbrain.org>
299 function rssListNamespace(&$rss,$opt){
300 require_once(DOKU_INC
.'inc/search.php');
303 $ns=':'.cleanID($opt['namespace']);
304 $ns=str_replace(':','/',$ns);
308 search($data,$conf['datadir'],'search_list','',$ns);
310 rss_buildItems($rss,$data,$opt);
314 * Add the result of a full text search to the feed object
316 * @author Andreas Gohr <andi@splitbrain.org>
318 function rssSearch(&$rss,$opt){
319 if(!$opt['search_query']) return;
321 require_once(DOKU_INC
.'inc/fulltext.php');
323 $data = ft_pageSearch($opt['search_query'],$poswords);
324 $data = array_keys($data);
325 rss_buildItems($rss,$data,$opt);
328 //Setup VIM: ex: et ts=4 enc=utf-8 :