bugfix
[ikiwiki] / 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 }
13
14 sub getsetup () {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => 0,
19                 },
20 }
21
22 sub genindex ($) {
23         my $page=shift;
24         my $file=newpagefile($page, $config{default_pageext});
25         my $template=template("autoindex.tmpl");
26         $template->param(page => $page);
27         writefile($file, $config{srcdir}, $template->output);
28         if ($config{rcs}) {
29                 IkiWiki::rcs_add($file);
30         }
31 }
32
33 sub refresh () {
34         eval q{use File::Find};
35         error($@) if $@;
36
37         my (%pages, %dirs);
38         foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
39                 require File::Spec;
40                 $dir=File::Spec->canonpath($dir);
41
42                 find({
43                         no_chdir => 1,
44                         wanted => sub {
45                                 my $file=File::Spec->canonpath(decode_utf8($_));
46                                 return if $file eq $dir;
47                                 $file=~s/^\Q$dir\E\/?//;
48                                 return unless length $file;
49                                 if (IkiWiki::file_pruned($file)) {
50                                         $File::Find::prune=1;
51                                 }
52                                 elsif (! -l $_) {
53                                         my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
54                                         return unless defined $f;
55                                         return if $f =~ /\._([^.]+)$/; # skip internal page
56                                         if (! -d _) {
57                                                 $pages{pagename($f)}=1;
58                                         }
59                                         elsif ($dir eq $config{srcdir}) {
60                                                 $dirs{$f}=1;
61                                         }
62                                 }
63                         }
64                 }, $dir);
65         }
66         
67         my %deleted;
68         if (ref $wikistate{autoindex}{deleted}) {
69                 %deleted=%{$wikistate{autoindex}{deleted}};
70         }
71         elsif (ref $pagestate{index}{autoindex}{deleted}) {
72                 # compatability code
73                 %deleted=%{$pagestate{index}{autoindex}{deleted}};
74                 delete $pagestate{index}{autoindex};
75         }
76
77         if (keys %deleted) {
78                 foreach my $dir (keys %deleted) {
79                         # remove deleted page state if the deleted page is re-added,
80                         # or if all its subpages are deleted
81                         if ($deleted{$dir} && (exists $pages{$dir} ||
82                                                ! grep /^$dir\/.*/, keys %pages)) {
83                                 delete $deleted{$dir};
84                         }
85                 }
86                 $wikistate{autoindex}{deleted}=\%deleted;
87         }
88
89         my @needed;
90         foreach my $dir (keys %dirs) {
91                 if (! exists $pages{$dir} && ! $deleted{$dir} &&
92                     grep /^$dir\/.*/, keys %pages) {
93                         if (exists $IkiWiki::pagemtime{$dir}) {
94                                 # This page must have just been deleted, so
95                                 # don't re-add it. And remember it was
96                                 # deleted.
97                                 if (! ref $wikistate{autoindex}{deleted}) {
98                                         $wikistate{autoindex}{deleted}={};
99                                 }
100                                 ${$wikistate{autoindex}{deleted}}{$dir}=1;
101                         }
102                         else {
103                                 push @needed, $dir;
104                         }
105                 }
106         }
107         
108         if (@needed) {
109                 if ($config{rcs}) {
110                         IkiWiki::disable_commit_hook();
111                 }
112                 foreach my $page (@needed) {
113                         genindex($page);
114                 }
115                 if ($config{rcs}) {
116                         IkiWiki::rcs_commit_staged(
117                                 gettext("automatic index generation"),
118                                 undef, undef);
119                         IkiWiki::enable_commit_hook();
120                 }
121         }
122 }
123
124 1