Bump version for release.
[catagits/XML-Feed.git] / lib / XML / Feed.pm
CommitLineData
0d5e38d1 1package XML::Feed;
2use strict;
3
62d92771 4use base qw( Class::ErrorHandler );
62d92771 5use Feed::Find;
fe71566d 6use URI::Fetch;
d39809aa 7use LWP::UserAgent;
973e1f9e 8use Carp;
9b6bc912 9use Module::Pluggable search_path => "XML::Feed::Format",
10 require => 1,
11 sub_name => 'formatters';
0d5e38d1 12
dfeacbfd 13our $VERSION = '0.50';
b1aa7a62 14our $MULTIPLE_ENCLOSURES = 0;
9b6bc912 15our @formatters;
16BEGIN {
17 @formatters = __PACKAGE__->formatters;
18}
973e1f9e 19
20sub new {
21 my $class = shift;
4e9c4625 22 my $format = shift || 'Atom';
729cd7a8 23 my $format_class = 'XML::Feed::Format::' . $format;
973e1f9e 24 eval "use $format_class";
25 Carp::croak("Unsupported format $format: $@") if $@;
729cd7a8 26 my $feed = bless {}, join('::', __PACKAGE__, "Format", $format);
4e9c4625 27 $feed->init_empty(@_) or return $class->error($feed->errstr);
973e1f9e 28 $feed;
29}
30
31sub init_empty { 1 }
0d5e38d1 32
33sub parse {
34 my $class = shift;
41e8c132 35 my($stream, $specified_format) = @_;
0d5e38d1 36 return $class->error("Stream parameter is required") unless $stream;
37 my $feed = bless {}, $class;
38 my $xml = '';
39 if (UNIVERSAL::isa($stream, 'URI')) {
d39809aa 40 my $ua = LWP::UserAgent->new;
41 $ua->env_proxy; # force allowing of proxies
42 my $res = URI::Fetch->fetch($stream, UserAgent => $ua)
fe71566d 43 or return $class->error(URI::Fetch->errstr);
44 return $class->error("This feed has been permanently removed")
b3b6d2fc 45 if $res->status == URI::Fetch::URI_GONE();
fe71566d 46 $xml = $res->content;
0d5e38d1 47 } elsif (ref($stream) eq 'SCALAR') {
48 $xml = $$stream;
49 } elsif (ref($stream)) {
50 while (read($stream, my($chunk), 8192)) {
51 $xml .= $chunk;
52 }
53 } else {
54 open my $fh, $stream
55 or return $class->error("Can't open $stream: $!");
56 while (read $fh, my($chunk), 8192) {
57 $xml .= $chunk;
58 }
59 close $fh;
60 }
61 return $class->error("Can't get feed XML content from $stream")
62 unless $xml;
41e8c132 63 my $format;
64 if ($specified_format) {
65 $format = $specified_format;
66 } else {
0c81cb25 67 $format = $feed->identify_format(\$xml) or return $class->error($feed->errstr);
41e8c132 68 }
69
729cd7a8 70 my $format_class = join '::', __PACKAGE__, "Format", $format;
fe71566d 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);
75 $feed;
76}
77
78sub identify_format {
9b6bc912 79 my $feed = shift;
80 my($xml) = @_;
81 foreach my $class (@formatters) {
82 my ($name) = ($class =~ m!([^:]+)$!);
83 # TODO ugly
84 my $tmp = $$xml;
85 return $name if eval { $class->identify(\$tmp) };
86 return $feed->error($@) if $@;
87 }
88 return $feed->error("Cannot detect feed type");
89}
90
91sub _get_first_tag {
92 my $class = shift;
93 my ($xml) = @_;
94
95
0d5e38d1 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
98 ## feed ourselves.
a749d9b9 99 my $tag;
fe71566d 100 while ($$xml =~ /<(\S+)/sg) {
62d92771 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 '!';
a749d9b9 104 }
9b6bc912 105 die ("Cannot find first element") unless $tag;
0d5e38d1 106 $tag =~ s/^.*://;
9b6bc912 107 return $tag;
0d5e38d1 108}
109
110sub find_feeds {
111 my $class = shift;
112 my($uri) = @_;
62d92771 113 my @feeds = Feed::Find->find($uri)
114 or return $class->error(Feed::Find->errstr);
0d5e38d1 115 @feeds;
116}
117
973e1f9e 118sub convert {
119 my $feed = shift;
120 my($format) = @_;
729cd7a8 121 my $new = XML::Feed->new($format);
ecac864a 122 for my $field (qw( title link description language author copyright modified generator )) {
23103173 123 my $val = $feed->$field();
124 next unless defined $val;
125 $new->$field($val);
973e1f9e 126 }
127 for my $entry ($feed->entries) {
128 $new->add_entry($entry->convert($format));
129 }
130 $new;
131}
132
23103173 133sub splice {
134 my $feed = shift;
135 my($other) = @_;
136 my %ids = map { $_->id => 1 } $feed->entries;
137 for my $entry ($other->entries) {
138 $feed->add_entry($entry) unless $ids{$entry->id}++;
139 }
140}
141
33d4cb3f 142sub _convert_entry {
143 my $feed = shift;
144 my $entry = shift;
729cd7a8 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::!!;
33d4cb3f 147 return $entry if $entry_format eq $feed_format;
148 return $entry->convert($feed_format);
149}
150
5383a560 151sub base;
0d5e38d1 152sub format;
153sub title;
154sub link;
9a36f82c 155sub self_link;
0d5e38d1 156sub description;
157sub language;
973e1f9e 158sub author;
0d5e38d1 159sub copyright;
160sub modified;
161sub generator;
973e1f9e 162sub add_entry;
0d5e38d1 163sub entries;
973e1f9e 164sub as_xml;
813f78d8 165sub id;
f2512641 166sub image;
0d5e38d1 167
973e1f9e 168sub tagline { shift->description(@_) }
0d5e38d1 169sub items { $_[0]->entries }
170
02de1790 171# RFC 5005
172sub first_link;
173sub last_link;
174sub previous_link;
175sub next_link;
176sub current_link;
177sub prev_archive_link;
178sub next_archive_link;
179
0d5e38d1 1801;
181__END__
182
183=head1 NAME
184
185XML::Feed - Syndication feed parser and auto-discovery
186
187=head1 SYNOPSIS
188
189 use XML::Feed;
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) {
194 }
195
196 ## Find all of the syndication feeds on a given page, using
197 ## auto-discovery.
198 my @feeds = XML::Feed->find_feeds('http://example.com/');
199
200=head1 DESCRIPTION
201
202I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
203also implements feed auto-discovery for finding feeds, given a URI.
204
205I<XML::Feed> supports the following syndication feed formats:
206
207=over 4
208
209=item * RSS 0.91
210
211=item * RSS 1.0
212
213=item * RSS 2.0
214
215=item * Atom
216
217=back
218
219The goal of I<XML::Feed> is to provide a unified API for parsing and using
220the various syndication formats. The different flavors of RSS and Atom
221handle data in different ways: date handling; summaries and content;
222escaping and quoting; etc. This module attempts to remove those differences
223by providing a wrapper around the formats and the classes implementing
200ce863 224those formats (L<XML::RSS> and L<XML::Atom::Feed>). For example, dates are
0d5e38d1 225handled differently in each of the above formats. To provide a unified API for
226date handling, I<XML::Feed> converts all date formats transparently into
200ce863 227L<DateTime> objects, which it then returns to the caller.
0d5e38d1 228
229=head1 USAGE
230
973e1f9e 231=head2 XML::Feed->new($format)
232
233Creates a new empty I<XML::Feed> object using the format I<$format>.
234
813f78d8 235 $feed = XML::Feed->new('Atom');
236 $feed = XML::Feed->new('RSS');
237 $feed = XML::Feed->new('RSS', version => '0.91');
238
0d5e38d1 239=head2 XML::Feed->parse($stream)
240
41e8c132 241=head2 XML::Feed->parse($stream, $format)
242
200ce863 243Parses a syndication feed identified by I<$stream> and returns an
244I<XML::Feed> obhect. I<$stream> can be any
0d5e38d1 245one of the following:
246
247=over 4
248
249=item * Scalar reference
250
251A reference to string containing the XML body of the feed.
252
253=item * Filehandle
254
255An open filehandle from which the feed XML will be read.
256
257=item * File name
258
259The name of a file containing the feed XML.
260
261=item * URI object
262
263A URI from which the feed XML will be retrieved.
264
265=back
266
8c30ad3d 267I<$format> allows you to override format guessing.
41e8c132 268
0d5e38d1 269=head2 XML::Feed->find_feeds($uri)
270
271Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
272from that page (using I<E<lt>linkE<gt>> tags).
273
274Returns a list of feed URIs.
275
8c30ad3d 276=head2 XML::Feed->identify_format($xml)
277
278Given the xml of a feed return what format it is in (C<Atom>, or some version of C<RSS>).
279
973e1f9e 280=head2 $feed->convert($format)
281
282Converts the I<XML::Feed> object into the I<$format> format, and returns
283the new object.
284
23103173 285=head2 $feed->splice($other_feed)
286
287Splices in all of the entries from the feed I<$other_feed> into I<$feed>,
288skipping posts that are already in I<$feed>.
289
0d5e38d1 290=head2 $feed->format
291
292Returns the format of the feed (C<Atom>, or some version of C<RSS>).
293
973e1f9e 294=head2 $feed->title([ $title ])
0d5e38d1 295
296The title of the feed/channel.
297
5383a560 298=head2 $feed->base([ $base ])
299
300The url base of the feed/channel.
301
973e1f9e 302=head2 $feed->link([ $uri ])
0d5e38d1 303
304The permalink of the feed/channel.
305
973e1f9e 306=head2 $feed->tagline([ $tagline ])
0d5e38d1 307
308The description or tagline of the feed/channel.
309
973e1f9e 310=head2 $feed->description([ $description ])
0d5e38d1 311
312Alias for I<$feed-E<gt>tagline>.
313
973e1f9e 314=head2 $feed->author([ $author ])
315
316The author of the feed/channel.
317
318=head2 $feed->language([ $language ])
0d5e38d1 319
320The language of the feed.
321
973e1f9e 322=head2 $feed->copyright([ $copyright ])
0d5e38d1 323
324The copyright notice of the feed.
325
973e1f9e 326=head2 $feed->modified([ $modified ])
0d5e38d1 327
328A I<DateTime> object representing the last-modified date of the feed.
329
973e1f9e 330If present, I<$modified> should be a I<DateTime> object.
331
332=head2 $feed->generator([ $generator ])
0d5e38d1 333
334The generator of the feed.
335
9a36f82c 336=head2 $feed->self_link ([ $uri ])
337
338The Atom Self-link of the feed:
339
340L<http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html>
341
342A string.
343
0d5e38d1 344=head2 $feed->entries
345
346A list of the entries/items in the feed. Returns an array containing
200ce863 347L<XML::Feed::Entry> objects.
0d5e38d1 348
8c30ad3d 349=head2 $feed->items
350
200ce863 351A synonym (alias) for <$feed-E<gt>entries>.
8c30ad3d 352
973e1f9e 353=head2 $feed->add_entry($entry)
354
200ce863 355Adds an entry to the feed. I<$entry> should be an L<XML::Feed::Entry>
973e1f9e 356object in the correct format for the feed.
357
358=head2 $feed->as_xml
359
360Returns an XML representation of the feed, in the format determined by
361the current format of the I<$feed> object.
362
02de1790 363=head2 $feed->first_link ([ $uri ])
364
365The Atom First-link for feed paging and archiving (RFC 5005).
366
367L<http://tools.ietf.org/html/rfc5005>
368
369=head2 $feed->last_link ([ $uri ])
370
371The Atom Last-link for feed paging and archiving.
372
373=head2 $feed->next_link ([ $uri ])
374
375The Atom Next-link for feed paging and archiving.
376
377=head2 $feed->previous_link ([ $uri ])
378
379The Atom Previous-link for feed paging and archiving.
380
381=head2 $feed->current_link ([ $uri ])
382
383The Atom Current-link for feed paging and archiving.
384
385=head2 $feed->next_archive_link ([ $uri ])
386
387The Atom Next-link for feed paging and archiving.
388
389=head2 $feed->prev_archive_link ([ $uri ])
390
391The Atom Prev-Archive-link for feed paging and archiving.
392
973e1f9e 393=head1 PACKAGE VARIABLES
394
395=over 4
396
7b5fccb1 397=item C<$XML::Feed::Format::RSS::PREFERRED_PARSER>
973e1f9e 398
399If you want to use another RSS parser class than XML::RSS (default), you can
7b5fccb1 400change the class by setting C<$PREFERRED_PARSER> variable in the
401XML::Feed::Format::RSS package.
973e1f9e 402
7b5fccb1 403 $XML::Feed::Format::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
973e1f9e 404
405B<Note:> this will only work for parsing feeds, not creating feeds.
406
0c201593 407B<Note:> Only C<XML::RSS::LibXML> version 0.3004 is known to work at the moment.
408
b1aa7a62 409=item C<$XML::Feed::MULTIPLE_ENCLOSURES>
410
411Although the RSS specification states that there can be at most one enclosure per item
412some feeds break this rule.
413
414If this variable is set then C<XML::Feed> captures all of them and makes them available as a list.
415
416Otherwise it returns the last enclosure parsed.
417
418B<Note:> C<XML::RSS> version 1.44 is needed for this to work.
419
973e1f9e 420=back
421
b1aa7a62 422=cut
423
b8bc97f3 424=head1 VALID FEEDS
425
426For 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,
4280.91, 1.0 and 2.0.
429
430 #!perl -w
431
432 use strict;
433 use CGI;
434 use CGI::Carp qw(fatalsToBrowser);
435 use DateTime;
436 use XML::Feed;
437
438 my $cgi = CGI->new;
439 my @args = ( $cgi->param('format') || "Atom" );
440 push @args, ( version => $cgi->param('version') ) if $cgi->param('version');
441
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);
448
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);
458
459 my $mime = ("Atom" eq $feed->format) ? "application/atom+xml" : "application/rss+xml";
460 print $cgi->header($mime);
461 print $feed->as_xml;
462
463
0d5e38d1 464=head1 LICENSE
465
466I<XML::Feed> is free software; you may redistribute it and/or modify it
467under the same terms as Perl itself.
468
469=head1 AUTHOR & COPYRIGHT
470
70f935cc 471Except where otherwise noted, I<XML::Feed> is Copyright 2004-2008
e933a73f 472Six Apart. All rights reserved.
0d5e38d1 473
e933a73f 474=head1 SUPPORT
475
476For support contact the XML::Feed mailing list - xml-feed@perlhacks.com.
477
478=head1 SOURCE CODE
70f935cc 479
480The latest version of I<XML::Feed> can be found at
481
e933a73f 482 http://github.com/davorg/XML-Feed
70f935cc 483
0d5e38d1 484=cut