]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/autoindex.pm
rcs_commit and rcs_commit_staged api changes
[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 }
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         eval q{use Cwd};
37         error($@) if $@;
38         my $origdir=getcwd();
39
40         my (%pages, %dirs);
41         foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
42                 chdir($dir) || next;
43
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                 }, '.');
66
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         }
79
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         }
91
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                                 message => gettext("automatic index generation"),
121                         );
122                         IkiWiki::enable_commit_hook();
123                 }
124         }
125 }
126
127 1