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