2 # Feed aggregation plugin.
3 package IkiWiki::Plugin::aggregate;
12 use open qw{:utf8 :std};
18 hook(type => "getopt", id => "aggregate", call => \&getopt);
19 hook(type => "getsetup", id => "aggregate", call => \&getsetup);
20 hook(type => "checkconfig", id => "aggregate", call => \&checkconfig);
21 hook(type => "needsbuild", id => "aggregate", call => \&needsbuild);
22 hook(type => "preprocess", id => "aggregate", call => \&preprocess);
23 hook(type => "delete", id => "aggregate", call => \&delete);
24 hook(type => "savestate", id => "aggregate", call => \&savestate);
25 hook(type => "htmlize", id => "_aggregated", call => \&htmlize);
26 if (exists $config{aggregate_webtrigger} && $config{aggregate_webtrigger}) {
27 hook(type => "cgi", id => "aggregate", call => \&cgi);
32 eval q{use Getopt::Long};
34 Getopt::Long::Configure('pass_through');
36 "aggregate" => \$config{aggregate},
37 "aggregateinternal!" => \$config{aggregateinternal},
41 sub getsetup () { #{{{
43 aggregateinternal => {
46 description => "enable aggregation to internal pages?",
47 safe => 0, # enabling needs manual transition
50 aggregate_webtrigger => {
53 description => "allow aggregation to be triggered via the web?",
59 sub checkconfig () { #{{{
60 if ($config{aggregate} && ! ($config{post_commit} &&
61 IkiWiki::commit_hook_enabled())) {
69 if (defined $cgi->param('do') &&
70 $cgi->param("do") eq "aggregate_webtrigger") {
72 print "Content-Type: text/plain\n\n";
76 print gettext("Aggregation triggered via web.")."\n\n";
77 if (launchaggregation()) {
80 require IkiWiki::Render;
85 print gettext("Nothing to do right now, all feeds are up-to-date!")."\n";
91 sub launchaggregation () { #{{{
92 # See if any feeds need aggregation.
94 my @feeds=needsaggregate();
96 if (! lockaggregate()) {
97 debug("an aggregation process is already running");
100 # force a later rebuild of source pages
101 $IkiWiki::forcerebuild{$_->{sourcepage}}=1
104 # Fork a child process to handle the aggregation.
105 # The parent process will then handle building the
106 # result. This avoids messy code to clear state
107 # accumulated while aggregating.
108 defined(my $pid = fork) or error("Can't fork: $!");
110 IkiWiki::loadindex();
111 # Aggregation happens without the main wiki lock
112 # being held. This allows editing pages etc while
113 # aggregation is running.
117 # Merge changes, since aggregation state may have
118 # changed on disk while the aggregation was happening.
127 error "aggregation failed with code $?";
136 # Pages with extension _aggregated have plain html markup, pass through.
137 sub htmlize (@) { #{{{
139 return $params{content};
142 # Used by ikiwiki-transition aggregateinternal.
143 sub migrate_to_internal { #{{{
144 if (! lockaggregate()) {
145 error("an aggregation process is currently running");
152 foreach my $data (values %guids) {
153 next unless $data->{page};
154 next if $data->{expired};
156 $config{aggregateinternal} = 0;
157 my $oldname = pagefile($data->{page});
158 my $oldoutput = $config{destdir}."/".IkiWiki::htmlpage($data->{page});
160 $config{aggregateinternal} = 1;
161 my $newname = pagefile($data->{page});
163 debug "moving $oldname -> $newname";
166 error("$newname already exists");
169 debug("already renamed to $newname?");
172 elsif (-e $oldname) {
173 rename($oldname, $newname) || error("$!");
176 debug("$oldname not found");
179 require IkiWiki::Render;
180 debug("removing output file $oldoutput");
181 IkiWiki::prune($oldoutput);
191 sub needsbuild (@) { #{{{
192 my $needsbuild=shift;
196 foreach my $feed (values %feeds) {
197 if (exists $pagesources{$feed->{sourcepage}} &&
198 grep { $_ eq $pagesources{$feed->{sourcepage}} } @$needsbuild) {
199 # Mark all feeds originating on this page as
200 # not yet seen; preprocess will unmark those that
202 markunseen($feed->{sourcepage});
207 sub preprocess (@) { #{{{
210 foreach my $required (qw{name url}) {
211 if (! exists $params{$required}) {
212 error sprintf(gettext("missing %s parameter"), $required)
217 my $name=$params{name};
218 if (exists $feeds{$name}) {
225 $feed->{sourcepage}=$params{page};
226 $feed->{url}=$params{url};
227 my $dir=exists $params{dir} ? $params{dir} : $params{page}."/".IkiWiki::titlepage($params{name});
229 ($dir)=$dir=~/$config{wiki_file_regexp}/;
231 $feed->{feedurl}=defined $params{feedurl} ? $params{feedurl} : "";
232 $feed->{updateinterval}=defined $params{updateinterval} ? $params{updateinterval} * 60 : 15 * 60;
233 $feed->{expireage}=defined $params{expireage} ? $params{expireage} : 0;
234 $feed->{expirecount}=defined $params{expirecount} ? $params{expirecount} : 0;
235 if (exists $params{template}) {
236 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
239 $params{template} = "aggregatepost"
241 $feed->{template}=$params{template} . ".tmpl";
242 delete $feed->{unseen};
243 $feed->{lastupdate}=0 unless defined $feed->{lastupdate};
244 $feed->{numposts}=0 unless defined $feed->{numposts};
245 $feed->{newposts}=0 unless defined $feed->{newposts};
246 $feed->{message}=gettext("new feed") unless defined $feed->{message};
247 $feed->{error}=0 unless defined $feed->{error};
253 push @{$feed->{tags}}, $value;
257 return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
258 ($feed->{error} ? "<em>" : "").$feed->{message}.
259 ($feed->{error} ? "</em>" : "").
260 " (".$feed->{numposts}." ".gettext("posts").
261 ($feed->{newposts} ? "; ".$feed->{newposts}.
262 " ".gettext("new") : "").
266 sub delete (@) { #{{{
269 # Remove feed data for removed pages.
270 foreach my $file (@files) {
271 my $page=pagename($file);
276 sub markunseen ($) { #{{{
279 foreach my $id (keys %feeds) {
280 if ($feeds{$id}->{sourcepage} eq $page) {
281 $feeds{$id}->{unseen}=1;
288 sub loadstate () { #{{{
289 return if $state_loaded;
291 if (-e "$config{wikistatedir}/aggregate") {
292 open(IN, "$config{wikistatedir}/aggregate") ||
293 die "$config{wikistatedir}/aggregate: $!";
295 $_=IkiWiki::possibly_foolish_untaint($_);
298 foreach my $i (split(/ /, $_)) {
299 my ($field, $val)=split(/=/, $i, 2);
300 if ($field eq "name" || $field eq "feed" ||
301 $field eq "guid" || $field eq "message") {
302 $data->{$field}=decode_entities($val, " \t\n");
304 elsif ($field eq "tag") {
305 push @{$data->{tags}}, $val;
308 $data->{$field}=$val;
312 if (exists $data->{name}) {
313 $feeds{$data->{name}}=$data;
315 elsif (exists $data->{guid}) {
316 $guids{$data->{guid}}=$data;
324 sub savestate () { #{{{
325 return unless $state_loaded;
327 my $newfile="$config{wikistatedir}/aggregate.new";
328 my $cleanup = sub { unlink($newfile) };
329 open (OUT, ">$newfile") || error("open $newfile: $!", $cleanup);
330 foreach my $data (values %feeds, values %guids) {
332 foreach my $field (keys %$data) {
333 if ($field eq "name" || $field eq "feed" ||
334 $field eq "guid" || $field eq "message") {
335 push @line, "$field=".encode_entities($data->{$field}, " \t\n");
337 elsif ($field eq "tags") {
338 push @line, "tag=$_" foreach @{$data->{tags}};
341 push @line, "$field=".$data->{$field};
344 print OUT join(" ", @line)."\n" || error("write $newfile: $!", $cleanup);
346 close OUT || error("save $newfile: $!", $cleanup);
347 rename($newfile, "$config{wikistatedir}/aggregate") ||
348 error("rename $newfile: $!", $cleanup);
351 sub garbage_collect () { #{{{
352 foreach my $name (keys %feeds) {
353 # remove any feeds that were not seen while building the pages
354 # that used to contain them
355 if ($feeds{$name}->{unseen}) {
356 delete $feeds{$name};
360 foreach my $guid (values %guids) {
361 # any guid whose feed is gone should be removed
362 if (! exists $feeds{$guid->{feed}}) {
363 unlink pagefile($guid->{page})
364 if exists $guid->{page};
365 delete $guids{$guid->{guid}};
367 # handle expired guids
368 elsif ($guid->{expired} && exists $guid->{page}) {
369 unlink pagefile($guid->{page});
370 delete $guid->{page};
376 sub mergestate () { #{{{
377 # Load the current state in from disk, and merge into it
378 # values from the state in memory that might have changed
379 # during aggregation.
385 # All that can change in feed state during aggregation is a few
387 foreach my $name (keys %myfeeds) {
388 if (exists $feeds{$name}) {
389 foreach my $field (qw{message lastupdate numposts
391 $feeds{$name}->{$field}=$myfeeds{$name}->{$field};
396 # New guids can be created during aggregation.
397 # It's also possible that guids were removed from the on-disk state
398 # while the aggregation was in process. That would only happen if
399 # their feed was also removed, so any removed guids added back here
400 # will be garbage collected later.
401 foreach my $guid (keys %myguids) {
402 if (! exists $guids{$guid}) {
403 $guids{$guid}=$myguids{$guid};
408 sub clearstate () { #{{{
415 foreach my $feed (values %feeds) {
416 next unless $feed->{expireage} || $feed->{expirecount};
419 foreach my $item (sort { $IkiWiki::pagectime{$b->{page}} <=> $IkiWiki::pagectime{$a->{page}} }
420 grep { exists $_->{page} && $_->{feed} eq $feed->{name} && $IkiWiki::pagectime{$_->{page}} }
422 if ($feed->{expireage}) {
423 my $days_old = (time - $IkiWiki::pagectime{$item->{page}}) / 60 / 60 / 24;
424 if ($days_old > $feed->{expireage}) {
425 debug(sprintf(gettext("expiring %s (%s days old)"),
426 $item->{page}, int($days_old)));
430 elsif ($feed->{expirecount} &&
431 $count >= $feed->{expirecount}) {
432 debug(sprintf(gettext("expiring %s"), $item->{page}));
436 if (! $seen{$item->{page}}) {
437 $seen{$item->{page}}=1;
445 sub needsaggregate () { #{{{
446 return values %feeds if $config{rebuild};
447 return grep { time - $_->{lastupdate} >= $_->{updateinterval} } values %feeds;
450 sub aggregate (@) { #{{{
451 eval q{use XML::Feed};
453 eval q{use URI::Fetch};
456 foreach my $feed (@_) {
457 $feed->{lastupdate}=time;
459 $feed->{message}=sprintf(gettext("processed ok at %s"),
460 displaytime($feed->{lastupdate}));
463 debug(sprintf(gettext("checking feed %s ..."), $feed->{name}));
465 if (! length $feed->{feedurl}) {
466 my @urls=XML::Feed->find_feeds($feed->{url});
468 $feed->{message}=sprintf(gettext("could not find feed at %s"), $feed->{url});
470 debug($feed->{message});
473 $feed->{feedurl}=pop @urls;
475 my $res=URI::Fetch->fetch($feed->{feedurl});
477 $feed->{message}=URI::Fetch->errstr;
479 debug($feed->{message});
482 if ($res->status == URI::Fetch::URI_GONE()) {
483 $feed->{message}=gettext("feed not found");
485 debug($feed->{message});
488 my $content=$res->content;
489 my $f=eval{XML::Feed->parse(\$content)};
491 # One common cause of XML::Feed crashing is a feed
492 # that contains invalid UTF-8 sequences. Convert
493 # feed to ascii to try to work around.
494 $feed->{message}.=" ".sprintf(gettext("(invalid UTF-8 stripped from feed)"));
495 $content=Encode::decode_utf8($content, 0);
496 $f=eval{XML::Feed->parse(\$content)};
499 # Another possibility is badly escaped entities.
500 $feed->{message}.=" ".sprintf(gettext("(feed entities escaped)"));
501 $content=~s/\&(?!amp)(\w+);/&$1;/g;
502 $content=Encode::decode_utf8($content, 0);
503 $f=eval{XML::Feed->parse(\$content)};
506 $feed->{message}=gettext("feed crashed XML::Feed!")." ($@)";
508 debug($feed->{message});
512 $feed->{message}=XML::Feed->errstr;
514 debug($feed->{message});
518 foreach my $entry ($f->entries) {
521 copyright => $f->copyright,
522 title => defined $entry->title ? decode_entities($entry->title) : "untitled",
523 link => $entry->link,
524 content => defined $entry->content->body ? $entry->content->body : "",
525 guid => defined $entry->id ? $entry->id : time."_".$feed->{name},
526 ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
532 sub add_page (@) { #{{{
535 my $feed=$params{feed};
538 if (exists $guids{$params{guid}}) {
539 # updating an existing post
540 $guid=$guids{$params{guid}};
541 return if $guid->{expired};
545 $guid->{guid}=$params{guid};
546 $guids{$params{guid}}=$guid;
547 $mtime=$params{ctime};
551 # assign it an unused page
552 my $page=IkiWiki::titlepage($params{title});
553 # escape slashes and periods in title so it doesn't specify
554 # directory name or trigger ".." disallowing code.
555 $page=~s!([/.])!"__".ord($1)."__"!eg;
556 $page=$feed->{dir}."/".$page;
557 ($page)=$page=~/$config{wiki_file_regexp}/;
558 if (! defined $page || ! length $page) {
559 $page=$feed->{dir}."/item";
562 while (exists $IkiWiki::pagecase{lc $page.$c} ||
563 -e pagefile($page.$c)) {
567 # Make sure that the file name isn't too long.
568 # NB: This doesn't check for path length limits.
569 my $max=POSIX::pathconf($config{srcdir}, &POSIX::_PC_NAME_MAX);
570 if (defined $max && length(htmlfn($page)) >= $max) {
572 $page=$feed->{dir}."/item";
573 while (exists $IkiWiki::pagecase{lc $page.$c} ||
574 -e pagefile($page.$c)) {
580 debug(sprintf(gettext("creating new page %s"), $page));
582 $guid->{feed}=$feed->{name};
584 # To write or not to write? Need to avoid writing unchanged pages
585 # to avoid unneccessary rebuilding. The mtime from rss cannot be
586 # trusted; let's use a digest.
587 eval q{use Digest::MD5 'md5_hex'};
590 my $digest=md5_hex(Encode::encode_utf8($params{content}));
591 return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $config{rebuild};
592 $guid->{md5}=$digest;
595 my $template=template($feed->{template}, blind_cache => 1);
596 $template->param(title => $params{title})
597 if defined $params{title} && length($params{title});
598 $template->param(content => htmlescape(htmlabs($params{content}, $feed->{feedurl})));
599 $template->param(name => $feed->{name});
600 $template->param(url => $feed->{url});
601 $template->param(copyright => $params{copyright})
602 if defined $params{copyright} && length $params{copyright};
603 $template->param(permalink => urlabs($params{link}, $feed->{feedurl}))
604 if defined $params{link};
605 if (ref $feed->{tags}) {
606 $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
608 writefile(htmlfn($guid->{page}), $config{srcdir},
611 # Set the mtime, this lets the build process get the right creation
612 # time on record for the new page.
613 utime $mtime, $mtime, pagefile($guid->{page})
614 if defined $mtime && $mtime <= time;
617 sub htmlescape ($) { #{{{
618 # escape accidental wikilinks and preprocessor stuff
620 $html=~s/(?<!\\)\[\[/\\\[\[/g;
624 sub urlabs ($$) { #{{{
628 URI->new_abs($url, $urlbase)->as_string;
631 sub htmlabs ($$) { #{{{
632 # Convert links in html from relative to absolute.
633 # Note that this is a heuristic, which is not specified by the rss
634 # spec and may not be right for all feeds. Also, see Debian
640 my $p = HTML::Parser->new(api_version => 3);
641 $p->handler(default => sub { $ret.=join("", @_) }, "text");
642 $p->handler(start => sub {
643 my ($tagname, $pos, $text) = @_;
644 if (ref $HTML::Tagset::linkElements{$tagname}) {
646 # use attribute sets from right to left
647 # to avoid invalidating the offsets
648 # when replacing the values
649 my($k_offset, $k_len, $v_offset, $v_len) =
651 my $attrname = lc(substr($text, $k_offset, $k_len));
652 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
653 next unless $v_offset; # 0 v_offset means no value
654 my $v = substr($text, $v_offset, $v_len);
655 $v =~ s/^([\'\"])(.*)\1$/$2/;
656 my $new_v=urlabs($v, $urlbase);
657 $new_v =~ s/\"/"/g; # since we quote with ""
658 substr($text, $v_offset, $v_len) = qq("$new_v");
662 }, "tagname, tokenpos, text");
669 sub pagefile ($) { #{{{
672 return "$config{srcdir}/".htmlfn($page);
675 sub htmlfn ($) { #{{{
676 return shift().".".($config{aggregateinternal} ? "_aggregated" : $config{htmlext});
681 sub lockaggregate () { #{{{
682 # Take an exclusive lock to prevent multiple concurrent aggregators.
683 # Returns true if the lock was aquired.
684 if (! -d $config{wikistatedir}) {
685 mkdir($config{wikistatedir});
687 open($aggregatelock, '>', "$config{wikistatedir}/aggregatelock") ||
688 error ("cannot open to $config{wikistatedir}/aggregatelock: $!");
689 if (! flock($aggregatelock, 2 | 4)) { # LOCK_EX | LOCK_NB
690 close($aggregatelock) || error("failed closing aggregatelock: $!");
696 sub unlockaggregate () { #{{{
697 return close($aggregatelock) if $aggregatelock;