3 package XML::Feed::Format::RSS;
6 use base qw( XML::Feed );
7 use DateTime::Format::Mail;
8 use DateTime::Format::W3CDTF;
10 our $PREFERRED_PARSER = "XML::RSS";
16 my $tag = $class->_get_first_tag($xml);
17 return ($tag eq 'rss' || $tag eq 'RDF');
21 my ($feed, %args) = @_;
22 $args{'version'} ||= '2.0';
23 eval "use $PREFERRED_PARSER"; die $@ if $@;
24 $feed->{rss} = $PREFERRED_PARSER->new(%args);
25 $feed->{rss}->add_module(prefix => "content", uri => 'http://purl.org/rss/1.0/modules/content/');
26 $feed->{rss}->add_module(prefix => "dcterms", uri => 'http://purl.org/dc/terms/');
27 $feed->{rss}->add_module(prefix => "atom", uri => 'http://www.w3.org/2005/Atom');
28 $feed->{rss}->add_module(prefix => "geo", uri => 'http://www.w3.org/2003/01/geo/wgs84_pos#');
37 hashrefs_instead_of_strings => 1,
39 $opts->{allow_multiple} = [ 'enclosure' ] if $XML::Feed::MULTIPLE_ENCLOSURES;
41 $feed->{rss}->parse($$str, $opts );
46 sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
48 ## The following elements are the same in all versions of RSS.
49 sub title { shift->{rss}->channel('title', @_) }
50 sub link { shift->{rss}->channel('link', @_) }
51 sub description { shift->{rss}->channel('description', @_) }
53 # This doesn't exist in RSS
56 ## This is RSS 2.0 only--what's the equivalent in RSS 1.0?
57 sub copyright { shift->{rss}->channel('copyright', @_) }
62 $feed->{rss}->{'xml:base'} = $_[0];
64 $feed->{rss}->{'xml:base'};
68 ## The following all work transparently in any RSS version.
72 $feed->{rss}->channel('language', $_[0]);
73 $feed->{rss}->channel->{dc}{language} = $_[0];
75 $feed->{rss}->channel('language') ||
76 $feed->{rss}->channel->{dc}{language};
86 $feed->{rss}->channel->{'atom'}{'link'} =
90 type => "application/rss+xml",
94 return $feed->{rss}->channel->{'atom'}{'link'};
101 $feed->{rss}->channel('generator', $_[0]);
102 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} =
105 $feed->{rss}->channel('generator') ||
106 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent};
113 $feed->{rss}->channel('webMaster', $_[0]);
114 $feed->{rss}->channel->{dc}{creator} = $_[0];
116 $feed->{rss}->channel('webMaster') ||
117 $feed->{rss}->channel->{dc}{creator};
122 my $rss = shift->{rss};
124 $rss->channel('pubDate',
125 DateTime::Format::Mail->format_datetime($_[0]));
126 ## XML::RSS is so weird... if I set this, it will try to use
127 ## the value for the lastBuildDate, which I don't want--because
128 ## this date is formatted for an RSS 1.0 feed. So it's commented out.
129 #$rss->channel->{dc}{date} =
130 # DateTime::Format::W3CDTF->format_datetime($_[0]);
134 if (my $ts = $rss->channel('pubDate')) {
135 $date = DateTime::Format::Mail->parse_datetime($ts);
136 } elsif ($ts = $rss->channel->{dc}{date}) {
137 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
145 my $rss = $_[0]->{rss};
147 for my $item (@{ $rss->{items} }) {
148 push @entries, XML::Feed::Entry::Format::RSS->wrap($item);
149 $entries[-1]->{_version} = $rss->{'version'};
156 my $entry = shift || return;
157 $entry = $feed->_convert_entry($entry);
158 $feed->{rss}->add_item(%{ $entry->unwrap });
161 sub as_xml { $_[0]->{rss}->as_string }
163 package XML::Feed::Entry::Format::RSS;
166 sub format { 'RSS ' . $_[0]->{'_version'} }
168 use XML::Feed::Content;
170 use base qw( XML::Feed::Entry );
172 sub init_empty { $_[0]->{entry} = { } }
176 @_ ? $entry->{entry}->{'xml:base'} = $_[0] : $entry->{entry}->{'xml:base'};
181 @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title};
187 $entry->{entry}{link} = $_[0];
188 ## For RSS 2.0 output from XML::RSS. Sigh.
189 $entry->{entry}{permaLink} = $_[0];
191 $entry->{entry}{link} || $entry->{entry}{guid};
196 my $item = shift->{entry};
198 $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ?
200 ## Because of the logic below, we need to add some dummy content,
201 ## so that we'll properly recognize the description we enter as
203 if (!$item->{content}{encoded} &&
204 !$item->{'http://www.w3.org/1999/xhtml'}{body}) {
205 $item->{content}{encoded} = ' ';
208 ## Some RSS feeds use <description> for a summary, and some use it
209 ## for the full content. Pretty gross. We don't want to return the
210 ## full content if the caller expects a summary, so the heuristic is:
211 ## if the <entry> contains both a <description> and one of the elements
212 ## typically used for the full content, use <description> as summary.
214 if ($item->{description} &&
215 ($item->{content}{encoded} ||
216 $item->{'http://www.w3.org/1999/xhtml'}{body})) {
217 $txt = $item->{description};
219 XML::Feed::Content->wrap({ type => 'text/plain', body => $txt });
224 my $item = shift->{entry};
227 if (ref($_[0]) eq 'XML::Feed::Content') {
228 if (defined $_[0]->base) {
229 $c = { 'content' => $_[0]->body, 'xml:base' => $_[0]->base };
236 $item->{content}{encoded} = $c;
240 $item->{content}{encoded} ||
241 $item->{'http://www.w3.org/1999/xhtml'}{body} ||
242 $item->{description};
243 if ('HASH' eq ref($body)) {
244 $base = $body->{'xml:base'};
245 $body = $body->{content};
247 XML::Feed::Content->wrap({ type => 'text/html', body => $body, base => $base });
253 my $item = $entry->{entry};
255 my @tmp = ($entry->category, @_);
256 $item->{category} = [@tmp];
257 $item->{dc}{subject} = [@tmp];
259 my $r = $item->{category} || $item->{dc}{subject};
260 my @r = ref($r) eq 'ARRAY' ? @$r : defined $r? ($r) : ();
261 return wantarray? @r : $r[0];
266 my $item = shift->{entry};
268 $item->{author} = $item->{dc}{creator} = $_[0];
270 $item->{author} || $item->{dc}{creator};
274 ## XML::RSS doesn't give us access to the rdf:about for the <item>,
275 ## so we have to fall back to the <link> element in RSS 1.0 feeds.
277 my $item = shift->{entry};
279 $item->{guid} = $_[0];
281 $item->{guid} || $item->{link};
286 my $item = shift->{entry};
288 $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]);
289 $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]);
291 ## Either of these could die if the format is invalid.
294 if (my $ts = $item->{pubDate}) {
295 my $parser = DateTime::Format::Mail->new;
297 $date = $parser->parse_datetime($ts);
298 } elsif ($ts = $item->{dc}{date}) {
299 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
307 my $item = shift->{entry};
309 $item->{dcterms}{modified} =
310 DateTime::Format::W3CDTF->format_datetime($_[0]);
312 if (my $ts = $item->{dcterms}{modified}) {
313 return eval { DateTime::Format::W3CDTF->parse_datetime($ts) };
319 my $item = shift->{entry};
321 $item->{geo}{lat} = $_[0];
323 return $item->{geo}{lat};
328 my $item = shift->{entry};
330 $item->{geo}{long} = $_[0];
332 return $item->{geo}{long};
340 my $enclosure = shift;
342 url => $enclosure->{url},
343 type => $enclosure->{type},
344 length => $enclosure->{length}
346 if ($XML::Feed::MULTIPLE_ENCLOSURES) {
347 push @{$entry->{entry}->{enclosure}}, $val;
349 $entry->{entry}->{enclosure} = $val;
352 my $tmp = $entry->{entry}->{enclosure};
353 my @encs = map { XML::Feed::Enclosure->new($_) } (ref $tmp eq 'ARRAY')? @$tmp : ($tmp);
354 return ($XML::Feed::MULTIPLE_ENCLOSURES)? @encs : $encs[-1];