4 use base qw( Class::ErrorHandler );
9 use Module::Pluggable search_path => "XML::Feed::Format",
11 sub_name => 'formatters';
13 our $VERSION = '0.49';
14 our $MULTIPLE_ENCLOSURES = 0;
17 @formatters = __PACKAGE__->formatters;
22 my $format = shift || 'Atom';
23 my $format_class = 'XML::Feed::Format::' . $format;
24 eval "use $format_class";
25 Carp::croak("Unsupported format $format: $@") if $@;
26 my $feed = bless {}, join('::', __PACKAGE__, "Format", $format);
27 $feed->init_empty(@_) or return $class->error($feed->errstr);
35 my($stream, $specified_format) = @_;
36 return $class->error("Stream parameter is required") unless $stream;
37 my $feed = bless {}, $class;
39 if (UNIVERSAL::isa($stream, 'URI')) {
40 my $ua = LWP::UserAgent->new;
41 $ua->env_proxy; # force allowing of proxies
42 my $res = URI::Fetch->fetch($stream, UserAgent => $ua)
43 or return $class->error(URI::Fetch->errstr);
44 return $class->error("This feed has been permanently removed")
45 if $res->status == URI::Fetch::URI_GONE();
47 } elsif (ref($stream) eq 'SCALAR') {
49 } elsif (ref($stream)) {
50 while (read($stream, my($chunk), 8192)) {
55 or return $class->error("Can't open $stream: $!");
56 while (read $fh, my($chunk), 8192) {
61 return $class->error("Can't get feed XML content from $stream")
64 if ($specified_format) {
65 $format = $specified_format;
67 $format = $feed->identify_format(\$xml) or return $class->error($feed->errstr);
70 my $format_class = join '::', __PACKAGE__, "Format", $format;
71 eval "use $format_class";
72 return $class->error("Unsupported format $format: $@") if $@;
73 bless $feed, $format_class;
74 $feed->init_string(\$xml) or return $class->error($feed->errstr);
81 foreach my $class (@formatters) {
82 my ($name) = ($class =~ m!([^:]+)$!);
85 return $name if eval { $class->identify(\$tmp) };
86 return $feed->error($@) if $@;
88 return $feed->error("Cannot detect feed type");
96 ## Auto-detect feed type based on first element. This is prone
97 ## to breakage, but then again we don't want to parse the whole
100 while ($$xml =~ /<(\S+)/sg) {
101 (my $t = $1) =~ tr/a-zA-Z0-9:\-\?!//cd;
102 my $first = substr $t, 0, 1;
103 $tag = $t, last unless $first eq '?' || $first eq '!';
105 die ("Cannot find first element") unless $tag;
113 my @feeds = Feed::Find->find($uri)
114 or return $class->error(Feed::Find->errstr);
121 my $new = XML::Feed->new($format);
122 for my $field (qw( title link description language author copyright modified generator )) {
123 my $val = $feed->$field();
124 next unless defined $val;
127 for my $entry ($feed->entries) {
128 $new->add_entry($entry->convert($format));
136 my %ids = map { $_->id => 1 } $feed->entries;
137 for my $entry ($other->entries) {
138 $feed->add_entry($entry) unless $ids{$entry->id}++;
145 my $feed_format = ref($feed); $feed_format =~ s!^XML::Feed::Format::!!;
146 my $entry_format = ref($entry); $entry_format =~ s!^XML::Feed::Entry::Format::!!;
147 return $entry if $entry_format eq $feed_format;
148 return $entry->convert($feed_format);
168 sub tagline { shift->description(@_) }
169 sub items { $_[0]->entries }
177 sub prev_archive_link;
178 sub next_archive_link;
185 XML::Feed - Syndication feed parser and auto-discovery
190 my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
191 or die XML::Feed->errstr;
192 print $feed->title, "\n";
193 for my $entry ($feed->entries) {
196 ## Find all of the syndication feeds on a given page, using
198 my @feeds = XML::Feed->find_feeds('http://example.com/');
202 I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
203 also implements feed auto-discovery for finding feeds, given a URI.
205 I<XML::Feed> supports the following syndication feed formats:
219 The goal of I<XML::Feed> is to provide a unified API for parsing and using
220 the various syndication formats. The different flavors of RSS and Atom
221 handle data in different ways: date handling; summaries and content;
222 escaping and quoting; etc. This module attempts to remove those differences
223 by providing a wrapper around the formats and the classes implementing
224 those formats (L<XML::RSS> and L<XML::Atom::Feed>). For example, dates are
225 handled differently in each of the above formats. To provide a unified API for
226 date handling, I<XML::Feed> converts all date formats transparently into
227 L<DateTime> objects, which it then returns to the caller.
231 =head2 XML::Feed->new($format)
233 Creates a new empty I<XML::Feed> object using the format I<$format>.
235 $feed = XML::Feed->new('Atom');
236 $feed = XML::Feed->new('RSS');
237 $feed = XML::Feed->new('RSS', version => '0.91');
239 =head2 XML::Feed->parse($stream)
241 =head2 XML::Feed->parse($stream, $format)
243 Parses a syndication feed identified by I<$stream> and returns an
244 I<XML::Feed> obhect. I<$stream> can be any
245 one of the following:
249 =item * Scalar reference
251 A reference to string containing the XML body of the feed.
255 An open filehandle from which the feed XML will be read.
259 The name of a file containing the feed XML.
263 A URI from which the feed XML will be retrieved.
267 I<$format> allows you to override format guessing.
269 =head2 XML::Feed->find_feeds($uri)
271 Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
272 from that page (using I<E<lt>linkE<gt>> tags).
274 Returns a list of feed URIs.
276 =head2 XML::Feed->identify_format($xml)
278 Given the xml of a feed return what format it is in (C<Atom>, or some version of C<RSS>).
280 =head2 $feed->convert($format)
282 Converts the I<XML::Feed> object into the I<$format> format, and returns
285 =head2 $feed->splice($other_feed)
287 Splices in all of the entries from the feed I<$other_feed> into I<$feed>,
288 skipping posts that are already in I<$feed>.
292 Returns the format of the feed (C<Atom>, or some version of C<RSS>).
294 =head2 $feed->title([ $title ])
296 The title of the feed/channel.
298 =head2 $feed->base([ $base ])
300 The url base of the feed/channel.
302 =head2 $feed->link([ $uri ])
304 The permalink of the feed/channel.
306 =head2 $feed->tagline([ $tagline ])
308 The description or tagline of the feed/channel.
310 =head2 $feed->description([ $description ])
312 Alias for I<$feed-E<gt>tagline>.
314 =head2 $feed->author([ $author ])
316 The author of the feed/channel.
318 =head2 $feed->language([ $language ])
320 The language of the feed.
322 =head2 $feed->copyright([ $copyright ])
324 The copyright notice of the feed.
326 =head2 $feed->modified([ $modified ])
328 A I<DateTime> object representing the last-modified date of the feed.
330 If present, I<$modified> should be a I<DateTime> object.
332 =head2 $feed->generator([ $generator ])
334 The generator of the feed.
336 =head2 $feed->self_link ([ $uri ])
338 The Atom Self-link of the feed:
340 L<http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html>
344 =head2 $feed->entries
346 A list of the entries/items in the feed. Returns an array containing
347 L<XML::Feed::Entry> objects.
351 A synonym (alias) for <$feed-E<gt>entries>.
353 =head2 $feed->add_entry($entry)
355 Adds an entry to the feed. I<$entry> should be an L<XML::Feed::Entry>
356 object in the correct format for the feed.
360 Returns an XML representation of the feed, in the format determined by
361 the current format of the I<$feed> object.
363 =head2 $feed->first_link ([ $uri ])
365 The Atom First-link for feed paging and archiving (RFC 5005).
367 L<http://tools.ietf.org/html/rfc5005>
369 =head2 $feed->last_link ([ $uri ])
371 The Atom Last-link for feed paging and archiving.
373 =head2 $feed->next_link ([ $uri ])
375 The Atom Next-link for feed paging and archiving.
377 =head2 $feed->previous_link ([ $uri ])
379 The Atom Previous-link for feed paging and archiving.
381 =head2 $feed->current_link ([ $uri ])
383 The Atom Current-link for feed paging and archiving.
385 =head2 $feed->next_archive_link ([ $uri ])
387 The Atom Next-link for feed paging and archiving.
389 =head2 $feed->prev_archive_link ([ $uri ])
391 The Atom Prev-Archive-link for feed paging and archiving.
393 =head1 PACKAGE VARIABLES
397 =item C<$XML::Feed::Format::RSS::PREFERRED_PARSER>
399 If you want to use another RSS parser class than XML::RSS (default), you can
400 change the class by setting C<$PREFERRED_PARSER> variable in the
401 XML::Feed::Format::RSS package.
403 $XML::Feed::Format::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
405 B<Note:> this will only work for parsing feeds, not creating feeds.
407 B<Note:> Only C<XML::RSS::LibXML> version 0.3004 is known to work at the moment.
409 =item C<$XML::Feed::MULTIPLE_ENCLOSURES>
411 Although the RSS specification states that there can be at most one enclosure per item
412 some feeds break this rule.
414 If this variable is set then C<XML::Feed> captures all of them and makes them available as a list.
416 Otherwise it returns the last enclosure parsed.
418 B<Note:> C<XML::RSS> version 1.44 is needed for this to work.
426 For reference, this cgi script will create valid, albeit nonsensical feeds
427 (according to C<http://feedvalidator.org> anyway) for Atom 1.0 and RSS 0.90,
434 use CGI::Carp qw(fatalsToBrowser);
439 my @args = ( $cgi->param('format') || "Atom" );
440 push @args, ( version => $cgi->param('version') ) if $cgi->param('version');
442 my $feed = XML::Feed->new(@args);
443 $feed->id("http://".time.rand()."/");
444 $feed->title('Test Feed');
445 $feed->link($cgi->url);
446 $feed->self_link($cgi->url( -query => 1, -full => 1, -rewrite => 1) );
447 $feed->modified(DateTime->now);
449 my $entry = XML::Feed::Entry->new();
450 $entry->id("http://".time.rand()."/");
451 $entry->link("http://example.com");
452 $entry->title("Test entry");
453 $entry->summary("Test summary");
454 $entry->content("Foo");
455 $entry->modified(DateTime->now);
456 $entry->author('test@example.com (Testy McTesterson)');
457 $feed->add_entry($entry);
459 my $mime = ("Atom" eq $feed->format) ? "application/atom+xml" : "application/rss+xml";
460 print $cgi->header($mime);
466 I<XML::Feed> is free software; you may redistribute it and/or modify it
467 under the same terms as Perl itself.
469 =head1 AUTHOR & COPYRIGHT
471 Except where otherwise noted, I<XML::Feed> is Copyright 2004-2008
472 Six Apart. All rights reserved.
476 For support contact the XML::Feed mailing list - xml-feed@perlhacks.com.
480 The latest version of I<XML::Feed> can be found at
482 http://github.com/davorg/XML-Feed