<?php
/*
     http://www.brambring.nl

     $Id: rssdisplay.php,v 1.19 2004/12/09 19:14:29 pts00065 Exp $

    Syntax
    [[$RSS http://url.to.rss.feed/feed.rss<,what><,parameters>]]

    Examples:
    displays the feed of the pmwiki pages with the defauft layout and number of lines:
    [[$RSS http://www.pmichaud.com/wiki/Pm/AllRecentChanges?action=rss]]  
    
    displays the pmwiki feed in long layout and  the top 5 lines 
    [[$RSS http://www.pmichaud.com/wiki/Pm/AllRecentChanges?action=rss,long,5]]  

    displays the pmwiki feed in short layout and  all lines in the feed
    [[$RSS http://www.pmichaud.com/wiki/Pm/AllRecentChanges?action=rss,short,-1]]  
     
    $Log: rssdisplay.php,v $
    Revision 1.19  2004/12/09 19:14:29  pts00065
    pmwiki2 two

    Revision 1.18  2004/11/30 19:49:25  pts00065
    pmwiki2
*/

Markup('rssdisplay','_begin','/\(:RSS *(.+):\)/e',"RSS('\$1')");

define('MAGPIE_CACHE_AGE', 3*60*60);
define('MAGPIE_CACHE_DIR', "$FarmD/cache");
define('MAGPIE_FETCH_TIME_OUT', 15);
define('MAGPIE_GZIP', true);

function RSS($regex) {
global $action;
global $FarmD;
# If you make a call to your own site
# and you have any kind of WritePage action ( Lock(2) ) for example
# because you do some kind of logging in a wiki-page.
# the whole thing will deadlock, so remove any locks.
# ReadPage will request a lock again when needed
Lock(-1);
$line="";
if ( $regex == '' ) {
 $line.="Empty parameter in RSS ";
} else {
  $tokens=explode(",",$regex);

  $url      =$tokens[0];
  $what     =$tokens[1];
  $num_items=$tokens[2];

  if ( $what == '' ) {
    $what='short';
  }

  if ( $num_items == 0 ) {
    $num_items=10;
  }
  if ( $action && (  $action != 'browse'  ) ) {
    # this is important
    #pmwiki processes DoubleBrackets in the rss module
    #recursion untill the server blows
    $line.= "RSS Feed : $url";
  } else {
    include_once("$FarmD/local/magpie/rss_fetch.inc");
    $rss = fetch_rss($url);
    #print "<PRE>";
    #print_r ($rss);
     #if fetch_rss fails magpie will produce some error message
     # we won't handle a check here
      if ( $num_items > 0 ) { 
        $items = array_slice($rss->items, 0, $num_items);
      } else {
        $items = $rss->items;
      }
      foreach ($items as $item) {
        #print_r ($item);
        $href = $item['link'];
        $title = $item['title'];	
        if ( isset ($item['description']) ) {
          $description = $item['description'];	
        }
        if ( isset ($item['atom_content']) ) {
          $description = $item['atom_content'];	
        }
        $link="<a class='urllink' href='$href'>$title</a>";
        if ( $what == 'short' ) { 
          $line .= "$link \n";
        }
        if ( $what == 'long' ) { 
          $line .= "<h3>$link</h3><BR>\n";
          $line .=  $description . "<BR>";
        }
      }
    }
  }

$line="<div class='rss'>$line</div>";
return Keep($line);
}
?>