]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/aggregate.pm
* ikiwiki can now download and aggregate feeds with its new aggregate
[ikiwiki.git] / IkiWiki / Plugin / aggregate.pm
1 #!/usr/bin/perl
2 # Blog aggregation plugin.
3 package IkiWiki::Plugin::aggregate;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8
9 my %feeds;
10 my %guids;
11
12 sub import { #{{{
13         IkiWiki::hook(type => "getopt", id => "aggregate", 
14                 call => \&getopt);
15         IkiWiki::hook(type => "checkconfig", id => "aggregate",
16                 call => \&checkconfig);
17         IkiWiki::hook(type => "filter", id => "aggregate", 
18                 call => \&filter);
19         IkiWiki::hook(type => "preprocess", id => "aggregate",
20                 call => \&preprocess);
21         IkiWiki::hook(type => "delete", id => "aggregate",
22                 call => \&delete);
23         IkiWiki::hook(type => "savestate", id => "aggregate",
24                 call => \&savestate);
25 } # }}}
26
27 sub getopt () { #{{{
28         eval q{use Getopt::Long};
29         Getopt::Long::Configure('pass_through');
30         GetOptions("aggregate" => \$IkiWiki::config{aggregate});
31 } #}}}
32
33 sub checkconfig () { #{{{
34         loadstate();
35         if ($IkiWiki::config{aggregate}) {
36                 IkiWiki::loadindex();
37                 aggregate();
38                 savestate();
39         }
40 } #}}}
41
42 sub filter (@) { #{{{
43         my %params=@_;
44         my $page=$params{page};
45
46         # Mark all feeds originating on this page as removable;
47         # preprocess will unmark those that still exist.
48         remove_feeds($page);
49
50         return $params{content};
51 } # }}}
52
53 sub preprocess (@) { #{{{
54         my %params=@_;
55
56         foreach my $required (qw{name url dir}) {
57                 if (! exists $params{$required}) {
58                         return "[[aggregate plugin missing $required parameter]]";
59                 }
60         }
61
62         my $feed={};
63         my $name=$params{name};
64         if (exists $feeds{$name}) {
65                 $feed=$feeds{$name};
66         }
67         else {
68                 $feeds{$name}=$feed;
69         }
70         $feed->{name}=$name;
71         $feed->{sourcepage}=$params{page};
72         $feed->{url}=$params{url};
73         ($feed->{dir})=$params{dir}=~/$IkiWiki::config{wiki_file_regexp}/;
74         $feed->{dir}=~s/^\/+//;
75         $feed->{feedurl}=defined $params{feedurl} ? $params{feedurl} : $params{url};
76         $feed->{updateinterval}=defined $params{updateinterval} ? $params{updateinterval} : 15;
77         $feed->{expireage}=defined $params{expireage} ? $params{expireage} : 0;
78         $feed->{expirecount}=defined $params{expirecount} ? $params{expirecount} : 0;
79         delete $feed->{remove};
80         $feed->{lastupdate}=0 unless defined $feed->{lastupdate};
81         $feed->{numposts}=0 unless defined $feed->{numposts};
82         $feed->{newposts}=0 unless defined $feed->{newposts};
83         $feed->{message}="new feed" unless defined $feed->{message};
84         while (@_) {
85                 my $key=shift;
86                 my $value=shift;
87                 if ($key eq 'tag') {
88                         push @{$feed->{tags}}, $value;
89                 }
90         }
91
92         return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
93                "<i>".$feed->{message}."</i> (".$feed->{numposts}.
94                " stored posts; ".$feed->{newposts}." new)";
95 } # }}}
96
97 sub delete (@) { #{{{
98         my @files=@_;
99
100         # Remove feed data for removed pages.
101         foreach my $file (@files) {
102                 my $page=IkiWiki::pagename($file);
103                 remove_feeds($page);
104         }
105 } #}}}
106
107 sub loadstate () { #{{{
108         if (-e "$IkiWiki::config{wikistatedir}/aggregate") {
109                 open (IN, "$IkiWiki::config{wikistatedir}/aggregate" ||
110                         die "$IkiWiki::config{wikistatedir}/aggregate: $!");
111                 while (<IN>) {
112                         $_=IkiWiki::possibly_foolish_untaint($_);
113                         chomp;
114                         my $data={};
115                         foreach my $i (split(/ /, $_)) {
116                                 my ($field, $val)=split(/=/, $i, 2);
117                                 if ($field eq "name" || $field eq "message") {
118                                         $data->{$field}=IkiWiki::pagetitle($val);
119                                 }
120                                 elsif ($field eq "tag") {
121                                         push @{$data->{tags}}, $val;
122                                 }
123                                 else {
124                                         $data->{$field}=$val;
125                                 }
126                         }
127                         
128                         if (exists $data->{name}) {
129                                 $feeds{$data->{name}}=$data;
130                         }
131                         elsif (exists $data->{guid}) {
132                                 $guids{$data->{guid}}=$data;
133                         }
134                 }
135
136                 close IN;
137         }
138 } #}}}
139
140 sub savestate () { #{{{
141         open (OUT, ">$IkiWiki::config{wikistatedir}/aggregate" ||
142                 die "$IkiWiki::config{wikistatedir}/aggregate: $!");
143         foreach my $data (values %feeds, values %guids) {
144                 if ($data->{remove}) {
145                         if ($data->{name}) {
146                                 foreach my $guid (values %guids) {
147                                         if ($guid->{feed} eq $data->{name}) {
148                                                 $guid->{remove}=1;
149                                         }
150                                 }
151                         }
152                         else {
153                                 unlink pagefile($data->{page});
154                         }
155                         next;
156                 }
157
158                 my @line;
159                 foreach my $field (keys %$data) {
160                         if ($field eq "name" || $field eq "message") {
161                                 push @line, "$field=".IkiWiki::titlepage($data->{$field});
162                         }
163                         elsif ($field eq "tags") {
164                                 push @line, "tag=$_" foreach @{$data->{tags}};
165                         }
166                         else {
167                                 push @line, "$field=".$data->{$field};
168                         }
169                 }
170                 print OUT join(" ", @line)."\n";
171         }
172         close OUT;
173 } #}}}
174
175 sub aggregate () { #{{{
176         eval q{use XML::Feed};
177         die $@ if $@;
178         eval q{use HTML::Entities};
179         die $@ if $@;
180
181 FEED:   foreach my $feed (values %feeds) {
182                 # TODO: check updateinterval
183                 $feed->{lastupdate}=time;
184                 $feed->{newposts}=0;
185                 $IkiWiki::forcerebuild{$feed->{sourcepage}}=1;
186
187                 IkiWiki::debug("checking feed ".$feed->{name}." ...");
188
189                 my @urls=XML::Feed->find_feeds($feed->{feedurl});
190                 if (! @urls) {
191                         $feed->{message}="could not find feed at ".$feed->{feedurl};
192                         IkiWiki::debug($feed->{message});
193                 }
194                 foreach my $url (@urls) {
195                         my $f=XML::Feed->parse(URI->new($url));
196                         if (! $f) {
197                                 $feed->{message}=XML::Feed->errstr;
198                                 IkiWiki::debug($feed->{message});
199                                 next FEED;
200                         }
201
202                         foreach my $entry ($f->entries) {
203                                 add_page(
204                                         feed => $feed,
205                                         title => decode_entities($entry->title),
206                                         link => $entry->link,
207                                         content => $entry->content->body,
208                                         guid => defined $entry->id ? $entry->id : time."_".$feed->name,
209                                         ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
210                                 );
211                         }
212                 }
213                 $feed->{message}="processed ok";
214         }
215
216         # TODO: expiry
217 } #}}}
218
219 sub add_page (@) { #{{{
220         my %params=@_;
221
222         my $feed=$params{feed};
223         my $guid={};
224         my $mtime;
225         if (exists $guids{$params{guid}}) {
226                 # updating an existing post
227                 $guid=$guids{$params{guid}};
228         }
229         else {
230                 # new post
231                 $guid->{guid}=$params{guid};
232                 $guids{$params{guid}}=$guid;
233                 $mtime=$params{ctime};
234                 $feed->{numposts}++;
235                 $feed->{newposts}++;
236
237                 # assign it an unused page
238                 my $page=$feed->{dir}."/".IkiWiki::titlepage($params{title});
239                 ($page)=$page=~/$IkiWiki::config{wiki_file_regexp}/;
240                 if (! defined $page || ! length $page) {
241                         $page=$feed->{dir}."/item";
242                 }
243                 $page=~s/\.\.//g; # avoid ".." directory tricks
244                 my $c="";
245                 while (exists $IkiWiki::pagesources{$page.$c} ||
246                        -e pagefile($page.$c)) {
247                         $c++
248                 }
249                 $guid->{page}=$page;
250                 IkiWiki::debug("creating new page $page");
251         }
252         $guid->{feed}=$feed->{name};
253         
254         # To write or not to write? Need to avoid writing unchanged pages
255         # to avoid unneccessary rebuilding. The mtime from rss cannot be
256         # trusted; let's use a digest.
257         eval q{use Digest::MD5 'md5_hex'};
258         my $digest=md5_hex($params{content});
259         return unless ! exists $guid->{md5} || $guid->{md5} ne $digest;
260         $guid->{md5}=$digest;
261
262         # Create the page.
263         my $template=IkiWiki::template("aggregatepost.tmpl", blind_cache => 1);
264         my $content=$params{content};
265         $params{content}=~s/(?<!\\)\[\[/\\\[\[/g; # escape accidental wikilinks
266                                                   # and preprocessor stuff
267         $template->param(content => $params{content});
268         $template->param(url => $feed->{url});
269         $template->param(name => $feed->{name});
270         $template->param(link => $params{link}) if defined $params{link};
271         if (ref $feed->{tags}) {
272                 $template->param(tags => map { tag => $_ }, @{$feed->{tags}});
273         }
274         IkiWiki::writefile($guid->{page}.".html", $IkiWiki::config{srcdir},
275                 $template->output);
276
277         # Set the mtime, this lets the build process get the right creation
278         # time on record for the new page.
279         utime $mtime, $mtime, pagefile($guid->{page}) if defined $mtime;
280 } #}}}
281
282 sub remove_feeds () { #{{{
283         my $page=shift;
284
285         my %removed;
286         foreach my $id (keys %feeds) {
287                 if ($feeds{$id}->{sourcepage} eq $page) {
288                         $feeds{$id}->{remove}=1;
289                         $removed{$id}=1;
290                 }
291         }
292 } #}}}
293
294 sub pagefile ($) { #{{{
295         my $page=shift;
296
297         return "$IkiWiki::config{srcdir}/$page.html";
298 } #}}}
299
300 1