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",
30 websetup_show_unsafe => {
33 description => "show unsafe settings, read-only, in web interface?",
40 if (! exists $config{websetup_show_unsafe}) {
41 $config{websetup_show_unsafe}=1;
45 sub formatexample ($$) {
49 if (defined $value && length $value) {
52 elsif (defined $example && ! ref $example && length $example) {
53 return "<br/ ><small>Example: <tt>$example</tt></small>";
60 sub showfields ($$$@) {
71 if ($key eq 'plugin') {
76 # skip internal settings
77 next if defined $info{type} && $info{type} eq "internal";
78 # XXX hashes not handled yet
79 next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
80 # maybe skip unsafe settings
81 next if ! $info{safe} && ! ($config{websetup_show_unsafe} && $config{websetup_advanced});
82 # maybe skip advanced settings
83 next if $info{advanced} && ! $config{websetup_advanced};
84 # these are handled specially, so don't show
85 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
87 push @show, $key, \%info;
90 my $section=defined $plugin
91 ? sprintf(gettext("%s plugin:"), $plugininfo{section}).$plugin
96 my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
97 (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
98 if ($plugin_forced && ! $enabled) {
99 # plugin is forced disabled, so skip its settings
103 my $section_fieldset;
104 if (defined $plugin) {
105 # Define the combined fieldset for the plugin's section.
106 # This ensures that this fieldset comes first.
107 $section_fieldset=sprintf(gettext("%s plugins"), $plugininfo{section});
108 $form->field(name => "placeholder.$plugininfo{section}",
110 fieldset => $section_fieldset);
114 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
115 my $name="enable.$plugin";
120 fieldset => $section,
121 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ]]
123 if (! $form->submitted) {
124 $form->field(name => $name, value => $enabled);
126 if ($plugin_forced) {
127 $form->field(name => $name, disabled => 1);
130 $enabledfields{$name}=[$name, \%plugininfo];
134 # show plugin settings
137 my %info=%{shift @show};
139 my $description=$info{description};
140 if (exists $info{link} && length $info{link}) {
141 if ($info{link} =~ /^\w+:\/\//) {
142 $description="<a href=\"$info{link}\">$description</a>";
145 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
149 # multiple plugins can have the same field
150 my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;
152 my $value=$config{$key};
153 if (! defined $value) {
157 if (ref $value eq 'ARRAY' || ref $info{example} eq 'ARRAY') {
158 $value=[(ref $value eq 'ARRAY' ? map { Encode::encode_utf8($_) } @{$value} : "")];
159 push @$value, "", "" if $info{safe}; # blank items for expansion
162 $value=Encode::encode_utf8($value);
165 if ($info{type} eq "string") {
168 label => $description,
169 comment => formatexample($info{example}, $value),
173 fieldset => $section,
176 elsif ($info{type} eq "pagespec") {
179 label => $description,
180 comment => formatexample($info{example}, $value),
184 validate => \&IkiWiki::pagespec_valid,
185 fieldset => $section,
188 elsif ($info{type} eq "integer") {
191 label => $description,
192 comment => formatexample($info{example}, $value),
196 validate => '/^[0-9]+$/',
197 fieldset => $section,
200 elsif ($info{type} eq "boolean") {
205 options => [ [ 1 => $description ] ],
206 fieldset => $section,
208 if (! $form->submitted) {
209 $form->field(name => $name, value => $value);
214 $form->field(name => $name, disabled => 1);
217 $enabledfields{$name}=[$key, \%info];
222 # if no fields were shown for the plugin, drop it into a combined
223 # fieldset for its section
224 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
226 $form->field(name => "enable.$plugin", fieldset => $section_fieldset);
229 return %enabledfields;
232 sub enable_plugin ($) {
235 $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
236 push @{$config{add_plugins}}, $plugin;
239 sub disable_plugin ($) {
242 if (grep { $_ eq $plugin } @{$config{add_plugins}}) {
243 $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
246 push @{$config{disable_plugins}}, $plugin;
254 if (! defined $session->param("name") ||
255 ! IkiWiki::is_admin($session->param("name"))) {
256 error(gettext("you are not logged in as an admin"));
259 if (! exists $config{setupfile}) {
260 error(gettext("setup file for this wiki is not known"));
263 eval q{use CGI::FormBuilder};
266 my $form = CGI::FormBuilder->new(
276 [main => gettext("main")],
278 action => $config{cgiurl},
279 template => {type => 'div'},
280 stylesheet => IkiWiki::baseurl()."style.css",
283 $form->field(name => "do", type => "hidden", value => "setup",
285 $form->field(name => "rebuild_asked", type => "hidden");
287 if ($form->submitted eq 'Basic Mode') {
288 $form->field(name => "showadvanced", type => "hidden",
289 value => 0, force => 1);
291 elsif ($form->submitted eq 'Advanced Mode') {
292 $form->field(name => "showadvanced", type => "hidden",
293 value => 1, force => 1);
296 if ($form->field("showadvanced")) {
297 $config{websetup_advanced}=1;
298 $advancedtoggle="Basic Mode";
301 $config{websetup_advanced}=0;
302 $advancedtoggle="Advanced Mode";
305 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
307 IkiWiki::decode_form_utf8($form);
308 IkiWiki::run_hooks(formbuilder_setup => sub {
309 shift->(form => $form, cgi => $cgi, session => $session,
310 buttons => $buttons);
313 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
315 # record all currently enabled plugins before all are loaded
316 my %enabled_plugins=%IkiWiki::loaded_plugins;
319 require IkiWiki::Setup;
320 foreach my $pair (IkiWiki::Setup::getsetup()) {
321 my $plugin=$pair->[0];
322 my $setup=$pair->[1];
324 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
326 $fields{$_}=$shown{$_} foreach keys %shown;
330 IkiWiki::decode_form_utf8($form);
332 if ($form->submitted eq "Cancel") {
333 IkiWiki::redirect($cgi, $config{url});
336 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
337 # Push values from form into %config, avoiding unnecessary
338 # changes, and keeping track of which changes need a
341 foreach my $field (keys %fields) {
342 my %info=%{$fields{$field}->[1]};
343 my $key=$fields{$field}->[0];
344 my @value=$form->field($field);
350 error("unsafe field $key"); # should never happen
353 if (exists $info{rebuild} &&
354 ($info{rebuild} || ! defined $info{rebuild})) {
355 $rebuild{$field}=$info{rebuild};
358 if ($field=~/^enable\.(.*)/) {
360 $value[0]=0 if ! length $value[0];
361 if ($value[0] != exists $enabled_plugins{$plugin}) {
363 enable_plugin($plugin);
366 disable_plugin($plugin);
371 delete $rebuild{$field};
376 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
377 @value=sort grep { length $_ } @value;
378 my @oldvalue=sort grep { length $_ }
379 (defined $config{$key} ? @{$config{$key}} : ());
380 my $same=(@oldvalue) == (@value);
381 for (my $x=0; $same && $x < @value; $x++) {
382 $same=0 if $value[$x] ne $oldvalue[$x];
385 delete $rebuild{$field};
388 $config{$key}=\@value;
391 elsif (ref $config{$key} || ref $info{example}) {
392 error("complex field $key"); # should never happen
395 if (defined $config{$key} && $config{$key} eq $value[0]) {
396 delete $rebuild{$field};
398 elsif (! defined $config{$key} && ! length $value[0]) {
399 delete $rebuild{$field};
401 elsif ((! defined $config{$key} || ! $config{$key}) &&
402 ! $value[0] && $info{type} eq "boolean") {
403 delete $rebuild{$field};
406 $config{$key}=$value[0];
411 if (%rebuild && ! $form->field("rebuild_asked")) {
413 foreach my $field ($form->field) {
414 $required=1 if $rebuild{$field};
415 next if exists $rebuild{$field};
416 $form->field(name => $field, type => "hidden");
419 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
420 $buttons=["Rebuild Wiki", "Cancel"];
423 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
424 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
426 $form->field(name => "rebuild_asked", value => 1, force => 1);
427 $form->reset(0); # doesn't really make sense here
430 my $oldsetup=readfile($config{setupfile});
431 IkiWiki::Setup::dump($config{setupfile});
433 IkiWiki::saveindex();
434 IkiWiki::unlockwiki();
436 # Print the top part of a standard misctemplate,
437 # then show the rebuild or refresh.
439 my $html=IkiWiki::misctemplate("setup", $divider);
440 IkiWiki::printheader($session);
441 my ($head, $tail)=split($divider, $html, 2);
442 print $head."<pre>\n";
445 if ($form->submitted eq 'Rebuild Wiki') {
446 @command=("ikiwiki", "-setup", $config{setupfile},
450 @command=("ikiwiki", "-setup", $config{setupfile},
451 "-refresh", "-wrappers", "-v");
455 open(STDERR, ">&STDOUT");
456 my $ret=system(@command);
459 print '<p class="error">'.
460 sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
461 join(" ", @command), $ret).
463 open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
473 IkiWiki::showform($form, $buttons, $session, $cgi);
476 sub sessioncgi ($$) {
480 if ($cgi->param("do") eq "setup") {
481 showform($cgi, $session);
486 sub formbuilder_setup (@) {
489 my $form=$params{form};
490 if ($form->title eq "preferences") {
491 push @{$params{buttons}}, "Wiki Setup";
492 if ($form->submitted && $form->submitted eq "Wiki Setup") {
493 showform($params{cgi}, $params{session});