Make feed validate
[catagits/XML-Feed.git] / lib / XML / Feed / RSS.pm
1 # $Id: RSS.pm 1934 2006-04-22 05:13:55Z btrott $
2
3 package XML::Feed::RSS;
4 use strict;
5
6 use base qw( XML::Feed );
7 use DateTime::Format::Mail;
8 use DateTime::Format::W3CDTF;
9
10 our $PREFERRED_PARSER = "XML::RSS";
11
12 sub init_empty {
13     my ($feed, %args) = @_;
14     $args{'version'} ||= '2.0';
15     eval "use $PREFERRED_PARSER"; die $@ if $@;
16     $feed->{rss} = $PREFERRED_PARSER->new(%args);
17     $feed->{rss}->add_module(prefix => "content", uri => 'http://purl.org/rss/1.0/modules/content/');
18     $feed->{rss}->add_module(prefix => "dcterms", uri => 'http://purl.org/dc/terms/');    
19     $feed;
20 }
21
22 sub init_string {
23     my $feed = shift;
24     my($str) = @_;
25     $feed->init_empty;
26     if ($str) {
27         $feed->{rss}->parse($$str);
28     }
29     $feed;
30 }
31
32 sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
33
34 ## The following elements are the same in all versions of RSS.
35 sub title       { shift->{rss}->channel('title', @_) }
36 sub link        { shift->{rss}->channel('link', @_) }
37 sub description { shift->{rss}->channel('description', @_) }
38
39 # This doesn't exist in RSS
40 sub id          { }
41
42 ## This is RSS 2.0 only--what's the equivalent in RSS 1.0?
43 sub copyright   { shift->{rss}->channel('copyright', @_) }
44
45 ## The following all work transparently in any RSS version.
46 sub language {
47     my $feed = shift;
48     if (@_) {
49         $feed->{rss}->channel('language', $_[0]);
50         $feed->{rss}->channel->{dc}{language} = $_[0];
51     } else {
52         $feed->{rss}->channel('language') ||
53         $feed->{rss}->channel->{dc}{language};
54     }
55 }
56
57 sub generator {
58     my $feed = shift;
59     if (@_) {
60         $feed->{rss}->channel('generator', $_[0]);
61         $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} =
62             $_[0];
63     } else {
64         $feed->{rss}->channel('generator') ||
65         $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent};
66     }
67 }
68
69 sub author {
70     my $feed = shift;
71     if (@_) {
72         $feed->{rss}->channel('webMaster', $_[0]);
73         $feed->{rss}->channel->{dc}{creator} = $_[0];
74     } else {
75         $feed->{rss}->channel('webMaster') ||
76         $feed->{rss}->channel->{dc}{creator};
77     }
78 }
79
80 sub modified {
81     my $rss = shift->{rss};
82     if (@_) {
83         $rss->channel('pubDate',
84             DateTime::Format::Mail->format_datetime($_[0]));
85         ## XML::RSS is so weird... if I set this, it will try to use
86         ## the value for the lastBuildDate, which I don't want--because
87         ## this date is formatted for an RSS 1.0 feed. So it's commented out.
88         #$rss->channel->{dc}{date} =
89         #    DateTime::Format::W3CDTF->format_datetime($_[0]);
90     } else {
91         my $date;
92         eval {
93             if (my $ts = $rss->channel('pubDate')) {
94                 $date = DateTime::Format::Mail->parse_datetime($ts);
95             } elsif ($ts = $rss->channel->{dc}{date}) {
96                 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
97             }
98         };
99         return $date;
100     }
101 }
102
103 sub entries {
104     my $rss = $_[0]->{rss};
105     my @entries;
106     for my $item (@{ $rss->{items} }) {
107         push @entries, XML::Feed::Entry::RSS->wrap($item);
108     }
109     @entries;
110 }
111
112 sub add_entry {
113     my $feed = shift;
114     my($entry) = @_;
115     $feed->{rss}->add_item(%{ $entry->unwrap });
116 }
117
118 sub as_xml { $_[0]->{rss}->as_string }
119
120 package XML::Feed::Entry::RSS;
121 use strict;
122
123 use XML::Feed::Content;
124
125 use base qw( XML::Feed::Entry );
126
127 sub init_empty { $_[0]->{entry} = { } }
128
129 sub title {
130     my $entry = shift;
131     @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title};
132 }
133
134 sub link {
135     my $entry = shift;
136     if (@_) {
137         $entry->{entry}{link} = $_[0];
138         ## For RSS 2.0 output from XML::RSS. Sigh.
139         $entry->{entry}{permaLink} = $_[0];
140     } else {
141         $entry->{entry}{link} || $entry->{entry}{guid};
142     }
143 }
144
145 sub summary {
146     my $item = shift->{entry};
147     if (@_) {
148         $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ?
149             $_[0]->body : $_[0];
150         ## Because of the logic below, we need to add some dummy content,
151         ## so that we'll properly recognize the description we enter as
152         ## the summary.
153         if (!$item->{content}{encoded} &&
154             !$item->{'http://www.w3.org/1999/xhtml'}{body}) {
155             $item->{content}{encoded} = ' ';
156         }
157     } else {
158         ## Some RSS feeds use <description> for a summary, and some use it
159         ## for the full content. Pretty gross. We don't want to return the
160         ## full content if the caller expects a summary, so the heuristic is:
161         ## if the <entry> contains both a <description> and one of the elements
162         ## typically used for the full content, use <description> as summary.
163         my $txt;
164         if ($item->{description} &&
165             ($item->{content}{encoded} ||
166              $item->{'http://www.w3.org/1999/xhtml'}{body})) {
167             $txt = $item->{description};
168         }
169         XML::Feed::Content->wrap({ type => 'text/plain', body => $txt });
170     }
171 }
172
173 sub content {
174     my $item = shift->{entry};
175     if (@_) {
176         my $c = ref($_[0]) eq 'XML::Feed::Content' ? $_[0]->body : $_[0];
177         $item->{content}{encoded} = $c;
178     } else {
179         my $body =
180             $item->{content}{encoded} ||
181             $item->{'http://www.w3.org/1999/xhtml'}{body} ||
182             $item->{description};
183         XML::Feed::Content->wrap({ type => 'text/html', body => $body });
184     }
185 }
186
187 sub category {
188     my $item = shift->{entry};
189     if (@_) {
190         $item->{category} = $item->{dc}{subject} = $_[0];
191     } else {
192         $item->{category} || $item->{dc}{subject};
193     }
194 }
195
196 sub author {
197     my $item = shift->{entry};
198     if (@_) {
199         $item->{author} = $item->{dc}{creator} = $_[0];
200     } else {
201         $item->{author} || $item->{dc}{creator};
202     }
203 }
204
205 ## XML::RSS doesn't give us access to the rdf:about for the <item>,
206 ## so we have to fall back to the <link> element in RSS 1.0 feeds.
207 sub id {
208     my $item = shift->{entry};
209     if (@_) {
210         $item->{guid} = $_[0];
211     } else {
212         $item->{guid} || $item->{link};
213     }
214 }
215
216 sub issued {
217     my $item = shift->{entry};
218     if (@_) {
219         $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]);
220         $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]);
221     } else {
222         ## Either of these could die if the format is invalid.
223         my $date;
224         eval {
225             if (my $ts = $item->{pubDate}) {
226                 my $parser = DateTime::Format::Mail->new;
227                 $parser->loose;
228                 $date = $parser->parse_datetime($ts);
229             } elsif ($ts = $item->{dc}{date}) {
230                 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
231             }
232         };
233         return $date;
234     }
235 }
236
237 sub modified {
238     my $item = shift->{entry};
239     if (@_) {
240         $item->{dcterms}{modified} =
241             DateTime::Format::W3CDTF->format_datetime($_[0]);
242     } else {
243         if (my $ts = $item->{dcterms}{modified}) {
244             return eval { DateTime::Format::W3CDTF->parse_datetime($ts) };
245         }
246     }
247 }
248
249 1;