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.
7 package IkiWiki::Setup;
12 use open qw{:utf8 :std};
16 my $file=IkiWiki::possibly_foolish_untaint(shift);
19 $config{setupfile}=File::Spec->rel2abs($file);
21 #translators: The first parameter is a filename, and the second
22 #translators: is a (probably not translated) error message.
23 open (IN, $file) || error(sprintf(gettext("cannot read %s: %s"), $file, $!));
27 $content=<IN> || error("$file: $!");
31 if ($content=~/(use\s+)?(IkiWiki::Setup::\w+)/) {
32 $config{setuptype}=$2;
34 error sprintf(gettext("cannot load %s in safe mode"), $file)
36 eval IkiWiki::possibly_foolish_untaint($content);
37 error("$file: ".$@) if $@;
40 eval qq{require $config{setuptype}};
42 $config{setuptype}->loaddump(IkiWiki::possibly_foolish_untaint($content));
46 error sprintf(gettext("failed to parse %s"), $file);
51 my $file=IkiWiki::possibly_foolish_untaint(shift);
53 eval qq{require $config{setuptype}};
55 my @dump=$config{setuptype}->gendump("Setup file for ikiwiki.");
57 open (OUT, ">", $file) || die "$file: $!";
58 print OUT "$_\n" foreach @dump;
63 # Merge setup into existing config and untaint.
66 if (exists $setup{add_plugins} && exists $config{add_plugins}) {
67 push @{$setup{add_plugins}}, @{$config{add_plugins}};
69 if (exists $setup{exclude}) {
70 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
72 foreach my $c (keys %setup) {
73 if (defined $setup{$c}) {
74 if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
75 $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
77 elsif (ref $setup{$c} eq 'ARRAY') {
78 if ($c eq 'wrappers') {
79 # backwards compatability code
80 $config{$c}=$setup{$c};
83 $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
86 elsif (ref $setup{$c} eq 'HASH') {
87 foreach my $key (keys %{$setup{$c}}) {
88 $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
97 if (length $config{cgi_wrapper}) {
98 push @{$config{wrappers}}, {
100 wrapper => $config{cgi_wrapper},
101 wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
107 # Gets all available setup data from all plugins. Returns an
108 # ordered list of [plugin, setup] pairs.
110 # disable logging to syslog while dumping, broken plugins may
112 my $syslog=$config{syslog};
113 $config{syslog}=undef;
115 # Load all plugins, so that all setup options are available.
116 my @plugins=IkiWiki::listplugins();
117 foreach my $plugin (@plugins) {
118 eval { IkiWiki::loadplugin($plugin) };
119 if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
120 my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
125 foreach my $plugin (@plugins) {
126 if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
127 # use an array rather than a hash, to preserve order
128 my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
131 # set default section value (note use of shared
132 # hashref between array and hash)
134 if (! exists $s{plugin} || ! $s{plugin}->{section}) {
135 $s{plugin}->{section}="other";
138 # only the selected rcs plugin is included
139 if ($config{rcs} && $plugin eq $config{rcs}) {
140 $s{plugin}->{section}="core";
142 elsif ($s{plugin}->{section} eq "rcs") {
146 push @{$sections{$s{plugin}->{section}}}, [ $plugin, \@s ];
150 $config{syslog}=$syslog;
152 return map { sort { $a->[0] cmp $b->[0] } @{$sections{$_}} }
153 sort { # core first, other last, otherwise alphabetical
154 ($b eq "core") <=> ($a eq "core")
156 ($a eq "other") <=> ($b eq "other")
162 sub commented_dump ($) {
168 # disable logging to syslog while dumping
169 $config{syslog}=undef;
171 eval q{use Text::Wrap};
175 push @ret, commented_dumpvalues($dumpline, \%setup, IkiWiki::getsetup());
176 foreach my $pair (IkiWiki::Setup::getsetup()) {
177 my $plugin=$pair->[0];
178 my $setup=$pair->[1];
180 my $section=$s{plugin}->{section};
181 push @{$section_plugins{$section}}, $plugin;
182 if (@{$section_plugins{$section}} == 1) {
183 push @ret, "", "\t".("#" x 70), "\t# $section plugins",
185 wrap("\t# (", "\t# ",
186 join(", ", @{$section_plugins{$section}})).")"
191 my @values=commented_dumpvalues($dumpline, \%setup, @{$setup});
193 push @ret, "", "\t# $plugin plugin", @values;
197 return map { ref $_ ? $_->() : $_ } @ret;
200 sub commented_dumpvalues ($$@) {
208 next if $key eq "plugin" || $info{type} eq "internal";
210 push @ret, "\t# ".$info{description} if exists $info{description};
212 if (exists $setup->{$key} && defined $setup->{$key}) {
213 push @ret, $dumpline->($key, $setup->{$key}, $info{type}, "");
214 delete $setup->{$key};
216 elsif (exists $info{example}) {
217 push @ret, $dumpline->($key, $info{example}, $info{type}, "#");
220 push @ret, $dumpline->($key, "", $info{type}, "#");