3 package XML::Feed::Format::RSS;
6 use base qw( XML::Feed );
7 use DateTime::Format::Mail;
8 use DateTime::Format::W3CDTF;
9 use XML::Atom::Util qw(iso2dt);
10 use XML::Feed::Enclosure;
12 our $PREFERRED_PARSER = "XML::RSS";
18 my $tag = $class->_get_first_tag($xml);
19 return ($tag eq 'rss' || $tag eq 'RDF');
23 my ($feed, %args) = @_;
24 $args{'version'} ||= '2.0';
25 eval "use $PREFERRED_PARSER"; die $@ if $@;
26 $feed->{rss} = $PREFERRED_PARSER->new(%args);
27 $feed->{rss}->add_module(prefix => "content", uri => 'http://purl.org/rss/1.0/modules/content/');
28 $feed->{rss}->add_module(prefix => "dcterms", uri => 'http://purl.org/dc/terms/');
29 $feed->{rss}->add_module(prefix => "atom", uri => 'http://www.w3.org/2005/Atom');
30 $feed->{rss}->add_module(prefix => "geo", uri => 'http://www.w3.org/2003/01/geo/wgs84_pos#');
39 hashrefs_instead_of_strings => 1,
41 $opts->{allow_multiple} = [ 'enclosure' ] if $XML::Feed::MULTIPLE_ENCLOSURES;
43 $feed->{rss}->parse($$str, $opts );
48 sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
50 ## The following elements are the same in all versions of RSS.
51 sub title { shift->{rss}->channel('title', @_) }
52 sub link { shift->{rss}->channel('link', @_) }
53 sub description { shift->{rss}->channel('description', @_) }
54 sub updated { shift->modified(@_) }
56 # This doesn't exist in RSS
59 ## This is RSS 2.0 only--what's the equivalent in RSS 1.0?
60 sub copyright { shift->{rss}->channel('copyright', @_) }
65 $feed->{rss}->{'xml:base'} = $_[0];
67 $feed->{rss}->{'xml:base'};
71 ## The following all work transparently in any RSS version.
75 $feed->{rss}->channel('language', $_[0]);
76 $feed->{rss}->channel->{dc}{language} = $_[0];
78 $feed->{rss}->channel('language') ||
79 $feed->{rss}->channel->{dc}{language};
89 $feed->{rss}->channel->{'atom'}{'link'} =
93 type => "application/rss+xml",
97 return $feed->{rss}->channel->{'atom'}{'link'};
104 $feed->{rss}->channel('generator', $_[0]);
105 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} =
108 $feed->{rss}->channel('generator') ||
109 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent};
116 $feed->{rss}->channel('webMaster', $_[0]);
117 $feed->{rss}->channel->{dc}{creator} = $_[0];
119 $feed->{rss}->channel('webMaster') ||
120 $feed->{rss}->channel->{dc}{creator};
125 my $rss = shift->{rss};
127 $rss->channel('pubDate',
128 DateTime::Format::Mail->format_datetime($_[0]));
129 ## XML::RSS is so weird... if I set this, it will try to use
130 ## the value for the lastBuildDate, which I don't want--because
131 ## this date is formatted for an RSS 1.0 feed. So it's commented out.
132 #$rss->channel->{dc}{date} =
133 # DateTime::Format::W3CDTF->format_datetime($_[0]);
137 if (my $ts = $rss->channel('pubDate')) {
138 $date = DateTime::Format::Mail->parse_datetime($ts);
139 } elsif ($ts = $rss->channel->{dc}{date}) {
140 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
148 my $rss = $_[0]->{rss};
150 for my $item (@{ $rss->{items} }) {
151 push @entries, XML::Feed::Entry::Format::RSS->wrap($item);
152 $entries[-1]->{_version} = $rss->{'version'};
159 my $entry = shift || return;
160 $entry = $feed->_convert_entry($entry);
161 $feed->{rss}->add_item(%{ $entry->unwrap });
164 sub as_xml { $_[0]->{rss}->as_string }
166 package XML::Feed::Entry::Format::RSS;
169 sub format { 'RSS ' . $_[0]->{'_version'} }
171 use XML::Feed::Content;
173 use base qw( XML::Feed::Entry );
175 sub init_empty { $_[0]->{entry} = { } }
179 @_ ? $entry->{entry}->{'xml:base'} = $_[0] : $entry->{entry}->{'xml:base'};
184 @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title};
190 $entry->{entry}{link} = $_[0];
191 ## For RSS 2.0 output from XML::RSS. Sigh.
192 $entry->{entry}{permaLink} = $_[0];
194 $entry->{entry}{link} ||
195 $entry->{entry}{permaLink} ||
196 $entry->{entry}{guid};
201 my $item = shift->{entry};
203 $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ?
205 ## Because of the logic below, we need to add some dummy content,
206 ## so that we'll properly recognize the description we enter as
208 if (!$item->{content}{encoded} &&
209 !$item->{'http://www.w3.org/1999/xhtml'}{body}) {
210 $item->{content}{encoded} = ' ';
213 ## Some RSS feeds use <description> for a summary, and some use it
214 ## for the full content. Pretty gross. We don't want to return the
215 ## full content if the caller expects a summary, so the heuristic is:
216 ## if the <entry> contains both a <description> and one of the elements
217 ## typically used for the full content, use <description> as summary.
219 if ($item->{description} &&
220 ($item->{content}{encoded} ||
221 $item->{'http://www.w3.org/1999/xhtml'}{body})) {
222 $txt = $item->{description};
223 ## Blogspot's 'short' RSS feeds do this in the Atom namespace
224 ## for no obviously good reason.
225 } elsif ($item->{'http://www.w3.org/2005/Atom'}{summary}) {
226 $txt = $item->{'http://www.w3.org/2005/Atom'}{summary};
228 XML::Feed::Content->wrap({ type => 'text/plain', body => $txt });
233 my $item = shift->{entry};
236 if (ref($_[0]) eq 'XML::Feed::Content') {
237 if (defined $_[0]->base) {
238 $c = { 'content' => $_[0]->body, 'xml:base' => $_[0]->base };
245 $item->{content}{encoded} = $c;
249 $item->{content}{encoded} ||
250 $item->{'http://www.w3.org/1999/xhtml'}{body} ||
251 $item->{description};
252 if ('HASH' eq ref($body)) {
253 $base = $body->{'xml:base'};
254 $body = $body->{content};
256 XML::Feed::Content->wrap({ type => 'text/html', body => $body, base => $base });
262 my $item = $entry->{entry};
264 my @tmp = ($entry->category, @_);
265 $item->{category} = [@tmp];
266 $item->{dc}{subject} = [@tmp];
268 my $r = $item->{category} || $item->{dc}{subject};
269 my @r = ref($r) eq 'ARRAY' ? @$r : defined $r? ($r) : ();
270 return wantarray? @r : $r[0];
275 my $item = shift->{entry};
277 $item->{author} = $item->{dc}{creator} = $_[0];
279 $item->{author} || $item->{dc}{creator};
283 ## XML::RSS doesn't give us access to the rdf:about for the <item>,
284 ## so we have to fall back to the <link> element in RSS 1.0 feeds.
286 my $item = shift->{entry};
288 $item->{guid} = $_[0];
290 $item->{guid} || $item->{link};
295 my $item = shift->{entry};
297 $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]);
298 $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]);
300 ## Either of these could die if the format is invalid.
303 if (my $ts = $item->{pubDate}) {
304 my $parser = DateTime::Format::Mail->new;
306 $date = $parser->parse_datetime($ts);
307 } elsif ($ts = $item->{dc}{date} or $ts = $item->{dcterms}{date}) {
308 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
316 my $item = shift->{entry};
318 $item->{dcterms}{modified} =
319 DateTime::Format::W3CDTF->format_datetime($_[0]);
321 if (my $ts = $item->{dcterms}{modified} || $item->{'http://www.w3.org/2005/Atom'}{updated}) {
322 return eval { DateTime::Format::W3CDTF->parse_datetime($ts) } || eval { XML::Atom::Util::iso2dt($ts) };
328 my $item = shift->{entry};
330 $item->{geo}{lat} = $_[0];
332 return $item->{geo}{lat};
337 my $item = shift->{entry};
339 $item->{geo}{long} = $_[0];
341 return $item->{geo}{long};
349 my $enclosure = shift;
351 url => $enclosure->{url},
352 type => $enclosure->{type},
353 length => $enclosure->{length}
355 if ($XML::Feed::MULTIPLE_ENCLOSURES) {
356 push @{$entry->{entry}->{enclosure}}, $val;
358 $entry->{entry}->{enclosure} = $val;
361 my $tmp = $entry->{entry}->{enclosure};
363 my @encs = map { XML::Feed::Enclosure->new($_) }
364 (ref $tmp eq 'ARRAY')? @$tmp : ($tmp);
365 return ($XML::Feed::MULTIPLE_ENCLOSURES)? @encs : $encs[-1];