* Many changes to make ikiwiki very resistant to write failures
[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 use HTML::Entities;
9 use HTML::Parser;
10 use HTML::Tagset;
11 use URI;
12 use open qw{:utf8 :std};
13
14 my %feeds;
15 my %guids;
16
17 sub import { #{{{
18         hook(type => "getopt", id => "aggregate", call => \&getopt);
19         hook(type => "checkconfig", id => "aggregate", call => \&checkconfig);
20         hook(type => "filter", id => "aggregate", call => \&filter);
21         hook(type => "preprocess", id => "aggregate", call => \&preprocess);
22         hook(type => "delete", id => "aggregate", call => \&delete);
23         hook(type => "savestate", id => "aggregate", call => \&savestate);
24 } # }}}
25
26 sub getopt () { #{{{
27         eval q{use Getopt::Long};
28         error($@) if $@;
29         Getopt::Long::Configure('pass_through');
30         GetOptions("aggregate" => \$config{aggregate});
31 } #}}}
32
33 sub checkconfig () { #{{{
34         IkiWiki::lockwiki();
35         loadstate();
36         if ($config{aggregate}) {
37                 IkiWiki::loadindex();
38                 aggregate();
39                 expire();
40                 savestate();
41         }
42         IkiWiki::unlockwiki();
43 } #}}}
44
45 sub filter (@) { #{{{
46         my %params=@_;
47         my $page=$params{page};
48
49         # Mark all feeds originating on this page as removable;
50         # preprocess will unmark those that still exist.
51         remove_feeds($page);
52
53         return $params{content};
54 } # }}}
55
56 sub preprocess (@) { #{{{
57         my %params=@_;
58
59         foreach my $required (qw{name url}) {
60                 if (! exists $params{$required}) {
61                         return "[[".sprintf(gettext("aggregate plugin missing %s parameter"), $required)."]]";
62                 }
63         }
64
65         my $feed={};
66         my $name=$params{name};
67         if (exists $feeds{$name}) {
68                 $feed=$feeds{$name};
69         }
70         else {
71                 $feeds{$name}=$feed;
72         }
73         $feed->{name}=$name;
74         $feed->{sourcepage}=$params{page};
75         $feed->{url}=$params{url};
76         my $dir=exists $params{dir} ? $params{dir} : $params{page}."/".IkiWiki::titlepage($params{name});
77         $dir=~s/^\/+//;
78         ($dir)=$dir=~/$config{wiki_file_regexp}/;
79         $feed->{dir}=$dir;
80         $feed->{feedurl}=defined $params{feedurl} ? $params{feedurl} : "";
81         $feed->{updateinterval}=defined $params{updateinterval} ? $params{updateinterval} * 60 : 15 * 60;
82         $feed->{expireage}=defined $params{expireage} ? $params{expireage} : 0;
83         $feed->{expirecount}=defined $params{expirecount} ? $params{expirecount} : 0;
84         delete $feed->{remove};
85         delete $feed->{expired};
86         $feed->{lastupdate}=0 unless defined $feed->{lastupdate};
87         $feed->{numposts}=0 unless defined $feed->{numposts};
88         $feed->{newposts}=0 unless defined $feed->{newposts};
89         $feed->{message}=gettext("new feed") unless defined $feed->{message};
90         $feed->{error}=0 unless defined $feed->{error};
91         $feed->{tags}=[];
92         while (@_) {
93                 my $key=shift;
94                 my $value=shift;
95                 if ($key eq 'tag') {
96                         push @{$feed->{tags}}, $value;
97                 }
98         }
99
100         return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
101                ($feed->{error} ? "<em>" : "").$feed->{message}.
102                ($feed->{error} ? "</em>" : "").
103                " (".$feed->{numposts}." ".gettext("posts").
104                ($feed->{newposts} ? "; ".$feed->{newposts}.
105                                     " ".gettext("new") : "").
106                ")";
107 } # }}}
108
109 sub delete (@) { #{{{
110         my @files=@_;
111
112         # Remove feed data for removed pages.
113         foreach my $file (@files) {
114                 my $page=pagename($file);
115                 remove_feeds($page);
116         }
117 } #}}}
118
119 sub loadstate () { #{{{
120         if (-e "$config{wikistatedir}/aggregate") {
121                 open (IN, "$config{wikistatedir}/aggregate" ||
122                         die "$config{wikistatedir}/aggregate: $!");
123                 while (<IN>) {
124                         $_=IkiWiki::possibly_foolish_untaint($_);
125                         chomp;
126                         my $data={};
127                         foreach my $i (split(/ /, $_)) {
128                                 my ($field, $val)=split(/=/, $i, 2);
129                                 if ($field eq "name" || $field eq "feed" ||
130                                     $field eq "guid" || $field eq "message") {
131                                         $data->{$field}=decode_entities($val, " \t\n");
132                                 }
133                                 elsif ($field eq "tag") {
134                                         push @{$data->{tags}}, $val;
135                                 }
136                                 else {
137                                         $data->{$field}=$val;
138                                 }
139                         }
140                         
141                         if (exists $data->{name}) {
142                                 $feeds{$data->{name}}=$data;
143                         }
144                         elsif (exists $data->{guid}) {
145                                 $guids{$data->{guid}}=$data;
146                         }
147                 }
148
149                 close IN;
150         }
151 } #}}}
152
153 sub savestate () { #{{{
154         eval q{use HTML::Entities};
155         error($@) if $@;
156         my $newfile="$config{wikistatedir}/aggregate.new";
157         # TODO: This cleanup function could use improvement. Any newly
158         # aggregated files are left behind unrecorded, and should be deleted.
159         my $cleanup = sub { unlink($newfile) };
160         open (OUT, ">$newfile") || error("open $newfile: $!", $cleanup);
161         foreach my $data (values %feeds, values %guids) {
162                 if ($data->{remove}) {
163                         if ($data->{name}) {
164                                 foreach my $guid (values %guids) {
165                                         if ($guid->{feed} eq $data->{name}) {
166                                                 $guid->{remove}=1;
167                                         }
168                                 }
169                         }
170                         else {
171                                 unlink pagefile($data->{page});
172                         }
173                         next;
174                 }
175                 elsif ($data->{expired} && exists $data->{page}) {
176                         unlink pagefile($data->{page});
177                         delete $data->{page};
178                         delete $data->{md5};
179                 }
180
181                 my @line;
182                 foreach my $field (keys %$data) {
183                         if ($field eq "name" || $field eq "feed" ||
184                             $field eq "guid" || $field eq "message") {
185                                 push @line, "$field=".encode_entities($data->{$field}, " \t\n");
186                         }
187                         elsif ($field eq "tags") {
188                                 push @line, "tag=$_" foreach @{$data->{tags}};
189                         }
190                         else {
191                                 push @line, "$field=".$data->{$field};
192                         }
193                 }
194                 print OUT join(" ", @line)."\n" || error("write $newfile: $!", $cleanup);
195         }
196         close OUT || error("save $newfile: $!", $cleanup);
197         rename($newfile, "$config{wikistatedir}/aggregate") ||
198                 error("rename $newfile: $!", $cleanup);
199 } #}}}
200
201 sub expire () { #{{{
202         foreach my $feed (values %feeds) {
203                 next unless $feed->{expireage} || $feed->{expirecount};
204                 my $count=0;
205                 foreach my $item (sort { $IkiWiki::pagectime{$b->{page}} <=> $IkiWiki::pagectime{$a->{page}} }
206                                   grep { exists $_->{page} && $_->{feed} eq $feed->{name} && $IkiWiki::pagectime{$_->{page}} }
207                                   values %guids) {
208                         if ($feed->{expireage}) {
209                                 my $days_old = (time - $IkiWiki::pagectime{$item->{page}}) / 60 / 60 / 24;
210                                 if ($days_old > $feed->{expireage}) {
211                                         debug(sprintf(gettext("expiring %s (%s days old)"),
212                                                 $item->{page}, $days_old));
213                                         $item->{expired}=1;
214                                 }
215                         }
216                         elsif ($feed->{expirecount} &&
217                                $count >= $feed->{expirecount}) {
218                                 debug(sprintf(gettext("expiring %s"), $item->{page}));
219                                 $item->{expired}=1;
220                         }
221                         else {
222                                 $count++;
223                         }
224                 }
225         }
226 } #}}}
227
228 sub aggregate () { #{{{
229         eval q{use XML::Feed};
230         error($@) if $@;
231         eval q{use HTML::Entities};
232         error($@) if $@;
233
234         foreach my $feed (values %feeds) {
235                 next unless $config{rebuild} || 
236                         time - $feed->{lastupdate} >= $feed->{updateinterval};
237                 $feed->{lastupdate}=time;
238                 $feed->{newposts}=0;
239                 $IkiWiki::forcerebuild{$feed->{sourcepage}}=1;
240
241                 debug(sprintf(gettext("checking feed %s ..."), $feed->{name}));
242
243                 if (! length $feed->{feedurl}) {
244                         my @urls=XML::Feed->find_feeds($feed->{url});
245                         if (! @urls) {
246                                 $feed->{message}=sprintf(gettext("could not find feed at %s"), $feed->{feedurl});
247                                 $feed->{error}=1;
248                                 debug($feed->{message});
249                                 next;
250                         }
251                         $feed->{feedurl}=pop @urls;
252                 }
253                 my $f=eval{XML::Feed->parse(URI->new($feed->{feedurl}))};
254                 if ($@) {
255                         $feed->{message}=gettext("feed crashed XML::Feed!")." ($@)";
256                         $feed->{error}=1;
257                         debug($feed->{message});
258                         next;
259                 }
260                 if (! $f) {
261                         $feed->{message}=XML::Feed->errstr;
262                         $feed->{error}=1;
263                         debug($feed->{message});
264                         next;
265                 }
266
267                 foreach my $entry ($f->entries) {
268                         add_page(
269                                 feed => $feed,
270                                 title => defined $entry->title ? decode_entities($entry->title) : "untitled",
271                                 link => $entry->link,
272                                 content => $entry->content->body,
273                                 guid => defined $entry->id ? $entry->id : time."_".$feed->name,
274                                 ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
275                         );
276                 }
277
278                 $feed->{message}=sprintf(gettext("processed ok at %s"),
279                         displaytime($feed->{lastupdate}));
280                 $feed->{error}=0;
281         }
282 } #}}}
283
284 sub add_page (@) { #{{{
285         my %params=@_;
286         
287         my $feed=$params{feed};
288         my $guid={};
289         my $mtime;
290         if (exists $guids{$params{guid}}) {
291                 # updating an existing post
292                 $guid=$guids{$params{guid}};
293                 return if $guid->{expired};
294         }
295         else {
296                 # new post
297                 $guid->{guid}=$params{guid};
298                 $guids{$params{guid}}=$guid;
299                 $mtime=$params{ctime};
300                 $feed->{numposts}++;
301                 $feed->{newposts}++;
302
303                 # assign it an unused page
304                 my $page=IkiWiki::titlepage($params{title});
305                 # escape slashes and periods in title so it doesn't specify
306                 # directory name or trigger ".." disallowing code.
307                 $page=~s!([/.])!"__".ord($1)."__"!eg;
308                 $page=$feed->{dir}."/".$page;
309                 ($page)=$page=~/$config{wiki_file_regexp}/;
310                 if (! defined $page || ! length $page) {
311                         $page=$feed->{dir}."/item";
312                 }
313                 my $c="";
314                 while (exists $IkiWiki::pagecase{lc $page.$c} ||
315                        -e pagefile($page.$c)) {
316                         $c++
317                 }
318
319                 # Make sure that the file name isn't too long. 
320                 # NB: This doesn't check for path length limits.
321                 eval q{use POSIX};
322                 my $max=POSIX::pathconf($config{srcdir}, &POSIX::_PC_NAME_MAX);
323                 if (defined $max && length(htmlpage($page)) >= $max) {
324                         $c="";
325                         $page=$feed->{dir}."/item";
326                         while (exists $IkiWiki::pagecase{lc $page.$c} ||
327                                -e pagefile($page.$c)) {
328                                 $c++
329                         }
330                 }
331
332                 $guid->{page}=$page;
333                 debug(sprintf(gettext("creating new page %s"), $page));
334         }
335         $guid->{feed}=$feed->{name};
336         
337         # To write or not to write? Need to avoid writing unchanged pages
338         # to avoid unneccessary rebuilding. The mtime from rss cannot be
339         # trusted; let's use a digest.
340         eval q{use Digest::MD5 'md5_hex'};
341         error($@) if $@;
342         require Encode;
343         my $digest=md5_hex(Encode::encode_utf8($params{content}));
344         return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $config{rebuild};
345         $guid->{md5}=$digest;
346
347         # Create the page.
348         my $template=template("aggregatepost.tmpl", blind_cache => 1);
349         $template->param(title => $params{title})
350                 if defined $params{title} && length($params{title});
351         $template->param(content => htmlescape(htmlabs($params{content}, $feed->{feedurl})));
352         $template->param(name => $feed->{name});
353         $template->param(url => $feed->{url});
354         $template->param(permalink => urlabs($params{link}, $feed->{feedurl}))
355                 if defined $params{link};
356         if (ref $feed->{tags}) {
357                 $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
358         }
359         writefile(htmlpage($guid->{page}), $config{srcdir},
360                 $template->output);
361
362         # Set the mtime, this lets the build process get the right creation
363         # time on record for the new page.
364         utime $mtime, $mtime, pagefile($guid->{page}) if defined $mtime;
365 } #}}}
366
367 sub htmlescape ($) { #{{{
368         # escape accidental wikilinks and preprocessor stuff
369         my $html=shift;
370         $html=~s/(?<!\\)\[\[/\\\[\[/g;
371         return $html;
372 } #}}}
373
374 sub urlabs ($$) { #{{{
375         my $url=shift;
376         my $urlbase=shift;
377
378         URI->new_abs($url, $urlbase)->as_string;
379 } #}}}
380
381 sub htmlabs ($$) { #{{{
382         # Convert links in html from relative to absolute.
383         # Note that this is a heuristic, which is not specified by the rss
384         # spec and may not be right for all feeds. Also, see Debian
385         # bug #381359.
386         my $html=shift;
387         my $urlbase=shift;
388
389         my $ret="";
390         my $p = HTML::Parser->new(api_version => 3);
391         $p->handler(default => sub { $ret.=join("", @_) }, "text");
392         $p->handler(start => sub {
393                 my ($tagname, $pos, $text) = @_;
394                 if (ref $HTML::Tagset::linkElements{$tagname}) {
395                         while (4 <= @$pos) {
396                                 # use attribute sets from right to left
397                                 # to avoid invalidating the offsets
398                                 # when replacing the values
399                                 my($k_offset, $k_len, $v_offset, $v_len) =
400                                         splice(@$pos, -4);
401                                 my $attrname = lc(substr($text, $k_offset, $k_len));
402                                 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
403                                 next unless $v_offset; # 0 v_offset means no value
404                                 my $v = substr($text, $v_offset, $v_len);
405                                 $v =~ s/^([\'\"])(.*)\1$/$2/;
406                                 my $new_v=urlabs($v, $urlbase);
407                                 $new_v =~ s/\"/&quot;/g; # since we quote with ""
408                                 substr($text, $v_offset, $v_len) = qq("$new_v");
409                         }
410                 }
411                 $ret.=$text;
412         }, "tagname, tokenpos, text");
413         $p->parse($html);
414         $p->eof;
415
416         return $ret;
417 } #}}}
418
419 sub remove_feeds () { #{{{
420         my $page=shift;
421
422         my %removed;
423         foreach my $id (keys %feeds) {
424                 if ($feeds{$id}->{sourcepage} eq $page) {
425                         $feeds{$id}->{remove}=1;
426                         $removed{$id}=1;
427                 }
428         }
429 } #}}}
430
431 sub pagefile ($) { #{{{
432         my $page=shift;
433
434         return "$config{srcdir}/".htmlpage($page);
435 } #}}}
436
437 1