Make the attachment plugin work with CGI.pm 4.x (Closes: #786586; workaround for...
[ikiwiki] / IkiWiki / Setup.pm
1 #!/usr/bin/perl
2 # Ikiwiki setup files can be perl files that 'use IkiWiki::Setup::foo',
3 # passing it some sort of configuration data. Or, they can contain
4 # the module name at the top, without the 'use', and the whole file is
5 # then fed into that module.
6
7 package IkiWiki::Setup;
8
9 use warnings;
10 use strict;
11 use IkiWiki;
12 use open qw{:utf8 :std};
13 use File::Spec;
14
15 sub load ($;$) {
16         my $file=IkiWiki::possibly_foolish_untaint(shift);
17         my $safemode=shift;
18
19         $config{setupfile}=File::Spec->rel2abs($file);
20
21         #translators: The first parameter is a filename, and the second
22         #translators: is a (probably not translated) error message.
23         open (IN, $file) || error(sprintf(gettext("cannot read %s: %s"), $file, $!));
24         my $content;
25         {
26                 local $/=undef;
27                 $content=<IN> || error("$file: $!");
28         }
29         close IN;
30
31         if ($content=~/((?:use|require)\s+)?IkiWiki::Setup::(\w+)/) {
32                 $config{setuptype}=$2;
33                 if ($1) {
34                         error sprintf(gettext("cannot load %s in safe mode"), $file)
35                                 if $safemode;
36                         no warnings;
37                         eval IkiWiki::possibly_foolish_untaint($content);
38                         error("$file: ".$@) if $@;
39                 }
40                 else {
41                         eval qq{require IkiWiki::Setup::$config{setuptype}};
42                         error $@ if $@;
43                         "IkiWiki::Setup::$config{setuptype}"->loaddump(IkiWiki::possibly_foolish_untaint($content));
44                 }
45         }
46         else {
47                 error sprintf(gettext("failed to parse %s"), $file);
48         }
49 }
50
51 sub dump ($) {
52         my $file=IkiWiki::possibly_foolish_untaint(shift);
53
54         my @header=(
55                 "Setup file for ikiwiki.",
56                 "",
57                 "Passing this to ikiwiki --setup will make ikiwiki generate",
58                 "wrappers and build the wiki.",
59                 "",
60                 "Remember to re-run ikiwiki --setup any time you edit this file.",
61         );
62
63         # Fork because dumping setup requires loading all plugins.
64         my $pid=fork();
65         if ($pid == 0) {
66                 eval qq{require IkiWiki::Setup::$config{setuptype}};
67                 error $@ if $@;
68                 my @dump="IkiWiki::Setup::$config{setuptype}"->gendump(@header);
69         
70                 open (OUT, ">", $file) || die "$file: $!";
71                 print OUT "$_\n" foreach @dump;
72                 close OUT;
73
74                 exit 0;
75         }
76         else {
77                 waitpid $pid, 0;
78                 exit($? >> 8) if $? >> 8;
79                 exit(1) if $?;
80         }
81 }
82
83 sub merge ($) {
84         # Merge setup into existing config and untaint.
85         my %setup=%{shift()};
86
87         if (exists $setup{add_plugins} && exists $config{add_plugins}) {
88                 push @{$setup{add_plugins}}, @{$config{add_plugins}};
89         }
90         if (exists $setup{exclude}) {
91                 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
92         }
93         foreach my $c (keys %setup) {
94                 if (defined $setup{$c}) {
95                         if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
96                                 $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
97                         }
98                         elsif (ref $setup{$c} eq 'ARRAY') {
99                                 if ($c eq 'wrappers') {
100                                         # backwards compatability code
101                                         $config{$c}=$setup{$c};
102                                 }
103                                 else {
104                                         $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
105                                 }
106                         }
107                         elsif (ref $setup{$c} eq 'HASH') {
108                                 foreach my $key (keys %{$setup{$c}}) {
109                                         $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
110                                 }
111                         }
112                 }
113                 else {
114                         $config{$c}=undef;
115                 }
116         }
117         
118         if (length $config{cgi_wrapper}) {
119                 push @{$config{wrappers}}, {
120                         cgi => 1,
121                         wrapper => $config{cgi_wrapper},
122                         wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
123                 };
124         }
125 }
126
127 sub disabled_plugins (@) {
128         # Handles running disable hooks of plugins that were enabled
129         # previously, but got disabled when a new setup file was loaded.
130         if (exists $config{setupfile} && @_) {
131                 # Fork a child to load the disabled plugins.
132                 my $pid=fork();
133                 if ($pid == 0) {
134                         foreach my $plugin (@_) {
135                                 eval { IkiWiki::loadplugin($plugin, 1) };
136                                 if (exists $IkiWiki::hooks{disable}{$plugin}{call}) {
137                                         eval { $IkiWiki::hooks{disable}{$plugin}{call}->() };
138                                 }
139                         }
140                         exit(0);
141                 }
142                 else {
143                         waitpid $pid, 0;
144                 }
145         }
146 }
147
148 sub getsetup () {
149         # Gets all available setup data from all plugins. Returns an
150         # ordered list of [plugin, setup] pairs.
151
152         # disable logging to syslog while dumping, broken plugins may
153         # whine when loaded
154         my $syslog=$config{syslog};
155         $config{syslog}=undef;
156
157         # Load all plugins, so that all setup options are available.
158         my %original_loaded_plugins=%IkiWiki::loaded_plugins;
159         my @plugins=IkiWiki::listplugins();
160         foreach my $plugin (@plugins) {
161                 eval { IkiWiki::loadplugin($plugin, 1) };
162                 if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
163                         my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
164                 }
165         }
166         %IkiWiki::loaded_plugins=%original_loaded_plugins;
167         
168         my %sections;
169         foreach my $plugin (@plugins) {
170                 if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
171                         # use an array rather than a hash, to preserve order
172                         my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
173                         next unless @s;
174
175                         if (scalar(@s) % 2 != 0) {
176                                 print STDERR "warning: plugin $plugin has a broken getsetup; ignoring\n";
177                                 next;
178                         }
179
180                         # set default section value (note use of shared
181                         # hashref between array and hash)
182                         my %s=@s;
183                         if (! exists $s{plugin} || ! $s{plugin}->{section}) {
184                                 $s{plugin}->{section}="other";
185                         }
186
187                         # only the selected rcs plugin is included
188                         if ($config{rcs} && $plugin eq $config{rcs}) {
189                                 $s{plugin}->{section}="core";
190                         }
191                         elsif ($s{plugin}->{section} eq "rcs") {
192                                 next;
193                         }
194
195                         push @{$sections{$s{plugin}->{section}}}, [ $plugin, \@s ];
196                 }
197         }
198         
199         $config{syslog}=$syslog;
200
201         return map { sort { $a->[0] cmp $b->[0] } @{$sections{$_}} }
202                 sort { # core first, other last, otherwise alphabetical
203                         ($b eq "core") <=> ($a eq "core")
204                            ||
205                         ($a eq "other") <=> ($b eq "other")
206                            ||
207                         $a cmp $b
208                 } keys %sections;
209 }
210
211 sub commented_dump ($$) {
212         my $dumpline=shift;
213         my $indent=shift;
214
215         my %setup=(%config);
216         my @ret;
217         
218         # disable logging to syslog while dumping
219         $config{syslog}=undef;
220
221         eval q{use Text::Wrap};
222         die $@ if $@;
223
224         my %section_plugins;
225         push @ret, commented_dumpvalues($dumpline, $indent, \%setup, IkiWiki::getsetup());
226         foreach my $pair (IkiWiki::Setup::getsetup()) {
227                 my $plugin=$pair->[0];
228                 my $setup=$pair->[1];
229                 my %s=@{$setup};
230                 my $section=$s{plugin}->{section};
231                 if (! defined $section) {
232                         print STDERR "warning: missing section in $plugin\n";
233                         $section="other";
234                 }
235                 push @{$section_plugins{$section}}, $plugin;
236                 if (@{$section_plugins{$section}} == 1) {
237                         push @ret, "", $indent.("#" x 70), "$indent# $section plugins",
238                                 sub {
239                                         wrap("$indent#   (", "$indent#    ",
240                                                 join(", ", @{$section_plugins{$section}})).")"
241                                 },
242                                 $indent.("#" x 70);
243                 }
244
245                 my @values=commented_dumpvalues($dumpline, $indent, \%setup, @{$setup});
246                 if (@values) {
247                         push @ret, "", "$indent# $plugin plugin", @values;
248                 }
249         }
250
251         return map { ref $_ ? $_->() : $_ } @ret;
252 }
253
254 sub commented_dumpvalues ($$$@) {
255         my $dumpline=shift;
256         my $indent=shift;
257         my $setup=shift;
258         my @ret;
259         while (@_) {
260                 my $key=shift;
261                 my %info=%{shift()};
262
263                 next if $key eq "plugin" || $info{type} eq "internal";
264                 
265                 push @ret, "$indent# ".$info{description} if exists $info{description};
266                 
267                 if (exists $setup->{$key} && defined $setup->{$key}) {
268                         push @ret, $dumpline->($key, $setup->{$key}, $info{type}, "");
269                         delete $setup->{$key};
270                 }
271                 elsif (exists $info{example}) {
272                         push @ret, $dumpline->($key, $info{example}, $info{type}, "#");
273                 }
274                 else {
275                         push @ret, $dumpline->($key, "", $info{type}, "#");
276                 }
277         }
278         return @ret;
279 }
280
281 1