]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Setup.pm
fixups
[ikiwiki.git] / IkiWiki / Setup.pm
1 #!/usr/bin/perl
2 # Ikiwiki setup files are perl files that 'use IkiWiki::Setup::foo',
3 # passing it some sort of configuration data.
4
5 package IkiWiki::Setup;
6
7 use warnings;
8 use strict;
9 use IkiWiki;
10 use open qw{:utf8 :std};
11
12 # There can be multiple modules, with different configuration styles.
13 # The setup modules each convert the data into the hashes used by ikiwiki
14 # internally (if it's not already in that format), and store it in
15 # IkiWiki::Setup::$raw_setup, to pass it back to this module.
16 our $raw_setup;
17
18 sub load ($) { # {{{
19         my $setup=IkiWiki::possibly_foolish_untaint(shift);
20         delete $config{setup};
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 $code;
25         {
26                 local $/=undef;
27                 $code=<IN>;
28         }
29         ($code)=$code=~/(.*)/s;
30         close IN;
31
32         eval $code;
33         error("$setup: ".$@) if $@;
34
35         my %setup=%{$raw_setup};
36         $raw_setup=undef;
37
38         # Merge setup into existing config and untaint.
39         if (exists $setup{add_plugins}) {
40                 push @{$setup{add_plugins}}, @{$config{add_plugins}};
41         }
42         if (exists $setup{exclude}) {
43                 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
44         }
45         foreach my $c (keys %setup) {
46                 if (defined $setup{$c}) {
47                         if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
48                                 $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
49                         }
50                         elsif (ref $setup{$c} eq 'ARRAY') {
51                                 if ($c eq 'wrappers') {
52                                         # backwards compatability code
53                                         $config{$c}=$setup{$c};
54                                 }
55                                 else {
56                                         $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
57                                 }
58                         }
59                         elsif (ref $setup{$c} eq 'HASH') {
60                                 foreach my $key (keys %{$setup{$c}}) {
61                                         $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
62                                 }
63                         }
64                 }
65                 else {
66                         $config{$c}=undef;
67                 }
68         }
69         
70         if (length $config{cgi_wrapper}) {
71                 push @{$config{wrappers}}, {
72                         cgi => 1,
73                         wrapper => $config{cgi_wrapper},
74                         wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
75                 };
76         }
77 } #}}}
78
79 sub dump ($) { #{{{
80         my $file=IkiWiki::possibly_foolish_untaint(shift);
81         
82         require IkiWiki::Setup::Standard;
83         my @dump=IkiWiki::Setup::Standard::gendump("Setup file for ikiwiki.");
84
85         open (OUT, ">", $file) || die "$file: $!";
86         print OUT "$_\n" foreach @dump;
87         close OUT;
88 }
89
90 1