xref: /dokuwiki/feed.php (revision 4bf3df7c70a3260fd2d9d78b0fe37d3519ed7f65)
1f3f0262cSandi<?php
215fae107Sandi/**
315fae107Sandi * XML feed export
415fae107Sandi *
515fae107Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
615fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
715fae107Sandi */
815fae107Sandi
9d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/');
10ed7b5f09Sandirequire_once(DOKU_INC.'inc/init.php');
114d58bd99Sandirequire_once(DOKU_INC.'inc/common.php');
1230e171a0Salexander.krauserequire_once(DOKU_INC.'inc/events.php');
134d58bd99Sandirequire_once(DOKU_INC.'inc/parserutils.php');
144d58bd99Sandirequire_once(DOKU_INC.'inc/feedcreator.class.php');
154d58bd99Sandirequire_once(DOKU_INC.'inc/auth.php');
16fbf82939SBen Coburnrequire_once(DOKU_INC.'inc/pageutils.php');
17758447cfSAndreas Gohrrequire_once(DOKU_INC.'inc/httputils.php');
18f3f0262cSandi
197131b668SAndreas Gohr//close session
208746e727Sandisession_write_close();
218746e727Sandi
224ab889eaSAndreas Gohr// get params
234ab889eaSAndreas Gohr$opt = rss_parseOptions();
24f3f0262cSandi
257131b668SAndreas Gohr// the feed is dynamic - we need a cache for each combo
267131b668SAndreas Gohr// (but most people just use the default feed so it's still effective)
272f1faf49SAndreas Gohr$cache = getCacheName(join('',array_values($opt)).$_SERVER['REMOTE_USER'],'.feed');
285d03233cSBen Coburn$cmod = @filemtime($cache); // 0 if not exists
295d03233cSBen Coburnif ($cmod && (@filemtime(DOKU_CONF.'local.php')>$cmod || @filemtime(DOKU_CONF.'dokuwiki.php')>$cmod)) {
305d03233cSBen Coburn    // ignore cache if feed prefs may have changed
315d03233cSBen Coburn    $cmod = 0;
325d03233cSBen Coburn}
33f3f0262cSandi
347131b668SAndreas Gohr// check cacheage and deliver if nothing has changed since last
35fbf82939SBen Coburn// time or the update interval has not passed, also handles conditional requests
36fbf82939SBen Coburnheader('Cache-Control: must-revalidate, post-check=0, pre-check=0');
37fbf82939SBen Coburnheader('Pragma: public');
387131b668SAndreas Gohrheader('Content-Type: application/xml; charset=utf-8');
3950ddb617SAndreas Gohrheader('X-Robots-Tag: noindex');
40fbf82939SBen Coburnif($cmod && (($cmod+$conf['rss_update']>time()) || ($cmod>@filemtime($conf['changelog'])))){
41fbf82939SBen Coburn    http_conditionalRequest($cmod);
428716966dSAndreas Gohr    if($conf['allowdebug']) header("X-CacheUsed: $cache");
437131b668SAndreas Gohr    print io_readFile($cache);
447131b668SAndreas Gohr    exit;
45fbf82939SBen Coburn} else {
46fbf82939SBen Coburn    http_conditionalRequest(time());
477131b668SAndreas Gohr }
487131b668SAndreas Gohr
497131b668SAndreas Gohr// create new feed
50f3f0262cSandi$rss = new DokuWikiFeedCreator();
514ab889eaSAndreas Gohr$rss->title = $conf['title'].(($opt['namespace']) ? ' '.$opt['namespace'] : '');
52ed7b5f09Sandi$rss->link  = DOKU_URL;
53f62ea8a1Sandi$rss->syndicationURL = DOKU_URL.'feed.php';
54615960feSTom N Harris$rss->cssStyleSheet  = DOKU_URL.'lib/exe/css.php?s=feed';
55f3f0262cSandi
5679b608ceSandi$image = new FeedImage();
5779b608ceSandi$image->title = $conf['title'];
58f62ea8a1Sandi$image->url = DOKU_URL."lib/images/favicon.ico";
5979b608ceSandi$image->link = DOKU_URL;
6079b608ceSandi$rss->image = $image;
6179b608ceSandi
62*4bf3df7cSGina Haeussge$data = null;
634ab889eaSAndreas Gohrif($opt['feed_mode'] == 'list'){
64*4bf3df7cSGina Haeussge    $data = rssListNamespace($opt);
654bb1b5aeSAndreas Gohr}elseif($opt['feed_mode'] == 'search'){
66*4bf3df7cSGina Haeussge    $data = rssSearch($opt);
67f3f0262cSandi}else{
68*4bf3df7cSGina Haeussge    $eventData = array(
69*4bf3df7cSGina Haeussge        'opt'  => &$opt,
70*4bf3df7cSGina Haeussge        'data' => &$data,
71*4bf3df7cSGina Haeussge    );
72*4bf3df7cSGina Haeussge    $event = new Doku_Event('FEED_MODE_UNKNOWN', $eventData);
73*4bf3df7cSGina Haeussge    if ($event->advise_before(true)) {
74*4bf3df7cSGina Haeussge        $data = rssRecentChanges($opt);
75*4bf3df7cSGina Haeussge    }
76*4bf3df7cSGina Haeussge    $event->advise_after();
77f3f0262cSandi}
78f3f0262cSandi
79*4bf3df7cSGina Haeussgerss_buildItems($rss, $data, $opt);
804ab889eaSAndreas Gohr$feed = $rss->createFeed($opt['feed_type'],'utf-8');
817131b668SAndreas Gohr
827131b668SAndreas Gohr// save cachefile
837131b668SAndreas Gohrio_saveFile($cache,$feed);
847131b668SAndreas Gohr
857131b668SAndreas Gohr// finally deliver
867131b668SAndreas Gohrprint $feed;
87f3f0262cSandi
8815fae107Sandi// ---------------------------------------------------------------- //
89f3f0262cSandi
9015fae107Sandi/**
914ab889eaSAndreas Gohr * Get URL parameters and config options and return a initialized option array
9215fae107Sandi *
9315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
9415fae107Sandi */
954ab889eaSAndreas Gohrfunction rss_parseOptions(){
96f62ea8a1Sandi    global $conf;
97c0f9af6dSNathan Neulinger
984ab889eaSAndreas Gohr    $opt['items']        = (int) $_REQUEST['num'];
994ab889eaSAndreas Gohr    $opt['feed_type']    = $_REQUEST['type'];
1004ab889eaSAndreas Gohr    $opt['feed_mode']    = $_REQUEST['mode'];
1014ab889eaSAndreas Gohr    $opt['show_minor']   = $_REQUEST['minor'];
1024ab889eaSAndreas Gohr    $opt['namespace']    = $_REQUEST['ns'];
1034ab889eaSAndreas Gohr    $opt['link_to']      = $_REQUEST['linkto'];
1044ab889eaSAndreas Gohr    $opt['item_content'] = $_REQUEST['content'];
1054bb1b5aeSAndreas Gohr    $opt['search_query'] = $_REQUEST['q'];
106f62ea8a1Sandi
1072f1faf49SAndreas Gohr    if(!$opt['feed_type'])    $opt['feed_type']    = $conf['rss_type'];
1082f1faf49SAndreas Gohr    if(!$opt['item_content']) $opt['item_content'] = $conf['rss_content'];
1092f1faf49SAndreas Gohr    if(!$opt['link_to'])      $opt['link_to']      = $conf['rss_linkto'];
1104ab889eaSAndreas Gohr    if(!$opt['items'])        $opt['items']        = $conf['recent'];
1114ab889eaSAndreas Gohr    $opt['guardmail']  = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
112b6912aeaSAndreas Gohr
1134ab889eaSAndreas Gohr    switch ($opt['feed_type']){
1144ab889eaSAndreas Gohr        case 'rss':
1154ab889eaSAndreas Gohr            $opt['feed_type'] = 'RSS0.91';
1164ab889eaSAndreas Gohr            $opt['mime_type'] = 'text/xml';
1174ab889eaSAndreas Gohr            break;
1184ab889eaSAndreas Gohr        case 'rss2':
1194ab889eaSAndreas Gohr            $opt['feed_type'] = 'RSS2.0';
1204ab889eaSAndreas Gohr            $opt['mime_type'] = 'text/xml';
1214ab889eaSAndreas Gohr            break;
1224ab889eaSAndreas Gohr        case 'atom':
1234ab889eaSAndreas Gohr            $opt['feed_type'] = 'ATOM0.3';
1244ab889eaSAndreas Gohr            $opt['mime_type'] = 'application/xml';
1254ab889eaSAndreas Gohr            break;
1264ab889eaSAndreas Gohr        case 'atom1':
1274ab889eaSAndreas Gohr            $opt['feed_type'] = 'ATOM1.0';
1284ab889eaSAndreas Gohr            $opt['mime_type'] = 'application/atom+xml';
1294ab889eaSAndreas Gohr            break;
1304ab889eaSAndreas Gohr        default:
1314ab889eaSAndreas Gohr            $opt['feed_type'] = 'RSS1.0';
1324ab889eaSAndreas Gohr            $opt['mime_type'] = 'application/xml';
1334ab889eaSAndreas Gohr    }
134*4bf3df7cSGina Haeussge
135*4bf3df7cSGina Haeussge    $eventData = array(
136*4bf3df7cSGina Haeussge        'opt' => &$opt,
137*4bf3df7cSGina Haeussge    );
138*4bf3df7cSGina Haeussge    trigger_event('FEED_OPTS_POSTPROCESS', $eventData);
1394ab889eaSAndreas Gohr    return $opt;
1404ab889eaSAndreas Gohr}
141b6912aeaSAndreas Gohr
1424ab889eaSAndreas Gohr/**
1434ab889eaSAndreas Gohr * Add recent changed pages to a feed object
1444ab889eaSAndreas Gohr *
1454ab889eaSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1464ab889eaSAndreas Gohr * @param  object $rss - the FeedCreator Object
1474ab889eaSAndreas Gohr * @param  array $data - the items to add
1484ab889eaSAndreas Gohr * @param  array $opt  - the feed options
1494ab889eaSAndreas Gohr */
1504ab889eaSAndreas Gohrfunction rss_buildItems(&$rss,&$data,$opt){
1514ab889eaSAndreas Gohr    global $conf;
1524ab889eaSAndreas Gohr    global $lang;
1533d581c29SAndreas Gohr    global $auth;
154883480fbSAndreas Gohr
155*4bf3df7cSGina Haeussge    $eventData = array(
156*4bf3df7cSGina Haeussge        'rss' => &$rss,
157*4bf3df7cSGina Haeussge        'data' => &$data,
158*4bf3df7cSGina Haeussge        'opt' => &$opt,
159*4bf3df7cSGina Haeussge    );
160*4bf3df7cSGina Haeussge    $event = new Doku_Event('FEED_DATA_PROCESS', $eventData);
161*4bf3df7cSGina Haeussge    if ($event->advise_before(false)){
1624ab889eaSAndreas Gohr        foreach($data as $ditem){
1634bb1b5aeSAndreas Gohr            if(!is_array($ditem)){
1644bb1b5aeSAndreas Gohr                // not an array? then only a list of IDs was given
1654bb1b5aeSAndreas Gohr                $ditem = array( 'id' => $ditem );
1664bb1b5aeSAndreas Gohr            }
1674bb1b5aeSAndreas Gohr
168f3f0262cSandi            $item = new FeedItem();
1694ab889eaSAndreas Gohr            $id   = $ditem['id'];
1704ab889eaSAndreas Gohr            $meta = p_get_metadata($id);
17103ee62cbSjoe.lapp
1724ab889eaSAndreas Gohr            // add date
1734ab889eaSAndreas Gohr            if($ditem['date']){
1744ab889eaSAndreas Gohr                $date = $ditem['date'];
1754ab889eaSAndreas Gohr            }elseif($meta['date']['modified']){
1764ab889eaSAndreas Gohr                $date = $meta['date']['modified'];
1774ab889eaSAndreas Gohr            }else{
1784ab889eaSAndreas Gohr                $date = @filemtime(wikiFN($id));
1794ab889eaSAndreas Gohr            }
1804ab889eaSAndreas Gohr            if($date) $item->date = date('r',$date);
1814ab889eaSAndreas Gohr
1824ab889eaSAndreas Gohr            // add title
1838716966dSAndreas Gohr            if($conf['useheading'] && $meta['title']){
1848716966dSAndreas Gohr                $item->title = $meta['title'];
1858716966dSAndreas Gohr            }else{
1864ab889eaSAndreas Gohr                $item->title = $ditem['id'];
18703ee62cbSjoe.lapp            }
1884ab889eaSAndreas Gohr            if($conf['rss_show_summary'] && !empty($ditem['sum'])){
1894ab889eaSAndreas Gohr                $item->title .= ' - '.strip_tags($ditem['sum']);
190b1a1915cSandi            }
1914d58bd99Sandi
1924ab889eaSAndreas Gohr            // add item link
1934ab889eaSAndreas Gohr            switch ($opt['link_to']){
1944d58bd99Sandi                case 'page':
1954ab889eaSAndreas Gohr                    $item->link = wl($id,'rev='.$date,true,'&');
1964d58bd99Sandi                    break;
1974d58bd99Sandi                case 'rev':
1984ab889eaSAndreas Gohr                    $item->link = wl($id,'do=revisions&rev='.$date,true,'&');
1994d58bd99Sandi                    break;
20092e52d8dSjoe.lapp                case 'current':
2014ab889eaSAndreas Gohr                    $item->link = wl($id, '', true,'&');
20292e52d8dSjoe.lapp                    break;
20392e52d8dSjoe.lapp                case 'diff':
2044d58bd99Sandi                default:
2054ab889eaSAndreas Gohr                    $item->link = wl($id,'rev='.$date.'&do=diff',true,'&');
2064d58bd99Sandi            }
2074d58bd99Sandi
2084ab889eaSAndreas Gohr            // add item content
2094ab889eaSAndreas Gohr            switch ($opt['item_content']){
2104ab889eaSAndreas Gohr                case 'diff':
2114ab889eaSAndreas Gohr                case 'htmldiff':
2124ab889eaSAndreas Gohr                    require_once(DOKU_INC.'inc/DifferenceEngine.php');
2134ab889eaSAndreas Gohr                    $revs = getRevisions($id, 0, 1);
2144ab889eaSAndreas Gohr                    $rev = $revs[0];
2157a98db20Sjoe.lapp
2164ab889eaSAndreas Gohr                    if($rev){
2174ab889eaSAndreas Gohr                        $df  = new Diff(explode("\n",htmlspecialchars(rawWiki($id,$rev))),
2184ab889eaSAndreas Gohr                                        explode("\n",htmlspecialchars(rawWiki($id,''))));
2194ab889eaSAndreas Gohr                    }else{
2204ab889eaSAndreas Gohr                        $df  = new Diff(array(''),
2214ab889eaSAndreas Gohr                                        explode("\n",htmlspecialchars(rawWiki($id,''))));
2224ab889eaSAndreas Gohr                    }
2234ab889eaSAndreas Gohr
2244ab889eaSAndreas Gohr                    if($opt['item_content'] == 'htmldiff'){
2254ab889eaSAndreas Gohr                        $tdf = new TableDiffFormatter();
2264ab889eaSAndreas Gohr                        $content  = '<table>';
2274ab889eaSAndreas Gohr                        $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
2284ab889eaSAndreas Gohr                        $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
2294ab889eaSAndreas Gohr                        $content .= $tdf->format($df);
2304ab889eaSAndreas Gohr                        $content .= '</table>';
2314ab889eaSAndreas Gohr                    }else{
2324ab889eaSAndreas Gohr                        $udf = new UnifiedDiffFormatter();
2334ab889eaSAndreas Gohr                        $content = "<pre>\n".$udf->format($df)."\n</pre>";
2344ab889eaSAndreas Gohr                    }
2354ab889eaSAndreas Gohr                    break;
2364ab889eaSAndreas Gohr                case 'html':
2374ab889eaSAndreas Gohr                    $content = p_wiki_xhtml($id,$date,false);
2384ab889eaSAndreas Gohr                    // no TOC in feeds
2394ab889eaSAndreas Gohr                    $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s','',$content);
2404ab889eaSAndreas Gohr
2414ab889eaSAndreas Gohr                    // make URLs work when canonical is not set, regexp instead of rerendering!
2424ab889eaSAndreas Gohr                    if(!$conf['canonical']){
2434ab889eaSAndreas Gohr                        $base = preg_quote(DOKU_REL,'/');
2444ab889eaSAndreas Gohr                        $content = preg_replace('/(<a href|<img src)="('.$base.')/s','$1="'.DOKU_URL,$content);
2454ab889eaSAndreas Gohr                    }
2464ab889eaSAndreas Gohr
2474ab889eaSAndreas Gohr                    break;
2484ab889eaSAndreas Gohr                case 'abstract':
2494ab889eaSAndreas Gohr                default:
2504ab889eaSAndreas Gohr                    $content = $meta['description']['abstract'];
2514ab889eaSAndreas Gohr            }
2524ab889eaSAndreas Gohr            $item->description = $content; //FIXME a plugin hook here could be senseful
2534ab889eaSAndreas Gohr
2544ab889eaSAndreas Gohr            // add user
2554ab889eaSAndreas Gohr            # FIXME should the user be pulled from metadata as well?
2567a98db20Sjoe.lapp            $user = null;
2574ab889eaSAndreas Gohr            $user = @$ditem['user']; // the @ spares time repeating lookup
2587a98db20Sjoe.lapp            $item->author = '';
2590f4f4adfSAndreas Gohr            if($user && $conf['useacl'] && $auth){
260c0f9af6dSNathan Neulinger                $userInfo = $auth->getUserData($user);
2617a98db20Sjoe.lapp                $item->author = $userInfo['name'];
262c1791678SAndreas Gohr                if($userInfo && !$opt['guardmail']){
263c1791678SAndreas Gohr                    $item->authorEmail = $userInfo['mail'];
264c1791678SAndreas Gohr                }else{
2657a98db20Sjoe.lapp                    //cannot obfuscate because some RSS readers may check validity
266d437bcc4SAndreas Gohr                    $item->authorEmail = $user.'@'.$recent['ip'];
267f3f0262cSandi                }
268c5983034SAndreas Gohr            }elseif($user){
269c5983034SAndreas Gohr                // this happens when no ACL but some Apache auth is used
270c5983034SAndreas Gohr                $item->author      = $user;
271c5983034SAndreas Gohr                $item->authorEmail = $user.'@'.$recent['ip'];
2727a98db20Sjoe.lapp            }else{
273d437bcc4SAndreas Gohr                $item->authorEmail = 'anonymous@'.$recent['ip'];
2747a98db20Sjoe.lapp            }
2754ab889eaSAndreas Gohr
2764ab889eaSAndreas Gohr            // add category
2774ab889eaSAndreas Gohr            if($meta['subject']){
2784ab889eaSAndreas Gohr                $item->category = $meta['subject'];
2794ab889eaSAndreas Gohr            }else{
2804ab889eaSAndreas Gohr                $cat = getNS($id);
2814ab889eaSAndreas Gohr                if($cat) $item->category = $cat;
2824ab889eaSAndreas Gohr            }
2834ab889eaSAndreas Gohr
284883480fbSAndreas Gohr            // finally add the item to the feed object, after handing it to registered plugins
285883480fbSAndreas Gohr            $evdata = array('item'  => &$item,
286883480fbSAndreas Gohr                            'opt'   => &$opt,
287883480fbSAndreas Gohr                            'ditem' => &$ditem,
288883480fbSAndreas Gohr                            'rss'   => &$rss);
289209cd8e1SAndreas Gohr            $evt = new Doku_Event('FEED_ITEM_ADD', $evdata);
290883480fbSAndreas Gohr            if ($evt->advise_before()){
291f3f0262cSandi                $rss->addItem($item);
292f3f0262cSandi            }
293883480fbSAndreas Gohr            $evt->advise_after(); // for completeness
294883480fbSAndreas Gohr        }
295f3f0262cSandi    }
296*4bf3df7cSGina Haeussge    $event->advise_after();
297*4bf3df7cSGina Haeussge}
298f3f0262cSandi
2994ab889eaSAndreas Gohr
3004ab889eaSAndreas Gohr/**
3014bb1b5aeSAndreas Gohr * Add recent changed pages to the feed object
3024ab889eaSAndreas Gohr *
3034ab889eaSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3044ab889eaSAndreas Gohr */
305*4bf3df7cSGina Haeussgefunction rssRecentChanges($opt){
3064ab889eaSAndreas Gohr    global $conf;
3074ab889eaSAndreas Gohr    global $auth;
3084ab889eaSAndreas Gohr
3094ab889eaSAndreas Gohr    $flags = RECENTS_SKIP_DELETED;
3104ab889eaSAndreas Gohr    if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS;
3114ab889eaSAndreas Gohr
3124ab889eaSAndreas Gohr    $recents = getRecents(0,$opt['items'],$opt['namespace'],$flags);
313*4bf3df7cSGina Haeussge    return $recents;
3144ab889eaSAndreas Gohr}
3154ab889eaSAndreas Gohr
31615fae107Sandi/**
3174bb1b5aeSAndreas Gohr * Add all pages of a namespace to the feed object
31815fae107Sandi *
31915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
32015fae107Sandi */
321*4bf3df7cSGina Haeussgefunction rssListNamespace($opt){
322f62ea8a1Sandi    require_once(DOKU_INC.'inc/search.php');
323f3f0262cSandi    global $conf;
324f3f0262cSandi
3254ab889eaSAndreas Gohr    $ns=':'.cleanID($opt['namespace']);
326f3f0262cSandi    $ns=str_replace(':','/',$ns);
327f3f0262cSandi
328f3f0262cSandi    $data = array();
329f3f0262cSandi    sort($data);
330f3f0262cSandi    search($data,$conf['datadir'],'search_list','',$ns);
33185cf8195SAndreas Gohr
332*4bf3df7cSGina Haeussge    return $data;
33385cf8195SAndreas Gohr}
33485cf8195SAndreas Gohr
3354bb1b5aeSAndreas Gohr/**
3364bb1b5aeSAndreas Gohr * Add the result of a full text search to the feed object
3374bb1b5aeSAndreas Gohr *
3384bb1b5aeSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3394bb1b5aeSAndreas Gohr */
340*4bf3df7cSGina Haeussgefunction rssSearch($opt){
3414bb1b5aeSAndreas Gohr    if(!$opt['search_query']) return;
3424ab889eaSAndreas Gohr
3434bb1b5aeSAndreas Gohr    require_once(DOKU_INC.'inc/fulltext.php');
3444bb1b5aeSAndreas Gohr    $data = array();
3454bb1b5aeSAndreas Gohr    $data = ft_pageSearch($opt['search_query'],$poswords);
3464bb1b5aeSAndreas Gohr    $data = array_keys($data);
347*4bf3df7cSGina Haeussge
348*4bf3df7cSGina Haeussge    return $data;
3494bb1b5aeSAndreas Gohr}
350f3f0262cSandi
3518716966dSAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
352