xref: /dokuwiki/feed.php (revision 0d67055cfede37804a2ad231c821d87ea6bc4e3e)
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');
28*0d67055cSMichael Klier$key   = join('', array_values($opt)) . $_SERVER['REMOTE_USER'];
29*0d67055cSMichael Klier$cache = new cache($key, '.feed');
30*0d67055cSMichael Klier
31*0d67055cSMichael Klier// prepare cache depends
32*0d67055cSMichael Klier$depends['files'] = getConfigFiles('main');
33*0d67055cSMichael Klier$depends['age']   = $conf['rss_update'];
34*0d67055cSMichael Klier$depends['purge'] = ($_REQUEST['purge']) ? true : false;
35f3f0262cSandi
367131b668SAndreas Gohr// check cacheage and deliver if nothing has changed since last
37fbf82939SBen Coburn// time or the update interval has not passed, also handles conditional requests
38fbf82939SBen Coburnheader('Cache-Control: must-revalidate, post-check=0, pre-check=0');
39fbf82939SBen Coburnheader('Pragma: public');
407131b668SAndreas Gohrheader('Content-Type: application/xml; charset=utf-8');
4150ddb617SAndreas Gohrheader('X-Robots-Tag: noindex');
42*0d67055cSMichael Klierif($cache->useCache($depends)) {
43*0d67055cSMichael Klier    http_conditionalRequest($cache->_time);
44*0d67055cSMichael Klier    if($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
45*0d67055cSMichael Klier    print $cache->retrieveCache();
467131b668SAndreas Gohr    exit;
47fbf82939SBen Coburn} else {
48fbf82939SBen Coburn    http_conditionalRequest(time());
497131b668SAndreas Gohr }
507131b668SAndreas Gohr
517131b668SAndreas Gohr// create new feed
52f3f0262cSandi$rss = new DokuWikiFeedCreator();
534ab889eaSAndreas Gohr$rss->title = $conf['title'].(($opt['namespace']) ? ' '.$opt['namespace'] : '');
54ed7b5f09Sandi$rss->link  = DOKU_URL;
55f62ea8a1Sandi$rss->syndicationURL = DOKU_URL.'feed.php';
56615960feSTom N Harris$rss->cssStyleSheet  = DOKU_URL.'lib/exe/css.php?s=feed';
57f3f0262cSandi
5879b608ceSandi$image = new FeedImage();
5979b608ceSandi$image->title = $conf['title'];
60f62ea8a1Sandi$image->url = DOKU_URL."lib/images/favicon.ico";
6179b608ceSandi$image->link = DOKU_URL;
6279b608ceSandi$rss->image = $image;
6379b608ceSandi
644bf3df7cSGina Haeussge$data = null;
654ab889eaSAndreas Gohrif($opt['feed_mode'] == 'list'){
664bf3df7cSGina Haeussge    $data = rssListNamespace($opt);
674bb1b5aeSAndreas Gohr}elseif($opt['feed_mode'] == 'search'){
684bf3df7cSGina Haeussge    $data = rssSearch($opt);
69f3f0262cSandi}else{
704bf3df7cSGina Haeussge    $eventData = array(
714bf3df7cSGina Haeussge        'opt'  => &$opt,
724bf3df7cSGina Haeussge        'data' => &$data,
734bf3df7cSGina Haeussge    );
744bf3df7cSGina Haeussge    $event = new Doku_Event('FEED_MODE_UNKNOWN', $eventData);
754bf3df7cSGina Haeussge    if ($event->advise_before(true)) {
764bf3df7cSGina Haeussge        $data = rssRecentChanges($opt);
774bf3df7cSGina Haeussge    }
784bf3df7cSGina Haeussge    $event->advise_after();
79f3f0262cSandi}
80f3f0262cSandi
814bf3df7cSGina Haeussgerss_buildItems($rss, $data, $opt);
824ab889eaSAndreas Gohr$feed = $rss->createFeed($opt['feed_type'],'utf-8');
837131b668SAndreas Gohr
847131b668SAndreas Gohr// save cachefile
85*0d67055cSMichael Klier$cache->storeCache($feed);
867131b668SAndreas Gohr
877131b668SAndreas Gohr// finally deliver
887131b668SAndreas Gohrprint $feed;
89f3f0262cSandi
9015fae107Sandi// ---------------------------------------------------------------- //
91f3f0262cSandi
9215fae107Sandi/**
934ab889eaSAndreas Gohr * Get URL parameters and config options and return a initialized option array
9415fae107Sandi *
9515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
9615fae107Sandi */
974ab889eaSAndreas Gohrfunction rss_parseOptions(){
98f62ea8a1Sandi    global $conf;
99c0f9af6dSNathan Neulinger
1004ab889eaSAndreas Gohr    $opt['items']        = (int) $_REQUEST['num'];
1014ab889eaSAndreas Gohr    $opt['feed_type']    = $_REQUEST['type'];
1024ab889eaSAndreas Gohr    $opt['feed_mode']    = $_REQUEST['mode'];
1034ab889eaSAndreas Gohr    $opt['show_minor']   = $_REQUEST['minor'];
1044ab889eaSAndreas Gohr    $opt['namespace']    = $_REQUEST['ns'];
1054ab889eaSAndreas Gohr    $opt['link_to']      = $_REQUEST['linkto'];
1064ab889eaSAndreas Gohr    $opt['item_content'] = $_REQUEST['content'];
1074bb1b5aeSAndreas Gohr    $opt['search_query'] = $_REQUEST['q'];
108f62ea8a1Sandi
1092f1faf49SAndreas Gohr    if(!$opt['feed_type'])    $opt['feed_type']    = $conf['rss_type'];
1102f1faf49SAndreas Gohr    if(!$opt['item_content']) $opt['item_content'] = $conf['rss_content'];
1112f1faf49SAndreas Gohr    if(!$opt['link_to'])      $opt['link_to']      = $conf['rss_linkto'];
1124ab889eaSAndreas Gohr    if(!$opt['items'])        $opt['items']        = $conf['recent'];
1134ab889eaSAndreas Gohr    $opt['guardmail']  = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
114b6912aeaSAndreas Gohr
1154ab889eaSAndreas Gohr    switch ($opt['feed_type']){
1164ab889eaSAndreas Gohr        case 'rss':
1174ab889eaSAndreas Gohr            $opt['feed_type'] = 'RSS0.91';
1184ab889eaSAndreas Gohr            $opt['mime_type'] = 'text/xml';
1194ab889eaSAndreas Gohr            break;
1204ab889eaSAndreas Gohr        case 'rss2':
1214ab889eaSAndreas Gohr            $opt['feed_type'] = 'RSS2.0';
1224ab889eaSAndreas Gohr            $opt['mime_type'] = 'text/xml';
1234ab889eaSAndreas Gohr            break;
1244ab889eaSAndreas Gohr        case 'atom':
1254ab889eaSAndreas Gohr            $opt['feed_type'] = 'ATOM0.3';
1264ab889eaSAndreas Gohr            $opt['mime_type'] = 'application/xml';
1274ab889eaSAndreas Gohr            break;
1284ab889eaSAndreas Gohr        case 'atom1':
1294ab889eaSAndreas Gohr            $opt['feed_type'] = 'ATOM1.0';
1304ab889eaSAndreas Gohr            $opt['mime_type'] = 'application/atom+xml';
1314ab889eaSAndreas Gohr            break;
1324ab889eaSAndreas Gohr        default:
1334ab889eaSAndreas Gohr            $opt['feed_type'] = 'RSS1.0';
1344ab889eaSAndreas Gohr            $opt['mime_type'] = 'application/xml';
1354ab889eaSAndreas Gohr    }
1364bf3df7cSGina Haeussge
1374bf3df7cSGina Haeussge    $eventData = array(
1384bf3df7cSGina Haeussge        'opt' => &$opt,
1394bf3df7cSGina Haeussge    );
1404bf3df7cSGina Haeussge    trigger_event('FEED_OPTS_POSTPROCESS', $eventData);
1414ab889eaSAndreas Gohr    return $opt;
1424ab889eaSAndreas Gohr}
143b6912aeaSAndreas Gohr
1444ab889eaSAndreas Gohr/**
1454ab889eaSAndreas Gohr * Add recent changed pages to a feed object
1464ab889eaSAndreas Gohr *
1474ab889eaSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1484ab889eaSAndreas Gohr * @param  object $rss - the FeedCreator Object
1494ab889eaSAndreas Gohr * @param  array $data - the items to add
1504ab889eaSAndreas Gohr * @param  array $opt  - the feed options
1514ab889eaSAndreas Gohr */
1524ab889eaSAndreas Gohrfunction rss_buildItems(&$rss,&$data,$opt){
1534ab889eaSAndreas Gohr    global $conf;
1544ab889eaSAndreas Gohr    global $lang;
1553d581c29SAndreas Gohr    global $auth;
156883480fbSAndreas Gohr
1574bf3df7cSGina Haeussge    $eventData = array(
1584bf3df7cSGina Haeussge        'rss' => &$rss,
1594bf3df7cSGina Haeussge        'data' => &$data,
1604bf3df7cSGina Haeussge        'opt' => &$opt,
1614bf3df7cSGina Haeussge    );
1624bf3df7cSGina Haeussge    $event = new Doku_Event('FEED_DATA_PROCESS', $eventData);
1634bf3df7cSGina Haeussge    if ($event->advise_before(false)){
1644ab889eaSAndreas Gohr        foreach($data as $ditem){
1654bb1b5aeSAndreas Gohr            if(!is_array($ditem)){
1664bb1b5aeSAndreas Gohr                // not an array? then only a list of IDs was given
1674bb1b5aeSAndreas Gohr                $ditem = array( 'id' => $ditem );
1684bb1b5aeSAndreas Gohr            }
1694bb1b5aeSAndreas Gohr
170f3f0262cSandi            $item = new FeedItem();
1714ab889eaSAndreas Gohr            $id   = $ditem['id'];
1724ab889eaSAndreas Gohr            $meta = p_get_metadata($id);
17303ee62cbSjoe.lapp
1744ab889eaSAndreas Gohr            // add date
1754ab889eaSAndreas Gohr            if($ditem['date']){
1764ab889eaSAndreas Gohr                $date = $ditem['date'];
1774ab889eaSAndreas Gohr            }elseif($meta['date']['modified']){
1784ab889eaSAndreas Gohr                $date = $meta['date']['modified'];
1794ab889eaSAndreas Gohr            }else{
1804ab889eaSAndreas Gohr                $date = @filemtime(wikiFN($id));
1814ab889eaSAndreas Gohr            }
1824ab889eaSAndreas Gohr            if($date) $item->date = date('r',$date);
1834ab889eaSAndreas Gohr
1844ab889eaSAndreas Gohr            // add title
1858716966dSAndreas Gohr            if($conf['useheading'] && $meta['title']){
1868716966dSAndreas Gohr                $item->title = $meta['title'];
1878716966dSAndreas Gohr            }else{
1884ab889eaSAndreas Gohr                $item->title = $ditem['id'];
18903ee62cbSjoe.lapp            }
1904ab889eaSAndreas Gohr            if($conf['rss_show_summary'] && !empty($ditem['sum'])){
1914ab889eaSAndreas Gohr                $item->title .= ' - '.strip_tags($ditem['sum']);
192b1a1915cSandi            }
1934d58bd99Sandi
1944ab889eaSAndreas Gohr            // add item link
1954ab889eaSAndreas Gohr            switch ($opt['link_to']){
1964d58bd99Sandi                case 'page':
1974ab889eaSAndreas Gohr                    $item->link = wl($id,'rev='.$date,true,'&');
1984d58bd99Sandi                    break;
1994d58bd99Sandi                case 'rev':
2004ab889eaSAndreas Gohr                    $item->link = wl($id,'do=revisions&rev='.$date,true,'&');
2014d58bd99Sandi                    break;
20292e52d8dSjoe.lapp                case 'current':
2034ab889eaSAndreas Gohr                    $item->link = wl($id, '', true,'&');
20492e52d8dSjoe.lapp                    break;
20592e52d8dSjoe.lapp                case 'diff':
2064d58bd99Sandi                default:
2074ab889eaSAndreas Gohr                    $item->link = wl($id,'rev='.$date.'&do=diff',true,'&');
2084d58bd99Sandi            }
2094d58bd99Sandi
2104ab889eaSAndreas Gohr            // add item content
2114ab889eaSAndreas Gohr            switch ($opt['item_content']){
2124ab889eaSAndreas Gohr                case 'diff':
2134ab889eaSAndreas Gohr                case 'htmldiff':
2144ab889eaSAndreas Gohr                    require_once(DOKU_INC.'inc/DifferenceEngine.php');
2154ab889eaSAndreas Gohr                    $revs = getRevisions($id, 0, 1);
2164ab889eaSAndreas Gohr                    $rev = $revs[0];
2177a98db20Sjoe.lapp
2184ab889eaSAndreas Gohr                    if($rev){
2194ab889eaSAndreas Gohr                        $df  = new Diff(explode("\n",htmlspecialchars(rawWiki($id,$rev))),
2204ab889eaSAndreas Gohr                                        explode("\n",htmlspecialchars(rawWiki($id,''))));
2214ab889eaSAndreas Gohr                    }else{
2224ab889eaSAndreas Gohr                        $df  = new Diff(array(''),
2234ab889eaSAndreas Gohr                                        explode("\n",htmlspecialchars(rawWiki($id,''))));
2244ab889eaSAndreas Gohr                    }
2254ab889eaSAndreas Gohr
2264ab889eaSAndreas Gohr                    if($opt['item_content'] == 'htmldiff'){
2274ab889eaSAndreas Gohr                        $tdf = new TableDiffFormatter();
2284ab889eaSAndreas Gohr                        $content  = '<table>';
2294ab889eaSAndreas Gohr                        $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
2304ab889eaSAndreas Gohr                        $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
2314ab889eaSAndreas Gohr                        $content .= $tdf->format($df);
2324ab889eaSAndreas Gohr                        $content .= '</table>';
2334ab889eaSAndreas Gohr                    }else{
2344ab889eaSAndreas Gohr                        $udf = new UnifiedDiffFormatter();
2354ab889eaSAndreas Gohr                        $content = "<pre>\n".$udf->format($df)."\n</pre>";
2364ab889eaSAndreas Gohr                    }
2374ab889eaSAndreas Gohr                    break;
2384ab889eaSAndreas Gohr                case 'html':
2394ab889eaSAndreas Gohr                    $content = p_wiki_xhtml($id,$date,false);
2404ab889eaSAndreas Gohr                    // no TOC in feeds
2414ab889eaSAndreas Gohr                    $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s','',$content);
2424ab889eaSAndreas Gohr
2434ab889eaSAndreas Gohr                    // make URLs work when canonical is not set, regexp instead of rerendering!
2444ab889eaSAndreas Gohr                    if(!$conf['canonical']){
2454ab889eaSAndreas Gohr                        $base = preg_quote(DOKU_REL,'/');
2464ab889eaSAndreas Gohr                        $content = preg_replace('/(<a href|<img src)="('.$base.')/s','$1="'.DOKU_URL,$content);
2474ab889eaSAndreas Gohr                    }
2484ab889eaSAndreas Gohr
2494ab889eaSAndreas Gohr                    break;
2504ab889eaSAndreas Gohr                case 'abstract':
2514ab889eaSAndreas Gohr                default:
2524ab889eaSAndreas Gohr                    $content = $meta['description']['abstract'];
2534ab889eaSAndreas Gohr            }
2544ab889eaSAndreas Gohr            $item->description = $content; //FIXME a plugin hook here could be senseful
2554ab889eaSAndreas Gohr
2564ab889eaSAndreas Gohr            // add user
2574ab889eaSAndreas Gohr            # FIXME should the user be pulled from metadata as well?
2587a98db20Sjoe.lapp            $user = null;
2594ab889eaSAndreas Gohr            $user = @$ditem['user']; // the @ spares time repeating lookup
2607a98db20Sjoe.lapp            $item->author = '';
2610f4f4adfSAndreas Gohr            if($user && $conf['useacl'] && $auth){
262c0f9af6dSNathan Neulinger                $userInfo = $auth->getUserData($user);
2637a98db20Sjoe.lapp                $item->author = $userInfo['name'];
264c1791678SAndreas Gohr                if($userInfo && !$opt['guardmail']){
265c1791678SAndreas Gohr                    $item->authorEmail = $userInfo['mail'];
266c1791678SAndreas Gohr                }else{
2677a98db20Sjoe.lapp                    //cannot obfuscate because some RSS readers may check validity
268d437bcc4SAndreas Gohr                    $item->authorEmail = $user.'@'.$recent['ip'];
269f3f0262cSandi                }
270c5983034SAndreas Gohr            }elseif($user){
271c5983034SAndreas Gohr                // this happens when no ACL but some Apache auth is used
272c5983034SAndreas Gohr                $item->author      = $user;
273c5983034SAndreas Gohr                $item->authorEmail = $user.'@'.$recent['ip'];
2747a98db20Sjoe.lapp            }else{
275d437bcc4SAndreas Gohr                $item->authorEmail = 'anonymous@'.$recent['ip'];
2767a98db20Sjoe.lapp            }
2774ab889eaSAndreas Gohr
2784ab889eaSAndreas Gohr            // add category
2794ab889eaSAndreas Gohr            if($meta['subject']){
2804ab889eaSAndreas Gohr                $item->category = $meta['subject'];
2814ab889eaSAndreas Gohr            }else{
2824ab889eaSAndreas Gohr                $cat = getNS($id);
2834ab889eaSAndreas Gohr                if($cat) $item->category = $cat;
2844ab889eaSAndreas Gohr            }
2854ab889eaSAndreas Gohr
286883480fbSAndreas Gohr            // finally add the item to the feed object, after handing it to registered plugins
287883480fbSAndreas Gohr            $evdata = array('item'  => &$item,
288883480fbSAndreas Gohr                            'opt'   => &$opt,
289883480fbSAndreas Gohr                            'ditem' => &$ditem,
290883480fbSAndreas Gohr                            'rss'   => &$rss);
291209cd8e1SAndreas Gohr            $evt = new Doku_Event('FEED_ITEM_ADD', $evdata);
292883480fbSAndreas Gohr            if ($evt->advise_before()){
293f3f0262cSandi                $rss->addItem($item);
294f3f0262cSandi            }
295883480fbSAndreas Gohr            $evt->advise_after(); // for completeness
296883480fbSAndreas Gohr        }
297f3f0262cSandi    }
2984bf3df7cSGina Haeussge    $event->advise_after();
2994bf3df7cSGina Haeussge}
300f3f0262cSandi
3014ab889eaSAndreas Gohr
3024ab889eaSAndreas Gohr/**
3034bb1b5aeSAndreas Gohr * Add recent changed pages to the feed object
3044ab889eaSAndreas Gohr *
3054ab889eaSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3064ab889eaSAndreas Gohr */
3074bf3df7cSGina Haeussgefunction rssRecentChanges($opt){
3084ab889eaSAndreas Gohr    global $conf;
3094ab889eaSAndreas Gohr    global $auth;
3104ab889eaSAndreas Gohr
3114ab889eaSAndreas Gohr    $flags = RECENTS_SKIP_DELETED;
3124ab889eaSAndreas Gohr    if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS;
3134ab889eaSAndreas Gohr
3144ab889eaSAndreas Gohr    $recents = getRecents(0,$opt['items'],$opt['namespace'],$flags);
3154bf3df7cSGina Haeussge    return $recents;
3164ab889eaSAndreas Gohr}
3174ab889eaSAndreas Gohr
31815fae107Sandi/**
3194bb1b5aeSAndreas Gohr * Add all pages of a namespace to the feed object
32015fae107Sandi *
32115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
32215fae107Sandi */
3234bf3df7cSGina Haeussgefunction rssListNamespace($opt){
324f62ea8a1Sandi    require_once(DOKU_INC.'inc/search.php');
325f3f0262cSandi    global $conf;
326f3f0262cSandi
3274ab889eaSAndreas Gohr    $ns=':'.cleanID($opt['namespace']);
328f3f0262cSandi    $ns=str_replace(':','/',$ns);
329f3f0262cSandi
330f3f0262cSandi    $data = array();
331f3f0262cSandi    sort($data);
332f3f0262cSandi    search($data,$conf['datadir'],'search_list','',$ns);
33385cf8195SAndreas Gohr
3344bf3df7cSGina Haeussge    return $data;
33585cf8195SAndreas Gohr}
33685cf8195SAndreas Gohr
3374bb1b5aeSAndreas Gohr/**
3384bb1b5aeSAndreas Gohr * Add the result of a full text search to the feed object
3394bb1b5aeSAndreas Gohr *
3404bb1b5aeSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3414bb1b5aeSAndreas Gohr */
3424bf3df7cSGina Haeussgefunction rssSearch($opt){
3434bb1b5aeSAndreas Gohr    if(!$opt['search_query']) return;
3444ab889eaSAndreas Gohr
3454bb1b5aeSAndreas Gohr    require_once(DOKU_INC.'inc/fulltext.php');
3464bb1b5aeSAndreas Gohr    $data = array();
3474bb1b5aeSAndreas Gohr    $data = ft_pageSearch($opt['search_query'],$poswords);
3484bb1b5aeSAndreas Gohr    $data = array_keys($data);
3494bf3df7cSGina Haeussge
3504bf3df7cSGina Haeussge    return $data;
3514bb1b5aeSAndreas Gohr}
352f3f0262cSandi
3538716966dSAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
354