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