Make it possible to load setup files w/o running them. Code needing to do so can...
[ikiwiki] / IkiWiki / Setup.pm
1 #!/usr/bin/perl
2
3 package IkiWiki::Setup;
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use open qw{:utf8 :std};
8
9 # This hashref is where setup files store settings while they're being
10 # loaded. It is not used otherwise.
11 our $raw_setup;
12
13 sub load ($) { # {{{
14         my $setup=IkiWiki::possibly_foolish_untaint(shift);
15         delete $config{setup};
16         #translators: The first parameter is a filename, and the second
17         #translators: is a (probably not translated) error message.
18         open (IN, $setup) || error(sprintf(gettext("cannot read %s: %s"), $setup, $!));
19         my $code;
20         {
21                 local $/=undef;
22                 $code=<IN>;
23         }
24         ($code)=$code=~/(.*)/s;
25         close IN;
26
27         eval $code;
28         error("$setup: ".$@) if $@;
29
30         my $ret=$raw_setup;
31         $raw_setup=undef;
32
33         return %$ret;
34 } #}}}
35
36 package IkiWiki;
37
38 sub setup () { #{{{
39         my %setup=IkiWiki::Setup::load($config{setup});
40
41         $setup{plugin}=$config{plugin};
42         if (exists $setup{add_plugins}) {
43                 push @{$setup{plugin}}, @{$setup{add_plugins}};
44                 delete $setup{add_plugins};
45         }
46         if (exists $setup{exclude}) {
47                 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
48         }
49
50         if (! $config{render} && (! $config{refresh} || $config{wrappers})) {
51                 debug(gettext("generating wrappers.."));
52                 my @wrappers=@{$setup{wrappers}};
53                 delete $setup{wrappers};
54                 my %startconfig=(%config);
55                 foreach my $wrapper (@wrappers) {
56                         %config=(%startconfig, rebuild => 0, verbose => 0, %setup, %{$wrapper});
57                         checkconfig();
58                         if (! $config{cgi} && ! $config{post_commit}) {
59                                 $config{post_commit}=1;
60                         }
61                         gen_wrapper();
62                 }
63                 %config=(%startconfig);
64         }
65         
66         foreach my $c (keys %setup) {
67                 next if $c eq 'syslog';
68                 if (defined $setup{$c}) {
69                         if (! ref $setup{$c}) {
70                                 $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
71                         }
72                         elsif (ref $setup{$c} eq 'ARRAY') {
73                                 $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
74                         }
75                         elsif (ref $setup{$c} eq 'HASH') {
76                                 foreach my $key (keys %{$setup{$c}}) {
77                                         $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
78                                 }
79                         }
80                 }
81                 else {
82                         $config{$c}=undef;
83                 }
84         }
85         
86         if (! $config{refresh}) {
87                 $config{rebuild}=1;
88         }
89         
90         loadplugins();
91         checkconfig();
92
93         if ($config{render}) {
94                 commandline_render();
95         }
96
97         if (! $config{refresh}) {
98                 debug(gettext("rebuilding wiki.."));
99         }
100         else {
101                 debug(gettext("refreshing wiki.."));
102         }
103
104         lockwiki();
105         loadindex();
106         refresh();
107
108         debug(gettext("done"));
109         saveindex();
110 } #}}}
111
112 1