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(
56 "Setup file for ikiwiki.",
58 "Passing this to ikiwiki --setup will make ikiwiki generate",
59 "wrappers and build the wiki.",
61 "Remember to re-run ikiwiki --setup any time you edit this file.",
64 open (OUT, ">", $file) || die "$file: $!";
65 print OUT "$_\n" foreach @dump;
70 # Merge setup into existing config and untaint.
73 if (exists $setup{add_plugins} && exists $config{add_plugins}) {
74 push @{$setup{add_plugins}}, @{$config{add_plugins}};
76 if (exists $setup{exclude}) {
77 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
79 foreach my $c (keys %setup) {
80 if (defined $setup{$c}) {
81 if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
82 $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
84 elsif (ref $setup{$c} eq 'ARRAY') {
85 if ($c eq 'wrappers') {
86 # backwards compatability code
87 $config{$c}=$setup{$c};
90 $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
93 elsif (ref $setup{$c} eq 'HASH') {
94 foreach my $key (keys %{$setup{$c}}) {
95 $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
104 if (length $config{cgi_wrapper}) {
105 push @{$config{wrappers}}, {
107 wrapper => $config{cgi_wrapper},
108 wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
114 # Gets all available setup data from all plugins. Returns an
115 # ordered list of [plugin, setup] pairs.
117 # disable logging to syslog while dumping, broken plugins may
119 my $syslog=$config{syslog};
120 $config{syslog}=undef;
122 # Load all plugins, so that all setup options are available.
123 my @plugins=IkiWiki::listplugins();
124 foreach my $plugin (@plugins) {
125 eval { IkiWiki::loadplugin($plugin) };
126 if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
127 my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
132 foreach my $plugin (@plugins) {
133 if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
134 # use an array rather than a hash, to preserve order
135 my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
138 # set default section value (note use of shared
139 # hashref between array and hash)
141 if (! exists $s{plugin} || ! $s{plugin}->{section}) {
142 $s{plugin}->{section}="other";
145 # only the selected rcs plugin is included
146 if ($config{rcs} && $plugin eq $config{rcs}) {
147 $s{plugin}->{section}="core";
149 elsif ($s{plugin}->{section} eq "rcs") {
153 push @{$sections{$s{plugin}->{section}}}, [ $plugin, \@s ];
157 $config{syslog}=$syslog;
159 return map { sort { $a->[0] cmp $b->[0] } @{$sections{$_}} }
160 sort { # core first, other last, otherwise alphabetical
161 ($b eq "core") <=> ($a eq "core")
163 ($a eq "other") <=> ($b eq "other")
169 sub commented_dump ($) {
175 # disable logging to syslog while dumping
176 $config{syslog}=undef;
178 eval q{use Text::Wrap};
182 push @ret, commented_dumpvalues($dumpline, \%setup, IkiWiki::getsetup());
183 foreach my $pair (IkiWiki::Setup::getsetup()) {
184 my $plugin=$pair->[0];
185 my $setup=$pair->[1];
187 my $section=$s{plugin}->{section};
188 push @{$section_plugins{$section}}, $plugin;
189 if (@{$section_plugins{$section}} == 1) {
190 push @ret, "", "\t".("#" x 70), "\t# $section plugins",
192 wrap("\t# (", "\t# ",
193 join(", ", @{$section_plugins{$section}})).")"
198 my @values=commented_dumpvalues($dumpline, \%setup, @{$setup});
200 push @ret, "", "\t# $plugin plugin", @values;
204 return map { ref $_ ? $_->() : $_ } @ret;
207 sub commented_dumpvalues ($$@) {
215 next if $key eq "plugin" || $info{type} eq "internal";
217 push @ret, "\t# ".$info{description} if exists $info{description};
219 if (exists $setup->{$key} && defined $setup->{$key}) {
220 push @ret, $dumpline->($key, $setup->{$key}, $info{type}, "");
221 delete $setup->{$key};
223 elsif (exists $info{example}) {
224 push @ret, $dumpline->($key, $info{example}, $info{type}, "#");
227 push @ret, $dumpline->($key, "", $info{type}, "#");