]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/autoindex.pm
avoid dying if cannot chdir to an underlaydir
[git.ikiwiki.info.git] / IkiWiki / Plugin / autoindex.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::autoindex;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use Encode;
9 sub import {
10         hook(type => "getsetup", id => "autoindex", call => \&getsetup);
11         hook(type => "refresh", id => "autoindex", call => \&refresh);
12 }
14 sub getsetup () {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => 0,
19                 },
20 }
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 }
33 sub refresh () {
34         eval q{use File::Find};
35         error($@) if $@;
36         eval q{use Cwd};
37         error($@) if $@;
38         my $origdir=getcwd();
40         my (%pages, %dirs);
41         foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
42                 chdir($dir) || next;
44                 find({
45                         no_chdir => 1,
46                         wanted => sub {
47                                 my $file=decode_utf8($_);
48                                 $file=~s/^\.\/?//;
49                                 return unless length $file;
50                                 if (IkiWiki::file_pruned($file)) {
51                                         $File::Find::prune=1;
52                                 }
53                                 elsif (! -l $_) {
54                                         my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
55                                         return unless defined $f;
56                                         return if $f =~ /\._([^.]+)$/; # skip internal page
57                                         if (! -d _) {
58                                                 $pages{pagename($f)}=1;
59                                         }
60                                         elsif ($dir eq $config{srcdir}) {
61                                                 $dirs{$f}=1;
62                                         }
63                                 }
64                         }
65                 }, '.');
67                 chdir($origdir) || die "chdir $origdir: $!";
68         }
69         
70         my %deleted;
71         if (ref $wikistate{autoindex}{deleted}) {
72                 %deleted=%{$wikistate{autoindex}{deleted}};
73         }
74         elsif (ref $pagestate{index}{autoindex}{deleted}) {
75                 # compatability code
76                 %deleted=%{$pagestate{index}{autoindex}{deleted}};
77                 delete $pagestate{index}{autoindex};
78         }
80         if (keys %deleted) {
81                 foreach my $dir (keys %deleted) {
82                         # remove deleted page state if the deleted page is re-added,
83                         # or if all its subpages are deleted
84                         if ($deleted{$dir} && (exists $pages{$dir} ||
85                                                ! grep /^$dir\/.*/, keys %pages)) {
86                                 delete $deleted{$dir};
87                         }
88                 }
89                 $wikistate{autoindex}{deleted}=\%deleted;
90         }
92         my @needed;
93         foreach my $dir (keys %dirs) {
94                 if (! exists $pages{$dir} && ! $deleted{$dir} &&
95                     grep /^$dir\/.*/, keys %pages) {
96                         if (exists $IkiWiki::pagemtime{$dir}) {
97                                 # This page must have just been deleted, so
98                                 # don't re-add it. And remember it was
99                                 # deleted.
100                                 if (! ref $wikistate{autoindex}{deleted}) {
101                                         $wikistate{autoindex}{deleted}={};
102                                 }
103                                 ${$wikistate{autoindex}{deleted}}{$dir}=1;
104                         }
105                         else {
106                                 push @needed, $dir;
107                         }
108                 }
109         }
110         
111         if (@needed) {
112                 if ($config{rcs}) {
113                         IkiWiki::disable_commit_hook();
114                 }
115                 foreach my $page (@needed) {
116                         genindex($page);
117                 }
118                 if ($config{rcs}) {
119                         IkiWiki::rcs_commit_staged(
120                                 gettext("automatic index generation"),
121                                 undef, undef);
122                         IkiWiki::enable_commit_hook();
123                 }
124         }