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|require)\s+)?IkiWiki::Setup::(\w+)/) {
32 $config{setuptype}=$2;
34 error sprintf(gettext("cannot load %s in safe mode"), $file)
37 eval IkiWiki::possibly_foolish_untaint($content);
38 error("$file: ".$@) if $@;
41 eval qq{require IkiWiki::Setup::$config{setuptype}};
43 "IkiWiki::Setup::$config{setuptype}"->loaddump(IkiWiki::possibly_foolish_untaint($content));
47 error sprintf(gettext("failed to parse %s"), $file);
52 my $file=IkiWiki::possibly_foolish_untaint(shift);
54 eval qq{require IkiWiki::Setup::$config{setuptype}};
56 my @dump="IkiWiki::Setup::$config{setuptype}"->gendump(
57 "Setup file for ikiwiki.",
59 "Passing this to ikiwiki --setup will make ikiwiki generate",
60 "wrappers and build the wiki.",
62 "Remember to re-run ikiwiki --setup any time you edit this file.",
65 open (OUT, ">", $file) || die "$file: $!";
66 print OUT "$_\n" foreach @dump;
71 # Merge setup into existing config and untaint.
74 if (exists $setup{add_plugins} && exists $config{add_plugins}) {
75 push @{$setup{add_plugins}}, @{$config{add_plugins}};
77 if (exists $setup{exclude}) {
78 push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
80 foreach my $c (keys %setup) {
81 if (defined $setup{$c}) {
82 if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
83 $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
85 elsif (ref $setup{$c} eq 'ARRAY') {
86 if ($c eq 'wrappers') {
87 # backwards compatability code
88 $config{$c}=$setup{$c};
91 $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
94 elsif (ref $setup{$c} eq 'HASH') {
95 foreach my $key (keys %{$setup{$c}}) {
96 $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
105 if (length $config{cgi_wrapper}) {
106 push @{$config{wrappers}}, {
108 wrapper => $config{cgi_wrapper},
109 wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
115 # Gets all available setup data from all plugins. Returns an
116 # ordered list of [plugin, setup] pairs.
118 # disable logging to syslog while dumping, broken plugins may
120 my $syslog=$config{syslog};
121 $config{syslog}=undef;
123 # Load all plugins, so that all setup options are available.
124 my @plugins=IkiWiki::listplugins();
125 foreach my $plugin (@plugins) {
126 eval { IkiWiki::loadplugin($plugin, 1) };
127 if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
128 my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
133 foreach my $plugin (@plugins) {
134 if (exists $IkiWiki::hooks{getsetup}{$plugin}{call}) {
135 # use an array rather than a hash, to preserve order
136 my @s=eval { $IkiWiki::hooks{getsetup}{$plugin}{call}->() };
139 # set default section value (note use of shared
140 # hashref between array and hash)
142 if (! exists $s{plugin} || ! $s{plugin}->{section}) {
143 $s{plugin}->{section}="other";
146 # only the selected rcs plugin is included
147 if ($config{rcs} && $plugin eq $config{rcs}) {
148 $s{plugin}->{section}="core";
150 elsif ($s{plugin}->{section} eq "rcs") {
154 push @{$sections{$s{plugin}->{section}}}, [ $plugin, \@s ];
158 $config{syslog}=$syslog;
160 return map { sort { $a->[0] cmp $b->[0] } @{$sections{$_}} }
161 sort { # core first, other last, otherwise alphabetical
162 ($b eq "core") <=> ($a eq "core")
164 ($a eq "other") <=> ($b eq "other")
170 sub commented_dump ($$) {
177 # disable logging to syslog while dumping
178 $config{syslog}=undef;
180 eval q{use Text::Wrap};
184 push @ret, commented_dumpvalues($dumpline, $indent, \%setup, IkiWiki::getsetup());
185 foreach my $pair (IkiWiki::Setup::getsetup()) {
186 my $plugin=$pair->[0];
187 my $setup=$pair->[1];
189 my $section=$s{plugin}->{section};
190 push @{$section_plugins{$section}}, $plugin;
191 if (@{$section_plugins{$section}} == 1) {
192 push @ret, "", $indent.("#" x 70), "$indent# $section plugins",
194 wrap("$indent# (", "$indent# ",
195 join(", ", @{$section_plugins{$section}})).")"
200 my @values=commented_dumpvalues($dumpline, $indent, \%setup, @{$setup});
202 push @ret, "", "$indent# $plugin plugin", @values;
206 return map { ref $_ ? $_->() : $_ } @ret;
209 sub commented_dumpvalues ($$$@) {
218 next if $key eq "plugin" || $info{type} eq "internal";
220 push @ret, "$indent# ".$info{description} if exists $info{description};
222 if (exists $setup->{$key} && defined $setup->{$key}) {
223 push @ret, $dumpline->($key, $setup->{$key}, $info{type}, "");
224 delete $setup->{$key};
226 elsif (exists $info{example}) {
227 push @ret, $dumpline->($key, $info{example}, $info{type}, "#");
230 push @ret, $dumpline->($key, "", $info{type}, "#");