+ print OUT join(" ", @line)."\n" || error("write $newfile: $!", $cleanup);
+ }
+ close OUT || error("save $newfile: $!", $cleanup);
+ rename($newfile, "$config{wikistatedir}/aggregate") ||
+ error("rename $newfile: $!", $cleanup);
+} #}}}
+
+sub garbage_collect () { #{{{
+ foreach my $name (keys %feeds) {
+ # remove any feeds that were not seen while building the pages
+ # that used to contain them
+ if ($feeds{$name}->{unseen}) {
+ delete $feeds{$name};
+ }
+ }
+
+ foreach my $guid (values %guids) {
+ # any guid whose feed is gone should be removed
+ if (! exists $feeds{$guid->{feed}}) {
+ unlink pagefile($guid->{page})
+ if exists $guid->{page};
+ delete $guids{$guid->{guid}};
+ }
+ # handle expired guids
+ elsif ($guid->{expired} && exists $guid->{page}) {
+ unlink pagefile($guid->{page});
+ delete $guid->{page};
+ delete $guid->{md5};
+ }
+ }
+} #}}}
+
+sub mergestate () { #{{{
+ # Load the current state in from disk, and merge into it
+ # values from the state in memory that might have changed
+ # during aggregation.
+ my %myfeeds=%feeds;
+ my %myguids=%guids;
+ clearstate();
+ loadstate();
+
+ # All that can change in feed state during aggregation is a few
+ # fields.
+ foreach my $name (keys %myfeeds) {
+ if (exists $feeds{$name}) {
+ foreach my $field (qw{message lastupdate numposts
+ newposts error}) {
+ $feeds{$name}->{$field}=$myfeeds{$name}->{$field};
+ }
+ }
+ }
+
+ # New guids can be created during aggregation.
+ # It's also possible that guids were removed from the on-disk state
+ # while the aggregation was in process. That would only happen if
+ # their feed was also removed, so any removed guids added back here
+ # will be garbage collected later.
+ foreach my $guid (keys %myguids) {
+ if (! exists $guids{$guid}) {
+ $guids{$guid}=$myguids{$guid};
+ }
+ }
+} #}}}
+
+sub clearstate () { #{{{
+ %feeds=();
+ %guids=();
+ $state_loaded=0;
+} #}}}
+
+sub expire () { #{{{
+ foreach my $feed (values %feeds) {
+ next unless $feed->{expireage} || $feed->{expirecount};
+ my $count=0;
+ my %seen;
+ foreach my $item (sort { $IkiWiki::pagectime{$b->{page}} <=> $IkiWiki::pagectime{$a->{page}} }
+ grep { exists $_->{page} && $_->{feed} eq $feed->{name} && $IkiWiki::pagectime{$_->{page}} }
+ values %guids) {
+ if ($feed->{expireage}) {
+ my $days_old = (time - $IkiWiki::pagectime{$item->{page}}) / 60 / 60 / 24;
+ if ($days_old > $feed->{expireage}) {
+ debug(sprintf(gettext("expiring %s (%s days old)"),
+ $item->{page}, int($days_old)));
+ $item->{expired}=1;
+ }
+ }
+ elsif ($feed->{expirecount} &&
+ $count >= $feed->{expirecount}) {
+ debug(sprintf(gettext("expiring %s"), $item->{page}));
+ $item->{expired}=1;
+ }
+ else {
+ if (! $seen{$item->{page}}) {
+ $seen{$item->{page}}=1;
+ $count++;
+ }
+ }
+ }