* aggregate: Forking a child broke the one state that mattered: Forcing
[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 2.00;
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 => "needsbuild", id => "aggregate", call => \&needsbuild);
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         if ($config{aggregate} && ! ($config{post_commit} && 
35                                      IkiWiki::commit_hook_enabled())) {
36                 if (! IkiWiki::lockwiki(0)) {
37                         debug("wiki is locked by another process, not aggregating");
38                         exit 1;
39                 }
40
41                 loadstate();
42                 my @feeds=needsaggregate();
43                 return unless @feeds;
44
45                 # Fork a child process to handle the aggregation.
46                 # The parent process will then handle building the
47                 # result. This avoids messy code to clear state
48                 # accumulated while aggregating.
49                 defined(my $pid = fork) or error("Can't fork: $!");
50                 if (! $pid) {
51                         IkiWiki::loadindex();
52                         aggregate(@feeds);
53                         expire();
54                         savestate();
55                         exit 0;
56                 }
57                 waitpid($pid,0);
58                 if ($?) {
59                         error "aggregation failed with code $?";
60                 }
61                 $IkiWiki::forcerebuild{$_->{sourcepage}}=1
62                         foreach @feeds;
63                 
64                 IkiWiki::unlockwiki();
65         }
66 } #}}}
67
68 sub needsbuild (@) { #{{{
69         my $needsbuild=shift;
70         
71         loadstate(); # if not already loaded
72
73         foreach my $feed (values %feeds) {
74                 if (exists $pagesources{$feed->{sourcepage}} && 
75                     grep { $_ eq $pagesources{$feed->{sourcepage}} } @$needsbuild) {
76                         # Mark all feeds originating on this page as removable;
77                         # preprocess will unmark those that still exist.
78                         remove_feeds($feed->{sourcepage});
79                 }
80         }
81 } # }}}
82
83 sub preprocess (@) { #{{{
84         my %params=@_;
85
86         foreach my $required (qw{name url}) {
87                 if (! exists $params{$required}) {
88                         return "[[aggregate ".sprintf(gettext("missing %s parameter"), $required)."]]";
89                 }
90         }
91
92         my $feed={};
93         my $name=$params{name};
94         if (exists $feeds{$name}) {
95                 $feed=$feeds{$name};
96         }
97         else {
98                 $feeds{$name}=$feed;
99         }
100         $feed->{name}=$name;
101         $feed->{sourcepage}=$params{page};
102         $feed->{url}=$params{url};
103         my $dir=exists $params{dir} ? $params{dir} : $params{page}."/".IkiWiki::titlepage($params{name});
104         $dir=~s/^\/+//;
105         ($dir)=$dir=~/$config{wiki_file_regexp}/;
106         $feed->{dir}=$dir;
107         $feed->{feedurl}=defined $params{feedurl} ? $params{feedurl} : "";
108         $feed->{updateinterval}=defined $params{updateinterval} ? $params{updateinterval} * 60 : 15 * 60;
109         $feed->{expireage}=defined $params{expireage} ? $params{expireage} : 0;
110         $feed->{expirecount}=defined $params{expirecount} ? $params{expirecount} : 0;
111         delete $feed->{remove};
112         delete $feed->{expired};
113         $feed->{lastupdate}=0 unless defined $feed->{lastupdate};
114         $feed->{numposts}=0 unless defined $feed->{numposts};
115         $feed->{newposts}=0 unless defined $feed->{newposts};
116         $feed->{message}=gettext("new feed") unless defined $feed->{message};
117         $feed->{error}=0 unless defined $feed->{error};
118         $feed->{tags}=[];
119         while (@_) {
120                 my $key=shift;
121                 my $value=shift;
122                 if ($key eq 'tag') {
123                         push @{$feed->{tags}}, $value;
124                 }
125         }
126
127         return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
128                ($feed->{error} ? "<em>" : "").$feed->{message}.
129                ($feed->{error} ? "</em>" : "").
130                " (".$feed->{numposts}." ".gettext("posts").
131                ($feed->{newposts} ? "; ".$feed->{newposts}.
132                                     " ".gettext("new") : "").
133                ")";
134 } # }}}
135
136 sub delete (@) { #{{{
137         my @files=@_;
138
139         # Remove feed data for removed pages.
140         foreach my $file (@files) {
141                 my $page=pagename($file);
142                 remove_feeds($page);
143         }
144 } #}}}
145
146 my $state_loaded=0;
147 sub loadstate () { #{{{
148         return if $state_loaded;
149         $state_loaded=1;
150         if (-e "$config{wikistatedir}/aggregate") {
151                 open(IN, "$config{wikistatedir}/aggregate") ||
152                         die "$config{wikistatedir}/aggregate: $!";
153                 while (<IN>) {
154                         $_=IkiWiki::possibly_foolish_untaint($_);
155                         chomp;
156                         my $data={};
157                         foreach my $i (split(/ /, $_)) {
158                                 my ($field, $val)=split(/=/, $i, 2);
159                                 if ($field eq "name" || $field eq "feed" ||
160                                     $field eq "guid" || $field eq "message") {
161                                         $data->{$field}=decode_entities($val, " \t\n");
162                                 }
163                                 elsif ($field eq "tag") {
164                                         push @{$data->{tags}}, $val;
165                                 }
166                                 else {
167                                         $data->{$field}=$val;
168                                 }
169                         }
170                         
171                         if (exists $data->{name}) {
172                                 $feeds{$data->{name}}=$data;
173                         }
174                         elsif (exists $data->{guid}) {
175                                 $guids{$data->{guid}}=$data;
176                         }
177                 }
178
179                 close IN;
180         }
181 } #}}}
182
183 sub savestate () { #{{{
184         return unless $state_loaded;
185         eval q{use HTML::Entities};
186         error($@) if $@;
187         my $newfile="$config{wikistatedir}/aggregate.new";
188         my $cleanup = sub { unlink($newfile) };
189         open (OUT, ">$newfile") || error("open $newfile: $!", $cleanup);
190         foreach my $data (values %feeds, values %guids) {
191                 if ($data->{remove}) {
192                         if ($data->{name}) {
193                                 foreach my $guid (values %guids) {
194                                         if ($guid->{feed} eq $data->{name}) {
195                                                 $guid->{remove}=1;
196                                         }
197                                 }
198                         }
199                         else {
200                                 unlink pagefile($data->{page})
201                                         if exists $data->{page};
202                         }
203                         next;
204                 }
205                 elsif ($data->{expired} && exists $data->{page}) {
206                         unlink pagefile($data->{page});
207                         delete $data->{page};
208                         delete $data->{md5};
209                 }
210
211                 my @line;
212                 foreach my $field (keys %$data) {
213                         if ($field eq "name" || $field eq "feed" ||
214                             $field eq "guid" || $field eq "message") {
215                                 push @line, "$field=".encode_entities($data->{$field}, " \t\n");
216                         }
217                         elsif ($field eq "tags") {
218                                 push @line, "tag=$_" foreach @{$data->{tags}};
219                         }
220                         else {
221                                 push @line, "$field=".$data->{$field};
222                         }
223                 }
224                 print OUT join(" ", @line)."\n" || error("write $newfile: $!", $cleanup);
225         }
226         close OUT || error("save $newfile: $!", $cleanup);
227         rename($newfile, "$config{wikistatedir}/aggregate") ||
228                 error("rename $newfile: $!", $cleanup);
229 } #}}}
230
231 sub expire () { #{{{
232         foreach my $feed (values %feeds) {
233                 next unless $feed->{expireage} || $feed->{expirecount};
234                 my $count=0;
235                 my %seen;
236                 foreach my $item (sort { $IkiWiki::pagectime{$b->{page}} <=> $IkiWiki::pagectime{$a->{page}} }
237                                   grep { exists $_->{page} && $_->{feed} eq $feed->{name} && $IkiWiki::pagectime{$_->{page}} }
238                                   values %guids) {
239                         if ($feed->{expireage}) {
240                                 my $days_old = (time - $IkiWiki::pagectime{$item->{page}}) / 60 / 60 / 24;
241                                 if ($days_old > $feed->{expireage}) {
242                                         debug(sprintf(gettext("expiring %s (%s days old)"),
243                                                 $item->{page}, int($days_old)));
244                                         $item->{expired}=1;
245                                 }
246                         }
247                         elsif ($feed->{expirecount} &&
248                                $count >= $feed->{expirecount}) {
249                                 debug(sprintf(gettext("expiring %s"), $item->{page}));
250                                 $item->{expired}=1;
251                         }
252                         else {
253                                 if (! $seen{$item->{page}}) {
254                                         $seen{$item->{page}}=1;
255                                         $count++;
256                                 }
257                         }
258                 }
259         }
260 } #}}}
261
262 sub needsaggregate () { #{{{
263         return values %feeds if $config{rebuild};
264         return grep { time - $_->{lastupdate} >= $_->{updateinterval} } values %feeds;
265 } #}}}
266
267 sub aggregate (@) { #{{{
268         eval q{use XML::Feed};
269         error($@) if $@;
270         eval q{use URI::Fetch};
271         error($@) if $@;
272         eval q{use HTML::Entities};
273         error($@) if $@;
274
275         foreach my $feed (@_) {
276                 $feed->{lastupdate}=time;
277                 $feed->{newposts}=0;
278                 $feed->{message}=sprintf(gettext("processed ok at %s"),
279                         displaytime($feed->{lastupdate}));
280                 $feed->{error}=0;
281
282                 debug(sprintf(gettext("checking feed %s ..."), $feed->{name}));
283
284                 if (! length $feed->{feedurl}) {
285                         my @urls=XML::Feed->find_feeds($feed->{url});
286                         if (! @urls) {
287                                 $feed->{message}=sprintf(gettext("could not find feed at %s"), $feed->{url});
288                                 $feed->{error}=1;
289                                 debug($feed->{message});
290                                 next;
291                         }
292                         $feed->{feedurl}=pop @urls;
293                 }
294                 my $res=URI::Fetch->fetch($feed->{feedurl});
295                 if (! $res) {
296                         $feed->{message}=URI::Fetch->errstr;
297                         $feed->{error}=1;
298                         debug($feed->{message});
299                         next;
300                 }
301                 if ($res->status == URI::Fetch::URI_GONE()) {
302                         $feed->{message}=gettext("feed not found");
303                         $feed->{error}=1;
304                         debug($feed->{message});
305                         next;
306                 }
307                 my $content=$res->content;
308                 my $f=eval{XML::Feed->parse(\$content)};
309                 if ($@) {
310                         # One common cause of XML::Feed crashing is a feed
311                         # that contains invalid UTF-8 sequences. Convert
312                         # feed to ascii to try to work around.
313                         $feed->{message}.=" ".sprintf(gettext("(invalid UTF-8 stripped from feed)"));
314                         $content=Encode::decode_utf8($content);
315                         $f=eval{XML::Feed->parse(\$content)};
316                 }
317                 if ($@) {
318                         # Another possibility is badly escaped entities.
319                         $feed->{message}.=" ".sprintf(gettext("(feed entities escaped)"));
320                         $content=~s/\&(?!amp)(\w+);/&amp;$1;/g;
321                         $content=Encode::decode_utf8($content);
322                         $f=eval{XML::Feed->parse(\$content)};
323                 }
324                 if ($@) {
325                         $feed->{message}=gettext("feed crashed XML::Feed!")." ($@)";
326                         $feed->{error}=1;
327                         debug($feed->{message});
328                         next;
329                 }
330                 if (! $f) {
331                         $feed->{message}=XML::Feed->errstr;
332                         $feed->{error}=1;
333                         debug($feed->{message});
334                         next;
335                 }
336
337                 foreach my $entry ($f->entries) {
338                         add_page(
339                                 feed => $feed,
340                                 copyright => $f->copyright,
341                                 title => defined $entry->title ? decode_entities($entry->title) : "untitled",
342                                 link => $entry->link,
343                                 content => defined $entry->content->body ? $entry->content->body : "",
344                                 guid => defined $entry->id ? $entry->id : time."_".$feed->name,
345                                 ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
346                         );
347                 }
348         }
349 } #}}}
350
351 sub add_page (@) { #{{{
352         my %params=@_;
353         
354         my $feed=$params{feed};
355         my $guid={};
356         my $mtime;
357         if (exists $guids{$params{guid}}) {
358                 # updating an existing post
359                 $guid=$guids{$params{guid}};
360                 return if $guid->{expired};
361         }
362         else {
363                 # new post
364                 $guid->{guid}=$params{guid};
365                 $guids{$params{guid}}=$guid;
366                 $mtime=$params{ctime};
367                 $feed->{numposts}++;
368                 $feed->{newposts}++;
369
370                 # assign it an unused page
371                 my $page=IkiWiki::titlepage($params{title});
372                 # escape slashes and periods in title so it doesn't specify
373                 # directory name or trigger ".." disallowing code.
374                 $page=~s!([/.])!"__".ord($1)."__"!eg;
375                 $page=$feed->{dir}."/".$page;
376                 ($page)=$page=~/$config{wiki_file_regexp}/;
377                 if (! defined $page || ! length $page) {
378                         $page=$feed->{dir}."/item";
379                 }
380                 my $c="";
381                 while (exists $IkiWiki::pagecase{lc $page.$c} ||
382                        -e pagefile($page.$c)) {
383                         $c++
384                 }
385
386                 # Make sure that the file name isn't too long. 
387                 # NB: This doesn't check for path length limits.
388                 my $max=POSIX::pathconf($config{srcdir}, &POSIX::_PC_NAME_MAX);
389                 if (defined $max && length(htmlfn($page)) >= $max) {
390                         $c="";
391                         $page=$feed->{dir}."/item";
392                         while (exists $IkiWiki::pagecase{lc $page.$c} ||
393                                -e pagefile($page.$c)) {
394                                 $c++
395                         }
396                 }
397
398                 $guid->{page}=$page;
399                 debug(sprintf(gettext("creating new page %s"), $page));
400         }
401         $guid->{feed}=$feed->{name};
402         
403         # To write or not to write? Need to avoid writing unchanged pages
404         # to avoid unneccessary rebuilding. The mtime from rss cannot be
405         # trusted; let's use a digest.
406         eval q{use Digest::MD5 'md5_hex'};
407         error($@) if $@;
408         require Encode;
409         my $digest=md5_hex(Encode::encode_utf8($params{content}));
410         return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $config{rebuild};
411         $guid->{md5}=$digest;
412
413         # Create the page.
414         my $template=template("aggregatepost.tmpl", blind_cache => 1);
415         $template->param(title => $params{title})
416                 if defined $params{title} && length($params{title});
417         $template->param(content => htmlescape(htmlabs($params{content}, $feed->{feedurl})));
418         $template->param(name => $feed->{name});
419         $template->param(url => $feed->{url});
420         $template->param(copyright => $params{copyright})
421                 if defined $params{copyright} && length $params{copyright};
422         $template->param(permalink => urlabs($params{link}, $feed->{feedurl}))
423                 if defined $params{link};
424         if (ref $feed->{tags}) {
425                 $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
426         }
427         writefile(htmlfn($guid->{page}), $config{srcdir},
428                 $template->output);
429
430         # Set the mtime, this lets the build process get the right creation
431         # time on record for the new page.
432         utime $mtime, $mtime, pagefile($guid->{page})
433                 if defined $mtime && $mtime <= time;
434 } #}}}
435
436 sub htmlescape ($) { #{{{
437         # escape accidental wikilinks and preprocessor stuff
438         my $html=shift;
439         $html=~s/(?<!\\)\[\[/\\\[\[/g;
440         return $html;
441 } #}}}
442
443 sub urlabs ($$) { #{{{
444         my $url=shift;
445         my $urlbase=shift;
446
447         URI->new_abs($url, $urlbase)->as_string;
448 } #}}}
449
450 sub htmlabs ($$) { #{{{
451         # Convert links in html from relative to absolute.
452         # Note that this is a heuristic, which is not specified by the rss
453         # spec and may not be right for all feeds. Also, see Debian
454         # bug #381359.
455         my $html=shift;
456         my $urlbase=shift;
457
458         my $ret="";
459         my $p = HTML::Parser->new(api_version => 3);
460         $p->handler(default => sub { $ret.=join("", @_) }, "text");
461         $p->handler(start => sub {
462                 my ($tagname, $pos, $text) = @_;
463                 if (ref $HTML::Tagset::linkElements{$tagname}) {
464                         while (4 <= @$pos) {
465                                 # use attribute sets from right to left
466                                 # to avoid invalidating the offsets
467                                 # when replacing the values
468                                 my($k_offset, $k_len, $v_offset, $v_len) =
469                                         splice(@$pos, -4);
470                                 my $attrname = lc(substr($text, $k_offset, $k_len));
471                                 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
472                                 next unless $v_offset; # 0 v_offset means no value
473                                 my $v = substr($text, $v_offset, $v_len);
474                                 $v =~ s/^([\'\"])(.*)\1$/$2/;
475                                 my $new_v=urlabs($v, $urlbase);
476                                 $new_v =~ s/\"/&quot;/g; # since we quote with ""
477                                 substr($text, $v_offset, $v_len) = qq("$new_v");
478                         }
479                 }
480                 $ret.=$text;
481         }, "tagname, tokenpos, text");
482         $p->parse($html);
483         $p->eof;
484
485         return $ret;
486 } #}}}
487
488 sub remove_feeds () { #{{{
489         my $page=shift;
490
491         my %removed;
492         foreach my $id (keys %feeds) {
493                 if ($feeds{$id}->{sourcepage} eq $page) {
494                         $feeds{$id}->{remove}=1;
495                         $removed{$id}=1;
496                 }
497         }
498 } #}}}
499
500 sub pagefile ($) { #{{{
501         my $page=shift;
502
503         return "$config{srcdir}/".htmlfn($page);
504 } #}}}
505
506 sub htmlfn ($) { #{{{
507         return shift().".".$config{htmlext};
508 } #}}}
509
510 1