root/trunk/plagger/lib/Plagger/Plugin/Publish/Feed.pm

Revision 1898 (checked in by miyagawa, 14 years ago)

safe guard to load XML::Feed::RSS so PREFERRED_PARSE won't get overriden

Line 
1 package Plagger::Plugin::Publish::Feed;
2
3 use strict;
4 use base qw( Plagger::Plugin );
5
6 use XML::Feed;
7 use XML::Feed::Entry;
8 use XML::Feed::RSS; # load explicitly to force LibXML
9 use XML::RSS::LibXML;
10 use File::Spec;
11
12 $XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
13
14 sub register {
15     my($self, $context) = @_;
16     $context->autoload_plugin({ module => 'Filter::FloatingDateTime' });
17     $context->register_hook(
18         $self,
19         'publish.feed' => \&publish_feed,
20         'plugin.init'  => \&plugin_init,
21     );
22 }
23
24 sub plugin_init {
25     my($self, $context, $args) = @_;
26
27     # check dir
28     my $dir = $self->conf->{dir};
29     unless (-e $dir && -d _) {
30         mkdir $dir, 0755 or $context->error("mkdir $dir: $!");
31     }
32
33     unless (exists $self->conf->{full_content}) {
34         $self->conf->{full_content} = 1;
35     }
36 }
37
38 sub publish_feed {
39     my($self, $context, $args) = @_;
40
41     my $conf = $self->conf;
42     my $f = $args->{feed};
43     my $feed_format = $conf->{format} || 'Atom';
44
45     # generate feed
46     my $feed = XML::Feed->new($feed_format);
47     $feed->title($f->title);
48     $feed->link($f->link);
49     $feed->modified(Plagger::Date->now);
50     $feed->generator("Plagger/$Plagger::VERSION");
51     $feed->description($f->description || '');
52     $feed->author( $self->make_author($f->author, $feed_format) )
53         if $f->primary_author;
54
55     my $taguri_base = $self->conf->{taguri_base} || do {
56         require Sys::Hostname;
57         Sys::Hostname::hostname();
58     };
59
60     if ($feed_format eq 'Atom') {
61         $feed->{atom}->id("tag:$taguri_base,2006:" . $f->id); # XXX what if id is empty?
62     }
63
64     # add entry
65     for my $e ($f->entries) {
66         my $entry = XML::Feed::Entry->new($feed_format);
67         $entry->title($e->title);
68         $entry->link($e->permalink);
69         $entry->summary($e->body_text) if defined $e->body;
70
71         # hack to bypass XML::Feed Atom 0.3 crufts (type="text/html")
72         if ($self->conf->{full_content} && defined $e->body) {
73             if ($feed_format eq 'RSS') {
74                 $entry->content($e->body);
75             } else {
76                 $entry->{entry}->content($e->body->utf8);
77             }
78         }
79
80         $entry->category(join(' ', @{$e->tags})) if @{$e->tags};
81         $entry->issued($e->date)   if $e->date;
82         $entry->modified($e->date) if $e->date;
83
84         if ($feed_format eq 'RSS') {
85             my $author = 'nobody@example.com';
86             $author .= ' (' . $e->author . ')' if $e->author;
87             $entry->author($author);
88         } else {
89             unless ($feed->author) {
90                 $entry->author($e->author || 'nobody');
91             }
92         }
93
94         $entry->id("tag:$taguri_base,2006:" . $e->id);
95
96         if ($e->has_enclosure) {
97             for my $enclosure (grep { defined $_->url && !$_->is_inline } $e->enclosures) {
98                 $entry->add_enclosure({
99                     url    => $enclosure->url,
100                     length => $enclosure->length,
101                     type   => $enclosure->type,
102                 });
103
104                 # RSS 2.0 by spec doesn't allow multiple enclosures
105                 last if $feed_format eq 'RSS';
106             }
107         }
108
109         $feed->add_entry($entry);
110     }
111
112     # generate file path
113     my $tmpl = '%i.' . ($feed_format eq 'RSS' ? 'rss' : 'atom');
114     my $file = Plagger::Util::filename_for($f, $self->conf->{filename} || $tmpl);
115     my $filepath = File::Spec->catfile($self->conf->{dir}, $file);
116
117     $context->log(info => "save feed for " . $f->link . " to $filepath");
118
119     my $xml = $feed->as_xml;
120     utf8::decode($xml) unless utf8::is_utf8($xml);
121     open my $output, ">:utf8", $filepath or $context->error("$filepath: $!");
122     print $output $xml;
123     close $output;
124 }
125
126 sub make_author {
127     my($self, $author, $feed_format) = @_;
128
129     if ($feed_format eq 'RSS') {
130         my $rfc822 = 'nobody@example.com';
131         $rfc822 .= ' (' . $author . ')' if $author;
132         return $rfc822;
133     } else {
134         return defined $author ? $author : 'nobody';
135     }
136 }
137
138 # XXX okay, this is a hack until XML::Feed is updated
139 *XML::Feed::Entry::Atom::add_enclosure = sub {
140     my($entry, $enclosure) = @_;
141     my $link = XML::Atom::Link->new;
142     $link->rel('enclosure');
143     $link->type($enclosure->{type});
144     $link->href($enclosure->{url});
145     $link->length($enclosure->{length});
146     $entry->{entry}->add_link($link);
147 };
148
149 *XML::Feed::Entry::RSS::add_enclosure = sub {
150     my($entry, $enclosure) = @_;
151     $entry->{entry}->{enclosure} = {
152         url    => $enclosure->{url},
153         type   => $enclosure->{type},
154         length => $enclosure->{length},
155     };
156 };
157
158
159 1;
160
161 __END__
162
163 =head1
164
165 Plagger::Plugin::Publish::Feed - republish RSS/Atom feeds
166
167 =head1 SYNOPSIS
168
169   - module: Publish::Feed
170     config:
171       format: RSS
172       dir: /home/yoshiki/plagger/feed
173       filename: my_%t.rss
174
175 =head1 CONFIG
176
177 =over 4
178
179 =item format
180
181 Specify the format of feed. C<Plagger::Plugin::Publish::Feed> supports
182 the following syndication feed formats:
183
184 =over 8
185
186 =item Atom (default)
187
188 =item RSS
189
190 =back
191
192 =item dir
193
194 Directory to save feed files in.
195
196 =item filename
197
198 Filename to be used to create feed files. It defaults to C<%i.rss> for
199 RSS and C<%i.atom> for Atom feed. It supports the following format
200 like printf():
201
202 =over 8
203
204 =item %u url
205
206 =item %l link
207
208 =item %t title
209
210 =item %i id
211
212 =back
213
214 =item full_content
215
216 Whether to publish full content feed. Defaults to 1.
217
218 =item taguri_base
219
220 Domain name to use with Tag URI base for Atom feed IDs. If it's not
221 set, the domain is grabbed using Sys::Hostname module Optional.
222
223 =back
224
225 =head1 AUTHOR
226
227 Tatsuhiko Miyagawa
228
229 =head1 CONTRIBUTORS
230
231 Yoshiki Kurihara
232
233 Gosuke Miyashita
234
235 =head1 SEE ALSO
236
237 L<Plagger>, L<XML::Feed>
238
239 =cut
Note: See TracBrowser for help on using the browser.