Author: earle Date: 2006-04-05 02:26:00 +0100 (Wed, 05 Apr 2006) New Revision: 753
Added: trunk/lib/OpenGuides/Feed.pm Modified: trunk/Build.PL trunk/MANIFEST trunk/PREREQUISITES trunk/lib/OpenGuides.pm trunk/lib/OpenGuides/RDF.pm trunk/t/22_rss_modwiki.t trunk/wiki.cgi Log: New module, OpenGuides::Feed: this extracts the stuff formerly in OpenGuides::RDF (as sub make_recentchanges_rss) that was invoking CGI::Wiki::Plugin::RSS::ModWiki, and replaces it with a single make_feed method that can be used for any feed format, including Atom (using the new CGI::Wiki::Plugin::Atom). Changes to OpenGuides.pm and wiki.cgi are to use this new module appropriately.
Modified: trunk/Build.PL =================================================================== --- trunk/Build.PL 2006-04-04 21:06:14 UTC (rev 752) +++ trunk/Build.PL 2006-04-05 01:26:00 UTC (rev 753) @@ -220,6 +220,7 @@ 'CGI::Cookie' => 0, 'CGI::Wiki' => '0.62', # fixed delete version 'CGI::Wiki::Formatter::UseMod' => '0.16', # macros + 'CGI::Wiki::Plugin::Atom' => 0, 'CGI::Wiki::Plugin::Categoriser' => 0, 'CGI::Wiki::Plugin::Diff' => '0.08', # earlier buggy 'CGI::Wiki::Plugin::Locator::Grid'=> '0.02', # cope with sqlite 3
Modified: trunk/MANIFEST =================================================================== --- trunk/MANIFEST 2006-04-04 21:06:14 UTC (rev 752) +++ trunk/MANIFEST 2006-04-05 01:26:00 UTC (rev 753) @@ -17,6 +17,7 @@ lib/OpenGuides/Build.pm lib/OpenGuides/CGI.pm lib/OpenGuides/Config.pm +lib/OpenGuides/Feed.pm lib/OpenGuides/RDF.pm lib/OpenGuides/Search.pm lib/OpenGuides/Template.pm
Modified: trunk/PREREQUISITES =================================================================== --- trunk/PREREQUISITES 2006-04-04 21:06:14 UTC (rev 752) +++ trunk/PREREQUISITES 2006-04-05 01:26:00 UTC (rev 753) @@ -8,6 +8,7 @@ CGI::Cookie CGI::Wiki (version 0.62 or later) CGI::Wiki::Formatter::UseMod (version 0.16 or later) +CGI::Wiki::Plugin::Atom CGI::Wiki::Plugin::Categoriser CGI::Wiki::Plugin::Diff (version 0.08 or later) CGI::Wiki::Plugin::Locator::Grid (version 0.02 or later)
Added: trunk/lib/OpenGuides/Feed.pm =================================================================== --- trunk/lib/OpenGuides/Feed.pm 2006-04-04 21:06:14 UTC (rev 752) +++ trunk/lib/OpenGuides/Feed.pm 2006-04-05 01:26:00 UTC (rev 753) @@ -0,0 +1,241 @@ +package OpenGuides::Feed; + +use strict; + +use vars qw( $VERSION ); +$VERSION = '0.01'; + +use CGI::Wiki::Plugin::Atom; +use CGI::Wiki::Plugin::RSS::ModWiki; +use Time::Piece; +use URI::Escape; +use Carp 'croak'; + +sub new { + my ($class, @args) = @_; + my $self = {}; + bless $self, $class; + $self->_init(@args); +} + +sub _init { + my ($self, %args) = @_; + + my $wiki = $args{wiki}; + + unless ( $wiki && UNIVERSAL::isa( $wiki, "CGI::Wiki" ) ) { + croak "No CGI::Wiki object supplied."; + } + $self->{wiki} = $wiki; + + my $config = $args{config}; + + unless ( $config && UNIVERSAL::isa( $config, "OpenGuides::Config" ) ) { + croak "No OpenGuides::Config object supplied."; + } + $self->{config} = $config; + + $self->{make_node_url} = sub { + my ($node_name, $version) = @_; + + my $config = $self->{config}; + + my $node_url = $config->script_url . uri_escape($config->script_name) . '?'; + $node_url .= 'id=' if defined $version; + $node_url .= uri_escape($self->{wiki}->formatter->node_name_to_node_param($node_name)); + $node_url .= ';version=' . uri_escape($version) if defined $version; + + $node_url; + }; + $self->{site_name} = $config->site_name; + $self->{default_city} = $config->default_city || ""; + $self->{default_country} = $config->default_country || ""; + $self->{site_description} = $config->site_desc || ""; + $self->{og_version} = $args{og_version}; + + $self; +} + +sub make_feed { + my ($self, %args) = @_; + + my $feed_type = $args{feed_type}; + + my %known_types = ( + 'rss' => 1, + 'atom' => 1, + ); + + croak "No feed type specified" unless $feed_type; + croak "Unknown feed type: $feed_type" unless $known_types{$feed_type}; + + if ($feed_type eq 'rss') { + return $self->rss_maker->recent_changes(%args); + } + elsif ($feed_type eq 'atom') { + return $self->atom_maker->recent_changes(%args); + } +} + +sub atom_maker { + my $self = shift; + + unless ($self->{atom_maker}) { + $self->{atom_maker} = CGI::Wiki::Plugin::Atom->new( + wiki => $self->{wiki}, + site_name => $self->{site_name}, + site_url => $self->{config}->script_url, + site_description => $self->{site_description}, + make_node_url => $self->{make_node_url}, + recent_changes_link => $self->{config}->script_url . '?action=rc', + atom_link => $self->{config}->script_url . '?action=rc&format=atom', + software_name => 'OpenGuides', + software_homepage => 'http://openguides.org/', + software_version => $self->{og_version}, + ); + } + + $self->{atom_maker}; +} + +sub rss_maker { + my $self = shift; + + unless ($self->{rss_maker}) { + $self->{rss_maker} = CGI::Wiki::Plugin::RSS::ModWiki->new( + wiki => $self->{wiki}, + site_name => $self->{site_name}, + site_url => $self->{config}->script_url, + site_description => $self->{site_description}, + make_node_url => $self->{make_node_url}, + recent_changes_link => $self->{config}->script_url . '?action=rc', + software_name => 'OpenGuides', + software_homepage => 'http://openguides.org/', + software_version => $self->{og_version}, + ); + } + + $self->{rss_maker}; +} + +sub feed_timestamp { + my ($self, %args) = @_; + + # The timestamp methods in our feeds are equivalent, we might as well + # use the RSS one. + $self->rss_maker->rss_timestamp(%args); +} + +=head1 NAME + +OpenGuides::Feed - generate data feeds for OpenGuides in various formats. + +=head1 DESCRIPTION + +Produces RSS 1.0 and Atom 1.0 feeds for OpenGuides. Distributed and +installed as part of the OpenGuides project, not intended for independent +installation. This documentation is probably only useful to OpenGuides +developers. + +=head1 SYNOPSIS + + use CGI::Wiki; + use OpenGuides::Config; + use OpenGuides::Feed; + + my $wiki = CGI::Wiki->new( ... ); + my $config = OpenGuides::Config->new( file => "wiki.conf" ); + my $feed = OpenGuides::Feed->new( wiki => $wiki, + config => $config, + og_version => '1.0', ); + + # Ten most recent changes in RSS format. + print "Content-Type: application/rdf+xml\n"; + print "Last-Modified: " . $self->feed_timestamp( items => 10 ) . "\n\n"; + my %args = ( items => 10, + feed_type => 'rss', ); + print $feed->make_feed( %args ); + +=head1 METHODS + +=over 4 + +=item B<new> + + my $feed = OpenGuides::Feed->new( wiki => $wiki, + config => $config, + og_version => '1.0', ); + +C<wiki> must be a LCGI::Wiki object and C<config> must be an +LOpenGuides::Config object. Both of these arguments are mandatory. +C<og_version> is an optional argument specifying the version of +OpenGuides for inclusion in the feed. + +=item B<rss_maker> + +Returns a raw LCGI::Wiki::Plugin::RSS::ModWiki object created with the values you +invoked this module with. + +=item B<atom_maker> + +Returns a raw LCGI::Wiki::Plugin::Atom object created with the values you +invoked this module with. + +=item B<make_feed> + + # Ten most recent changes in RSS format. + print "Content-Type: application/rdf+xml\n"; + print "Last-Modified: " . $feed->feed_timestamp( items => 10 ) . "\n\n"; + my %args = ( items => 10, + feed_type => 'rss', ); + print $rdf_writer->make_feed( %args ); + + # All the changes made by bob in the past week, ignoring minor edits, in Atom. + $args{days} = 7; + $args{ignore_minor_edits = 1; + $args{filter_on_metadata} => { username => "bob" }; + + print "Content-Type: application/atom+xml\n"; + print "Last-Modified: " . $feed->feed_timestamp( %args ) . "\n\n"; + print $feed->make_feed( %args ); + +=item B<feed_timestamp> + + print "Last-Modified: " . $feed->feed_timestamp( %args ) . "\n\n"; + +Returns the timestamp of the data feed in POSIX::strftime style ("Tue, 29 Feb 2000 +12:34:56 GMT"), which is equivalent to the timestamp of the most recent item +in the feed. Takes the same arguments as make_recentchanges_rss(). You will most +likely need this to print a Last-Modified HTTP header so user-agents can determine +whether they need to reload the feed or not. + +=back + +=head1 SEE ALSO + +=over 4 + +=item * LCGI::Wiki, LCGI::Wiki::Plugin::RSS::ModWiki and LCGI::Wiki::Plugin::Atom + +=item * Lhttp://openguides.org/ + +=back + +=head1 AUTHOR + +The OpenGuides Project (openguides-dev@openguides.org) + +=head1 COPYRIGHT + +Copyright (C) 2003-2006 The OpenGuides Project. All Rights Reserved. + +This module is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=head1 CREDITS + +Written by Earle Martin, based on the original OpenGuides::RDF by Kake Pugh. + +=cut + +1;
Modified: trunk/lib/OpenGuides/RDF.pm =================================================================== --- trunk/lib/OpenGuides/RDF.pm 2006-04-04 21:06:14 UTC (rev 752) +++ trunk/lib/OpenGuides/RDF.pm 2006-04-05 01:26:00 UTC (rev 753) @@ -3,7 +3,7 @@ use strict;
use vars qw( $VERSION ); -$VERSION = '0.08'; +$VERSION = '0.09';
use CGI::Wiki::Plugin::RSS::ModWiki; use Time::Piece; @@ -199,39 +199,6 @@ return $rdf; }
-sub rss_maker { - my $self = shift; - - # OAOO, please. - unless ($self->{rss_maker}) { - $self->{rss_maker} = CGI::Wiki::Plugin::RSS::ModWiki->new( - wiki => $self->{wiki}, - site_name => $self->{site_name}, - site_url => $self->{config}->script_url, - site_description => $self->{site_description}, - make_node_url => $self->{make_node_url}, - recent_changes_link => $self->{config}->script_url . '?action=rc', - software_name => 'OpenGuides', - software_homepage => 'http://openguides.org/', - software_version => $self->{og_version}, - ); - } - - $self->{rss_maker}; -} - -sub make_recentchanges_rss { - my ($self, %args) = @_; - - $self->rss_maker->recent_changes(%args); -} - -sub rss_timestamp { - my ($self, %args) = @_; - - $self->rss_maker->rss_timestamp(%args); -} - =head1 NAME
OpenGuides::RDF - An OpenGuides plugin to output RDF/XML. @@ -258,11 +225,6 @@ print "Content-Type: application/rdf+xml\n\n"; print $rdf_writer->emit_rdfxml( node => "Masala Zone, N1 0NU" );
- # Ten most recent changes. - print "Content-Type: application/rdf+xml\n"; - print "Last-Modified: " . $self->rss_timestamp( items => 10 ) . "\n\n"; - print $rdf_writer->make_recentchanges_rss( items => 10 ); - =head1 METHODS
=over 4 @@ -314,42 +276,6 @@
=back
-=item B<rss_maker> - -Returns a raw LCGI::Wiki::Plugin::RSS::ModWiki object created with the values you -invoked this module with. - -=item B<make_recentchanges_rss> - - # Ten most recent changes. - print "Content-Type: application/rdf+xml\n"; - print "Last-Modified: " . $rdf_writer->rss_timestamp( items => 10 ) . "\n\n"; - print $rdf_writer->make_recentchanges_rss( items => 10 ); - - # All the changes made by bob in the past week, ignoring minor edits. - - my %args = ( - days => 7, - ignore_minor_edits => 1, - filter_on_metadata => { username => "bob" }, - ); - - print "Content-Type: application/rdf+xml\n"; - print "Last-Modified: " . $rdf_writer->rss_timestamp( %args ) . "\n\n"; - print $rdf_writer->make_recentchanges_rss( %args ); - -=item B<rss_timestamp> - - print "Last-Modified: " . $rdf_writer->rss_timestamp( %args ) . "\n\n"; - -Returns the timestamp of the RSS feed in POSIX::strftime style ("Tue, 29 Feb 2000 -12:34:56 GMT"), which is equivalent to the timestamp of the most recent item -in the feed. Takes the same arguments as make_recentchanges_rss(). You will most -likely need this to print a Last-Modified HTTP header so user-agents can determine -whether they need to reload the feed or not. - -=back - =head1 SEE ALSO
=over 4 @@ -368,7 +294,7 @@
=head1 COPYRIGHT
-Copyright (C) 2003-2005 The OpenGuides Project. All Rights Reserved. +Copyright (C) 2003-2006 The OpenGuides Project. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Modified: trunk/lib/OpenGuides.pm =================================================================== --- trunk/lib/OpenGuides.pm 2006-04-04 21:06:14 UTC (rev 752) +++ trunk/lib/OpenGuides.pm 2006-04-05 01:26:00 UTC (rev 753) @@ -6,6 +6,7 @@ use CGI::Wiki::Plugin::Diff; use CGI::Wiki::Plugin::Locator::Grid; use OpenGuides::CGI; +use OpenGuides::Feed; use OpenGuides::Template; use OpenGuides::Utils; use Time::Piece; @@ -13,7 +14,7 @@
use vars qw( $VERSION );
-$VERSION = '0.52'; +$VERSION = '0.53';
=head1 NAME
@@ -155,6 +156,9 @@ $tt_vars{"rss_".lc($type)."_url"} = $config->script_name . "?action=rc;format=rss;" . lc($type) . "=" . lc(CGI->escape($2)); + $tt_vars{"atom_".lc($type)."_url"} = + $config->script_name . "?action=rc;format=atom;" + . lc($type) . "=" . lc(CGI->escape($2)); }
my %current_data = $wiki->retrieve_node( $id ); @@ -636,30 +640,38 @@ print $output; }
-=item B<display_rss> +=item B<display_feed>
- # Last ten non-minor edits to Hammersmith pages. - $guide->display_rss( + # Last ten non-minor edits to Hammersmith pages in RSS 1.0 format + $guide->display_feed( + feed_type => 'rss', items => 10, ignore_minor_edits => 1, locale => "Hammersmith", );
- # All edits bob has made to pub pages in the last week. - $guide->display_rss( - days => 7, - username => "bob", - category => "Pubs", + # All edits bob has made to pub pages in the last week in Atom format + $guide->display_feed( + feed_type => 'atom', + days => 7, + username => "bob", + category => "Pubs", );
+C<feed_type> is a mandatory parameter. Supported values at present are +"rss" and "atom". + As with other methods, the C<return_output> parameter can be used to return the output instead of printing it to STDOUT.
=cut
-sub display_rss { +sub display_feed { my ($self, %args) = @_;
+ my $feed_type = $args{feed_type}; + croak "No feed type given" unless $feed_type; + my $return_output = $args{return_output} ? 1 : 0;
my $items = $args{items} || ""; @@ -672,6 +684,7 @@ items => $items, days => $days, ignore_minor_edits => $ignore_minor_edits, + feed_type => $feed_type, ); my %filter; $filter{username} = $username if $username; @@ -681,14 +694,28 @@ $criteria{filter_on_metadata} = %filter; }
- my $rdf_writer = OpenGuides::RDF->new( - wiki => $self->wiki, - config => $self->config, - og_version => $VERSION, - ); - my $output = "Content-Type: application/rdf+xml\n"; - $output .= "Last-Modified: " . $rdf_writer->rss_timestamp( %criteria ) . "\n\n"; - $output .= $rdf_writer->make_recentchanges_rss( %criteria ); + my $feed = OpenGuides::Feed->new( + wiki => $self->wiki, + config => $self->config, + og_version => $VERSION, + ); + + my $output; + + if ($feed_type eq 'rss') { + $output = "Content-Type: application/rdf+xml\n"; + } + elsif ($feed_type eq 'atom') { + $output = "Content-Type: application/atom+xml\n"; + } + else { + croak "Unknown feed type given: $feed_type"; + } + + $output .= "Last-Modified: " . $feed->feed_timestamp( %criteria ) . "\n\n"; + + $output .= $feed->make_feed( %criteria ); + return $output if $return_output; print $output; }
Modified: trunk/t/22_rss_modwiki.t =================================================================== --- trunk/t/22_rss_modwiki.t 2006-04-04 21:06:14 UTC (rev 752) +++ trunk/t/22_rss_modwiki.t 2006-04-05 01:26:00 UTC (rev 753) @@ -42,11 +42,11 @@ # Basic sanity check first. my $wiki = OpenGuides::Utils->make_wiki_object( config => $config );
-my $rdf_writer = OpenGuides::RDF->new( wiki => $wiki, - config => $config ); +my $feed = OpenGuides::Feed->new( wiki => $wiki, + config => $config );
-my $rss = eval { $rdf_writer->make_recentchanges_rss; }; -is( $@, "", "->make_recentchanges_rss doesn't croak" ); +my $rss = eval { $feed->make_feed(feed_type => 'rss'); }; +is( $@, "", "->make_feed for rss doesn't croak" );
# Now write some data, first a minor edit then a non-minor one. my $guide = OpenGuides->new( config => $config ); @@ -97,7 +97,8 @@ ok( $wiki->node_exists( "Wombles" ), "Wombles written" );
# Check that the minor edits can be filtered out. -$output = $guide->display_rss( +$output = $guide->display_feed( + feed_type => "rss", items => 5, username => "bob", ignore_minor_edits => 1,
Modified: trunk/wiki.cgi =================================================================== --- trunk/wiki.cgi 2006-04-04 21:06:14 UTC (rev 752) +++ trunk/wiki.cgi 2006-04-05 01:26:00 UTC (rev 753) @@ -4,7 +4,7 @@ use warnings;
use vars qw( $VERSION ); -$VERSION = '0.52'; +$VERSION = '0.53';
use CGI qw/:standard/; use CGI::Carp qw(croak); @@ -103,12 +103,19 @@ my %args = map { $_ => ( $q->param($_) || "" ) } qw( feed items days ignore_minor_edits username category locale ); - $guide->display_rss( %args ); + $args{feed_type} = 'rss'; + $guide->display_feed( %args ); } elsif ( $feed eq "chef_dan" ) { display_node_rdf( node => $node ); } else { croak "Unknown RSS feed type '$feed'"; } + } elsif ($format && $format eq 'atom') { + my %args = map { $_ => ( $q->param($_) || "" ) } + qw( feed items days ignore_minor_edits username + category locale ); + $args{feed_type} = 'atom'; + $guide->display_feed( %args ); } else { $guide->display_node( id => 'RecentChanges' ); }