change the User Agent string
[catagits/XML-Feed.git] / lib / XML / Feed.pm
index ee6cdd5..d51ab5b 100644 (file)
@@ -1,22 +1,29 @@
-# $Id: Feed.pm 1958 2006-08-14 05:31:27Z btrott $
-
 package XML::Feed;
 use strict;
 
 use base qw( Class::ErrorHandler );
 use Feed::Find;
 use URI::Fetch;
+use LWP::UserAgent;
 use Carp;
-
-our $VERSION = '0.12';
+use Module::Pluggable search_path => "XML::Feed::Format",
+                      require     => 1,
+                      sub_name    => 'formatters';
+
+our $VERSION = '0.50';
+our $MULTIPLE_ENCLOSURES = 0;
+our @formatters;
+BEGIN {
+       @formatters = __PACKAGE__->formatters;
+}
 
 sub new {
     my $class = shift;
     my $format = shift || 'Atom';
-    my $format_class = 'XML::Feed::' . $format;
+    my $format_class = 'XML::Feed::Format::' . $format;
     eval "use $format_class";
     Carp::croak("Unsupported format $format: $@") if $@;
-    my $feed = bless {}, join('::', __PACKAGE__, $format);
+    my $feed = bless {}, join('::', __PACKAGE__, "Format", $format);
     $feed->init_empty(@_) or return $class->error($feed->errstr);
     $feed;
 }
