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