year calendar: Avoid highlighting the current month in a different year
[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 package IkiWiki::Setup;
6
7 use warnings;
8 use strict;
9 use IkiWiki;
10 use open qw{:utf8 :std};
11 use File::Spec;
12
13 sub load ($) {
14         my $setup=IkiWiki::possibly_foolish_untaint(shift);
15         $config{setupfile}=File::Spec->rel2abs($setup);
16
17         #translators: The first parameter is a filename, and the second
18         #translators: is a (probably not translated) error message.
19         open (IN, $setup) || error(sprintf(gettext("cannot read %s: %s"), $setup, $!));
20         my $code;
21         {
22                 local $/=undef;
23                 $code=<IN> || error("$setup: $!");
24         }
25         
26         ($code)=$code=~/(.*)/s;
27         close IN;
28
29         eval $code;
30         error("$setup: ".$@) if $@;
31 }
32
33 sub merge ($) {
34         # Merge setup into existing config and untaint.
35         my %setup=%{shift()};
36
37         if (exists $setup{add_plugins} && exists $config{add_plugins}) {
38                 push @{$setup{add_plugins}}, @{$config{add_plugins}};
39         }
40         if (exists $setup{exclude}) {
41                 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
42         }
43         foreach my $c (keys %setup) {
44                 if (defined $setup{$c}) {
45                         if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
46                                 $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
47                         }
48                         elsif (ref $setup{$c} eq 'ARRAY') {
49                                 if ($c eq 'wrappers') {
50                                         # backwards compatability code
51                                         $config{$c}=$setup{$c};
52                                 }
53                                 else {
54                                         $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
55                                 }
56                         }
57                         elsif (ref $setup{$c} eq 'HASH') {
58                                 foreach my $key (keys %{$setup{$c}}) {
59                                         $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
60                                 }
61                         }
62                 }
63                 else {
64                         $config{$c}=undef;
65                 }
66         }
67         
68         if (length $config{cgi_wrapper}) {
69                 push @{$config{wrappers}}, {
70                         cgi => 1,
71                         wrapper => $config{cgi_wrapper},
72                         wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
73                 };
74         }
75 }
76
77 sub getsetup () {
78         # Gets all available setup data from all plugins. Returns an
79         # ordered list of [plugin, setup] pairs.
80         my @ret;
81
82         # disable logging to syslog while dumping, broken plugins may
83         # whine when loaded
84         my $syslog=$config{syslog};
85         $config{syslog}=undef;
86
87         # Load all plugins, so that all setup options are available.
88         my @plugins=grep { $_ ne $config{rcs} } sort(IkiWiki::listplugins());
89         unshift @plugins, $config{rcs} if $config{rcs}; # rcs plugin 1st
90         foreach my $plugin (@plugins) {
91                 eval { IkiWiki::loadplugin($plugin) };
92                 if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
93                         my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
94                 }
95         }
96
97         foreach my $plugin (@plugins) {
98                 if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
99                         # use an array rather than a hash, to preserve order
100                         my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
101                         next unless @s;
102                         push @ret, [ $plugin, \@s ],
103                 }
104         }
105         
106         $config{syslog}=$syslog;
107
108         return @ret;
109 }
110
111 sub dump ($) {
112         my $file=IkiWiki::possibly_foolish_untaint(shift);
113         
114         require IkiWiki::Setup::Standard;
115         my @dump=IkiWiki::Setup::Standard::gendump("Setup file for ikiwiki.");
116
117         open (OUT, ">", $file) || die "$file: $!";
118         print OUT "$_\n" foreach @dump;
119         close OUT;
120 }
121
122 1