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