After reading how to make a newsfeed page from ClintMoore in the SpecialPagesTutorial, but I thought it would be simpler to have a
KwikiKwiki : KwikiPlugin instead, so that anyone can put an RDF feed anywhere on any page.
So here is a simple plugin to display an RDF news feed on a kwiki page. -- Mike
Save the bottom portion of this page to a file named NewsFormatter.pm in your kwiki directory and change your config.yaml from:
formatter_class: CGI::Kwiki::Formatter
to:
formatter_class: NewsPlugin
You will need to create a subdirectory called 'caches' to hold the cached RDF feeds. You must make the directory writable by the user the webserver is running as.
You can then embed any RDF news feed on any page by using (removing the spaces around the '&'):
[ & NEWS label rdf-url ]
'label' is the label you want to give to the RDF feed.
It is used as a filename to cache the document.
'rdf-url' is the URL for the RDF feed.
Some example RDF feeds might be (again removing spaces around the '&'):
[ & NEWS slashdot http://slashdot.org/slashdot.rdf ]
package NewsPlugin;
$VERSION='0.1';
use strict;
use base 'CGI::Kwiki::Formatter';
use CGI::Kwiki ':char_classes';
use XML::RSS;
use LWP::Simple;
sub NEWS {
my ($self, @params) = @_;
my ($url) = pop @params;
my $NewsName = join(" ", @params);
my $page_id = $NewsName;
$page_id =~ s/[^$ALPHANUM\-]/_/g;
$self->ccache( $page_id, $url);
open( IN, "<caches/$page_id" ) || die( "Gack!" );
my $xml;
while( <IN> ) { $xml .= $_; }
close( IN );
my $rss = new XML::RSS;
my $html;
SWITCH: {
# In case something went wrong with the get() in ccache
eval { $rss->parse( $xml ) };
last SWITCH if $@;
last SWITCH unless defined( $rss->{channel} );
$html .= "<h2>";
$html .= "<a href=\"$rss->{channel}->{link}\">";
$html .= $rss->{channel}->{title};
$html .= "</a>";
$html .= "</h2>\n";
my $html2;
foreach my $item ( @{$rss->{items}} ) {
my $html3 = $item->{title};
if (defined $html3) {
$html3 = "<a href=\"$item->{link}\">$html3</a>"
if defined $item->{link};
$html2 .= "<li>$html3</li>\n";
}
next unless defined( $item->{description} );
$html2 .= "<ul><li>$item->{description}</li></ul>\n";
}
$html .= "<ul>\n$html2</ul>\n" if $html2;
}
return $html;
}
sub ccache {
my ( $self, $page_id, $url ) = @_;
my $age = 0;
my $cache_timeout = 30 * 60; # 30 minutes
unless (-d "caches") {
mkdir "caches" || die "Cannot create caches directory";
}
if ( -f "caches/$page_id" ) {
my $age = (stat "caches/$page_id")[9];
}
if ( ( time() - $age ) > $cache_timeout ) {
open( OUT, ">caches/$page_id" ) || die "Cannot write to caches/$page_id";
print OUT get( $url );
close OUT;
}
}
1;