xref: /dokuwiki/feed.php (revision 26d75eef3ac77f55d63b06c86be12de47fcdf4ac)
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');
124d58bd99Sandi  require_once(DOKU_INC.'inc/parserutils.php');
134d58bd99Sandi  require_once(DOKU_INC.'inc/feedcreator.class.php');
144d58bd99Sandi  require_once(DOKU_INC.'inc/auth.php');
15fbf82939SBen Coburn  require_once(DOKU_INC.'inc/pageutils.php');
16f3f0262cSandi
177131b668SAndreas Gohr  //close session
188746e727Sandi  session_write_close();
198746e727Sandi
20f3f0262cSandi
21f3f0262cSandi  $num   = $_REQUEST['num'];
22f3f0262cSandi  $type  = $_REQUEST['type'];
23f3f0262cSandi  $mode  = $_REQUEST['mode'];
24b6912aeaSAndreas Gohr  $minor = $_REQUEST['minor'];
25f3f0262cSandi  $ns    = $_REQUEST['ns'];
264d58bd99Sandi  $ltype = $_REQUEST['linkto'];
27f3f0262cSandi
2831f1284dSjoe.lapp  if($type == '')
2931f1284dSjoe.lapp    $type = $conf['rss_type'];
3031f1284dSjoe.lapp
31f3f0262cSandi  switch ($type){
32f3f0262cSandi    case 'rss':
33*26d75eefSAndreas Gohr       $type = 'RSS0.91';
34*26d75eefSAndreas Gohr       $mime = 'text/xml';
35f3f0262cSandi       break;
36f3f0262cSandi    case 'rss2':
37f3f0262cSandi       $type = 'RSS2.0';
38*26d75eefSAndreas Gohr       $mime = 'text/xml';
39f3f0262cSandi       break;
40f3f0262cSandi    case 'atom':
41f3f0262cSandi       $type = 'ATOM0.3';
42*26d75eefSAndreas Gohr       $mime = 'application/xml';
43*26d75eefSAndreas Gohr       break;
44*26d75eefSAndreas Gohr    case 'atom1':
45*26d75eefSAndreas Gohr       $type = 'ATOM1.0';
46*26d75eefSAndreas Gohr       $mime = 'application/atom+xml';
47f3f0262cSandi       break;
48f3f0262cSandi    default:
49f3f0262cSandi       $type = 'RSS1.0';
50*26d75eefSAndreas Gohr       $mime = 'application/xml';
51f3f0262cSandi  }
52f3f0262cSandi
537131b668SAndreas Gohr  // the feed is dynamic - we need a cache for each combo
547131b668SAndreas Gohr  // (but most people just use the default feed so it's still effective)
557131b668SAndreas Gohr  $cache = getCacheName($num.$type.$mode.$ns.$ltype.$_SERVER['REMOTE_USER'],'.feed');
56f3f0262cSandi
577131b668SAndreas Gohr  // check cacheage and deliver if nothing has changed since last
58fbf82939SBen Coburn  // time or the update interval has not passed, also handles conditional requests
59fbf82939SBen Coburn  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
60fbf82939SBen Coburn  header('Pragma: public');
617131b668SAndreas Gohr  header('Content-Type: application/xml; charset=utf-8');
62fbf82939SBen Coburn  $cmod = @filemtime($cache); // 0 if not exists
63fbf82939SBen Coburn  if($cmod && (($cmod+$conf['rss_update']>time()) || ($cmod>@filemtime($conf['changelog'])))){
64fbf82939SBen Coburn    http_conditionalRequest($cmod);
657131b668SAndreas Gohr    print io_readFile($cache);
667131b668SAndreas Gohr    exit;
67fbf82939SBen Coburn  } else {
68fbf82939SBen Coburn    http_conditionalRequest(time());
697131b668SAndreas Gohr  }
707131b668SAndreas Gohr
717131b668SAndreas Gohr  // create new feed
72f3f0262cSandi  $rss = new DokuWikiFeedCreator();
73dbb00abcSEsther Brunner  $rss->title = $conf['title'].(($ns) ? ' '.$ns : '');
74ed7b5f09Sandi  $rss->link  = DOKU_URL;
75f62ea8a1Sandi  $rss->syndicationURL = DOKU_URL.'feed.php';
76f62ea8a1Sandi  $rss->cssStyleSheet  = DOKU_URL.'lib/styles/feed.css';
77f3f0262cSandi
7879b608ceSandi  $image = new FeedImage();
7979b608ceSandi  $image->title = $conf['title'];
80f62ea8a1Sandi  $image->url = DOKU_URL."lib/images/favicon.ico";
8179b608ceSandi  $image->link = DOKU_URL;
8279b608ceSandi  $rss->image = $image;
8379b608ceSandi
84f3f0262cSandi  if($mode == 'list'){
85f3f0262cSandi    rssListNamespace($rss,$ns);
86f3f0262cSandi  }else{
87b6912aeaSAndreas Gohr    rssRecentChanges($rss,$num,$ltype,$ns,$minor);
88f3f0262cSandi  }
89f3f0262cSandi
907131b668SAndreas Gohr  $feed = $rss->createFeed($type,'utf-8');
917131b668SAndreas Gohr
927131b668SAndreas Gohr  // save cachefile
937131b668SAndreas Gohr  io_saveFile($cache,$feed);
947131b668SAndreas Gohr
957131b668SAndreas Gohr  // finally deliver
967131b668SAndreas Gohr  print $feed;
97f3f0262cSandi
9815fae107Sandi// ---------------------------------------------------------------- //
99f3f0262cSandi
10015fae107Sandi/**
10115fae107Sandi * Add recent changed to a feed object
10215fae107Sandi *
10315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
10415fae107Sandi */
105b6912aeaSAndreas Gohrfunction rssRecentChanges(&$rss,$num,$ltype,$ns,$minor){
106f62ea8a1Sandi  global $conf;
107c0f9af6dSNathan Neulinger  global $auth;
108c0f9af6dSNathan Neulinger
109f62ea8a1Sandi  if(!$num) $num = $conf['recent'];
1107a98db20Sjoe.lapp  $guardmail = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
111f62ea8a1Sandi
112b6912aeaSAndreas Gohr
113b6912aeaSAndreas Gohr  $flags = RECENTS_SKIP_DELETED;
114b6912aeaSAndreas Gohr  if(!$minor) $flags += RECENTS_SKIP_MINORS;
115b6912aeaSAndreas Gohr
116b6912aeaSAndreas Gohr  $recents = getRecents(0,$num,$ns,$flags);
117f62ea8a1Sandi
118f62ea8a1Sandi  //this can take some time if a lot of recaching has to be done
119f62ea8a1Sandi  @set_time_limit(90); // set max execution time
120f62ea8a1Sandi
121d437bcc4SAndreas Gohr  foreach($recents as $recent){
12203ee62cbSjoe.lapp
123f3f0262cSandi    $item = new FeedItem();
124d437bcc4SAndreas Gohr    $item->title = $recent['id'];
125d437bcc4SAndreas Gohr    $xhtml = p_wiki_xhtml($recent['id'],'',false);
12603ee62cbSjoe.lapp
12703ee62cbSjoe.lapp    if($conf['useheading']) {
12803ee62cbSjoe.lapp        $matches = array();
12903ee62cbSjoe.lapp        if(preg_match('|<h([1-9])>(.*?)</h\1>|', $xhtml, $matches))
13003ee62cbSjoe.lapp            $item->title = trim($matches[2]);
13103ee62cbSjoe.lapp    }
132d437bcc4SAndreas Gohr    if(!empty($recent['sum'])){
133d437bcc4SAndreas Gohr      $item->title .= ' - '.strip_tags($recent['sum']);
134b1a1915cSandi    }
1354d58bd99Sandi
13603ee62cbSjoe.lapp    $desc = cleanDesc($xhtml);
13703ee62cbSjoe.lapp
13892e52d8dSjoe.lapp    if(empty($ltype))
13992e52d8dSjoe.lapp      $ltype = $conf['rss_linkto'];
14092e52d8dSjoe.lapp
1414d58bd99Sandi    switch ($ltype){
1424d58bd99Sandi      case 'page':
143d437bcc4SAndreas Gohr        $item->link = wl($recent['id'],'rev='.$recent['date'],true);
1444d58bd99Sandi        break;
1454d58bd99Sandi      case 'rev':
146aa11db09SAndreas Gohr        $item->link = wl($recent['id'],'do=revisions&rev='.$recent['date'],true);
1474d58bd99Sandi        break;
14892e52d8dSjoe.lapp      case 'current':
149d437bcc4SAndreas Gohr        $item->link = wl($recent['id'], '', true);
15092e52d8dSjoe.lapp        break;
15192e52d8dSjoe.lapp      case 'diff':
1524d58bd99Sandi      default:
15337beb806SAndreas Gohr        $item->link = wl($recent['id'],'rev='.$recent['date'].'&do=diff'.$recent['date'],true);
1544d58bd99Sandi    }
1554d58bd99Sandi
156f3f0262cSandi    $item->description = $desc;
157d437bcc4SAndreas Gohr    $item->date        = date('r',$recent['date']);
158d437bcc4SAndreas Gohr    $cat = getNS($recent['id']);
159d437bcc4SAndreas Gohr    if($cat) $item->category = $cat;
1607a98db20Sjoe.lapp
1617a98db20Sjoe.lapp    $user = null;
162d437bcc4SAndreas Gohr    $user = @$recent['user']; // the @ spares time repeating lookup
1637a98db20Sjoe.lapp    $item->author = '';
1647a98db20Sjoe.lapp
1657a98db20Sjoe.lapp    if($user){
166c0f9af6dSNathan Neulinger      $userInfo = $auth->getUserData($user);
1677a98db20Sjoe.lapp      $item->author = $userInfo['name'];
1687a98db20Sjoe.lapp      if($guardmail) {
1697a98db20Sjoe.lapp        //cannot obfuscate because some RSS readers may check validity
170d437bcc4SAndreas Gohr        $item->authorEmail = $user.'@'.$recent['ip'];
171f3f0262cSandi      }else{
1727a98db20Sjoe.lapp        $item->authorEmail = $userInfo['mail'];
173f3f0262cSandi      }
1747a98db20Sjoe.lapp    }else{
175d437bcc4SAndreas Gohr      $item->authorEmail = 'anonymous@'.$recent['ip'];
1767a98db20Sjoe.lapp    }
177f3f0262cSandi    $rss->addItem($item);
178f3f0262cSandi  }
179f3f0262cSandi}
180f3f0262cSandi
18115fae107Sandi/**
18215fae107Sandi * Add all pages of a namespace to a feedobject
18315fae107Sandi *
18415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
18515fae107Sandi */
186f3f0262cSandifunction rssListNamespace(&$rss,$ns){
187f62ea8a1Sandi  require_once(DOKU_INC.'inc/search.php');
188f3f0262cSandi  global $conf;
189f3f0262cSandi
190f3f0262cSandi  $ns=':'.cleanID($ns);
191f3f0262cSandi  $ns=str_replace(':','/',$ns);
192f3f0262cSandi
193f3f0262cSandi  $data = array();
194f3f0262cSandi  sort($data);
195f3f0262cSandi  search($data,$conf['datadir'],'search_list','',$ns);
196f3f0262cSandi  foreach($data as $row){
19785cf8195SAndreas Gohr    $item = new FeedItem();
19885cf8195SAndreas Gohr
199f3f0262cSandi    $id    = $row['id'];
200f3f0262cSandi    $date  = filemtime(wikiFN($id));
20185cf8195SAndreas Gohr    $xhtml = p_wiki_xhtml($id,'',false);
20285cf8195SAndreas Gohr    $desc  = cleanDesc($xhtml);
203f3f0262cSandi    $item->title       = $id;
20485cf8195SAndreas Gohr
20585cf8195SAndreas Gohr    if($conf['useheading']) {
20685cf8195SAndreas Gohr        $matches = array();
20785cf8195SAndreas Gohr        if(preg_match('|<h([1-9])>(.*?)</h\1>|', $xhtml, $matches))
20885cf8195SAndreas Gohr            $item->title = trim($matches[2]);
20985cf8195SAndreas Gohr    }
21085cf8195SAndreas Gohr
211ed7b5f09Sandi    $item->link        = wl($id,'rev='.$date,true);
212f3f0262cSandi    $item->description = $desc;
213f3f0262cSandi    $item->date        = date('r',$date);
214f3f0262cSandi    $rss->addItem($item);
215f3f0262cSandi  }
216f3f0262cSandi}
217f3f0262cSandi
21815fae107Sandi/**
21915fae107Sandi * Clean description for feed inclusion
22015fae107Sandi *
22115fae107Sandi * Removes HTML tags and line breaks and trims the text to
22215fae107Sandi * 250 chars
22315fae107Sandi *
22415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
22515fae107Sandi */
226f3f0262cSandifunction cleanDesc($desc){
227d190794cSjoe.lapp  //start description at text of first paragraph
228d190794cSjoe.lapp  $matches = array();
229d190794cSjoe.lapp  if(preg_match('/<p>|<p\s.*?>/', $desc, $matches, PREG_OFFSET_CAPTURE))
230d190794cSjoe.lapp      $desc = substr($desc, $matches[0][1]);
231d190794cSjoe.lapp
232f3f0262cSandi  //remove TOC
2334d58bd99Sandi  $desc = preg_replace('!<div class="toc">.*?(</div>\n</div>)!s','',$desc);
234f3f0262cSandi  $desc = strip_tags($desc);
235f3f0262cSandi  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
236f3f0262cSandi  $desc = preg_replace('/  /',' ',$desc);
2377d8be200Sandi  $desc = utf8_substr($desc,0,250);
238f3f0262cSandi  $desc = $desc.'...';
239f3f0262cSandi  return $desc;
240f3f0262cSandi}
241f3f0262cSandi
242f3f0262cSandi?>
243