dede9eb05cf3fafc9e4cc8e4d9ad013a088417a5
[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 => "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");
14 }
15
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                 },
22                 autoindex_commit => {
23                         type => "boolean",
24                         example => 1,
25                         default => 1,
26                         description => "commit autocreated index pages",
27                         safe => 1,
28                         rebuild => 0,
29                 },
30 }
31
32 sub checkconfig () {
33         if (! defined $config{autoindex_commit}) {
34                 $config{autoindex_commit} = 1;
35         }
36         if (! $config{autoindex_commit}) {
37                 $config{only_committed_changes}=0;
38         }
39 }
40
41 sub genindex ($) {
42         my $page=shift;
43         my $file=newpagefile($page, $config{default_pageext});
44
45         add_autofile($file, "autoindex", sub {
46                         my $message = sprintf(gettext("creating index page %s"),
47                                 $page);
48                         debug($message);
49
50                         my $dir = $config{srcdir};
51                         if (! $config{autoindex_commit}) {
52                                 $dir = $IkiWiki::Plugin::transient::transientdir;
53                         }
54
55                         my $template = template("autoindex.tmpl");
56                         $template->param(page => $page);
57                         writefile($file, $dir, $template->output);
58
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();
64                         }
65                 });
66 }
67
68 sub refresh () {
69         eval q{use File::Find};
70         error($@) if $@;
71         eval q{use Cwd};
72         error($@) if $@;
73         my $origdir=getcwd();
74
75         my (%pages, %dirs);
76         foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
77                 next if $dir eq $IkiWiki::Plugin::transient::transientdir;
78                 chdir($dir) || next;
79
80                 find({
81                         no_chdir => 1,
82                         wanted => sub {
83                                 my $file=decode_utf8($_);
84                                 $file=~s/^\.\/?//;
85                                 return unless length $file;
86                                 if (IkiWiki::file_pruned($file)) {
87                                         $File::Find::prune=1;
88                                 }
89                                 elsif (! -l $_) {
90                                         my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
91                                         return unless defined $f;
92                                         return if $f =~ /\._([^.]+)$/; # skip internal page
93                                         if (! -d _) {
94                                                 $pages{pagename($f)}=1;
95                                         }
96                                         elsif ($dir eq $config{srcdir}) {
97                                                 $dirs{$f}=1;
98                                         }
99                                 }
100                         }
101                 }, '.');
102
103                 chdir($origdir) || die "chdir $origdir: $!";
104         }
105
106         # Compatibility code.
107         #
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.
112         #
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.
117         my %deleted;
118         if (ref $wikistate{autoindex}{deleted}) {
119                 %deleted=%{$wikistate{autoindex}{deleted}};
120                 delete $wikistate{autoindex}{deleted};
121         }
122         elsif (ref $pagestate{index}{autoindex}{deleted}) {
123                 # an even older version
124                 %deleted=%{$pagestate{index}{autoindex}{deleted}};
125                 delete $pagestate{index}{autoindex};
126         }
127
128         if (keys %deleted) {
129                 foreach my $dir (keys %deleted) {
130                         my $file=newpagefile($dir, $config{default_pageext});
131                         $wikistate{autoindex}{autofile}{$file} = 1;
132                 }
133         }
134
135         foreach my $dir (keys %dirs) {
136                 if (! exists $pages{$dir} && grep /^$dir\/.*/, keys %pages) {
137                         genindex($dir);
138                 }
139         }
140 }
141
142 1