disable warnings when evaling setup files
[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         eval qq{require IkiWiki::Setup::$config{setuptype}};
55         error $@ if $@;
56         my @dump="IkiWiki::Setup::$config{setuptype}"->gendump(
57                 "Setup file for ikiwiki.",
58                 "",
59                 "Passing this to ikiwiki --setup will make ikiwiki generate",
60                 "wrappers and build the wiki.",
61                 "",
62                 "Remember to re-run ikiwiki --setup any time you edit this file.",
63         );
64
65         open (OUT, ">", $file) || die "$file: $!";
66         print OUT "$_\n" foreach @dump;
67         close OUT;
68 }
69
70 sub merge ($) {
71         # Merge setup into existing config and untaint.
72         my %setup=%{shift()};
73
74         if (exists $setup{add_plugins} && exists $config{add_plugins}) {
75                 push @{$setup{add_plugins}}, @{$config{add_plugins}};
76         }
77         if (exists $setup{exclude}) {
78                 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
79         }
80         foreach my $c (keys %setup) {
81                 if (defined $setup{$c}) {
82                         if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
83                                 $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
84                         }
85                         elsif (ref $setup{$c} eq 'ARRAY') {
86                                 if ($c eq 'wrappers') {
87                                         # backwards compatability code
88                                         $config{$c}=$setup{$c};
89                                 }
90                                 else {
91                                         $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
92                                 }
93                         }
94                         elsif (ref $setup{$c} eq 'HASH') {
95                                 foreach my $key (keys %{$setup{$c}}) {
96                                         $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
97                                 }
98                         }
99                 }
100                 else {
101                         $config{$c}=undef;
102                 }
103         }
104         
105         if (length $config{cgi_wrapper}) {
106                 push @{$config{wrappers}}, {
107                         cgi => 1,
108                         wrapper => $config{cgi_wrapper},
109                         wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
110                 };
111         }
112 }
113
114 sub getsetup () {
115         # Gets all available setup data from all plugins. Returns an
116         # ordered list of [plugin, setup] pairs.
117
118         # disable logging to syslog while dumping, broken plugins may
119         # whine when loaded
120         my $syslog=$config{syslog};
121         $config{syslog}=undef;
122
123         # Load all plugins, so that all setup options are available.
124         my @plugins=IkiWiki::listplugins();
125         foreach my $plugin (@plugins) {
126                 eval { IkiWiki::loadplugin($plugin) };
127                 if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
128                         my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
129                 }
130         }
131         
132         my %sections;
133         foreach my $plugin (@plugins) {
134                 if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
135                         # use an array rather than a hash, to preserve order
136                         my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
137                         next unless @s;
138
139                         # set default section value (note use of shared
140                         # hashref between array and hash)
141                         my %s=@s;
142                         if (! exists $s{plugin} || ! $s{plugin}->{section}) {
143                                 $s{plugin}->{section}="other";
144                         }
145
146                         # only the selected rcs plugin is included
147                         if ($config{rcs} && $plugin eq $config{rcs}) {
148                                 $s{plugin}->{section}="core";
149                         }
150                         elsif ($s{plugin}->{section} eq "rcs") {
151                                 next;
152                         }
153
154                         push @{$sections{$s{plugin}->{section}}}, [ $plugin, \@s ];
155                 }
156         }
157         
158         $config{syslog}=$syslog;
159
160         return map { sort { $a->[0] cmp $b->[0] } @{$sections{$_}} }
161                 sort { # core first, other last, otherwise alphabetical
162                         ($b eq "core") <=> ($a eq "core")
163                            ||
164                         ($a eq "other") <=> ($b eq "other")
165                            ||
166                         $a cmp $b
167                 } keys %sections;
168 }
169
170 sub commented_dump ($$) {
171         my $dumpline=shift;
172         my $indent=shift;
173
174         my %setup=(%config);
175         my @ret;
176         
177         # disable logging to syslog while dumping
178         $config{syslog}=undef;
179
180         eval q{use Text::Wrap};
181         die $@ if $@;
182
183         my %section_plugins;
184         push @ret, commented_dumpvalues($dumpline, $indent, \%setup, IkiWiki::getsetup());
185         foreach my $pair (IkiWiki::Setup::getsetup()) {
186                 my $plugin=$pair->[0];
187                 my $setup=$pair->[1];
188                 my %s=@{$setup};
189                 my $section=$s{plugin}->{section};
190                 push @{$section_plugins{$section}}, $plugin;
191                 if (@{$section_plugins{$section}} == 1) {
192                         push @ret, "", $indent.("#" x 70), "$indent# $section plugins",
193                                 sub {
194                                         wrap("$indent#   (", "$indent#    ",
195                                                 join(", ", @{$section_plugins{$section}})).")"
196                                 },
197                                 $indent.("#" x 70);
198                 }
199
200                 my @values=commented_dumpvalues($dumpline, $indent, \%setup, @{$setup});
201                 if (@values) {
202                         push @ret, "", "$indent# $plugin plugin", @values;
203                 }
204         }
205
206         return map { ref $_ ? $_->() : $_ } @ret;
207 }
208
209 sub commented_dumpvalues ($$$@) {
210         my $dumpline=shift;
211         my $indent=shift;
212         my $setup=shift;
213         my @ret;
214         while (@_) {
215                 my $key=shift;
216                 my %info=%{shift()};
217
218                 next if $key eq "plugin" || $info{type} eq "internal";
219                 
220                 push @ret, "$indent# ".$info{description} if exists $info{description};
221                 
222                 if (exists $setup->{$key} && defined $setup->{$key}) {
223                         push @ret, $dumpline->($key, $setup->{$key}, $info{type}, "");
224                         delete $setup->{$key};
225                 }
226                 elsif (exists $info{example}) {
227                         push @ret, $dumpline->($key, $info{example}, $info{type}, "#");
228                 }
229                 else {
230                         push @ret, $dumpline->($key, "", $info{type}, "#");
231                 }
232         }
233         return @ret;
234 }
235
236 1