Allow autoindex files to be written into the transient underlay
[ikiwiki.git] / IkiWiki / Plugin / autoindex.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::autoindex;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use Encode;
8
9 sub import {
10         hook(type => "getsetup", id => "autoindex", call => \&getsetup);
11         hook(type => "refresh", id => "autoindex", call => \&refresh);
12         IkiWiki::loadplugin("transient");
13 }
14
15 sub getsetup () {
16         return
17                 plugin => {
18                         safe => 1,
19                         rebuild => 0,
20                 },
21                 autoindex_commit => {
22                         type => "boolean",
23                         example => 1,
24                         default => 1,
25                         description => "commit autocreated index pages",
26                         safe => 1,
27                         rebuild => 0,
28                 },
29 }
30
31 sub genindex ($) {
32         my $page=shift;
33         my $file=newpagefile($page, $config{default_pageext});
34
35         add_autofile($file, "autoindex", sub {
36                         my $message = sprintf(gettext("creating index page %s"),
37                                 $page);
38                         debug($message);
39
40                         my $dir = $config{srcdir};
41                         if (! $config{autoindex_commit}) {
42                                 $dir = $IkiWiki::Plugin::transient::transientdir;
43                         }
44
45                         my $template = template("autoindex.tmpl");
46                         $template->param(page => $page);
47                         writefile($file, $dir, $template->output);
48
49                         if ($config{rcs} && $config{autoindex_commit}) {
50                                 IkiWiki::disable_commit_hook();
51                                 IkiWiki::rcs_add($file);
52                                 IkiWiki::rcs_commit_staged(message => $message);
53                                 IkiWiki::enable_commit_hook();
54                         }
55                 });
56 }
57
58 sub refresh () {
59         eval q{use File::Find};
60         error($@) if $@;
61         eval q{use Cwd};
62         error($@) if $@;
63         my $origdir=getcwd();
64
65         my (%pages, %dirs);
66         foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
67                 chdir($dir) || next;
68
69                 find({
70                         no_chdir => 1,
71                         wanted => sub {
72                                 my $file=decode_utf8($_);
73                                 $file=~s/^\.\/?//;
74                                 return unless length $file;
75                                 if (IkiWiki::file_pruned($file)) {
76                                         $File::Find::prune=1;
77                                 }
78                                 elsif (! -l $_) {
79                                         my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
80                                         return unless defined $f;
81                                         return if $f =~ /\._([^.]+)$/; # skip internal page
82                                         if (! -d _) {
83                                                 $pages{pagename($f)}=1;
84                                         }
85                                         elsif ($dir eq $config{srcdir}) {
86                                                 $dirs{$f}=1;
87                                         }
88                                 }
89                         }
90                 }, '.');
91
92                 chdir($origdir) || die "chdir $origdir: $!";
93         }
94
95         # Compatibility code.
96         #
97         # {deleted} contains pages that have been deleted at some point.
98         # This plugin used to delete from the hash sometimes, but no longer
99         # does; in [[todo/autoindex_should_use_add__95__autofile]] Joey
100         # thought the old behaviour was probably a bug.
101         #
102         # The effect of listing a page in {deleted} was to avoid re-creating
103         # it; we migrate these pages to {autofile} which has the same effect.
104         # However, {autofile} contains source filenames whereas {deleted}
105         # contains page names.
106         my %deleted;
107         if (ref $wikistate{autoindex}{deleted}) {
108                 %deleted=%{$wikistate{autoindex}{deleted}};
109                 delete $wikistate{autoindex}{deleted};
110         }
111         elsif (ref $pagestate{index}{autoindex}{deleted}) {
112                 # an even older version
113                 %deleted=%{$pagestate{index}{autoindex}{deleted}};
114                 delete $pagestate{index}{autoindex};
115         }
116
117         if (keys %deleted) {
118                 foreach my $dir (keys %deleted) {
119                         my $file=newpagefile($dir, $config{default_pageext});
120                         $wikistate{autoindex}{autofile}{$file} = 1;
121                 }
122         }
123
124         foreach my $dir (keys %dirs) {
125                 if (! exists $pages{$dir} && grep /^$dir\/.*/, keys %pages) {
126                         genindex($dir);
127                 }
128         }
129 }
130
131 1