6 use base qw( Class::ErrorHandler );
11 use Module::Pluggable search_path => "XML::Feed::Format",
13 sub_name => 'formatters';
15 our $VERSION = '0.46';
16 our $MULTIPLE_ENCLOSURES = 0;
19 @formatters = __PACKAGE__->formatters;
24 my $format = shift || 'Atom';
25 my $format_class = 'XML::Feed::Format::' . $format;
26 eval "use $format_class";
27 Carp::croak("Unsupported format $format: $@") if $@;
28 my $feed = bless {}, join('::', __PACKAGE__, "Format", $format);
29 $feed->init_empty(@_) or return $class->error($feed->errstr);
37 my($stream, $specified_format) = @_;
38 return $class->error("Stream parameter is required") unless $stream;
39 my $feed = bless {}, $class;
41 if (UNIVERSAL::isa($stream, 'URI')) {
42 my $ua = LWP::UserAgent->new;
43 $ua->env_proxy; # force allowing of proxies
44 my $res = URI::Fetch->fetch($stream, UserAgent => $ua)
45 or return $class->error(URI::Fetch->errstr);
46 return $class->error("This feed has been permanently removed")
47 if $res->status == URI::Fetch::URI_GONE();
49 } elsif (ref($stream) eq 'SCALAR') {
51 } elsif (ref($stream)) {
52 while (read($stream, my($chunk), 8192)) {
57 or return $class->error("Can't open $stream: $!");
58 while (read $fh, my($chunk), 8192) {
63 return $class->error("Can't get feed XML content from $stream")
66 if ($specified_format) {
67 $format = $specified_format;
69 $format = $feed->identify_format(\$xml) or return $class->error($feed->errstr);
72 my $format_class = join '::', __PACKAGE__, "Format", $format;
73 eval "use $format_class";
74 return $class->error("Unsupported format $format: $@") if $@;
75 bless $feed, $format_class;
76 $feed->init_string(\$xml) or return $class->error($feed->errstr);
83 foreach my $class (@formatters) {
84 my ($name) = ($class =~ m!([^:]+)$!);
87 return $name if eval { $class->identify(\$tmp) };
88 return $feed->error($@) if $@;
90 return $feed->error("Cannot detect feed type");
98 ## Auto-detect feed type based on first element. This is prone
99 ## to breakage, but then again we don't want to parse the whole
102 while ($$xml =~ /<(\S+)/sg) {
103 (my $t = $1) =~ tr/a-zA-Z0-9:\-\?!//cd;
104 my $first = substr $t, 0, 1;
105 $tag = $t, last unless $first eq '?' || $first eq '!';
107 die ("Cannot find first element") unless $tag;
115 my @feeds = Feed::Find->find($uri)
116 or return $class->error(Feed::Find->errstr);
123 my $new = XML::Feed->new($format);
124 for my $field (qw( title link description language author copyright modified generator )) {
125 my $val = $feed->$field();
126 next unless defined $val;
129 for my $entry ($feed->entries) {
130 $new->add_entry($entry->convert($format));
138 my %ids = map { $_->id => 1 } $feed->entries;
139 for my $entry ($other->entries) {
140 $feed->add_entry($entry) unless $ids{$entry->id}++;
147 my $feed_format = ref($feed); $feed_format =~ s!^XML::Feed::Format::!!;
148 my $entry_format = ref($entry); $entry_format =~ s!^XML::Feed::Entry::Format::!!;
149 return $entry if $entry_format eq $feed_format;
150 return $entry->convert($feed_format);
169 sub tagline { shift->description(@_) }
170 sub items { $_[0]->entries }
177 XML::Feed - Syndication feed parser and auto-discovery
182 my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
183 or die XML::Feed->errstr;
184 print $feed->title, "\n";
185 for my $entry ($feed->entries) {
188 ## Find all of the syndication feeds on a given page, using
190 my @feeds = XML::Feed->find_feeds('http://example.com/');
194 I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
195 also implements feed auto-discovery for finding feeds, given a URI.
197 I<XML::Feed> supports the following syndication feed formats:
211 The goal of I<XML::Feed> is to provide a unified API for parsing and using
212 the various syndication formats. The different flavors of RSS and Atom
213 handle data in different ways: date handling; summaries and content;
214 escaping and quoting; etc. This module attempts to remove those differences
215 by providing a wrapper around the formats and the classes implementing
216 those formats (L<XML::RSS> and L<XML::Atom::Feed>). For example, dates are
217 handled differently in each of the above formats. To provide a unified API for
218 date handling, I<XML::Feed> converts all date formats transparently into
219 L<DateTime> objects, which it then returns to the caller.
223 =head2 XML::Feed->new($format)
225 Creates a new empty I<XML::Feed> object using the format I<$format>.
227 $feed = XML::Feed->new('Atom');
228 $feed = XML::Feed->new('RSS');
229 $feed = XML::Feed->new('RSS', version => '0.91');
231 =head2 XML::Feed->parse($stream)
233 =head2 XML::Feed->parse($stream, $format)
235 Parses a syndication feed identified by I<$stream> and returns an
236 I<XML::Feed> obhect. I<$stream> can be any
237 one of the following:
241 =item * Scalar reference
243 A reference to string containing the XML body of the feed.
247 An open filehandle from which the feed XML will be read.
251 The name of a file containing the feed XML.
255 A URI from which the feed XML will be retrieved.
259 I<$format> allows you to override format guessing.
261 =head2 XML::Feed->find_feeds($uri)
263 Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
264 from that page (using I<E<lt>linkE<gt>> tags).
266 Returns a list of feed URIs.
268 =head2 XML::Feed->identify_format($xml)
270 Given the xml of a feed return what format it is in (C<Atom>, or some version of C<RSS>).
272 =head2 $feed->convert($format)
274 Converts the I<XML::Feed> object into the I<$format> format, and returns
277 =head2 $feed->splice($other_feed)
279 Splices in all of the entries from the feed I<$other_feed> into I<$feed>,
280 skipping posts that are already in I<$feed>.
284 Returns the format of the feed (C<Atom>, or some version of C<RSS>).
286 =head2 $feed->title([ $title ])
288 The title of the feed/channel.
290 =head2 $feed->base([ $base ])
292 The url base of the feed/channel.
294 =head2 $feed->link([ $uri ])
296 The permalink of the feed/channel.
298 =head2 $feed->tagline([ $tagline ])
300 The description or tagline of the feed/channel.
302 =head2 $feed->description([ $description ])
304 Alias for I<$feed-E<gt>tagline>.
306 =head2 $feed->author([ $author ])
308 The author of the feed/channel.
310 =head2 $feed->language([ $language ])
312 The language of the feed.
314 =head2 $feed->copyright([ $copyright ])
316 The copyright notice of the feed.
318 =head2 $feed->modified([ $modified ])
320 A I<DateTime> object representing the last-modified date of the feed.
322 If present, I<$modified> should be a I<DateTime> object.
324 =head2 $feed->generator([ $generator ])
326 The generator of the feed.
328 =head2 $feed->self_link ([ $uri ])
330 The Atom Self-link of the feed:
332 L<http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html>
336 =head2 $feed->entries
338 A list of the entries/items in the feed. Returns an array containing
339 L<XML::Feed::Entry> objects.
343 A synonym (alias) for <$feed-E<gt>entries>.
345 =head2 $feed->add_entry($entry)
347 Adds an entry to the feed. I<$entry> should be an L<XML::Feed::Entry>
348 object in the correct format for the feed.
352 Returns an XML representation of the feed, in the format determined by
353 the current format of the I<$feed> object.
355 =head1 PACKAGE VARIABLES
359 =item C<$XML::Feed::Format::RSS::PREFERRED_PARSER>
361 If you want to use another RSS parser class than XML::RSS (default), you can
362 change the class by setting C<$PREFERRED_PARSER> variable in the
363 XML::Feed::Format::RSS package.
365 $XML::Feed::Format::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
367 B<Note:> this will only work for parsing feeds, not creating feeds.
369 B<Note:> Only C<XML::RSS::LibXML> version 0.3004 is known to work at the moment.
371 =item C<$XML::Feed::MULTIPLE_ENCLOSURES>
373 Although the RSS specification states that there can be at most one enclosure per item
374 some feeds break this rule.
376 If this variable is set then C<XML::Feed> captures all of them and makes them available as a list.
378 Otherwise it returns the last enclosure parsed.
380 B<Note:> C<XML::RSS> version 1.44 is needed for this to work.
388 For reference, this cgi script will create valid, albeit nonsensical feeds
389 (according to C<http://feedvalidator.org> anyway) for Atom 1.0 and RSS 0.90,
396 use CGI::Carp qw(fatalsToBrowser);
401 my @args = ( $cgi->param('format') || "Atom" );
402 push @args, ( version => $cgi->param('version') ) if $cgi->param('version');
404 my $feed = XML::Feed->new(@args);
405 $feed->id("http://".time.rand()."/");
406 $feed->title('Test Feed');
407 $feed->link($cgi->url);
408 $feed->self_link($cgi->url( -query => 1, -full => 1, -rewrite => 1) );
409 $feed->modified(DateTime->now);
411 my $entry = XML::Feed::Entry->new();
412 $entry->id("http://".time.rand()."/");
413 $entry->link("http://example.com");
414 $entry->title("Test entry");
415 $entry->summary("Test summary");
416 $entry->content("Foo");
417 $entry->modified(DateTime->now);
418 $entry->author('test@example.com (Testy McTesterson)');
419 $feed->add_entry($entry);
421 my $mime = ("Atom" eq $feed->format) ? "application/atom+xml" : "application/rss+xml";
422 print $cgi->header($mime);
428 I<XML::Feed> is free software; you may redistribute it and/or modify it
429 under the same terms as Perl itself.
431 =head1 AUTHOR & COPYRIGHT
433 Except where otherwise noted, I<XML::Feed> is Copyright 2004-2008
434 Six Apart. All rights reserved.
438 For support contact the XML::Feed mailing list - xml-feed@perlhacks.com.
442 The latest version of I<XML::Feed> can be found at
444 http://github.com/davorg/XML-Feed