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);
22 websetup_force_plugins => {
25 description => "list of plugins that cannot be enabled/disabled via the web interface",
29 websetup_show_unsafe => {
32 description => "show unsafe settings, read-only, in web interface?",
39 if (! exists $config{websetup_show_unsafe}) {
40 $config{websetup_show_unsafe}=1;
44 sub formatexample ($$) {
48 if (defined $value && length $value) {
51 elsif (defined $example && ! ref $example && length $example) {
52 return "<br/ ><small>Example: <tt>$example</tt></small>";
59 sub showfields ($$$@) {
70 if ($key eq 'plugin') {
75 # skip internal settings
76 next if defined $info{type} && $info{type} eq "internal";
77 # XXX hashes not handled yet
78 next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
79 # maybe skip unsafe settings
80 next if ! $info{safe} && ! ($config{websetup_show_unsafe} && $config{websetup_advanced});
81 # maybe skip advanced settings
82 next if $info{advanced} && ! $config{websetup_advanced};
83 # these are handled specially, so don't show
84 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
86 push @show, $key, \%info;
89 my $section=defined $plugin
90 ? sprintf(gettext("%s plugin:"), $plugininfo{section}).$plugin
95 my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
96 (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
97 if ($plugin_forced && ! $enabled) {
98 # plugin is forced disabled, so skip its settings
102 my $section_fieldset;
103 if (defined $plugin) {
104 # Define the combined fieldset for the plugin's section.
105 # This ensures that this fieldset comes first.
106 $section_fieldset=sprintf(gettext("%s plugins"), $plugininfo{section});
107 $form->field(name => "placeholder.$plugininfo{section}",
109 fieldset => $section_fieldset);
113 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
114 my $name="enable.$plugin";
119 fieldset => $section,
120 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ]]
122 if (! $form->submitted) {
123 $form->field(name => $name, value => $enabled);
125 if ($plugin_forced) {
126 $form->field(name => $name, disabled => 1);
129 $enabledfields{$name}=[$name, \%plugininfo];
133 # show plugin settings
136 my %info=%{shift @show};
138 my $description=$info{description};
139 if (exists $info{link} && length $info{link}) {
140 if ($info{link} =~ /^\w+:\/\//) {
141 $description="<a href=\"$info{link}\">$description</a>";
144 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
148 # multiple plugins can have the same field
149 my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;
151 my $value=$config{$key};
152 if (! defined $value) {
156 if (ref $value eq 'ARRAY' || ref $info{example} eq 'ARRAY') {
157 $value=[(ref $value eq 'ARRAY' ? map { Encode::encode_utf8($_) } @{$value} : "")];
158 push @$value, "", "" if $info{safe}; # blank items for expansion
161 $value=Encode::encode_utf8($value);
164 if ($info{type} eq "string") {
167 label => $description,
168 comment => formatexample($info{example}, $value),
172 fieldset => $section,
175 elsif ($info{type} eq "pagespec") {
178 label => $description,
179 comment => formatexample($info{example}, $value),
183 validate => \&IkiWiki::pagespec_valid,
184 fieldset => $section,
187 elsif ($info{type} eq "integer") {
190 label => $description,
191 comment => formatexample($info{example}, $value),
195 validate => '/^[0-9]+$/',
196 fieldset => $section,
199 elsif ($info{type} eq "boolean") {
204 options => [ [ 1 => $description ] ],
205 fieldset => $section,
207 if (! $form->submitted) {
208 $form->field(name => $name, value => $value);
213 $form->field(name => $name, disabled => 1);
216 $enabledfields{$name}=[$key, \%info];
221 # if no fields were shown for the plugin, drop it into a combined
222 # fieldset for its section
223 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
225 $form->field(name => "enable.$plugin", fieldset => $section_fieldset);
228 return %enabledfields;
231 sub enable_plugin ($) {
234 $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
235 push @{$config{add_plugins}}, $plugin;
238 sub disable_plugin ($) {
241 if (grep { $_ eq $plugin } @{$config{add_plugins}}) {
242 $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
245 push @{$config{disable_plugins}}, $plugin;
253 if (! defined $session->param("name") ||
254 ! IkiWiki::is_admin($session->param("name"))) {
255 error(gettext("you are not logged in as an admin"));
258 if (! exists $config{setupfile}) {
259 error(gettext("setup file for this wiki is not known"));
262 eval q{use CGI::FormBuilder};
265 my $form = CGI::FormBuilder->new(
275 [main => gettext("main")],
277 action => $config{cgiurl},
278 template => {type => 'div'},
279 stylesheet => IkiWiki::baseurl()."style.css",
282 $form->field(name => "do", type => "hidden", value => "setup",
284 $form->field(name => "rebuild_asked", type => "hidden");
286 if ($form->submitted eq 'Basic Mode') {
287 $form->field(name => "showadvanced", type => "hidden",
288 value => 0, force => 1);
290 elsif ($form->submitted eq 'Advanced Mode') {
291 $form->field(name => "showadvanced", type => "hidden",
292 value => 1, force => 1);
295 if ($form->field("showadvanced")) {
296 $config{websetup_advanced}=1;
297 $advancedtoggle="Basic Mode";
300 $config{websetup_advanced}=0;
301 $advancedtoggle="Advanced Mode";
304 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
306 IkiWiki::decode_form_utf8($form);
307 IkiWiki::run_hooks(formbuilder_setup => sub {
308 shift->(form => $form, cgi => $cgi, session => $session,
309 buttons => $buttons);
312 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
314 # record all currently enabled plugins before all are loaded
315 my %enabled_plugins=%IkiWiki::loaded_plugins;
318 require IkiWiki::Setup;
319 foreach my $pair (IkiWiki::Setup::getsetup()) {
320 my $plugin=$pair->[0];
321 my $setup=$pair->[1];
323 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
325 $fields{$_}=$shown{$_} foreach keys %shown;
329 IkiWiki::decode_form_utf8($form);
331 if ($form->submitted eq "Cancel") {
332 IkiWiki::redirect($cgi, $config{url});
335 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
336 # Push values from form into %config, avoiding unnecessary
337 # changes, and keeping track of which changes need a
340 foreach my $field (keys %fields) {
341 my %info=%{$fields{$field}->[1]};
342 my $key=$fields{$field}->[0];
343 my @value=$form->field($field);
349 error("unsafe field $key"); # should never happen
352 if (exists $info{rebuild} &&
353 ($info{rebuild} || ! defined $info{rebuild})) {
354 $rebuild{$field}=$info{rebuild};
357 if ($field=~/^enable\.(.*)/) {
359 $value[0]=0 if ! length $value[0];
360 if ($value[0] != exists $enabled_plugins{$plugin}) {
362 enable_plugin($plugin);
365 disable_plugin($plugin);
370 delete $rebuild{$field};
375 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
376 @value=sort grep { length $_ } @value;
377 my @oldvalue=sort grep { length $_ }
378 (defined $config{$key} ? @{$config{$key}} : ());
379 my $same=(@oldvalue) == (@value);
380 for (my $x=0; $same && $x < @value; $x++) {
381 $same=0 if $value[$x] ne $oldvalue[$x];
384 delete $rebuild{$field};
387 $config{$key}=\@value;
390 elsif (ref $config{$key} || ref $info{example}) {
391 error("complex field $key"); # should never happen
394 if (defined $config{$key} && $config{$key} eq $value[0]) {
395 delete $rebuild{$field};
397 elsif (! defined $config{$key} && ! length $value[0]) {
398 delete $rebuild{$field};
400 elsif ((! defined $config{$key} || ! $config{$key}) &&
401 ! $value[0] && $info{type} eq "boolean") {
402 delete $rebuild{$field};
405 $config{$key}=$value[0];
410 if (%rebuild && ! $form->field("rebuild_asked")) {
412 foreach my $field ($form->field) {
413 $required=1 if $rebuild{$field};
414 next if exists $rebuild{$field};
415 $form->field(name => $field, type => "hidden");
418 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
419 $buttons=["Rebuild Wiki", "Cancel"];
422 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
423 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
425 $form->field(name => "rebuild_asked", value => 1, force => 1);
426 $form->reset(0); # doesn't really make sense here
429 my $oldsetup=readfile($config{setupfile});
430 IkiWiki::Setup::dump($config{setupfile});
432 IkiWiki::saveindex();
433 IkiWiki::unlockwiki();
435 # Print the top part of a standard misctemplate,
436 # then show the rebuild or refresh.
438 my $html=IkiWiki::misctemplate("setup", $divider);
439 IkiWiki::printheader($session);
440 my ($head, $tail)=split($divider, $html, 2);
441 print $head."<pre>\n";
444 if ($form->submitted eq 'Rebuild Wiki') {
445 @command=("ikiwiki", "-setup", $config{setupfile},
449 @command=("ikiwiki", "-setup", $config{setupfile},
450 "-refresh", "-wrappers", "-v");
454 open(STDERR, ">&STDOUT");
455 my $ret=system(@command);
458 print '<p class="error">'.
459 sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
460 join(" ", @command), $ret).
462 open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
472 IkiWiki::showform($form, $buttons, $session, $cgi);
475 sub sessioncgi ($$) {
479 if ($cgi->param("do") eq "setup") {
480 showform($cgi, $session);
485 sub formbuilder_setup (@) {
488 my $form=$params{form};
489 if ($form->title eq "preferences") {
490 push @{$params{buttons}}, "Wiki Setup";
491 if ($form->submitted && $form->submitted eq "Wiki Setup") {
492 showform($params{cgi}, $params{session});