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{link} && length $info{link}) {
155 if ($info{link} =~ /^\w+:\/\//) {
156 $description="<a href=\"$info{link}\">$description</a>";
159 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
163 # multiple plugins can have the same field
164 my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;
166 my $value=$config{$key};
167 if (! defined $value) {
171 if (ref $value eq 'ARRAY' || ref $info{example} eq 'ARRAY') {
172 $value=[(ref $value eq 'ARRAY' ? map { Encode::encode_utf8($_) } @{$value} : "")];
173 push @$value, "", "" if $info{safe} && issafe($key); # blank items for expansion
176 $value=Encode::encode_utf8($value);
179 if ($info{type} eq "string") {
182 label => $description,
183 comment => formatexample($info{example}, $value),
187 fieldset => $section,
190 elsif ($info{type} eq "pagespec") {
193 label => $description,
194 comment => formatexample($info{example}, $value),
198 validate => \&IkiWiki::pagespec_valid,
199 fieldset => $section,
202 elsif ($info{type} eq "integer") {
205 label => $description,
206 comment => formatexample($info{example}, $value),
210 validate => '/^[0-9]+$/',
211 fieldset => $section,
214 elsif ($info{type} eq "boolean") {
219 options => [ [ 1 => $description ] ],
220 fieldset => $section,
222 if (! $form->submitted) {
223 $form->field(name => $name, value => $value);
227 if (! $info{safe} || ! issafe($key)) {
228 $form->field(name => $name, disabled => 1);
231 $enabledfields{$name}=[$key, \%info];
236 # if no fields were shown for the plugin, drop it into a combined
237 # fieldset for its section
238 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
240 $form->field(name => "enable.$plugin", fieldset => $section_fieldset);
243 return %enabledfields;
246 sub enable_plugin ($) {
249 $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
250 push @{$config{add_plugins}}, $plugin;
253 sub disable_plugin ($) {
256 $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
257 push @{$config{disable_plugins}}, $plugin;
264 IkiWiki::needsignin($cgi, $session);
266 if (! defined $session->param("name") ||
267 ! IkiWiki::is_admin($session->param("name"))) {
268 error(gettext("you are not logged in as an admin"));
271 if (! exists $config{setupfile}) {
272 error(gettext("setup file for this wiki is not known"));
275 eval q{use CGI::FormBuilder};
278 my $form = CGI::FormBuilder->new(
288 [main => gettext("main")],
290 action => $config{cgiurl},
291 template => {type => 'div'},
295 $form->field(name => "do", type => "hidden", value => "setup",
297 $form->field(name => "rebuild_asked", type => "hidden");
298 $form->field(name => "showadvanced", type => "hidden");
300 if ($form->submitted eq 'Basic Mode') {
301 $form->field(name => "showadvanced", type => "hidden",
302 value => 0, force => 1);
304 elsif ($form->submitted eq 'Advanced Mode') {
305 $form->field(name => "showadvanced", type => "hidden",
306 value => 1, force => 1);
309 if ($form->field("showadvanced")) {
310 $config{websetup_advanced}=1;
311 $advancedtoggle="Basic Mode";
314 $config{websetup_advanced}=0;
315 $advancedtoggle="Advanced Mode";
318 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
320 IkiWiki::decode_form_utf8($form);
321 IkiWiki::run_hooks(formbuilder_setup => sub {
322 shift->(form => $form, cgi => $cgi, session => $session,
323 buttons => $buttons);
326 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
328 # record all currently enabled plugins before all are loaded
329 my %enabled_plugins=%IkiWiki::loaded_plugins;
332 require IkiWiki::Setup;
333 foreach my $pair (IkiWiki::Setup::getsetup()) {
334 my $plugin=$pair->[0];
335 my $setup=$pair->[1];
337 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
339 $fields{$_}=$shown{$_} foreach keys %shown;
343 IkiWiki::decode_form_utf8($form);
345 if ($form->submitted eq "Cancel") {
346 IkiWiki::redirect($cgi, $config{url});
349 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
350 # Push values from form into %config, avoiding unnecessary
351 # changes, and keeping track of which changes need a
354 foreach my $field (keys %fields) {
355 my %info=%{$fields{$field}->[1]};
356 my $key=$fields{$field}->[0];
357 my @value=$form->field($field);
362 if (! $info{safe} || ! issafe($key)) {
363 error("unsafe field $key"); # should never happen
366 if (exists $info{rebuild} &&
367 ($info{rebuild} || ! defined $info{rebuild})) {
368 $rebuild{$field}=$info{rebuild};
371 if ($field=~/^enable\.(.*)/) {
373 $value[0]=0 if ! length $value[0];
374 if ($value[0] != exists $enabled_plugins{$plugin}) {
376 enable_plugin($plugin);
379 disable_plugin($plugin);
384 delete $rebuild{$field};
389 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
390 @value=sort grep { length $_ } @value;
391 my @oldvalue=sort grep { length $_ }
392 (defined $config{$key} ? @{$config{$key}} : ());
393 my $same=(@oldvalue) == (@value);
394 for (my $x=0; $same && $x < @value; $x++) {
395 $same=0 if $value[$x] ne $oldvalue[$x];
398 delete $rebuild{$field};
401 $config{$key}=\@value;
404 elsif (ref $config{$key} || ref $info{example}) {
405 error("complex field $key"); # should never happen
408 if (defined $config{$key} && $config{$key} eq $value[0]) {
409 delete $rebuild{$field};
411 elsif (! defined $config{$key} && ! length $value[0]) {
412 delete $rebuild{$field};
414 elsif ((! defined $config{$key} || ! $config{$key}) &&
415 ! $value[0] && $info{type} eq "boolean") {
416 delete $rebuild{$field};
419 $config{$key}=$value[0];
424 if (%rebuild && ! $form->field("rebuild_asked")) {
426 foreach my $field ($form->field) {
427 $required=1 if $rebuild{$field};
428 next if exists $rebuild{$field};
429 $form->field(name => $field, type => "hidden");
432 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
433 $buttons=["Rebuild Wiki", "Cancel"];
436 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
437 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
439 $form->field(name => "rebuild_asked", value => 1, force => 1);
440 $form->reset(0); # doesn't really make sense here
443 my $oldsetup=readfile($config{setupfile});
444 IkiWiki::Setup::dump($config{setupfile});
446 IkiWiki::saveindex();
447 IkiWiki::unlockwiki();
449 # Print the top part of a standard misctemplate,
450 # then show the rebuild or refresh, live.
452 my $html=IkiWiki::misctemplate("setup", $divider);
453 IkiWiki::printheader($session);
454 my ($head, $tail)=split($divider, $html, 2);
455 print $head."<pre>\n";
458 if ($form->submitted eq 'Rebuild Wiki') {
459 @command=("ikiwiki", "-setup", $config{setupfile},
463 @command=("ikiwiki", "-setup", $config{setupfile},
464 "-refresh", "-wrappers", "-v");
468 open(STDERR, ">&STDOUT");
469 my $ret=system(@command);
472 print '<p class="error">'.
473 sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
474 join(" ", @command), $ret).
476 open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
486 IkiWiki::showform($form, $buttons, $session, $cgi);
489 sub sessioncgi ($$) {
493 if ($cgi->param("do") eq "setup") {
494 showform($cgi, $session);
499 sub formbuilder_setup (@) {
502 my $form=$params{form};
503 if ($form->title eq "preferences" &&
504 IkiWiki::is_admin($params{session}->param("name"))) {
505 push @{$params{buttons}}, "Setup";
506 if ($form->submitted && $form->submitted eq "Setup") {
507 showform($params{cgi}, $params{session});