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