Fixing RT #47981
[catagits/XML-Feed.git] / lib / XML / Feed / Format / RSS.pm
1 # $Id$
2
3 package XML::Feed::Format::RSS;
4 use strict;
5
6 use base qw( XML::Feed );
7 use DateTime::Format::Mail;
8 use DateTime::Format::W3CDTF;
9 use XML::Atom::Util qw(iso2dt);
10
11 our $PREFERRED_PARSER = "XML::RSS";
12
13
14 sub identify {
15     my $class   = shift;
16     my $xml     = shift;
17     my $tag     = $class->_get_first_tag($xml);
18     return ($tag eq 'rss' || $tag eq 'RDF');
19 }
20
21 sub init_empty {
22     my ($feed, %args) = @_;
23     $args{'version'} ||= '2.0';
24     eval "use $PREFERRED_PARSER"; die $@ if $@;
25     $feed->{rss} = $PREFERRED_PARSER->new(%args);
26     $feed->{rss}->add_module(prefix => "content", uri => 'http://purl.org/rss/1.0/modules/content/');
27     $feed->{rss}->add_module(prefix => "dcterms", uri => 'http://purl.org/dc/terms/');    
28     $feed->{rss}->add_module(prefix => "atom", uri => 'http://www.w3.org/2005/Atom');
29     $feed->{rss}->add_module(prefix => "geo", uri => 'http://www.w3.org/2003/01/geo/wgs84_pos#');
30     $feed;
31 }
32
33 sub init_string {
34     my $feed = shift;
35     my($str) = @_;
36     $feed->init_empty;
37     my $opts = {
38          hashrefs_instead_of_strings => 1,
39     };
40     $opts->{allow_multiple} = [ 'enclosure' ] if $XML::Feed::MULTIPLE_ENCLOSURES;
41     if ($str) {
42         $feed->{rss}->parse($$str, $opts );
43     }
44     $feed;
45 }
46
47 sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
48
49 ## The following elements are the same in all versions of RSS.
50 sub title       { shift->{rss}->channel('title', @_) }
51 sub link        { shift->{rss}->channel('link', @_) }
52 sub description { shift->{rss}->channel('description', @_) }
53
54 # This doesn't exist in RSS
55 sub id          { }
56
57 ## This is RSS 2.0 only--what's the equivalent in RSS 1.0?
58 sub copyright   { shift->{rss}->channel('copyright', @_) }
59
60 sub base {
61     my $feed = shift;
62     if (@_) {
63         $feed->{rss}->{'xml:base'} = $_[0];
64     } else {
65         $feed->{rss}->{'xml:base'};
66     }
67 }
68
69 ## The following all work transparently in any RSS version.
70 sub language {
71     my $feed = shift;
72     if (@_) {
73         $feed->{rss}->channel('language', $_[0]);
74         $feed->{rss}->channel->{dc}{language} = $_[0];
75     } else {
76         $feed->{rss}->channel('language') ||
77         $feed->{rss}->channel->{dc}{language};
78     }
79 }
80
81 sub self_link {
82     my $feed = shift;
83
84     if (@_) {
85         my $uri = shift;
86
87         $feed->{rss}->channel->{'atom'}{'link'} =
88         {
89             rel => "self",
90             href => $uri,
91             type => "application/rss+xml",
92         };
93     }
94
95     return $feed->{rss}->channel->{'atom'}{'link'};
96 }
97
98
99 sub generator {
100     my $feed = shift;
101     if (@_) {
102         $feed->{rss}->channel('generator', $_[0]);
103         $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} =
104             $_[0];
105     } else {
106         $feed->{rss}->channel('generator') ||
107         $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent};
108     }
109 }
110
111 sub author {
112     my $feed = shift;
113     if (@_) {
114         $feed->{rss}->channel('webMaster', $_[0]);
115         $feed->{rss}->channel->{dc}{creator} = $_[0];
116     } else {
117         $feed->{rss}->channel('webMaster') ||
118         $feed->{rss}->channel->{dc}{creator};
119     }
120 }
121
122 sub modified {
123     my $rss = shift->{rss};
124     if (@_) {
125         $rss->channel('pubDate',
126             DateTime::Format::Mail->format_datetime($_[0]));
127         ## XML::RSS is so weird... if I set this, it will try to use
128         ## the value for the lastBuildDate, which I don't want--because
129         ## this date is formatted for an RSS 1.0 feed. So it's commented out.
130         #$rss->channel->{dc}{date} =
131         #    DateTime::Format::W3CDTF->format_datetime($_[0]);
132     } else {
133         my $date;
134         eval {
135             if (my $ts = $rss->channel('pubDate')) {
136                 $date = DateTime::Format::Mail->parse_datetime($ts);
137             } elsif ($ts = $rss->channel->{dc}{date}) {
138                 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
139             }
140         };
141         return $date;
142     }
143 }
144
145 sub entries {
146     my $rss = $_[0]->{rss};
147     my @entries;
148     for my $item (@{ $rss->{items} }) {
149         push @entries, XML::Feed::Entry::Format::RSS->wrap($item);
150                 $entries[-1]->{_version} = $rss->{'version'};           
151     }
152     @entries;
153 }
154
155 sub add_entry {
156     my $feed  = shift;
157     my $entry = shift || return;
158     $entry    = $feed->_convert_entry($entry);
159     $feed->{rss}->add_item(%{ $entry->unwrap });
160 }
161
162 sub as_xml { $_[0]->{rss}->as_string }
163
164 package XML::Feed::Entry::Format::RSS;
165 use strict;
166
167 sub format { 'RSS ' . $_[0]->{'_version'} }
168
169 use XML::Feed::Content;
170
171 use base qw( XML::Feed::Entry );
172
173 sub init_empty { $_[0]->{entry} = { } }
174
175 sub base {
176     my $entry = shift;
177     @_ ? $entry->{entry}->{'xml:base'} = $_[0] : $entry->{entry}->{'xml:base'};
178 }
179
180 sub title {
181     my $entry = shift;
182     @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title};
183 }
184
185 sub link {
186     my $entry = shift;
187     if (@_) {
188         $entry->{entry}{link} = $_[0];
189         ## For RSS 2.0 output from XML::RSS. Sigh.
190         $entry->{entry}{permaLink} = $_[0];
191     } else {
192         $entry->{entry}{link} || $entry->{entry}{guid};
193     }
194 }
195
196 sub summary {
197     my $item = shift->{entry};
198     if (@_) {
199         $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ?
200             $_[0]->body : $_[0];
201         ## Because of the logic below, we need to add some dummy content,
202         ## so that we'll properly recognize the description we enter as
203         ## the summary.
204         if (!$item->{content}{encoded} &&
205             !$item->{'http://www.w3.org/1999/xhtml'}{body}) {
206             $item->{content}{encoded} = ' ';
207         }
208     } else {
209         ## Some RSS feeds use <description> for a summary, and some use it
210         ## for the full content. Pretty gross. We don't want to return the
211         ## full content if the caller expects a summary, so the heuristic is:
212         ## if the <entry> contains both a <description> and one of the elements
213         ## typically used for the full content, use <description> as summary.
214         my $txt;
215         if ($item->{description} &&
216             ($item->{content}{encoded} ||
217              $item->{'http://www.w3.org/1999/xhtml'}{body})) {
218             $txt = $item->{description};
219         ## Blogspot's 'short' RSS feeds do this in the Atom namespace
220         ## for no obviously good reason.
221         } elsif ($item->{'http://www.w3.org/2005/Atom'}{summary}) {
222             $txt = $item->{'http://www.w3.org/2005/Atom'}{summary};
223         }
224         XML::Feed::Content->wrap({ type => 'text/plain', body => $txt });
225     }
226 }
227
228 sub content {
229     my $item = shift->{entry};
230     if (@_) {
231         my $c;
232         if (ref($_[0]) eq 'XML::Feed::Content') {
233             if (defined $_[0]->base) {
234                 $c = { 'content' => $_[0]->body, 'xml:base' => $_[0]->base };
235             } else {
236                 $c = $_[0]->body;
237             }
238         } else {
239             $c = $_[0];
240         }
241         $item->{content}{encoded} = $c;
242     } else {
243         my $base;
244         my $body =
245             $item->{content}{encoded} ||
246             $item->{'http://www.w3.org/1999/xhtml'}{body} ||
247             $item->{description};
248         if ('HASH' eq ref($body)) {
249             $base = $body->{'xml:base'};
250             $body = $body->{content};
251         }
252         XML::Feed::Content->wrap({ type => 'text/html', body => $body, base => $base });
253     }
254 }
255
256 sub category {
257     my $entry = shift;
258     my $item  = $entry->{entry};
259     if (@_) {
260         my @tmp = ($entry->category, @_);
261         $item->{category}    = [@tmp];
262         $item->{dc}{subject} = [@tmp];
263     } else {
264         my $r = $item->{category} || $item->{dc}{subject};
265         my @r = ref($r) eq 'ARRAY' ? @$r : defined $r? ($r) : ();
266         return wantarray? @r : $r[0];
267     }
268 }
269
270 sub author {
271     my $item = shift->{entry};
272     if (@_) {
273         $item->{author} = $item->{dc}{creator} = $_[0];
274     } else {
275         $item->{author} || $item->{dc}{creator};
276     }
277 }
278
279 ## XML::RSS doesn't give us access to the rdf:about for the <item>,
280 ## so we have to fall back to the <link> element in RSS 1.0 feeds.
281 sub id {
282     my $item = shift->{entry};
283     if (@_) {
284         $item->{guid} = $_[0];
285     } else {
286         $item->{guid} || $item->{link};
287     }
288 }
289
290 sub issued {
291     my $item = shift->{entry};
292     if (@_) {
293         $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]);
294         $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]);
295     } else {
296         ## Either of these could die if the format is invalid.
297         my $date;
298         eval {
299             if (my $ts = $item->{pubDate}) {
300                 my $parser = DateTime::Format::Mail->new;
301                 $parser->loose;
302                 $date = $parser->parse_datetime($ts);
303             } elsif ($ts = $item->{dc}{date} or $ts = $item->{dcterms}{date}) {
304                $date = DateTime::Format::W3CDTF->parse_datetime($ts);
305             }
306         };
307         return $date;
308     }
309 }
310
311 sub modified {
312     my $item = shift->{entry};
313     if (@_) {
314         $item->{dcterms}{modified} =
315             DateTime::Format::W3CDTF->format_datetime($_[0]);
316     } else {
317         if (my $ts = $item->{dcterms}{modified} || $item->{'http://www.w3.org/2005/Atom'}{updated}) {
318             return eval { DateTime::Format::W3CDTF->parse_datetime($ts) } || eval { XML::Atom::Util::iso2dt($ts) };
319         } 
320     }
321 }
322
323 sub lat {
324     my $item = shift->{entry};
325     if (@_) {
326         $item->{geo}{lat} = $_[0];
327     } else {
328         return $item->{geo}{lat};
329     }
330 }
331
332 sub long {
333     my $item = shift->{entry};
334     if (@_) {
335         $item->{geo}{long} = $_[0];
336     } else {
337          return $item->{geo}{long};
338     }
339 }
340
341 sub enclosure {
342     my $entry  = shift;
343
344     if (@_) {
345         my $enclosure = shift;
346         my $val       =  {
347                  url    => $enclosure->{url},
348                  type   => $enclosure->{type},
349                  length => $enclosure->{length}
350         };
351         if ($XML::Feed::MULTIPLE_ENCLOSURES) {
352             push @{$entry->{entry}->{enclosure}}, $val;
353         } else {
354             $entry->{entry}->{enclosure} =  $val;
355         }
356     } else {
357         my $tmp  = $entry->{entry}->{enclosure};
358         my @encs = map { XML::Feed::Enclosure->new($_) } (ref $tmp eq 'ARRAY')? @$tmp : ($tmp);
359         return ($XML::Feed::MULTIPLE_ENCLOSURES)? @encs : $encs[-1];
360     }
361 }
362
363 1;