fix use ordering
[ikiwiki] / 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 # There can be multiple modules, with different configuration styles.
6 # The setup modules each convert the data into the hashes used by ikiwiki
7 # internally (if it's not already in that format), and store it in
8 # IkiWiki::Setup::$raw_setup, to pass it back to this module.
9
10 package IkiWiki::Setup;
11
12 use warnings;
13 use strict;
14 use IkiWiki;
15 use IkiWiki::Wrapper;
16 use open qw{:utf8 :std};
17
18 our $raw_setup;
19
20 sub load ($) { # {{{
21         my $setup=IkiWiki::possibly_foolish_untaint(shift);
22         delete $config{setup};
23         #translators: The first parameter is a filename, and the second
24         #translators: is a (probably not translated) error message.
25         open (IN, $setup) || error(sprintf(gettext("cannot read %s: %s"), $setup, $!));
26         my $code;
27         {
28                 local $/=undef;
29                 $code=<IN>;
30         }
31         ($code)=$code=~/(.*)/s;
32         close IN;
33
34         eval $code;
35         error("$setup: ".$@) if $@;
36
37         my $ret=$raw_setup;
38         $raw_setup=undef;
39
40         return %$ret;
41 } #}}}
42
43 package IkiWiki;
44
45 sub setup () { #{{{
46         my %setup=IkiWiki::Setup::load($config{setup});
47
48         $setup{plugin}=$config{plugin};
49         if (exists $setup{add_plugins}) {
50                 push @{$setup{plugin}}, @{$setup{add_plugins}};
51                 delete $setup{add_plugins};
52         }
53         if (exists $setup{exclude}) {
54                 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
55         }
56
57         if (! $config{render} && (! $config{refresh} || $config{wrappers})) {
58                 debug(gettext("generating wrappers.."));
59                 my @wrappers=@{$setup{wrappers}};
60                 delete $setup{wrappers};
61                 my %startconfig=(%config);
62                 foreach my $wrapper (@wrappers) {
63                         %config=(%startconfig, rebuild => 0, verbose => 0, %setup, %{$wrapper});
64                         checkconfig();
65                         if (! $config{cgi} && ! $config{post_commit}) {
66                                 $config{post_commit}=1;
67                         }
68                         gen_wrapper();
69                 }
70                 %config=(%startconfig);
71         }
72         
73         foreach my $c (keys %setup) {
74                 next if $c eq 'syslog';
75                 if (defined $setup{$c}) {
76                         if (! ref $setup{$c}) {
77                                 $config{$c}=possibly_foolish_untaint($setup{$c});
78                         }
79                         elsif (ref $setup{$c} eq 'ARRAY') {
80                                 $config{$c}=[map { possibly_foolish_untaint($_) } @{$setup{$c}}]
81                         }
82                         elsif (ref $setup{$c} eq 'HASH') {
83                                 foreach my $key (keys %{$setup{$c}}) {
84                                         $config{$c}{$key}=possibly_foolish_untaint($setup{$c}{$key});
85                                 }
86                         }
87                 }
88                 else {
89                         $config{$c}=undef;
90                 }
91         }
92         
93         if (! $config{refresh}) {
94                 $config{rebuild}=1;
95         }
96         
97         loadplugins();
98         checkconfig();
99
100         if ($config{render}) {
101                 commandline_render();
102         }
103
104         if (! $config{refresh}) {
105                 debug(gettext("rebuilding wiki.."));
106         }
107         else {
108                 debug(gettext("refreshing wiki.."));
109         }
110
111         lockwiki();
112         loadindex();
113         refresh();
114
115         debug(gettext("done"));
116         saveindex();
117 } #}}}
118
119 1