xref: /dokuwiki/feed.php (revision 681a59b23795166231cfebc0da4d0d219d528795)
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');
11f3f0262cSandi
127131b668SAndreas Gohr//close session
138746e727Sandisession_write_close();
148746e727Sandi
154ab889eaSAndreas Gohr// get params
164ab889eaSAndreas Gohr$opt = rss_parseOptions();
17f3f0262cSandi
187131b668SAndreas Gohr// the feed is dynamic - we need a cache for each combo
197131b668SAndreas Gohr// (but most people just use the default feed so it's still effective)
202f1faf49SAndreas Gohr$cache = getCacheName(join('',array_values($opt)).$_SERVER['REMOTE_USER'],'.feed');
210d67055cSMichael Klier$key   = join('', array_values($opt)) . $_SERVER['REMOTE_USER'];
220d67055cSMichael Klier$cache = new cache($key, '.feed');
230d67055cSMichael Klier
240d67055cSMichael Klier// prepare cache depends
250d67055cSMichael Klier$depends['files'] = getConfigFiles('main');
260d67055cSMichael Klier$depends['age']   = $conf['rss_update'];
270d67055cSMichael Klier$depends['purge'] = ($_REQUEST['purge']) ? true : false;
28f3f0262cSandi
297131b668SAndreas Gohr// check cacheage and deliver if nothing has changed since last
30fbf82939SBen Coburn// time or the update interval has not passed, also handles conditional requests
31fbf82939SBen Coburnheader('Cache-Control: must-revalidate, post-check=0, pre-check=0');
32fbf82939SBen Coburnheader('Pragma: public');
337131b668SAndreas Gohrheader('Content-Type: application/xml; charset=utf-8');
3450ddb617SAndreas Gohrheader('X-Robots-Tag: noindex');
350d67055cSMichael Klierif($cache->useCache($depends)) {
360d67055cSMichael Klier    http_conditionalRequest($cache->_time);
370d67055cSMichael Klier    if($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
380d67055cSMichael Klier    print $cache->retrieveCache();
397131b668SAndreas Gohr    exit;
40fbf82939SBen Coburn} else {
41fbf82939SBen Coburn    http_conditionalRequest(time());
427131b668SAndreas Gohr }
437131b668SAndreas Gohr
447131b668SAndreas Gohr// create new feed
45f3f0262cSandi$rss = new DokuWikiFeedCreator();
464ab889eaSAndreas Gohr$rss->title = $conf['title'].(($opt['namespace']) ? ' '.$opt['namespace'] : '');
47ed7b5f09Sandi$rss->link  = DOKU_URL;
48f62ea8a1Sandi$rss->syndicationURL = DOKU_URL.'feed.php';
49615960feSTom N Harris$rss->cssStyleSheet  = DOKU_URL.'lib/exe/css.php?s=feed';
50f3f0262cSandi
5179b608ceSandi$image = new FeedImage();
5279b608ceSandi$image->title = $conf['title'];
53f62ea8a1Sandi$image->url = DOKU_URL."lib/images/favicon.ico";
5479b608ceSandi$image->link = DOKU_URL;
5579b608ceSandi$rss->image = $image;
5679b608ceSandi
574bf3df7cSGina Haeussge$data = null;
584ab889eaSAndreas Gohrif($opt['feed_mode'] == 'list'){
594bf3df7cSGina Haeussge    $data = rssListNamespace($opt);
604bb1b5aeSAndreas Gohr}elseif($opt['feed_mode'] == 'search'){
614bf3df7cSGina Haeussge    $data = rssSearch($opt);
62f3f0262cSandi}else{
634bf3df7cSGina Haeussge    $eventData = array(
644bf3df7cSGina Haeussge        'opt'  => &$opt,
654bf3df7cSGina Haeussge        'data' => &$data,
664bf3df7cSGina Haeussge    );
674bf3df7cSGina Haeussge    $event = new Doku_Event('FEED_MODE_UNKNOWN', $eventData);
684bf3df7cSGina Haeussge    if ($event->advise_before(true)) {
694bf3df7cSGina Haeussge        $data = rssRecentChanges($opt);
704bf3df7cSGina Haeussge    }
714bf3df7cSGina Haeussge    $event->advise_after();
72f3f0262cSandi}
73f3f0262cSandi
744bf3df7cSGina Haeussgerss_buildItems($rss, $data, $opt);
754ab889eaSAndreas Gohr$feed = $rss->createFeed($opt['feed_type'],'utf-8');
767131b668SAndreas Gohr
777131b668SAndreas Gohr// save cachefile
780d67055cSMichael Klier$cache->storeCache($feed);
797131b668SAndreas Gohr
807131b668SAndreas Gohr// finally deliver
817131b668SAndreas Gohrprint $feed;
82f3f0262cSandi
8315fae107Sandi// ---------------------------------------------------------------- //
84f3f0262cSandi
8515fae107Sandi/**
864ab889eaSAndreas Gohr * Get URL parameters and config options and return a initialized option array
8715fae107Sandi *
8815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
8915fae107Sandi */
904ab889eaSAndreas Gohrfunction rss_parseOptions(){
91f62ea8a1Sandi    global $conf;
92c0f9af6dSNathan Neulinger
934ab889eaSAndreas Gohr    $opt['items']        = (int) $_REQUEST['num'];
944ab889eaSAndreas Gohr    $opt['feed_type']    = $_REQUEST['type'];
954ab889eaSAndreas Gohr    $opt['feed_mode']    = $_REQUEST['mode'];
964ab889eaSAndreas Gohr    $opt['show_minor']   = $_REQUEST['minor'];
974ab889eaSAndreas Gohr    $opt['namespace']    = $_REQUEST['ns'];
984ab889eaSAndreas Gohr    $opt['link_to']      = $_REQUEST['linkto'];
994ab889eaSAndreas Gohr    $opt['item_content'] = $_REQUEST['content'];
1004bb1b5aeSAndreas Gohr    $opt['search_query'] = $_REQUEST['q'];
101f62ea8a1Sandi
1022f1faf49SAndreas Gohr    if(!$opt['feed_type'])    $opt['feed_type']    = $conf['rss_type'];
1032f1faf49SAndreas Gohr    if(!$opt['item_content']) $opt['item_content'] = $conf['rss_content'];
1042f1faf49SAndreas Gohr    if(!$opt['link_to'])      $opt['link_to']      = $conf['rss_linkto'];
1054ab889eaSAndreas Gohr    if(!$opt['items'])        $opt['items']        = $conf['recent'];
1064ab889eaSAndreas Gohr    $opt['guardmail']  = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
107b6912aeaSAndreas Gohr
1084ab889eaSAndreas Gohr    switch ($opt['feed_type']){
1094ab889eaSAndreas Gohr        case 'rss':
1104ab889eaSAndreas Gohr            $opt['feed_type'] = 'RSS0.91';
1114ab889eaSAndreas Gohr            $opt['mime_type'] = 'text/xml';
1124ab889eaSAndreas Gohr            break;
1134ab889eaSAndreas Gohr        case 'rss2':
1144ab889eaSAndreas Gohr            $opt['feed_type'] = 'RSS2.0';
1154ab889eaSAndreas Gohr            $opt['mime_type'] = 'text/xml';
1164ab889eaSAndreas Gohr            break;
1174ab889eaSAndreas Gohr        case 'atom':
1184ab889eaSAndreas Gohr            $opt['feed_type'] = 'ATOM0.3';
1194ab889eaSAndreas Gohr            $opt['mime_type'] = 'application/xml';
1204ab889eaSAndreas Gohr            break;
1214ab889eaSAndreas Gohr        case 'atom1':
1224ab889eaSAndreas Gohr            $opt['feed_type'] = 'ATOM1.0';
1234ab889eaSAndreas Gohr            $opt['mime_type'] = 'application/atom+xml';
1244ab889eaSAndreas Gohr            break;
1254ab889eaSAndreas Gohr        default:
1264ab889eaSAndreas Gohr            $opt['feed_type'] = 'RSS1.0';
1274ab889eaSAndreas Gohr            $opt['mime_type'] = 'application/xml';
1284ab889eaSAndreas Gohr    }
1294bf3df7cSGina Haeussge
1304bf3df7cSGina Haeussge    $eventData = array(
1314bf3df7cSGina Haeussge        'opt' => &$opt,
1324bf3df7cSGina Haeussge    );
1334bf3df7cSGina Haeussge    trigger_event('FEED_OPTS_POSTPROCESS', $eventData);
1344ab889eaSAndreas Gohr    return $opt;
1354ab889eaSAndreas Gohr}
136b6912aeaSAndreas Gohr
1374ab889eaSAndreas Gohr/**
1384ab889eaSAndreas Gohr * Add recent changed pages to a feed object
1394ab889eaSAndreas Gohr *
1404ab889eaSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1414ab889eaSAndreas Gohr * @param  object $rss - the FeedCreator Object
1424ab889eaSAndreas Gohr * @param  array $data - the items to add
1434ab889eaSAndreas Gohr * @param  array $opt  - the feed options
1444ab889eaSAndreas Gohr */
1454ab889eaSAndreas Gohrfunction rss_buildItems(&$rss,&$data,$opt){
1464ab889eaSAndreas Gohr    global $conf;
1474ab889eaSAndreas Gohr    global $lang;
1483d581c29SAndreas Gohr    global $auth;
149883480fbSAndreas Gohr
1504bf3df7cSGina Haeussge    $eventData = array(
1514bf3df7cSGina Haeussge        'rss' => &$rss,
1524bf3df7cSGina Haeussge        'data' => &$data,
1534bf3df7cSGina Haeussge        'opt' => &$opt,
1544bf3df7cSGina Haeussge    );
1554bf3df7cSGina Haeussge    $event = new Doku_Event('FEED_DATA_PROCESS', $eventData);
1564bf3df7cSGina Haeussge    if ($event->advise_before(false)){
1574ab889eaSAndreas Gohr        foreach($data as $ditem){
1584bb1b5aeSAndreas Gohr            if(!is_array($ditem)){
1594bb1b5aeSAndreas Gohr                // not an array? then only a list of IDs was given
1604bb1b5aeSAndreas Gohr                $ditem = array( 'id' => $ditem );
1614bb1b5aeSAndreas Gohr            }
1624bb1b5aeSAndreas Gohr
163f3f0262cSandi            $item = new FeedItem();
1644ab889eaSAndreas Gohr            $id   = $ditem['id'];
1654ab889eaSAndreas Gohr            $meta = p_get_metadata($id);
16603ee62cbSjoe.lapp
1674ab889eaSAndreas Gohr            // add date
1684ab889eaSAndreas Gohr            if($ditem['date']){
1694ab889eaSAndreas Gohr                $date = $ditem['date'];
1704ab889eaSAndreas Gohr            }elseif($meta['date']['modified']){
1714ab889eaSAndreas Gohr                $date = $meta['date']['modified'];
1724ab889eaSAndreas Gohr            }else{
1734ab889eaSAndreas Gohr                $date = @filemtime(wikiFN($id));
1744ab889eaSAndreas Gohr            }
1754ab889eaSAndreas Gohr            if($date) $item->date = date('r',$date);
1764ab889eaSAndreas Gohr
1774ab889eaSAndreas Gohr            // add title
1788716966dSAndreas Gohr            if($conf['useheading'] && $meta['title']){
1798716966dSAndreas Gohr                $item->title = $meta['title'];
1808716966dSAndreas Gohr            }else{
1814ab889eaSAndreas Gohr                $item->title = $ditem['id'];
18203ee62cbSjoe.lapp            }
1834ab889eaSAndreas Gohr            if($conf['rss_show_summary'] && !empty($ditem['sum'])){
1844ab889eaSAndreas Gohr                $item->title .= ' - '.strip_tags($ditem['sum']);
185b1a1915cSandi            }
1864d58bd99Sandi
1874ab889eaSAndreas Gohr            // add item link
1884ab889eaSAndreas Gohr            switch ($opt['link_to']){
1894d58bd99Sandi                case 'page':
1904ab889eaSAndreas Gohr                    $item->link = wl($id,'rev='.$date,true,'&');
1914d58bd99Sandi                    break;
1924d58bd99Sandi                case 'rev':
1934ab889eaSAndreas Gohr                    $item->link = wl($id,'do=revisions&rev='.$date,true,'&');
1944d58bd99Sandi                    break;
19592e52d8dSjoe.lapp                case 'current':
1964ab889eaSAndreas Gohr                    $item->link = wl($id, '', true,'&');
19792e52d8dSjoe.lapp                    break;
19892e52d8dSjoe.lapp                case 'diff':
1994d58bd99Sandi                default:
2004ab889eaSAndreas Gohr                    $item->link = wl($id,'rev='.$date.'&do=diff',true,'&');
2014d58bd99Sandi            }
2024d58bd99Sandi
2034ab889eaSAndreas Gohr            // add item content
2044ab889eaSAndreas Gohr            switch ($opt['item_content']){
2054ab889eaSAndreas Gohr                case 'diff':
2064ab889eaSAndreas Gohr                case 'htmldiff':
2074ab889eaSAndreas Gohr                    require_once(DOKU_INC.'inc/DifferenceEngine.php');
2084ab889eaSAndreas Gohr                    $revs = getRevisions($id, 0, 1);
2094ab889eaSAndreas Gohr                    $rev = $revs[0];
2107a98db20Sjoe.lapp
2114ab889eaSAndreas Gohr                    if($rev){
2124ab889eaSAndreas Gohr                        $df  = new Diff(explode("\n",htmlspecialchars(rawWiki($id,$rev))),
2134ab889eaSAndreas Gohr                                        explode("\n",htmlspecialchars(rawWiki($id,''))));
2144ab889eaSAndreas Gohr                    }else{
2154ab889eaSAndreas Gohr                        $df  = new Diff(array(''),
2164ab889eaSAndreas Gohr                                        explode("\n",htmlspecialchars(rawWiki($id,''))));
2174ab889eaSAndreas Gohr                    }
2184ab889eaSAndreas Gohr
2194ab889eaSAndreas Gohr                    if($opt['item_content'] == 'htmldiff'){
2204ab889eaSAndreas Gohr                        $tdf = new TableDiffFormatter();
2214ab889eaSAndreas Gohr                        $content  = '<table>';
2224ab889eaSAndreas Gohr                        $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
2234ab889eaSAndreas Gohr                        $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
2244ab889eaSAndreas Gohr                        $content .= $tdf->format($df);
2254ab889eaSAndreas Gohr                        $content .= '</table>';
2264ab889eaSAndreas Gohr                    }else{
2274ab889eaSAndreas Gohr                        $udf = new UnifiedDiffFormatter();
2284ab889eaSAndreas Gohr                        $content = "<pre>\n".$udf->format($df)."\n</pre>";
2294ab889eaSAndreas Gohr                    }
2304ab889eaSAndreas Gohr                    break;
2314ab889eaSAndreas Gohr                case 'html':
2324ab889eaSAndreas Gohr                    $content = p_wiki_xhtml($id,$date,false);
2334ab889eaSAndreas Gohr                    // no TOC in feeds
2344ab889eaSAndreas Gohr                    $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s','',$content);
2354ab889eaSAndreas Gohr
2364ab889eaSAndreas Gohr                    // make URLs work when canonical is not set, regexp instead of rerendering!
2374ab889eaSAndreas Gohr                    if(!$conf['canonical']){
2384ab889eaSAndreas Gohr                        $base = preg_quote(DOKU_REL,'/');
2394ab889eaSAndreas Gohr                        $content = preg_replace('/(<a href|<img src)="('.$base.')/s','$1="'.DOKU_URL,$content);
2404ab889eaSAndreas Gohr                    }
2414ab889eaSAndreas Gohr
2424ab889eaSAndreas Gohr                    break;
2434ab889eaSAndreas Gohr                case 'abstract':
2444ab889eaSAndreas Gohr                default:
2454ab889eaSAndreas Gohr                    $content = $meta['description']['abstract'];
2464ab889eaSAndreas Gohr            }
2474ab889eaSAndreas Gohr            $item->description = $content; //FIXME a plugin hook here could be senseful
2484ab889eaSAndreas Gohr
2494ab889eaSAndreas Gohr            // add user
2504ab889eaSAndreas Gohr            # FIXME should the user be pulled from metadata as well?
2517a98db20Sjoe.lapp            $user = null;
2524ab889eaSAndreas Gohr            $user = @$ditem['user']; // the @ spares time repeating lookup
2537a98db20Sjoe.lapp            $item->author = '';
2540f4f4adfSAndreas Gohr            if($user && $conf['useacl'] && $auth){
255c0f9af6dSNathan Neulinger                $userInfo = $auth->getUserData($user);
256*681a59b2SGina Haeussge                if ($userInfo){
257*681a59b2SGina Haeussge                    switch ($conf['showuseras']){
258*681a59b2SGina Haeussge                        case 'username':
2597a98db20Sjoe.lapp                            $item->author = $userInfo['name'];
260*681a59b2SGina Haeussge                            break;
261*681a59b2SGina Haeussge                        default:
262*681a59b2SGina Haeussge                            $item->author = $user;
263*681a59b2SGina Haeussge                            break;
264*681a59b2SGina Haeussge                    }
265*681a59b2SGina Haeussge                } else {
266*681a59b2SGina Haeussge                    $item->author = $user;
267*681a59b2SGina Haeussge                }
268c1791678SAndreas Gohr                if($userInfo && !$opt['guardmail']){
269c1791678SAndreas Gohr                    $item->authorEmail = $userInfo['mail'];
270c1791678SAndreas Gohr                }else{
2717a98db20Sjoe.lapp                    //cannot obfuscate because some RSS readers may check validity
2720bda0363SAdrian Lang                    $item->authorEmail = $user.'@'.$ditem['ip'];
273f3f0262cSandi                }
274c5983034SAndreas Gohr            }elseif($user){
275c5983034SAndreas Gohr                // this happens when no ACL but some Apache auth is used
276c5983034SAndreas Gohr                $item->author      = $user;
2770bda0363SAdrian Lang                $item->authorEmail = $user.'@'.$ditem['ip'];
2787a98db20Sjoe.lapp            }else{
2790bda0363SAdrian Lang                $item->authorEmail = 'anonymous@'.$ditem['ip'];
2807a98db20Sjoe.lapp            }
2814ab889eaSAndreas Gohr
2824ab889eaSAndreas Gohr            // add category
2834ab889eaSAndreas Gohr            if($meta['subject']){
2844ab889eaSAndreas Gohr                $item->category = $meta['subject'];
2854ab889eaSAndreas Gohr            }else{
2864ab889eaSAndreas Gohr                $cat = getNS($id);
2874ab889eaSAndreas Gohr                if($cat) $item->category = $cat;
2884ab889eaSAndreas Gohr            }
2894ab889eaSAndreas Gohr
290883480fbSAndreas Gohr            // finally add the item to the feed object, after handing it to registered plugins
291883480fbSAndreas Gohr            $evdata = array('item'  => &$item,
292883480fbSAndreas Gohr                            'opt'   => &$opt,
293883480fbSAndreas Gohr                            'ditem' => &$ditem,
294883480fbSAndreas Gohr                            'rss'   => &$rss);
295209cd8e1SAndreas Gohr            $evt = new Doku_Event('FEED_ITEM_ADD', $evdata);
296883480fbSAndreas Gohr            if ($evt->advise_before()){
297f3f0262cSandi                $rss->addItem($item);
298f3f0262cSandi            }
299883480fbSAndreas Gohr            $evt->advise_after(); // for completeness
300883480fbSAndreas Gohr        }
301f3f0262cSandi    }
3024bf3df7cSGina Haeussge    $event->advise_after();
3034bf3df7cSGina Haeussge}
304f3f0262cSandi
3054ab889eaSAndreas Gohr
3064ab889eaSAndreas Gohr/**
3074bb1b5aeSAndreas Gohr * Add recent changed pages to the feed object
3084ab889eaSAndreas Gohr *
3094ab889eaSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3104ab889eaSAndreas Gohr */
3114bf3df7cSGina Haeussgefunction rssRecentChanges($opt){
3124ab889eaSAndreas Gohr    global $conf;
3134ab889eaSAndreas Gohr    global $auth;
3144ab889eaSAndreas Gohr
3154ab889eaSAndreas Gohr    $flags = RECENTS_SKIP_DELETED;
3164ab889eaSAndreas Gohr    if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS;
3174ab889eaSAndreas Gohr
3184ab889eaSAndreas Gohr    $recents = getRecents(0,$opt['items'],$opt['namespace'],$flags);
3194bf3df7cSGina Haeussge    return $recents;
3204ab889eaSAndreas Gohr}
3214ab889eaSAndreas Gohr
32215fae107Sandi/**
3234bb1b5aeSAndreas Gohr * Add all pages of a namespace to the feed object
32415fae107Sandi *
32515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
32615fae107Sandi */
3274bf3df7cSGina Haeussgefunction rssListNamespace($opt){
328f62ea8a1Sandi    require_once(DOKU_INC.'inc/search.php');
329f3f0262cSandi    global $conf;
330f3f0262cSandi
3314ab889eaSAndreas Gohr    $ns=':'.cleanID($opt['namespace']);
332f3f0262cSandi    $ns=str_replace(':','/',$ns);
333f3f0262cSandi
334f3f0262cSandi    $data = array();
335f3f0262cSandi    sort($data);
336f3f0262cSandi    search($data,$conf['datadir'],'search_list','',$ns);
33785cf8195SAndreas Gohr
3384bf3df7cSGina Haeussge    return $data;
33985cf8195SAndreas Gohr}
34085cf8195SAndreas Gohr
3414bb1b5aeSAndreas Gohr/**
3424bb1b5aeSAndreas Gohr * Add the result of a full text search to the feed object
3434bb1b5aeSAndreas Gohr *
3444bb1b5aeSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3454bb1b5aeSAndreas Gohr */
3464bf3df7cSGina Haeussgefunction rssSearch($opt){
3474bb1b5aeSAndreas Gohr    if(!$opt['search_query']) return;
3484ab889eaSAndreas Gohr
3494bb1b5aeSAndreas Gohr    require_once(DOKU_INC.'inc/fulltext.php');
3504bb1b5aeSAndreas Gohr    $data = array();
3514bb1b5aeSAndreas Gohr    $data = ft_pageSearch($opt['search_query'],$poswords);
3524bb1b5aeSAndreas Gohr    $data = array_keys($data);
3534bf3df7cSGina Haeussge
3544bf3df7cSGina Haeussge    return $data;
3554bb1b5aeSAndreas Gohr}
356f3f0262cSandi
3578716966dSAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
358