@@ -30,7 +37,10 @@ sub parse {
     my $feed = bless {}, $class;
     my $xml = '';
     if (UNIVERSAL::isa($stream, 'URI')) {
-        my $res = URI::Fetch->fetch($stream)
+        my $ua  = LWP::UserAgent->new;
+        $ua->agent(__PACKAGE__ . "/$VERSION");
+        $ua->env_proxy; # force allowing of proxies
+        my $res = URI::Fetch->fetch($stream, UserAgent => $ua)
             or return $class->error(URI::Fetch->errstr);
         return $class->error("This feed has been permanently removed")
             if $res->status == URI::Fetch::URI_GONE();
@@ -55,10 +65,10 @@ sub parse {
     if ($specified_format) {
         $format = $specified_format;
     } else {
-        $feed->identify_format(\$xml) or return $class->error($feed->errstr);
+        $format = $feed->identify_format(\$xml) or return $class->error($feed->errstr);
     }
 
-    my $format_class = join '::', __PACKAGE__, $format;
+    my $format_class = join '::', __PACKAGE__, "Format", $format;
     eval "use $format_class";
     return $class->error("Unsupported format $format: $@") if $@;
     bless $feed, $format_class;
@@ -67,8 +77,23 @@ sub parse {
 }
 
 sub identify_format {
-    my $feed = shift;
-    my($xml) = @_;
+    my $feed   = shift;
+    my($xml)   = @_;
+       foreach my $class (@formatters) {
+               my ($name) = ($class =~ m!([^:]+)$!);
+               # TODO ugly
+               my $tmp = $$xml;
+               return $name if eval { $class->identify(\$tmp) };
+               return $feed->error($@) if $@;
+       } 
+       return $feed->error("Cannot detect feed type");
+}
+
+sub _get_first_tag {
+       my $class  = shift;
+       my ($xml)  = @_;
+
+
     ## Auto-detect feed type based on first element. This is prone
     ## to breakage, but then again we don't want to parse the whole
     ## feed ourselves.
@@ -78,15 +103,9 @@ sub identify_format {
         my $first = substr $t, 0, 1;
         $tag = $t, last unless $first eq '?' || $first eq '!';
     }
-    return $feed->error("Cannot find first element") unless $tag;
+       die ("Cannot find first element") unless $tag;
     $tag =~ s/^.*://;
-    if ($tag eq 'rss' || $tag eq 'RDF') {
-        return 'RSS';
-    } elsif ($tag eq 'feed') {
-        return 'Atom';
-    } else {
-        return $feed->error("Cannot detect feed type");
-    }
+       return $tag;
 }
 
 sub find_feeds {
@@ -100,7 +119,7 @@ sub find_feeds {
 sub convert {
     my $feed = shift;
     my($format) = @_;
-    my $new = __PACKAGE__->new($format);
+    my $new = XML::Feed->new($format);
     for my $field (qw( title link description language author copyright modified generator )) {
         my $val = $feed->$field();
         next unless defined $val;
@@ -121,9 +140,20 @@ sub splice {
     }
 }
 
+sub _convert_entry {
+    my $feed   = shift;
+    my $entry  = shift;
+    my $feed_format  = ref($feed);   $feed_format  =~ s!^XML::Feed::Format::!!;
+    my $entry_format = ref($entry);  $entry_format =~ s!^XML::Feed::Entry::Format::!!;
+    return $entry if $entry_format eq $feed_format;
+    return $entry->convert($feed_format); 
+}
+
+sub base;
 sub format;
 sub title;
 sub link;
+sub self_link;
 sub description;
 sub language;
 sub author;
@@ -133,10 +163,21 @@ sub generator;
 sub add_entry;
 sub entries;
 sub as_xml;
+sub id;
+sub image;
 
 sub tagline { shift->description(@_) }
 sub items   { $_[0]->entries     }
 
+# RFC 5005
+sub first_link;
+sub last_link;
+sub previous_link;
+sub next_link;
+sub current_link;
+sub prev_archive_link;
+sub next_archive_link;
+
 1;
 __END__
 
@@ -181,10 +222,10 @@ the various syndication formats. The different flavors of RSS and Atom
 handle data in different ways: date handling; summaries and content;
 escaping and quoting; etc. This module attempts to remove those differences
 by providing a wrapper around the formats and the classes implementing
-those formats (I<XML::RSS> and I<XML::Atom::Feed>). For example, dates are
+those formats (L<XML::RSS> and L<XML::Atom::Feed>). For example, dates are
 handled differently in each of the above formats. To provide a unified API for
 date handling, I<XML::Feed> converts all date formats transparently into
-I<DateTime> objects, which it then returns to the caller.
+L<DateTime> objects, which it then returns to the caller.
 
 =head1 USAGE
 
@@ -192,11 +233,16 @@ I<DateTime> objects, which it then returns to the caller.
 
 Creates a new empty I<XML::Feed> object using the format I<$format>.
 
+    $feed = XML::Feed->new('Atom');
+    $feed = XML::Feed->new('RSS');
+    $feed = XML::Feed->new('RSS', version => '0.91');
+
 =head2 XML::Feed->parse($stream)
 
 =head2 XML::Feed->parse($stream, $format)
 
-Parses a syndication feed identified by I<$stream>. I<$stream> can be any
+Parses a syndication feed identified by I<$stream> and returns an
+I<XML::Feed> obhect. I<$stream> can be any
 one of the following:
 
 =over 4
@@ -219,7 +265,7 @@ A URI from which the feed XML will be retrieved.
 
 =back
 
-C<$format> allows you to override format guessing.
+I<$format> allows you to override format guessing.
 
 =head2 XML::Feed->find_feeds($uri)
 
@@ -228,6 +274,10 @@ from that page (using I<E<lt>linkE<gt>> tags).
 
 Returns a list of feed URIs.
 
+=head2 XML::Feed->identify_format($xml)
+
+Given the xml of a feed return what format it is in (C<Atom>, or some version of C<RSS>).
+
 =head2 $feed->convert($format)
 
 Converts the I<XML::Feed> object into the I<$format> format, and returns
@@ -246,6 +296,10 @@ Returns the format of the feed (C<Atom>, or some version of C<RSS>).
 
 The title of the feed/channel.
 
+=head2 $feed->base([ $base ])
+
+The url base of the feed/channel.
+
 =head2 $feed->link([ $uri ])
 
 The permalink of the feed/channel.
@@ -280,14 +334,26 @@ If present, I<$modified> should be a I<DateTime> object.
 
 The generator of the feed.
 
+=head2 $feed->self_link ([ $uri ])
+
+The Atom Self-link of the feed:
+
+L<http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html>
+
+A string.
+
 =head2 $feed->entries
 
 A list of the entries/items in the feed. Returns an array containing
-I<XML::Feed::Entry> objects.
+L<XML::Feed::Entry> objects.
+
+=head2 $feed->items
+
+A synonym (alias) for <$feed-E<gt>entries>.
 
 =head2 $feed->add_entry($entry)
 
-Adds an entry to the feed. I<$entry> should be an I<XML::Feed::Entry>
+Adds an entry to the feed. I<$entry> should be an L<XML::Feed::Entry>
 object in the correct format for the feed.
 
 =head2 $feed->as_xml
@@ -295,22 +361,107 @@ object in the correct format for the feed.
 Returns an XML representation of the feed, in the format determined by
 the current format of the I<$feed> object.
 
+=head2 $feed->first_link ([ $uri ])
+
+The Atom First-link for feed paging and archiving (RFC 5005).
+
+L<http://tools.ietf.org/html/rfc5005>
+
+=head2 $feed->last_link ([ $uri ])
+
+The Atom Last-link for feed paging and archiving.
+
+=head2 $feed->next_link ([ $uri ])
+
+The Atom Next-link for feed paging and archiving.
+
+=head2 $feed->previous_link ([ $uri ])
+
+The Atom Previous-link for feed paging and archiving.
+
+=head2 $feed->current_link ([ $uri ])
+
+The Atom Current-link for feed paging and archiving.
+
+=head2 $feed->next_archive_link ([ $uri ])
+
+The Atom Next-link for feed paging and archiving.
+
+=head2 $feed->prev_archive_link ([ $uri ])
+
+The Atom Prev-Archive-link for feed paging and archiving.
+
 =head1 PACKAGE VARIABLES
 
 =over 4
 
-=item C<$XML::Feed::RSS::PREFERRED_PARSER>
+=item C<$XML::Feed::Format::RSS::PREFERRED_PARSER>
 
 If you want to use another RSS parser class than XML::RSS (default), you can
-change the class by setting C<$PREFERRED_PARSER> variable in XML::Feed::RSS
-package.
+change the class by setting C<$PREFERRED_PARSER> variable in the
+XML::Feed::Format::RSS package.
 
-    $XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
+    $XML::Feed::Format::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
 
 B<Note:> this will only work for parsing feeds, not creating feeds.
 
+B<Note:> Only C<XML::RSS::LibXML> version 0.3004 is known to work at the moment.
+
+=item C<$XML::Feed::MULTIPLE_ENCLOSURES>
+
+Although the RSS specification states that there can be at most one enclosure per item 
+some feeds break this rule.
+
+If this variable is set then C<XML::Feed> captures all of them and makes them available as a list.
+
+Otherwise it returns the last enclosure parsed.
+
+B<Note:> C<XML::RSS> version 1.44 is needed for this to work.
+
 =back
 
+=cut
+
+=head1 VALID FEEDS
+
+For reference, this cgi script will create valid, albeit nonsensical feeds 
+(according to C<http://feedvalidator.org> anyway) for Atom 1.0 and RSS 0.90, 
+0.91, 1.0 and 2.0. 
+
+    #!perl -w
+
+    use strict;
+    use CGI;
+    use CGI::Carp qw(fatalsToBrowser);
+    use DateTime;
+    use XML::Feed;
+
+    my $cgi  = CGI->new;
+    my @args = ( $cgi->param('format') || "Atom" );
+    push @args, ( version => $cgi->param('version') ) if $cgi->param('version');
+
+    my $feed = XML::Feed->new(@args);
+    $feed->id("http://".time.rand()."/");
+    $feed->title('Test Feed');
+    $feed->link($cgi->url);
+    $feed->self_link($cgi->url( -query => 1, -full => 1, -rewrite => 1) );
+    $feed->modified(DateTime->now);
+
+    my $entry = XML::Feed::Entry->new();
+    $entry->id("http://".time.rand()."/");
+    $entry->link("http://example.com");
+    $entry->title("Test entry");
+    $entry->summary("Test summary");
+    $entry->content("Foo");
+    $entry->modified(DateTime->now);
+    $entry->author('test@example.com (Testy McTesterson)');
+    $feed->add_entry($entry);
+
+    my $mime = ("Atom" eq $feed->format) ? "application/atom+xml" : "application/rss+xml";
+    print $cgi->header($mime);
+    print $feed->as_xml;
+
+
 =head1 LICENSE
 
 I<XML::Feed> is free software; you may redistribute it and/or modify it
@@ -318,7 +469,17 @@ under the same terms as Perl itself.
 
 =head1 AUTHOR & COPYRIGHT
 
-Except where otherwise noted, I<XML::Feed> is Copyright 2004-2005
-Six Apart, cpan@sixapart.com. All rights reserved.
+Except where otherwise noted, I<XML::Feed> is Copyright 2004-2008
+Six Apart. All rights reserved.
+
+=head1 SUPPORT
+
+For support contact the XML::Feed mailing list - xml-feed@perlhacks.com.
+
+=head1 SOURCE CODE
+
+The latest version of I<XML::Feed> can be found at
+
+    http://github.com/davorg/XML-Feed
 
 =cut