Author: nick Date: 2006-07-06 17:09:18 +0100 (Thu, 06 Jul 2006) New Revision: 817
Added: trunk/t/24_feed_node_listings.t Modified: trunk/lib/OpenGuides.pm trunk/lib/OpenGuides/Feed.pm trunk/t/22_feed_recent_changes.t Log: Shift the content type method Add mini feed method Start to do urls, names etc for feeds properly New tests
Modified: trunk/lib/OpenGuides/Feed.pm =================================================================== --- trunk/lib/OpenGuides/Feed.pm 2006-06-22 14:51:06 UTC (rev 816) +++ trunk/lib/OpenGuides/Feed.pm 2006-07-06 16:09:18 UTC (rev 817) @@ -103,7 +103,29 @@ =cut sub build_feed_for_nodes { my ($self, $format, @nodes) = @_; + return $self->render_feed_for_nodes($format, undef, 1, @nodes); +} +=item B<build_mini_feed_for_nodes> +For the given feed type, build a mini feed (name and distance) from the + supplied list of nodes. +Will figure out the feed timestamp from the newest node, and output a + last modified header based on this.
+my @nodes = $wiki->search_near_here(); +my $feed_contents = $feed->build_mini_feed_for_nodes("rss", @nodes); +=cut +sub build_mini_feed_for_nodes { + my ($self, $format, @nodes) = @_; + return $self->render_feed_for_nodes($format, undef, 0, @nodes); +} + +=item B<render_feed_for_nodes> +Normally internal method to perform the appropriate building of a feed + based on a list of nodes. +=cut +sub render_feed_for_nodes { + my ($self, $format, $html_url, $is_full, @nodes) = @_; + # Grab our feed maker my $maker = $self->fetch_maker($format);
@@ -123,11 +145,38 @@ my $feed = "Last-Modified: ".$timestamp."\n\n";
# Generate the feed itself - $feed .= $maker->generate_node_list_feed($timestamp, @nodes); + if($is_full) { + $feed .= $maker->generate_node_list_feed($timestamp, @nodes); + } else { + $feed .= $maker->generate_node_name_distance_feed($timestamp, @nodes); + }
return $feed; }
+=item B<default_content_type> +For the given feed type, return the default content type for that feed. + +my $content_type = $feed->default_content_type("rss"); +=cut +sub default_content_type { + my ($self,$feed_type) = @_; + + my $content_type; + + if ($feed_type eq 'rss') { + $content_type = "application/rdf+xml"; + } + elsif ($feed_type eq 'atom') { + $content_type = "application/atom+xml"; + } + else { + croak "Unknown feed type given: $feed_type"; + } + + return $content_type; +} + =item B<fetch_maker> For the given feed type, identify and return the maker routine for feeds of that type. @@ -161,7 +210,7 @@ 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', + html_equiv_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/', @@ -182,7 +231,7 @@ 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', + html_equiv_link => $self->{config}->script_url . '?action=rc', software_name => 'OpenGuides', software_homepage => 'http://openguides.org/', software_version => $self->{og_version},
Modified: trunk/lib/OpenGuides.pm =================================================================== --- trunk/lib/OpenGuides.pm 2006-06-22 14:51:06 UTC (rev 816) +++ trunk/lib/OpenGuides.pm 2006-07-06 16:09:18 UTC (rev 817) @@ -681,17 +681,7 @@ og_version => $VERSION, );
- my $content_type; - - if ($feed_type eq 'rss') { - $content_type = "application/rdf+xml"; - } - elsif ($feed_type eq 'atom') { - $content_type = "application/atom+xml"; - } - else { - croak "Unknown feed type given: $feed_type"; - } + my $content_type = $feed->default_content_type($feed_type);
return ($feed, $content_type); }
Modified: trunk/t/22_feed_recent_changes.t =================================================================== --- trunk/t/22_feed_recent_changes.t 2006-06-22 14:51:06 UTC (rev 816) +++ trunk/t/22_feed_recent_changes.t 2006-07-06 16:09:18 UTC (rev 817) @@ -21,8 +21,9 @@
# Which feed types do we test? my @feed_types = qw( rss atom ); -plan tests => 8 * scalar @feed_types; +plan tests => 9 * scalar @feed_types;
+my %content_types = (rss=>'application/rdf+xml', atom=>'application/atom+xml');
foreach my $feed_type (@feed_types) { # Clear out the database from any previous runs. @@ -49,6 +50,7 @@
my $feed = OpenGuides::Feed->new( wiki => $wiki, config => $config ); + is( $feed->default_content_type($feed_type), $content_types{$feed_type}, "Return the right content type" );
my $feed_output = eval { $feed->make_feed(feed_type => $feed_type, feed_listing => 'recent_changes'); }; is( $@, "", "->make_feed for $feed_type doesn't croak" );
Added: trunk/t/24_feed_node_listings.t =================================================================== --- trunk/t/24_feed_node_listings.t 2006-06-22 14:51:06 UTC (rev 816) +++ trunk/t/24_feed_node_listings.t 2006-07-06 16:09:18 UTC (rev 817) @@ -0,0 +1,176 @@ +use strict; +use Wiki::Toolkit::Setup::SQLite; +use OpenGuides::Config; +use OpenGuides; +use OpenGuides::Feed; +use OpenGuides::Utils; +use Test::More; + +eval { require DBD::SQLite; }; +if ( $@ ) { + plan skip_all => "DBD::SQLite not installed"; + exit 0; +} + +eval { require Wiki::Toolkit::Search::Plucene; }; +if ( $@ ) { + plan skip_all => "Plucene not installed"; + exit 0; +} + + +# Which feed types do we test? +my @feed_types = qw( rss atom ); +plan tests => 17 * scalar @feed_types; + + +foreach my $feed_type (@feed_types) { + # Clear out the database from any previous runs. + unlink "t/node.db"; + unlink <t/indexes/*>; + + Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); + my $config = OpenGuides::Config->new( + vars => { + dbtype => "sqlite", + dbname => "t/node.db", + indexing_directory => "t/indexes", + script_name => "wiki.cgi", + script_url => "http://example.com/", + site_name => "Test Site", + template_path => "./templates", + home_name => "Home", + use_plucene => 1 + } + ); + + # Basic sanity check first. + my $wiki = OpenGuides::Utils->make_wiki_object( config => $config ); + + my $feed = OpenGuides::Feed->new( wiki => $wiki, + config => $config ); + + my $feed_output = eval { $feed->make_feed(feed_type => $feed_type, feed_listing => 'recent_changes'); }; + is( $@, "", "->make_feed for $feed_type doesn't croak" ); + + # Ensure that the feed actually contained rss/atom (a good guide + # that we actually got the right feed) + like( $feed_output, "/$feed_type/i", "Does contain the feed type" ); + + + # Now write some data: 3 versions of one node, and 1 of another + my $guide = OpenGuides->new( config => $config ); + + # Set up CGI parameters ready for a node write. + # Most of these are in here to avoid uninitialised value warnings. + my $q = CGI->new; + $q->param( -name => "content", -value => "foo" ); + $q->param( -name => "categories", -value => "" ); + $q->param( -name => "locales", -value => "" ); + $q->param( -name => "phone", -value => "" ); + $q->param( -name => "fax", -value => "" ); + $q->param( -name => "website", -value => "" ); + $q->param( -name => "hours_text", -value => "" ); + $q->param( -name => "address", -value => "" ); + $q->param( -name => "postcode", -value => "" ); + $q->param( -name => "map_link", -value => "" ); + $q->param( -name => "os_x", -value => "" ); + $q->param( -name => "os_y", -value => "" ); + $q->param( -name => "username", -value => "bob" ); + $q->param( -name => "comment", -value => "foo" ); + $q->param( -name => "edit_type", -value => "Minor tidying" ); + $ENV{REMOTE_ADDR} = "127.0.0.1"; + + # First version of Wombats + my $output = $guide->commit_node( + return_output => 1, + id => "Wombats", + cgi_obj => $q, + ); + my %node = $wiki->retrieve_node(name=>"Wombats"); + + # Now second and third + $q->param( -name => "edit_type", -value => "Normal edit" ); + $q->param( -name => "checksum", -value => $node{"checksum"} ); + $output = $guide->commit_node( + return_output => 1, + id => "Wombats", + cgi_obj => $q, + ); + + %node = $wiki->retrieve_node(name=>"Wombats"); + $q->param( -name => "username", -value => "Kake" ); + $q->param( -name => "checksum", -value => $node{"checksum"} ); + $output = $guide->commit_node( + return_output => 1, + id => "Wombats", + cgi_obj => $q, + ); + + # Now a different node + $q->delete('checksum'); + $output = $guide->commit_node( + return_output => 1, + id => "Badgers", + cgi_obj => $q, + ); + + # Check that the writes went in. + ok( $wiki->node_exists( "Wombats" ), "Wombats written" ); + ok( $wiki->node_exists( "Badgers" ), "Badgers written" ); + is( scalar $wiki->list_node_all_versions("Wombats"), 3, "3 Wombat versions"); + is( scalar $wiki->list_node_all_versions("Badgers"), 1, "1 Badger version"); + + + + # Grab a list of all the nodes + my @all_names = $wiki->list_all_nodes(); + my @all_nodes; + foreach my $name (@all_names) { + my %node = $wiki->retrieve_node(name=>$name); + $node{name} = $name; + push @all_nodes, %node; + } + + # Ask build_feed_for_nodes to make a feed of these + my $output = $feed->build_feed_for_nodes($feed_type,@all_nodes); + + like( $output, qr/<title>Wombats/, "Found wombats" ); + like( $output, qr/<title>Badgers/, "Found badgers" ); + + # Check it had the extra metadata + if($feed_type eq "rss") { + like( $output, qr/dc:date/, "Found metadata" ); + like( $output, qr/modwiki:diff/, "Found metadata" ); + like( $output, qr/modwiki:version/, "Found metadata" ); + } else { + like( $output, qr/<updated>/, "Found metadata" ); + like( $output, qr/<summary>/, "Found metadata" ); + like( $output, qr/<author>/, "Found metadata" ); + } + + + # Grab a list of the different versions of Wombats + my @wombats = $wiki->list_node_all_versions("Wombats"); + + # Ask build_mini_feed_for_nodes to make a mini feed of these + $output = $feed->build_mini_feed_for_nodes($feed_type,@wombats); + + like( $output, qr/<title>Wombats/, "Wombats had wombats" ); + unlike( $output, qr/<title>Badgers/, "Wombats didn't have Badgers" ); + + my @wombats = $output =~ /(<title>Wombats)/g; + is( scalar @wombats, 3, "All 3 wombat versions found" ); + + # Check it was really the mini version + + if($feed_type eq "rss") { + like( $output, qr/<link>/, "Has link" ); + unlike( $output, qr/dc:contributor/, "Really mini version" ); + unlike( $output, qr/modwiki:history/, "Really mini version" ); + } else { + like( $output, qr/<link href=/, "Has link" ); + unlike( $output, qr/<summary>/, "Really mini version" ); + unlike( $output, qr/<author>/, "Really mini version" ); + } +}