+ elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
+ # Push values from form into %config, avoiding unnecessary
+ # changes, and keeping track of which changes need a
+ # rebuild.
+ my %rebuild;
+ foreach my $field (keys %fields) {
+ my %info=%{$fields{$field}->[1]};
+ my $key=$fields{$field}->[0];
+ my @value=$form->field($field);
+ if (! @value) {
+ @value=0;
+ }
+
+ if (! $info{safe} || ! issafe($key)) {
+ error("unsafe field $key"); # should never happen
+ }
+
+ if (exists $info{rebuild} &&
+ ($info{rebuild} || ! defined $info{rebuild})) {
+ $rebuild{$field}=$info{rebuild};
+ }
+
+ if ($field=~/^enable\.(.*)/) {
+ my $plugin=$1;
+ $value[0]=0 if ! length $value[0];
+ if ($value[0] != exists $enabled_plugins{$plugin}) {
+ if ($value[0]) {
+ enable_plugin($plugin);
+ }
+ else {
+ disable_plugin($plugin);
+
+ }
+ }
+ else {
+ delete $rebuild{$field};
+ }
+ next;
+ }
+
+ if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
+ @value=sort grep { length $_ } @value;
+ my @oldvalue=sort grep { length $_ }
+ (defined $config{$key} ? @{$config{$key}} : ());
+ my $same=(@oldvalue) == (@value);
+ for (my $x=0; $same && $x < @value; $x++) {
+ $same=0 if $value[$x] ne $oldvalue[$x];
+ }
+ if ($same) {
+ delete $rebuild{$field};
+ }
+ else {
+ $config{$key}=\@value;
+ }
+ }
+ elsif (ref $config{$key} || ref $info{example}) {
+ error("complex field $key"); # should never happen
+ }
+ else {
+ if (defined $config{$key} && $config{$key} eq $value[0]) {
+ delete $rebuild{$field};
+ }
+ elsif (! defined $config{$key} && ! length $value[0]) {
+ delete $rebuild{$field};
+ }
+ elsif ((! defined $config{$key} || ! $config{$key}) &&
+ ! $value[0] && $info{type} eq "boolean") {
+ delete $rebuild{$field};
+ }
+ else {
+ $config{$key}=$value[0];
+ }
+ }
+ }
+
+ if (%rebuild && ! $form->field("rebuild_asked")) {
+ my $required=0;
+ foreach my $field ($form->field) {
+ $required=1 if $rebuild{$field};
+ next if exists $rebuild{$field};
+ $form->field(name => $field, type => "hidden");
+ }
+ if ($required) {
+ $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
+ $buttons=["Rebuild Wiki", "Cancel"];
+ }
+ else {
+ $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
+ $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
+ }
+ $form->field(name => "rebuild_asked", value => 1, force => 1);
+ $form->reset(0); # doesn't really make sense here
+ }
+ else {
+ my $oldsetup=readfile($config{setupfile});
+ IkiWiki::Setup::dump($config{setupfile});
+
+ IkiWiki::saveindex();
+ IkiWiki::unlockwiki();
+
+ # Print the top part of a standard misctemplate,
+ # then show the rebuild or refresh.
+ my $divider="xxx";
+ my $html=IkiWiki::misctemplate("setup", $divider);
+ IkiWiki::printheader($session);
+ my ($head, $tail)=split($divider, $html, 2);
+ print $head."<pre>\n";
+
+ my @command;
+ if ($form->submitted eq 'Rebuild Wiki') {
+ @command=("ikiwiki", "-setup", $config{setupfile},
+ "-rebuild", "-v");
+ }
+ else {
+ @command=("ikiwiki", "-setup", $config{setupfile},
+ "-refresh", "-wrappers", "-v");
+ }
+
+ close STDERR;
+ open(STDERR, ">&STDOUT");
+ my $ret=system(@command);
+ print "\n<\/pre>";
+ if ($ret != 0) {
+ print '<p class="error">'.
+ sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
+ join(" ", @command), $ret).
+ '</p>';
+ open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
+ print OUT $oldsetup;
+ close OUT;
+ }
+
+ print $tail;
+ exit 0;
+ }