GC unused pages in the transient underlay
[ikiwiki] / IkiWiki / Plugin / transient.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::transient;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 sub import {
9         hook(type => "getsetup", id => "transient",  call => \&getsetup);
10         hook(type => "checkconfig", id => "transient", call => \&checkconfig);
11         hook(type => "change", id => "transient", call => \&change);
12 }
13
14 sub getsetup () {
15         return
16                 plugin => {
17                         # this plugin is safe but only makes sense as a
18                         # dependency; similarly, it needs a rebuild but
19                         # only if something else does
20                         safe => 0,
21                         rebuild => 0,
22                 },
23 }
24
25 our $transientdir;
26
27 sub checkconfig () {
28         eval q{use Cwd 'abs_path'};
29         error($@) if $@;
30         $transientdir = abs_path($config{wikistatedir})."/transient";
31         add_underlay($transientdir);
32 }
33
34 sub change (@) {
35         foreach my $file (@_) {
36                 # if the corresponding file exists in the transient underlay
37                 # and isn't actually being used, we can get rid of it
38                 my $page = pagename($file);
39                 my $casualty = "$transientdir/$page.$config{default_pageext}";
40                 if (srcfile($file) ne $casualty && -e $casualty) {
41                         debug(sprintf(gettext("removing transient version of %s"), $page));
42                         IkiWiki::prune($casualty);
43                 }
44         }
45 }
46
47 1;