[Home]WikiPatches/RssInclusion

UseModWiki | WikiPatches | RecentChanges | Preferences

The following patch includes RSS feeds. It takes a tag such as the following, and turns it into a simple list, with the titles linked, and descriptions added.

  <rss "http://meatballwiki.org/wiki/action=rss&days=1">

See MeatBall:RssInclusion for a discussion on the impact on online communities.

Make sure you set your HttpCharset to either "UTF-8" or "ISO-8859-1". Any other charset results in undefined behaviour -- you should check the StoreRss? subroutine first, before doing it.

You need [:RSS] to use this patch - but there is only a RSS.pm? Renaming it into RSS2.pm does not work.

In CommonMarkup, add a new rule for the rss tag after the nowiki, pre and code tags:

  if ($doLines < 2) { # 2 = do line-oriented only
    # The <nowiki> tag stores text with no markup (except quoting HTML)
    s/\<nowiki\>((.|\n)*?)\<\/nowiki\>/&StoreRaw($1)/ige;
    # The <pre> tag wraps the stored text with the HTML <pre> tag
    s/\<pre\>((.|\n)*?)\<\/pre\>/&StorePre($1, "pre")/ige;
    s/\<code\>((.|\n)*?)\<\/code\>/&StorePre($1, "code")/ige;
    # The <rss "uri..."> stores the parsed RSS of the given URI
    s/\<rss +"(.*)"\>/&StoreRss($1)/ige;

Add a new subroutine after StoreRaw:

sub StoreRss {
  require XML::RSS2;
  require LWP::UserAgent;
  my ($uri) = @_;
  my $rss = new XML::RSS;
  my $ua = LWP::UserAgent->new;
  my $request = HTTP::Request->new('GET', $uri);
  my $response = $ua->request($request);
  my $data = $response->content;
  my $maxitems = 15; # recommended max. by the validator
  eval {
    $rss->parse($data);
  };
  if ($@) {
    return "[RSS parsing failed for $uri]";
  } else {
    my $counter = 0;
    my $str .= "<ul>\n";
    for my $i (@{$rss->{items}}) {
      $counter++;
      last if $counter == $maxitems;
      $str .= qq{<li><a href="$i->{'link'}">[$i->{'title'}]</a>};
      $str .= qq{ -- $i->{'description'}} if $i->{'description'};
    }
    $str .= "</ul>\n";
    if ($HttpCharset eq '' or $HttpCharset eq 'UTF-8') {
      return &StoreRaw($str);
    } elsif ($HttpCharset eq "ISO-8859-1") {
      require Unicode::String;
      my $u = Unicode::String->new($str);
      return &StoreRaw($u->latin1);




    } else {
      # FIXME: This is perhaps broken.
      require Unicode::String;
      require Unicode::Map8;
      my $u = Unicode::String->new($str);
      my $m = Unicode::Map8->new($HttpCharset);
      return &StoreRaw($m->to8($u->ucs2));
    }
  }
}


Q: Which XML-RSS-Module is needed?

AFAICS standard XML::RSS works just fine from CPAN.

Q: Could anyone fix the bug in this patch? (require XML::RSS2 and my new=XML::RSS ?)

Well, I'm baffled - it looks like an issue with your Perl setup. I have no RSS2.pm here to require and (at least in a standalone test, I haven't had time to try patching our UseMod yet) loads with use XML::RSS and $rss=XML::RSS->new quite happily both on Perl 5.6.1/XML::RSS 0.97 and Perl 5.8/XML::RSS 1.02.

Here's the output (set to output max 2 items):

horus@offler: ~ $ testrss https://www.usemod.org/cgi-bin/wiki.pl?action=rss
'https://www.usemod.org/cgi-bin/wiki.pl?SiteList' 'SiteList''new server url for wikiuniverse (ns propagation in progress)'
'https://www.usemod.org/cgi-bin/wiki.pl?SandBox' 'SandBox'
horus@offler: ~ $ 

Not hugely helpful for you, I know...
-- PeteJordan

Thanx for help. --BernhardZechmann

OK, I've tried an installation now (after setting up a public test Wiki to test it on :) and, after hacking the s/// to look for /\?lt;rss/ rather than /\<rss/ as that seems to be what 1.0 wants, it works fine (caveats below apply) [here].

I've extended the syntax slightly to allow an optional "maxitems=\d+" in the <rss> tag so something other than the default can be displayed (and I corrected the displayed count code while I was at it; it was displaying one less than the number in $maxitems).

Caveat At least on my (Apache 1.3.27, mod_perl 1.27, Perl 5.8.0, XML::RSS 1.02, XML::Parser 2.33) setup, mod_perl SEGVs in XML::Parser::Expat. It all works fine as CGI and this appears to be a known if unresolved problem eith Expat and mod_perl. I'll try and find the time to follow it up...


Patch for 1.0

--StefanTrcek

Here's the updated patch to 1.0 (press edit page and copy from edit view):

*** wiki	2003-11-01 12:16:19.000000000 +0000
--- testwiki	2003-11-01 12:11:19.000000000 +0000
***************
*** 1608,1613 ****
--- 1608,1615 ----
      # The <pre> tag wraps the stored text with the HTML <pre> tag
      s/\<pre\>((.|\n)*?)\<\/pre\>/&StorePre($1, "pre")/ige;
      s/\<code\>((.|\n)*?)\<\/code\>/&StorePre($1, "code")/ige;
+     # The <rss "uri..."> stores the parsed RSS of the given URI
+     s/\<rss +"([^"]*)"\s*(?:maxitems\s*=\s*(\d+))?\s*\>/&StoreRss($1, $2)/ige;
      if ($EarlyRules ne '') {
        $_ = &EvalLocalRules($EarlyRules, $_, !$useImage);
      }
***************
*** 1888,1893 ****
--- 1890,1937 ----
    return $FS . $SaveUrlIndex++ . $FS;
  }
  
+ sub StoreRss {
+   require XML::RSS;
+   require LWP::UserAgent;
+   my ($uri, $maxitems) = @_;
+   my $rss = XML::RSS->new;
+   my $ua = LWP::UserAgent->new;
+   my $request = HTTP::Request->new('GET', $uri);
+   my $response = $ua->request($request);
+   my $data = $response->content;
+   $maxitems ||= 15; # recommended max. by the validator
+   eval {
+     $rss->parse($data);
+   };
+   if ($@) {
+     return "[RSS parsing failed for $uri]";
+   } else {
+     my $counter = 0;
+     my $str .= "<ul>\n";
+     for my $i (@{$rss->{items}}) {
+       $counter++;
+       last if $counter > $maxitems;
+       $str .= qq{<li><a href="$i->{'link'}">[$i->{'title'}]</a>};
+       $str .= QuoteHtml(qq{ -- $i->{'description'}}) if $i->{'description'};
+     }
+     $str .= "</ul>\n";
+     if ($HttpCharset eq '' or $HttpCharset eq 'UTF-8') {
+       return &StoreRaw($str);
+     } elsif ($HttpCharset eq "ISO-8859-1") {
+       require Unicode::String;
+       my $u = Unicode::String->new($str);
+       return &StoreRaw($u->latin1);
+     } else {
+       # FIXME: This is perhaps broken.
+       require Unicode::String;
+       require Unicode::Map8;
+       my $u = Unicode::String->new($str);
+       my $m = Unicode::Map8->new($HttpCharset);
+       return &StoreRaw($m->to8($u->ucs2));
+     }
+   }
+ }
+ 
  sub StorePre {
    my ($html, $tag) = @_;
  


WikiPatches

UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited November 6, 2023 9:05 pm by MarkusLude (diff)
Search: