Make feed validate
[catagits/XML-Feed.git] / lib / XML / Feed.pm
1 # $Id: Feed.pm 1958 2006-08-14 05:31:27Z btrott $
2
3 package XML::Feed;
4 use strict;
5
6 use base qw( Class::ErrorHandler );
7 use Feed::Find;
8 use URI::Fetch;
9 use Carp;
10
11 our $VERSION = '0.21';
12
13 sub new {
14     my $class = shift;
15     my $format = shift || 'Atom';
16     my $format_class = 'XML::Feed::' . $format;
17     eval "use $format_class";
18     Carp::croak("Unsupported format $format: $@") if $@;
19     my $feed = bless {}, join('::', __PACKAGE__, $format);
20     $feed->init_empty(@_) or return $class->error($feed->errstr);
21     $feed;
22 }
23
24 sub init_empty { 1 }
25
26 sub parse {
27     my $class = shift;
28     my($stream, $specified_format) = @_;
29     return $class->error("Stream parameter is required") unless $stream;
30     my $feed = bless {}, $class;
31     my $xml = '';
32     if (UNIVERSAL::isa($stream, 'URI')) {
33         my $res = URI::Fetch->fetch($stream)
34             or return $class->error(URI::Fetch->errstr);
35         return $class->error("This feed has been permanently removed")
36             if $res->status == URI::Fetch::URI_GONE();
37         $xml = $res->content;
38     } elsif (ref($stream) eq 'SCALAR') {
39         $xml = $$stream;
40     } elsif (ref($stream)) {
41         while (read($stream, my($chunk), 8192)) {
42             $xml .= $chunk;
43         }
44     } else {
45         open my $fh, $stream
46             or return $class->error("Can't open $stream: $!");
47         while (read $fh, my($chunk), 8192) {
48             $xml .= $chunk;
49         }
50         close $fh;
51     }
52     return $class->error("Can't get feed XML content from $stream")
53         unless $xml;
54     my $format;
55     if ($specified_format) {
56         $format = $specified_format;
57     } else {
58         $format = $feed->identify_format(\$xml) or return $class->error($feed->errstr);
59     }
60
61     my $format_class = join '::', __PACKAGE__, $format;
62     eval "use $format_class";
63     return $class->error("Unsupported format $format: $@") if $@;
64     bless $feed, $format_class;
65     $feed->init_string(\$xml) or return $class->error($feed->errstr);
66     $feed;
67 }
68
69 sub identify_format {
70     my $feed = shift;
71     my($xml) = @_;
72     ## Auto-detect feed type based on first element. This is prone
73     ## to breakage, but then again we don't want to parse the whole
74     ## feed ourselves.
75     my $tag;
76     while ($$xml =~ /<(\S+)/sg) {
77         (my $t = $1) =~ tr/a-zA-Z0-9:\-\?!//cd;
78         my $first = substr $t, 0, 1;
79         $tag = $t, last unless $first eq '?' || $first eq '!';
80     }
81     return $feed->error("Cannot find first element") unless $tag;
82     $tag =~ s/^.*://;
83     if ($tag eq 'rss' || $tag eq 'RDF') {
84         return 'RSS';
85     } elsif ($tag eq 'feed') {
86         return 'Atom';
87     } else {
88         return $feed->error("Cannot detect feed type");
89     }
90 }
91
92 sub find_feeds {
93     my $class = shift;
94     my($uri) = @_;
95     my @feeds = Feed::Find->find($uri)
96         or return $class->error(Feed::Find->errstr);
97     @feeds;
98 }
99
100 sub convert {
101     my $feed = shift;
102     my($format) = @_;
103     my $new = __PACKAGE__->new($format);
104     for my $field (qw( title link description language author copyright modified generator )) {
105         my $val = $feed->$field();
106         next unless defined $val;
107         $new->$field($val);
108     }
109     for my $entry ($feed->entries) {
110         $new->add_entry($entry->convert($format));
111     }
112     $new;
113 }
114
115 sub splice {
116     my $feed = shift;
117     my($other) = @_;
118     my %ids = map { $_->id => 1 } $feed->entries;
119     for my $entry ($other->entries) {
120         $feed->add_entry($entry) unless $ids{$entry->id}++;
121     }
122 }
123
124 sub format;
125 sub title;
126 sub link;
127 sub description;
128 sub language;
129 sub author;
130 sub copyright;
131 sub modified;
132 sub generator;
133 sub add_entry;
134 sub entries;
135 sub as_xml;
136 sub id;
137
138 sub tagline { shift->description(@_) }
139 sub items   { $_[0]->entries     }
140
141 1;
142 __END__
143
144 =head1 NAME
145
146 XML::Feed - Syndication feed parser and auto-discovery
147
148 =head1 SYNOPSIS
149
150     use XML::Feed;
151     my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
152         or die XML::Feed->errstr;
153     print $feed->title, "\n";
154     for my $entry ($feed->entries) {
155     }
156
157     ## Find all of the syndication feeds on a given page, using
158     ## auto-discovery.
159     my @feeds = XML::Feed->find_feeds('http://example.com/');
160
161 =head1 DESCRIPTION
162
163 I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
164 also implements feed auto-discovery for finding feeds, given a URI.
165
166 I<XML::Feed> supports the following syndication feed formats:
167
168 =over 4
169
170 =item * RSS 0.91
171
172 =item * RSS 1.0
173
174 =item * RSS 2.0
175
176 =item * Atom
177
178 =back
179
180 The goal of I<XML::Feed> is to provide a unified API for parsing and using
181 the various syndication formats. The different flavors of RSS and Atom
182 handle data in different ways: date handling; summaries and content;
183 escaping and quoting; etc. This module attempts to remove those differences
184 by providing a wrapper around the formats and the classes implementing
185 those formats (I<XML::RSS> and I<XML::Atom::Feed>). For example, dates are
186 handled differently in each of the above formats. To provide a unified API for
187 date handling, I<XML::Feed> converts all date formats transparently into
188 I<DateTime> objects, which it then returns to the caller.
189
190 =head1 USAGE
191
192 =head2 XML::Feed->new($format)
193
194 Creates a new empty I<XML::Feed> object using the format I<$format>.
195
196     $feed = XML::Feed->new('Atom');
197     $feed = XML::Feed->new('RSS');
198     $feed = XML::Feed->new('RSS', version => '0.91');
199
200 =head2 XML::Feed->parse($stream)
201
202 =head2 XML::Feed->parse($stream, $format)
203
204 Parses a syndication feed identified by I<$stream>. I<$stream> can be any
205 one of the following:
206
207 =over 4
208
209 =item * Scalar reference
210
211 A reference to string containing the XML body of the feed.
212
213 =item * Filehandle
214
215 An open filehandle from which the feed XML will be read.
216
217 =item * File name
218
219 The name of a file containing the feed XML.
220
221 =item * URI object
222
223 A URI from which the feed XML will be retrieved.
224
225 =back
226
227 C<$format> allows you to override format guessing.
228
229 =head2 XML::Feed->find_feeds($uri)
230
231 Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
232 from that page (using I<E<lt>linkE<gt>> tags).
233
234 Returns a list of feed URIs.
235
236 =head2 $feed->convert($format)
237
238 Converts the I<XML::Feed> object into the I<$format> format, and returns
239 the new object.
240
241 =head2 $feed->splice($other_feed)
242
243 Splices in all of the entries from the feed I<$other_feed> into I<$feed>,
244 skipping posts that are already in I<$feed>.
245
246 =head2 $feed->format
247
248 Returns the format of the feed (C<Atom>, or some version of C<RSS>).
249
250 =head2 $feed->title([ $title ])
251
252 The title of the feed/channel.
253
254 =head2 $feed->link([ $uri ])
255
256 The permalink of the feed/channel.
257
258 =head2 $feed->tagline([ $tagline ])
259
260 The description or tagline of the feed/channel.
261
262 =head2 $feed->description([ $description ])
263
264 Alias for I<$feed-E<gt>tagline>.
265
266 =head2 $feed->author([ $author ])
267
268 The author of the feed/channel.
269
270 =head2 $feed->language([ $language ])
271
272 The language of the feed.
273
274 =head2 $feed->copyright([ $copyright ])
275
276 The copyright notice of the feed.
277
278 =head2 $feed->modified([ $modified ])
279
280 A I<DateTime> object representing the last-modified date of the feed.
281
282 If present, I<$modified> should be a I<DateTime> object.
283
284 =head2 $feed->generator([ $generator ])
285
286 The generator of the feed.
287
288 =head2 $feed->entries
289
290 A list of the entries/items in the feed. Returns an array containing
291 I<XML::Feed::Entry> objects.
292
293 =head2 $feed->add_entry($entry)
294
295 Adds an entry to the feed. I<$entry> should be an I<XML::Feed::Entry>
296 object in the correct format for the feed.
297
298 =head2 $feed->as_xml
299
300 Returns an XML representation of the feed, in the format determined by
301 the current format of the I<$feed> object.
302
303 =head1 PACKAGE VARIABLES
304
305 =over 4
306
307 =item C<$XML::Feed::RSS::PREFERRED_PARSER>
308
309 If you want to use another RSS parser class than XML::RSS (default), you can
310 change the class by setting C<$PREFERRED_PARSER> variable in XML::Feed::RSS
311 package.
312
313     $XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
314
315 B<Note:> this will only work for parsing feeds, not creating feeds.
316
317 =back
318
319 =head1 LICENSE
320
321 I<XML::Feed> is free software; you may redistribute it and/or modify it
322 under the same terms as Perl itself.
323
324 =head1 AUTHOR & COPYRIGHT
325
326 Except where otherwise noted, I<XML::Feed> is Copyright 2004-2008
327 Six Apart, cpan@sixapart.com. All rights reserved.
328
329 =head1 SUBVERSION 
330
331 The latest version of I<XML::Feed> can be found at
332
333     http://code.sixapart.com/svn/XML-Feed/trunk/
334
335 =cut