]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Setup.pm
allow multiple setup file types, and support safe parsing
[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 $setup=IkiWiki::possibly_foolish_untaint(shift);
17         my $safemode=shift;
18
19         $config{setupfile}=File::Spec->rel2abs($setup);
20
21         #translators: The first parameter is a filename, and the second
22         #translators: is a (probably not translated) error message.
23         open (IN, $setup) || error(sprintf(gettext("cannot read %s: %s"), $setup, $!));
24         my $content;
25         {
26                 local $/=undef;
27                 $content=<IN> || error("$setup: $!");
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"), $setup)
35                                 if $safemode;
36                         eval IkiWiki::possibly_foolish_untaint($content);
37                         error("$setup: ".$@) 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"), $setup);
47         }
48 }
49
50 sub merge ($) {
51         # Merge setup into existing config and untaint.
52         my %setup=%{shift()};
53
54         if (exists $setup{add_plugins} && exists $config{add_plugins}) {
55                 push @{$setup{add_plugins}}, @{$config{add_plugins}};
56         }
57         if (exists $setup{exclude}) {
58                 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
59         }
60         foreach my $c (keys %setup) {
61                 if (defined $setup{$c}) {
62                         if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
63                                 $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
64                         }
65                         elsif (ref $setup{$c} eq 'ARRAY') {
66                                 if ($c eq 'wrappers') {
67                                         # backwards compatability code
68                                         $config{$c}=$setup{$c};
69                                 }
70                                 else {
71                                         $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
72                                 }
73                         }
74                         elsif (ref $setup{$c} eq 'HASH') {
75                                 foreach my $key (keys %{$setup{$c}}) {
76                                         $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
77                                 }
78                         }
79                 }
80                 else {
81                         $config{$c}=undef;
82                 }
83         }
84         
85         if (length $config{cgi_wrapper}) {
86                 push @{$config{wrappers}}, {
87                         cgi => 1,
88                         wrapper => $config{cgi_wrapper},
89                         wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
90                 };
91         }
92 }
93
94 sub getsetup () {
95         # Gets all available setup data from all plugins. Returns an
96         # ordered list of [plugin, setup] pairs.
97
98         # disable logging to syslog while dumping, broken plugins may
99         # whine when loaded
100         my $syslog=$config{syslog};
101         $config{syslog}=undef;
102
103         # Load all plugins, so that all setup options are available.
104         my @plugins=IkiWiki::listplugins();
105         foreach my $plugin (@plugins) {
106                 eval { IkiWiki::loadplugin($plugin) };
107                 if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
108                         my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
109                 }
110         }
111         
112         my %sections;
113         foreach my $plugin (@plugins) {
114                 if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
115                         # use an array rather than a hash, to preserve order
116                         my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
117                         next unless @s;
118
119                         # set default section value (note use of shared
120                         # hashref between array and hash)
121                         my %s=@s;
122                         if (! exists $s{plugin} || ! $s{plugin}->{section}) {
123                                 $s{plugin}->{section}="other";
124                         }
125
126                         # only the selected rcs plugin is included
127                         if ($config{rcs} && $plugin eq $config{rcs}) {
128                                 $s{plugin}->{section}="core";
129                         }
130                         elsif ($s{plugin}->{section} eq "rcs") {
131                                 next;
132                         }
133
134                         push @{$sections{$s{plugin}->{section}}}, [ $plugin, \@s ];
135                 }
136         }
137         
138         $config{syslog}=$syslog;
139
140         return map { sort { $a->[0] cmp $b->[0] } @{$sections{$_}} }
141                 sort { # core first, other last, otherwise alphabetical
142                         ($b eq "core") <=> ($a eq "core")
143                            ||
144                         ($a eq "other") <=> ($b eq "other")
145                            ||
146                         $a cmp $b
147                 } keys %sections;
148 }
149
150 sub dump ($) {
151         my $file=IkiWiki::possibly_foolish_untaint(shift);
152         
153         eval qq{require $config{setuptype}};
154         error $@ if $@;
155         my @dump=$config{setuptype}->gendump("Setup file for ikiwiki.");
156
157         open (OUT, ">", $file) || die "$file: $!";
158         print OUT "$_\n" foreach @dump;
159         close OUT;
160 }
161
162 1