xref: /dokuwiki/feed.php (revision d437bcc4a25f62fad65e625f4bc13cab8873f994)
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');
15f3f0262cSandi
167131b668SAndreas Gohr  //close session
178746e727Sandi  session_write_close();
188746e727Sandi
19f3f0262cSandi
20f3f0262cSandi  $num   = $_REQUEST['num'];
21f3f0262cSandi  $type  = $_REQUEST['type'];
22f3f0262cSandi  $mode  = $_REQUEST['mode'];
23f3f0262cSandi  $ns    = $_REQUEST['ns'];
244d58bd99Sandi  $ltype = $_REQUEST['linkto'];
25f3f0262cSandi
2631f1284dSjoe.lapp  if($type == '')
2731f1284dSjoe.lapp    $type = $conf['rss_type'];
2831f1284dSjoe.lapp
29f3f0262cSandi  switch ($type){
30f3f0262cSandi    case 'rss':
31f3f0262cSandi       $type = 'RSS0.9';
32f3f0262cSandi       break;
33f3f0262cSandi    case 'rss2':
34f3f0262cSandi       $type = 'RSS2.0';
35f3f0262cSandi       break;
36f3f0262cSandi    case 'atom':
37f3f0262cSandi       $type = 'ATOM0.3';
38f3f0262cSandi       break;
39f3f0262cSandi    default:
40f3f0262cSandi       $type = 'RSS1.0';
41f3f0262cSandi  }
42f3f0262cSandi
437131b668SAndreas Gohr	// the feed is dynamic - we need a cache for each combo
447131b668SAndreas Gohr  // (but most people just use the default feed so it's still effective)
457131b668SAndreas Gohr	$cache = getCacheName($num.$type.$mode.$ns.$ltype.$_SERVER['REMOTE_USER'],'.feed');
46f3f0262cSandi
477131b668SAndreas Gohr	// check cacheage and deliver if nothing has changed since last
487131b668SAndreas Gohr  // time (with 5 minutes settletime)
497131b668SAndreas Gohr	$cmod = @filemtime($cache); // 0 if not exists
507131b668SAndreas Gohr	if($cmod && ($cmod+(5*60) >= @filemtime($conf['changelog']))){
517131b668SAndreas Gohr  	header('Content-Type: application/xml; charset=utf-8');
527131b668SAndreas Gohr		print io_readFile($cache);
537131b668SAndreas Gohr		exit;
547131b668SAndreas Gohr  }
557131b668SAndreas Gohr
567131b668SAndreas Gohr	// create new feed
57f3f0262cSandi  $rss = new DokuWikiFeedCreator();
58dbb00abcSEsther Brunner  $rss->title = $conf['title'].(($ns) ? ' '.$ns : '');
59ed7b5f09Sandi  $rss->link  = DOKU_URL;
60f62ea8a1Sandi  $rss->syndicationURL = DOKU_URL.'feed.php';
61f62ea8a1Sandi  $rss->cssStyleSheet  = DOKU_URL.'lib/styles/feed.css';
62f3f0262cSandi
6379b608ceSandi  $image = new FeedImage();
6479b608ceSandi  $image->title = $conf['title'];
65f62ea8a1Sandi  $image->url = DOKU_URL."lib/images/favicon.ico";
6679b608ceSandi  $image->link = DOKU_URL;
6779b608ceSandi  $rss->image = $image;
6879b608ceSandi
69f3f0262cSandi  if($mode == 'list'){
70f3f0262cSandi    rssListNamespace($rss,$ns);
71f3f0262cSandi  }else{
72dbb00abcSEsther Brunner    rssRecentChanges($rss,$num,$ltype,$ns);
73f3f0262cSandi  }
74f3f0262cSandi
757131b668SAndreas Gohr  $feed = $rss->createFeed($type,'utf-8');
767131b668SAndreas Gohr
777131b668SAndreas Gohr  // save cachefile
787131b668SAndreas Gohr	io_saveFile($cache,$feed);
797131b668SAndreas Gohr
807131b668SAndreas Gohr	// finally deliver
814d58bd99Sandi  header('Content-Type: application/xml; charset=utf-8');
827131b668SAndreas Gohr  print $feed;
83f3f0262cSandi
8415fae107Sandi// ---------------------------------------------------------------- //
85f3f0262cSandi
8615fae107Sandi/**
8715fae107Sandi * Add recent changed to a feed object
8815fae107Sandi *
8915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
9015fae107Sandi */
91dbb00abcSEsther Brunnerfunction rssRecentChanges(&$rss,$num,$ltype,$ns){
92f62ea8a1Sandi  global $conf;
93f62ea8a1Sandi  if(!$num) $num = $conf['recent'];
947a98db20Sjoe.lapp  $guardmail = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
95f62ea8a1Sandi
96dbb00abcSEsther Brunner  $recents = getRecents(0,$num,false,$ns);
97f62ea8a1Sandi
98f62ea8a1Sandi  //this can take some time if a lot of recaching has to be done
99f62ea8a1Sandi  @set_time_limit(90); // set max execution time
100f62ea8a1Sandi
101*d437bcc4SAndreas Gohr	foreach($recents as $recent){
10203ee62cbSjoe.lapp
103f3f0262cSandi    $item = new FeedItem();
104*d437bcc4SAndreas Gohr    $item->title = $recent['id'];
105*d437bcc4SAndreas Gohr    $xhtml = p_wiki_xhtml($recent['id'],'',false);
10603ee62cbSjoe.lapp
10703ee62cbSjoe.lapp    if($conf['useheading']) {
10803ee62cbSjoe.lapp        $matches = array();
10903ee62cbSjoe.lapp        if(preg_match('|<h([1-9])>(.*?)</h\1>|', $xhtml, $matches))
11003ee62cbSjoe.lapp            $item->title = trim($matches[2]);
11103ee62cbSjoe.lapp    }
112*d437bcc4SAndreas Gohr    if(!empty($recent['sum'])){
113*d437bcc4SAndreas Gohr      $item->title .= ' - '.strip_tags($recent['sum']);
114b1a1915cSandi    }
1154d58bd99Sandi
11603ee62cbSjoe.lapp    $desc = cleanDesc($xhtml);
11703ee62cbSjoe.lapp
11892e52d8dSjoe.lapp    if(empty($ltype))
11992e52d8dSjoe.lapp      $ltype = $conf['rss_linkto'];
12092e52d8dSjoe.lapp
1214d58bd99Sandi    switch ($ltype){
1224d58bd99Sandi      case 'page':
123*d437bcc4SAndreas Gohr        $item->link = wl($recent['id'],'rev='.$recent['date'],true);
1244d58bd99Sandi        break;
1254d58bd99Sandi      case 'rev':
126*d437bcc4SAndreas Gohr        $item->link = wl($recent['id'],'do=revisions&amp;rev='.$recent['date'],true);
1274d58bd99Sandi        break;
12892e52d8dSjoe.lapp      case 'current':
129*d437bcc4SAndreas Gohr        $item->link = wl($recent['id'], '', true);
13092e52d8dSjoe.lapp        break;
13192e52d8dSjoe.lapp      case 'diff':
1324d58bd99Sandi      default:
133*d437bcc4SAndreas Gohr        $item->link = wl($recent['id'],'do=diff&amp;'.$recent['date'],true);
1344d58bd99Sandi    }
1354d58bd99Sandi
136f3f0262cSandi    $item->description = $desc;
137*d437bcc4SAndreas Gohr    $item->date        = date('r',$recent['date']);
138*d437bcc4SAndreas Gohr		$cat = getNS($recent['id']);
139*d437bcc4SAndreas Gohr		if($cat) $item->category = $cat;
1407a98db20Sjoe.lapp
1417a98db20Sjoe.lapp    $user = null;
142*d437bcc4SAndreas Gohr    $user = @$recent['user']; // the @ spares time repeating lookup
1437a98db20Sjoe.lapp    $item->author = '';
1447a98db20Sjoe.lapp
1457a98db20Sjoe.lapp    if($user){
1467a98db20Sjoe.lapp      $userInfo = auth_getUserData($user);
1477a98db20Sjoe.lapp      $item->author = $userInfo['name'];
1487a98db20Sjoe.lapp      if($guardmail) {
1497a98db20Sjoe.lapp        //cannot obfuscate because some RSS readers may check validity
150*d437bcc4SAndreas Gohr        $item->authorEmail = $user.'@'.$recent['ip'];
151f3f0262cSandi      }else{
1527a98db20Sjoe.lapp        $item->authorEmail = $userInfo['mail'];
153f3f0262cSandi      }
1547a98db20Sjoe.lapp    }else{
155*d437bcc4SAndreas Gohr      $item->authorEmail = 'anonymous@'.$recent['ip'];
1567a98db20Sjoe.lapp    }
157f3f0262cSandi    $rss->addItem($item);
158f3f0262cSandi  }
159f3f0262cSandi}
160f3f0262cSandi
16115fae107Sandi/**
16215fae107Sandi * Add all pages of a namespace to a feedobject
16315fae107Sandi *
16415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
16515fae107Sandi */
166f3f0262cSandifunction rssListNamespace(&$rss,$ns){
167f62ea8a1Sandi  require_once(DOKU_INC.'inc/search.php');
168f3f0262cSandi  global $conf;
169f3f0262cSandi
170f3f0262cSandi  $ns=':'.cleanID($ns);
171f3f0262cSandi  $ns=str_replace(':','/',$ns);
172f3f0262cSandi
173f3f0262cSandi  $data = array();
174f3f0262cSandi  sort($data);
175f3f0262cSandi  search($data,$conf['datadir'],'search_list','',$ns);
176f3f0262cSandi  foreach($data as $row){
177f3f0262cSandi    $id = $row['id'];
178f3f0262cSandi    $date = filemtime(wikiFN($id));
1794d58bd99Sandi    $desc = cleanDesc(p_wiki_xhtml($id,'',false));
180f3f0262cSandi    $item = new FeedItem();
181f3f0262cSandi    $item->title       = $id;
182ed7b5f09Sandi    $item->link        = wl($id,'rev='.$date,true);
183f3f0262cSandi    $item->description = $desc;
184f3f0262cSandi    $item->date        = date('r',$date);
185f3f0262cSandi    $rss->addItem($item);
186f3f0262cSandi  }
187f3f0262cSandi}
188f3f0262cSandi
18915fae107Sandi/**
19015fae107Sandi * Clean description for feed inclusion
19115fae107Sandi *
19215fae107Sandi * Removes HTML tags and line breaks and trims the text to
19315fae107Sandi * 250 chars
19415fae107Sandi *
19515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
19615fae107Sandi */
197f3f0262cSandifunction cleanDesc($desc){
198d190794cSjoe.lapp  //start description at text of first paragraph
199d190794cSjoe.lapp  $matches = array();
200d190794cSjoe.lapp  if(preg_match('/<p>|<p\s.*?>/', $desc, $matches, PREG_OFFSET_CAPTURE))
201d190794cSjoe.lapp      $desc = substr($desc, $matches[0][1]);
202d190794cSjoe.lapp
203f3f0262cSandi  //remove TOC
2044d58bd99Sandi  $desc = preg_replace('!<div class="toc">.*?(</div>\n</div>)!s','',$desc);
205f3f0262cSandi  $desc = strip_tags($desc);
206f3f0262cSandi  $desc = preg_replace('/[\n\r\t]/',' ',$desc);
207f3f0262cSandi  $desc = preg_replace('/  /',' ',$desc);
2087d8be200Sandi  $desc = utf8_substr($desc,0,250);
209f3f0262cSandi  $desc = $desc.'...';
210f3f0262cSandi  return $desc;
211f3f0262cSandi}
212f3f0262cSandi
213f3f0262cSandi?>
214