2 package IkiWiki
::Plugin
::websetup
;
9 hook
(type
=> "getsetup", id
=> "websetup", call
=> \
&getsetup
);
10 hook
(type
=> "checkconfig", id
=> "websetup", call
=> \
&checkconfig
);
11 hook
(type
=> "sessioncgi", id
=> "websetup", call
=> \
&sessioncgi
);
12 hook
(type
=> "formbuilder_setup", id
=> "websetup",
13 call
=> \
&formbuilder_setup
);
23 websetup_force_plugins
=> {
26 description
=> "list of plugins that cannot be enabled/disabled via the web interface",
33 description
=> "list of additional setup field keys to treat as unsafe",
37 websetup_show_unsafe
=> {
40 description
=> "show unsafe settings, read-only, in web interface?",
47 if (! exists $config{websetup_show_unsafe
}) {
48 $config{websetup_show_unsafe
}=1;
52 sub formatexample
($$) {
56 if (defined $value && length $value) {
59 elsif (defined $example && ! ref $example && length $example) {
60 return "<br/ ><small>Example: <tt>$example</tt></small>";
70 return ! grep { $_ eq $key } @
{$config{websetup_unsafe
}};
73 sub showfields
($$$@
) {
84 if ($key eq 'plugin') {
89 # skip internal settings
90 next if defined $info{type
} && $info{type
} eq "internal";
91 # XXX hashes not handled yet
92 next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example
} eq 'HASH';
93 # maybe skip unsafe settings
94 next if ! ($config{websetup_show_unsafe
} && $config{websetup_advanced
}) &&
95 (! $info{safe
} || ! issafe
($key));
96 # maybe skip advanced settings
97 next if $info{advanced
} && ! $config{websetup_advanced
};
98 # these are handled specially, so don't show
99 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
101 push @show, $key, \
%info;
104 my $section=defined $plugin
105 ?
sprintf(gettext
("%s plugin:"), $plugininfo{section
})." ".$plugin
110 my $plugin_forced=defined $plugin && (! $plugininfo{safe
} ||
111 (exists $config{websetup_force_plugins
} && grep { $_ eq $plugin } @
{$config{websetup_force_plugins
}}));
112 if ($plugin_forced && ! $enabled) {
113 # plugin is forced disabled, so skip its settings
117 my $section_fieldset;
118 if (defined $plugin) {
119 # Define the combined fieldset for the plugin's section.
120 # This ensures that this fieldset comes first.
121 $section_fieldset=sprintf(gettext
("%s plugins"), $plugininfo{section
});
122 $form->field(name
=> "placeholder.$plugininfo{section}",
124 fieldset
=> $section_fieldset);
128 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced
})) {
129 my $name="enable.$plugin";
134 fieldset
=> $section,
135 options
=> [ [ 1 => sprintf(gettext
("enable %s?"), $plugin) ]]
137 if (! $form->submitted) {
138 $form->field(name
=> $name, value
=> $enabled);
140 if ($plugin_forced) {
141 $form->field(name
=> $name, disabled
=> 1);
144 $enabledfields{$name}=[$name, \
%plugininfo];
148 # show plugin settings
151 my %info=%{shift @show};
153 my $description=$info{description
};
154 if (exists $info{htmldescription
}) {
155 $description=$info{htmldescription
};
157 elsif (exists $info{link} && length $info{link}) {
158 if ($info{link} =~ /^\w+:\/\
//) {
159 $description="<a href=\"$info{link}\">$description</a>";
162 $description=htmllink
("", "", $info{link}, noimageinline
=> 1, linktext
=> $description);
166 # multiple plugins can have the same field
167 my $name=defined $plugin ?
$plugin.".".$key : $section.".".$key;
169 my $value=$config{$key};
170 if (! defined $value) {
174 if (ref $value eq 'ARRAY' || ref $info{example
} eq 'ARRAY') {
175 $value=[(ref $value eq 'ARRAY' ?
map { Encode
::encode_utf8
($_) } @
{$value} : "")];
176 push @
$value, "", "" if $info{safe
} && issafe
($key); # blank items for expansion
179 $value=Encode
::encode_utf8
($value);
182 if ($info{type
} eq "string") {
185 label
=> $description,
186 comment
=> formatexample
($info{example
}, $value),
190 fieldset
=> $section,
193 elsif ($info{type
} eq "pagespec") {
196 label
=> $description,
197 comment
=> formatexample
($info{example
}, $value),
201 validate
=> \
&IkiWiki
::pagespec_valid
,
202 fieldset
=> $section,
205 elsif ($info{type
} eq "integer") {
208 label
=> $description,
209 comment
=> formatexample
($info{example
}, $value),
213 validate
=> '/^[0-9]+$/',
214 fieldset
=> $section,
217 elsif ($info{type
} eq "boolean") {
222 options
=> [ [ 1 => $description ] ],
223 fieldset
=> $section,
225 if (! $form->submitted ||
226 ($info{advanced
} && $form->submitted eq 'Advanced Mode')) {
227 $form->field(name
=> $name, value
=> $value);
231 if (! $info{safe
} || ! issafe
($key)) {
232 $form->field(name
=> $name, disabled
=> 1);
235 $enabledfields{$name}=[$key, \
%info];
240 # if no fields were shown for the plugin, drop it into a combined
241 # fieldset for its section
242 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced
}) &&
244 $form->field(name
=> "enable.$plugin", fieldset
=> $section_fieldset);
247 return %enabledfields;
250 sub enable_plugin
($) {
253 $config{disable_plugins
}=[grep { $_ ne $plugin } @
{$config{disable_plugins
}}];
254 push @
{$config{add_plugins
}}, $plugin;
257 sub disable_plugin
($) {
260 $config{add_plugins
}=[grep { $_ ne $plugin } @
{$config{add_plugins
}}];
261 push @
{$config{disable_plugins
}}, $plugin;
268 IkiWiki
::needsignin
($cgi, $session);
270 if (! defined $session->param("name") ||
271 ! IkiWiki
::is_admin
($session->param("name"))) {
272 error
(gettext
("you are not logged in as an admin"));
275 if (! exists $config{setupfile
}) {
276 error
(gettext
("setup file for this wiki is not known"));
279 eval q{use CGI::FormBuilder};
282 my $form = CGI
::FormBuilder
->new(
292 [main
=> gettext
("main")],
294 action
=> IkiWiki
::cgiurl
(),
295 template
=> {type
=> 'div'},
299 $form->field(name
=> "do", type
=> "hidden", value
=> "setup",
301 $form->field(name
=> "rebuild_asked", type
=> "hidden");
302 $form->field(name
=> "showadvanced", type
=> "hidden");
304 if ($form->submitted eq 'Basic Mode') {
305 $form->field(name
=> "showadvanced", type
=> "hidden",
306 value
=> 0, force
=> 1);
308 elsif ($form->submitted eq 'Advanced Mode') {
309 $form->field(name
=> "showadvanced", type
=> "hidden",
310 value
=> 1, force
=> 1);
313 if ($form->field("showadvanced")) {
314 $config{websetup_advanced
}=1;
315 $advancedtoggle="Basic Mode";
318 $config{websetup_advanced
}=0;
319 $advancedtoggle="Advanced Mode";
322 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
324 IkiWiki
::decode_form_utf8
($form);
325 IkiWiki
::run_hooks
(formbuilder_setup
=> sub {
326 shift->(form
=> $form, cgi
=> $cgi, session
=> $session,
327 buttons
=> $buttons);
330 my %fields=showfields
($form, undef, undef, IkiWiki
::getsetup
());
332 # record all currently enabled plugins before all are loaded
333 my %enabled_plugins=%IkiWiki::loaded_plugins
;
336 require IkiWiki
::Setup
;
337 foreach my $pair (IkiWiki
::Setup
::getsetup
()) {
338 my $plugin=$pair->[0];
339 my $setup=$pair->[1];
341 my %shown=showfields
($form, $plugin, $enabled_plugins{$plugin}, @
{$setup});
343 $fields{$_}=$shown{$_} foreach keys %shown;
347 IkiWiki
::decode_form_utf8
($form);
349 if ($form->submitted eq "Cancel") {
350 IkiWiki
::redirect
($cgi, IkiWiki
::baseurl
(undef));
353 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
354 # Push values from form into %config, avoiding unnecessary
355 # changes, and keeping track of which changes need a
358 foreach my $field (keys %fields) {
359 my %info=%{$fields{$field}->[1]};
360 my $key=$fields{$field}->[0];
361 my @value=$form->field($field);
366 if (! $info{safe
} || ! issafe
($key)) {
367 error
("unsafe field $key"); # should never happen
370 if (exists $info{rebuild
} &&
371 ($info{rebuild
} || ! defined $info{rebuild
})) {
372 $rebuild{$field}=$info{rebuild
};
375 if ($field=~/^enable\.(.*)/) {
377 $value[0]=0 if ! length $value[0];
378 if ($value[0] != exists $enabled_plugins{$plugin}) {
380 enable_plugin
($plugin);
383 disable_plugin
($plugin);
388 delete $rebuild{$field};
393 if (ref $config{$key} eq "ARRAY" || ref $info{example
} eq "ARRAY") {
394 @value=sort grep { length $_ } @value;
395 my @oldvalue=sort grep { length $_ }
396 (defined $config{$key} ? @
{$config{$key}} : ());
397 my $same=(@oldvalue) == (@value);
398 for (my $x=0; $same && $x < @value; $x++) {
399 $same=0 if $value[$x] ne $oldvalue[$x];
402 delete $rebuild{$field};
405 $config{$key}=\
@value;
408 elsif (ref $config{$key} || ref $info{example
}) {
409 error
("complex field $key"); # should never happen
412 if (defined $config{$key} && $config{$key} eq $value[0]) {
413 delete $rebuild{$field};
415 elsif (! defined $config{$key} && ! length $value[0]) {
416 delete $rebuild{$field};
418 elsif ((! defined $config{$key} || ! $config{$key}) &&
419 ! $value[0] && $info{type
} eq "boolean") {
420 delete $rebuild{$field};
423 $config{$key}=$value[0];
428 if (%rebuild && ! $form->field("rebuild_asked")) {
430 foreach my $field ($form->field) {
431 $required=1 if $rebuild{$field};
432 next if exists $rebuild{$field};
433 $form->field(name
=> $field, type
=> "hidden");
436 $form->text(gettext
("The configuration changes shown below require a wiki rebuild to take effect."));
437 $buttons=["Rebuild Wiki", "Cancel"];
440 $form->text(gettext
("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
441 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
443 $form->field(name
=> "rebuild_asked", value
=> 1, force
=> 1);
444 $form->reset(0); # doesn't really make sense here
447 my $oldsetup=readfile
($config{setupfile
});
448 IkiWiki
::Setup
::dump($config{setupfile
});
450 IkiWiki
::saveindex
();
451 IkiWiki
::unlockwiki
();
453 # Print the top part of a standard cgitemplate,
454 # then show the rebuild or refresh, live.
456 my $html=IkiWiki
::cgitemplate
($cgi, "setup", $divider);
457 IkiWiki
::printheader
($session);
458 my ($head, $tail)=split($divider, $html, 2);
459 print $head."<pre>\n";
462 if ($form->submitted eq 'Rebuild Wiki') {
463 @command=("ikiwiki", "-setup", $config{setupfile
},
467 @command=("ikiwiki", "-setup", $config{setupfile
},
468 "-refresh", "-wrappers", "-v");
472 open(STDERR
, ">&STDOUT");
473 my $ret=system(@command);
476 print '<p class="error">'.
477 sprintf(gettext
("Error: %s exited nonzero (%s). Discarding setup changes."),
478 join(" ", @command), $ret).
480 open(OUT
, ">", $config{setupfile
}) || error
("$config{setupfile}: $!");
481 print OUT Encode
::encode_utf8
($oldsetup);
490 IkiWiki
::showform
($form, $buttons, $session, $cgi);
493 sub sessioncgi
($$) {
497 if ($cgi->param("do") eq "setup") {
498 showform
($cgi, $session);
503 sub formbuilder_setup
(@
) {
506 my $form=$params{form
};
507 if ($form->title eq "preferences" &&
508 IkiWiki
::is_admin
($params{session
}->param("name"))) {
509 push @
{$params{buttons
}}, "Setup";
510 if ($form->submitted && $form->submitted eq "Setup") {
511 showform
($params{cgi
}, $params{session
});