xref: /dokuwiki/feed.php (revision 30e171a066ac1f11bc699e4ed7309d868d9713f4)
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
9ed7b5f09Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__)).'/');
10ed7b5f09Sandi  require_once(DOKU_INC.'inc/init.php');
114d58bd99Sandi  require_once(DOKU_INC.'inc/common.php');
12*30e171a0Salexander.krause  require_once(DOKU_INC.'inc/events.php');
134d58bd99Sandi  require_once(DOKU_INC.'inc/parserutils.php');
144d58bd99Sandi  require_once(DOKU_INC.'inc/feedcreator.class.php');
154d58bd99Sandi  require_once(DOKU_INC.'inc/auth.php');
16fbf82939SBen Coburn  require_once(DOKU_INC.'inc/pageutils.php');
17f3f0262cSandi
187131b668SAndreas Gohr  //close session
198746e727Sandi  session_write_close();
208746e727Sandi
21f3f0262cSandi
22f3f0262cSandi  $num   = $_REQUEST['num'];
23f3f0262cSandi  $type  = $_REQUEST['type'];
24f3f0262cSandi  $mode  = $_REQUEST['mode'];
25b6912aeaSAndreas Gohr  $minor = $_REQUEST['minor'];
26f3f0262cSandi  $ns    = $_REQUEST['ns'];
274d58bd99Sandi  $ltype = $_REQUEST['linkto'];
28f3f0262cSandi
2931f1284dSjoe.lapp  if($type == '')
3031f1284dSjoe.lapp    $type = $conf['rss_type'];
3131f1284dSjoe.lapp
32f3f0262cSandi  switch ($type){
33f3f0262cSandi    case 'rss':
3426d75eefSAndreas Gohr       $type = 'RSS0.91';
3526d75eefSAndreas Gohr       $mime = 'text/xml';
36f3f0262cSandi       break;
37f3f0262cSandi    case 'rss2':
38f3f0262cSandi       $type = 'RSS2.0';
3926d75eefSAndreas Gohr       $mime = 'text/xml';
40f3f0262cSandi       break;
41f3f0262cSandi    case 'atom':
42f3f0262cSandi       $type = 'ATOM0.3';
4326d75eefSAndreas Gohr       $mime = 'application/xml';
4426d75eefSAndreas Gohr       break;
4526d75eefSAndreas Gohr    case 'atom1':
4626d75eefSAndreas Gohr       $type = 'ATOM1.0';
4726d75eefSAndreas Gohr       $mime = 'application/atom+xml';
48f3f0262cSandi       break;
49f3f0262cSandi    default:
50f3f0262cSandi       $type = 'RSS1.0';
5126d75eefSAndreas Gohr       $mime = 'application/xml';
52f3f0262cSandi  }
53f3f0262cSandi
547131b668SAndreas Gohr  // the feed is dynamic - we need a cache for each combo
557131b668SAndreas Gohr  // (but most people just use the default feed so it's still effective)
567131b668SAndreas Gohr  $cache = getCacheName($num.$type.$mode.$ns.$ltype.$_SERVER['REMOTE_USER'],'.feed');
57f3f0262cSandi
587131b668SAndreas Gohr  // check cacheage and deliver if nothing has changed since last
59fbf82939SBen Coburn  // time or the update interval has not passed, also handles conditional requests
60fbf82939SBen Coburn  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
61fbf82939SBen Coburn  header('Pragma: public');
627131b668SAndreas Gohr  header('Content-Type: application/xml; charset=utf-8');
63fbf82939SBen Coburn  $cmod = @filemtime($cache); // 0 if not exists
64fbf82939SBen Coburn  if($cmod && (($cmod+$conf['rss_update']>time()) || ($cmod>@filemtime($conf['changelog'])))){
65fbf82939SBen Coburn    http_conditionalRequest($cmod);
667131b668SAndreas Gohr    print io_readFile($cache);
677131b668SAndreas Gohr    exit;
68fbf82939SBen Coburn  } else {
69fbf82939SBen Coburn    http_conditionalRequest(time());
707131b668SAndreas Gohr  }
717131b668SAndreas Gohr
727131b668SAndreas Gohr  // create new feed
73f3f0262cSandi  $rss = new DokuWikiFeedCreator();
74dbb00abcSEsther Brunner  $rss->title = $conf['title'].(($ns) ? ' '.$ns : '');
75ed7b5f09Sandi  $rss->link  = DOKU_URL;
76f62ea8a1Sandi  $rss->syndicationURL = DOKU_URL.'feed.php';
77f62ea8a1Sandi  $rss->cssStyleSheet  = DOKU_URL.'lib/styles/feed.css';
78f3f0262cSandi
7979b608ceSandi  $image = new FeedImage();
8079b608ceSandi  $image->title = $conf['title'];
81f62ea8a1Sandi  $image->url = DOKU_URL."lib/images/favicon.ico";
8279b608ceSandi  $image->link = DOKU_URL;
8379b608ceSandi  $rss->image = $image;
8479b608ceSandi
85f3f0262cSandi  if($mode == 'list'){
86f3f0262cSandi    rssListNamespace($rss,$ns);
87f3f0262cSandi  }else{
88b6912aeaSAndreas Gohr    rssRecentChanges($rss,$num,$ltype,$ns,$minor);
89f3f0262cSandi  }
90f3f0262cSandi
917131b668SAndreas Gohr  $feed = $rss->createFeed($type,'utf-8');
927131b668SAndreas Gohr
937131b668SAndreas Gohr  // save cachefile
947131b668SAndreas Gohr  io_saveFile($cache,$feed);
957131b668SAndreas Gohr
967131b668SAndreas Gohr  // finally deliver
977131b668SAndreas Gohr  print $feed;
98f3f0262cSandi
9915fae107Sandi// ---------------------------------------------------------------- //
100f3f0262cSandi
10115fae107Sandi/**
10215fae107Sandi * Add recent changed to a feed object
10315fae107Sandi *
10415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
10515fae107Sandi */
106b6912aeaSAndreas Gohrfunction rssRecentChanges(&$rss,$num,$ltype,$ns,$minor){
107f62ea8a1Sandi  global $conf;
108c0f9af6dSNathan Neulinger  global $auth;
109c0f9af6dSNathan Neulinger
110f62ea8a1Sandi  if(!$num) $num = $conf['recent'];
1117a98db20Sjoe.lapp  $guardmail = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
112f62ea8a1Sandi
113b6912aeaSAndreas Gohr
114b6912aeaSAndreas Gohr  $flags = RECENTS_SKIP_DELETED;
115b6912aeaSAndreas Gohr  if(!$minor) $flags += RECENTS_SKIP_MINORS;
116b6912aeaSAndreas Gohr
117b6912aeaSAndreas Gohr  $recents = getRecents(0,$num,$ns,$flags);
118f62ea8a1Sandi
119f62ea8a1Sandi  //this can take some time if a lot of recaching has to be done
120f62ea8a1Sandi  @set_time_limit(90); // set max execution time
121f62ea8a1Sandi
122d437bcc4SAndreas Gohr  foreach($recents as $recent){
12303ee62cbSjoe.lapp
124f3f0262cSandi    $item = new FeedItem();
125d437bcc4SAndreas Gohr    $item->title = $recent['id'];
126d437bcc4SAndreas Gohr    $xhtml = p_wiki_xhtml($recent['id'],'',false);
12703ee62cbSjoe.lapp
12803ee62cbSjoe.lapp    if($conf['useheading']) {
12903ee62cbSjoe.lapp        $matches = array();
13003ee62cbSjoe.lapp        if(preg_match('|<h([1-9])>(.*?)</h\1>|', $xhtml, $matches))
13103ee62cbSjoe.lapp            $item->title = trim($matches[2]);
13203ee62cbSjoe.lapp    }
133d437bcc4SAndreas Gohr    if(!empty($recent['sum'])){
134d437bcc4SAndreas Gohr      $item->title .= ' - '.strip_tags($recent['sum']);
135b1a1915cSandi    }
1364d58bd99Sandi
13703ee62cbSjoe.lapp    $desc = cleanDesc($xhtml);
13803ee62cbSjoe.lapp
13992e52d8dSjoe.lapp    if(empty($ltype))
14092e52d8dSjoe.lapp      $ltype = $conf['rss_linkto'];
14192e52d8dSjoe.lapp
1424d58bd99Sandi    switch ($ltype){
1434d58bd99Sandi      case 'page':
144d437bcc4SAndreas Gohr        $item->link = wl($recent['id'],'rev='.$recent['date'],true);
1454d58bd99Sandi        break;
1464d58bd99Sandi      case 'rev':
147aa11db09SAndreas Gohr        $item->link = wl($recent['id'],'do=revisions&rev='.$recent['date'],true);
1484d58bd99Sandi        break;
14992e52d8dSjoe.lapp      case 'current':
150d437bcc4SAndreas Gohr        $item->link = wl($recent['id'], '', true);
15192e52d8dSjoe.lapp        break;
15292e52d8dSjoe.lapp      case 'diff':
1534d58bd99Sandi      default:
15437beb806SAndreas Gohr        $item->link = wl($recent['id'],'rev='.$recent['date'].'&do=diff'.$recent['date'],true);
1554d58bd99Sandi    }
1564d58bd99Sandi
157f3f0262cSandi    $item->description = $desc;
158d437bcc4SAndreas Gohr    $item->date        = date('r',$recent['date']);
159d437bcc4SAndreas Gohr    $cat = getNS($recent['id']);
160d437bcc4SAndreas Gohr    if($cat) $item->category = $cat;
1617a98db20Sjoe.lapp
1627a98db20Sjoe.lapp    $user = null;
163d437bcc4SAndreas Gohr    $user = @$recent['user']; // the @ spares time repeating lookup
1647a98db20Sjoe.lapp    $item->author = '';
1657a98db20Sjoe.lapp
1667a98db20Sjoe.lapp    if($user){
167c0f9af6dSNathan Neulinger      $userInfo = $auth->getUserData($user);
1687a98db20Sjoe.lapp      $item->author = $userInfo['name'];
1697a98db20Sjoe.lapp      if($guardmail) {
1707a98db20Sjoe.lapp        //cannot obfuscate because some RSS readers may check validity
171d437bcc4SAndreas Gohr        $item->authorEmail = $user.'@'.$recent['ip'];
172f3f0262cSandi      }else{
1737a98db20Sjoe.lapp        $item->authorEmail = $userInfo['mail'];
174f3f0262cSandi      }
1757a98db20Sjoe.lapp    }else{
176d437bcc4SAndreas Gohr      $item->authorEmail = 'anonymous@'.$recent['ip'];
1777a98db20Sjoe.lapp    }
178f3f0262cSandi    $rss->addItem($item);
179f3f0262cSandi  }
180f3f0262cSandi}
181f3f0262cSandi
18215fae107Sandi/**
18315fae107Sandi * Add all pages of a namespace to a feedobject
18415fae107Sandi *
18515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
18615fae107Sandi */
187f3f0262cSandifunction rssListNamespace(&$rss,$ns){
188f62ea8a1Sandi  require_once(DOKU_INC.'inc/search.php');
189f3f0262cSandi  global $conf;
190f3f0262cSandi
191f3f0262cSandi  $ns=':'.cleanID($ns);
192f3f0262cSandi  $ns=str_replace(':','/',$ns);
193f3f0262cSandi
194f3f0262cSandi  $data = array();
195f3f0262cSandi  sort($data);
196f3f0262cSandi  search($data,$conf['datadir'],'search_list','',$ns);
197f3f0262cSandi  foreach($data as $row){
19885cf8195SAndreas Gohr    $item = new FeedItem();
19985cf8195SAndreas Gohr
200f3f0262cSandi    $id    = $row['id'];
201f3f0262cSandi    $date  = filemtime(wikiFN($id));
20285cf8195SAndreas Gohr    $xhtml = p_wiki_xhtml($id,'',false);
20385cf8195SAndreas Gohr    $desc  = cleanDesc($xhtml);
204f3f0262cSandi    $item->title       = $id;
20585cf8195SAndreas Gohr
20685cf8195SAndreas Gohr    if($conf['useheading']) {
20785cf8195SAndreas Gohr        $matches = array();
20885cf8195SAndreas Gohr        if(preg_match('|<h([1-9])>(.*?)</h\1>|', $xhtml, $matches))
20985cf8195SAndreas Gohr            $item->title = trim($matches[2]);
21085cf8195SAndreas Gohr    }
21185cf8195SAndreas Gohr
212ed7b5f09Sandi    $item->link        = wl($id,'rev='.$date,true);
213f3f0262cSandi    $item->description = $desc;
214f3f0262cSandi    $item->date        = date('r',$date);
215f3f0262cSandi    $rss->addItem($item);
216f3f0262cSandi  }
217f3f0262cSandi}
218f3f0262cSandi
21915fae107Sandi/**
22015fae107Sandi * Clean description for feed inclusion
22115fae107Sandi *
22215fae107Sandi * Removes HTML tags and line breaks and trims the text to
22315fae107Sandi * 250 chars
22415fae107Sandi *
22515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
22615fae107Sandi */
227f3f0262cSandifunction cleanDesc($desc){
228d190794cSjoe.lapp  //start description at text of first paragraph
229d190794cSjoe.lapp  $matches = array();
230d190794cSjoe.lapp  if(preg_match('/<p>|<p\s.*?>/', $desc, $matches, PREG_OFFSET_CAPTURE))
231d190794cSjoe.lapp      $desc = substr($desc, $matches[0][1]);
232d190794cSjoe.lapp
233f3f0262cSandi  //remove TOC
2344d58bd99Sandi  $desc = preg_replace('!<div class="toc">.*?(</div>\n</div>)!s','',$desc);
235f3f0262cSandi  $desc = strip_tags($desc);
236f3f0262cSandi  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
237f3f0262cSandi  $desc = preg_replace('/  /',' ',$desc);
2387d8be200Sandi  $desc = utf8_substr($desc,0,250);
239f3f0262cSandi  $desc = $desc.'...';
240f3f0262cSandi  return $desc;
241f3f0262cSandi}
242f3f0262cSandi
243f3f0262cSandi?>
244