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