2 package IkiWiki::Plugin::autoindex;
10 hook(type => "checkconfig", id => "autoindex", call => \&checkconfig);
11 hook(type => "getsetup", id => "autoindex", call => \&getsetup);
12 hook(type => "refresh", id => "autoindex", call => \&refresh);
13 IkiWiki::loadplugin("transient");
26 description => "commit autocreated index pages",
33 if (! defined $config{autoindex_commit}) {
34 $config{autoindex_commit} = 1;
36 if (! $config{autoindex_commit}) {
37 $config{only_committed_changes}=0;
43 my $file=newpagefile($page, $config{default_pageext});
45 add_autofile($file, "autoindex", sub {
46 my $message = sprintf(gettext("creating index page %s"),
50 my $dir = $config{srcdir};
51 if (! $config{autoindex_commit}) {
52 $dir = $IkiWiki::Plugin::transient::transientdir;
55 my $template = template("autoindex.tmpl");
56 $template->param(page => $page);
57 writefile($file, $dir, $template->output);
59 if ($config{rcs} && $config{autoindex_commit}) {
60 IkiWiki::disable_commit_hook();
61 IkiWiki::rcs_add($file);
62 IkiWiki::rcs_commit_staged(message => $message);
63 IkiWiki::enable_commit_hook();
69 eval q{use File::Find};
76 foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
77 next if $dir eq $IkiWiki::Plugin::transient::transientdir;
83 my $file=decode_utf8($_);
85 return unless length $file;
86 if (IkiWiki::file_pruned($file)) {
90 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
91 return unless defined $f;
92 return if $f =~ /\._([^.]+)$/; # skip internal page
94 $pages{pagename($f)}=1;
96 elsif ($dir eq $config{srcdir}) {
103 chdir($origdir) || die "chdir $origdir: $!";
106 # Compatibility code.
108 # {deleted} contains pages that have been deleted at some point.
109 # This plugin used to delete from the hash sometimes, but no longer
110 # does; in [[todo/autoindex_should_use_add__95__autofile]] Joey
111 # thought the old behaviour was probably a bug.
113 # The effect of listing a page in {deleted} was to avoid re-creating
114 # it; we migrate these pages to {autofile} which has the same effect.
115 # However, {autofile} contains source filenames whereas {deleted}
116 # contains page names.
118 if (ref $wikistate{autoindex}{deleted}) {
119 %deleted=%{$wikistate{autoindex}{deleted}};
120 delete $wikistate{autoindex}{deleted};
122 elsif (ref $pagestate{index}{autoindex}{deleted}) {
123 # an even older version
124 %deleted=%{$pagestate{index}{autoindex}{deleted}};
125 delete $pagestate{index}{autoindex};
129 foreach my $dir (keys %deleted) {
130 my $file=newpagefile($dir, $config{default_pageext});
131 $wikistate{autoindex}{autofile}{$file} = 1;
135 foreach my $dir (keys %dirs) {
136 if (! exists $pages{$dir} && grep /^$dir\/.*/, keys %pages) {