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