Author: nick Date: 2006-06-22 15:30:44 +0100 (Thu, 22 Jun 2006) New Revision: 814
Modified: trunk/lib/OpenGuides.pm trunk/t/53_show_index.t Log: Support RSS and Atom for category and locale indicies, + test for this
Modified: trunk/lib/OpenGuides.pm =================================================================== --- trunk/lib/OpenGuides.pm 2006-06-22 14:11:27 UTC (rev 813) +++ trunk/lib/OpenGuides.pm 2006-06-22 14:30:44 UTC (rev 814) @@ -484,6 +484,13 @@ format => "rdf", );
+ # RSS / Atom version (recent changes style). + $guide->show_index( + type => "locale", + value => "Holborn", + format => "rss", + ); + # Or return output as a string (useful for writing tests). $guide->show_index( type => "category", @@ -567,6 +574,24 @@ $tt_vars{display_google_maps} = 1; # override for this page $template = "map_index.tt";
+ } elsif( $args{format} eq "rss" || $args{format} eq "atom") { + # They really wanted a recent changes style rss/atom feed + my $feed_type = $args{format}; + my ($feed,$content_type) = $self->get_feed_and_content_type($feed_type); + + # Grab the actual node data out of @nodes + my @node_data; + foreach my $node (@nodes) { + $node->{node_data}->{name} = $node->{name}; + push @node_data, $node->{node_data}; + } + + my $output = "Content-Type: ".$content_type."\n"; + $output .= $feed->build_feed_for_nodes($feed_type, @node_data); + + return $output if $args{return_output}; + print $output; + return; } } else { $template = "site_index.tt"; @@ -640,6 +665,37 @@ print $output; }
+=item B<get_feed_and_content_type> + +Fetch the OpenGuides feed object, and the output content type, for the +supplied feed type. + +Handles all the setup for the OpenGuides feed object. +=cut +sub get_feed_and_content_type { + my ($self, $feed_type) = @_; + + my $feed = OpenGuides::Feed->new( + wiki => $self->wiki, + config => $self->config, + 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"; + } + + return ($feed, $content_type); +} + =item B<display_feed>
# Last ten non-minor edits to Hammersmith pages in RSS 1.0 format @@ -712,24 +768,11 @@ }
- my $feed = OpenGuides::Feed->new( - wiki => $self->wiki, - config => $self->config, - og_version => $VERSION, - ); + # Get the feed object, and the content type + my ($feed,$content_type) = $self->get_feed_and_content_type($feed_type);
- my $output; + my $output = "Content-Type: ".$content_type."\n";
- 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"; - } - # Get the feed, and the timestamp, in one go my ($feed_output, $feed_timestamp) = $feed->make_feed( %criteria );
Modified: trunk/t/53_show_index.t =================================================================== --- trunk/t/53_show_index.t 2006-06-22 14:11:27 UTC (rev 813) +++ trunk/t/53_show_index.t 2006-06-22 14:30:44 UTC (rev 814) @@ -2,13 +2,13 @@ use Wiki::Toolkit::Setup::SQLite; use OpenGuides; use OpenGuides::Test; -use Test::More tests => 6; +use Test::More tests => 17; # 19 when all enabled
eval { require DBD::SQLite; }; my $have_sqlite = $@ ? 0 : 1;
SKIP: { - skip "DBD::SQLite not installed - no database to test with", 6 + skip "DBD::SQLite not installed - no database to test with", 17 unless $have_sqlite;
Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); @@ -32,6 +32,8 @@ $wiki->write_node( "Test Page 2", "foo", undef, { category => "Alpha" } ) or die "Couldn't write node"; + + # Test the normal, HTML version my $output = eval { $guide->show_index( type => "category", @@ -44,6 +46,7 @@ "...and includes correct links" ); unlike( $output, qr|<title>\s*-|, "...sets <title> correctly" );
+ # Test the RDF version $output = $guide->show_index( type => "category", value => "Alpha", @@ -52,4 +55,43 @@ ); like( $output, qr|Content-Type: application/rdf+xml|, "RDF output gets content-type of application/rdf+xml" ); + like( $output, qr|<rdf:RDF|, "Really is rdf" ); + like( $output, qr|dc:titleCategory Alpha</dc:title>|, "Right rdf title" ); + my @entries = ($output =~ /(<rdf:li>)/g); + is( 2, scalar @entries, "Right number of nodes included in rdf" ); + + # Test the RSS version + $output = eval { + $guide->show_index( + type => "category", + value => "Alpha", + return_output => 1, + format => "rss", + ); + }; + is( $@, "", "->show_index doesn't die when asked for rss" ); + like( $output, qr|Content-Type: application/rdf+xml|, + "RSS output gets content-type of application/rdf+xml" ); + like( $output, "/<rdf:RDF.*?http://purl.org/rss//s", "Really is rss" ); + #like( $output, qr|<title>Category Alpha</title>|, "Right rss title" ); + my @entries = ($output =~ /(</item>)/g); + is( 2, scalar @entries, "Right number of nodes included in rss" ); +warn($output); + + # Test the Atom version + $output = eval { + $guide->show_index( + type => "category", + value => "Alpha", + return_output => 1, + format => "atom", + ); + }; + is( $@, "", "->show_index doesn't die when asked for atom" ); + like( $output, qr|Content-Type: application/atom+xml|, + "Atom output gets content-type of application/atom+xml" ); + like( $output, qr|<feed|, "Really is atom" ); + #like( $output, qr|<title>Category Alpha</title>|, "Right atom title" ); + my @entries = ($output =~ /(<entry>)/g); + is( 2, scalar @entries, "Right number of nodes included in atom" ); }
openguides-commits@lists.openguides.org