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 # skip internal settings
71 next if defined $info{type} && $info{type} eq "internal";
72 # XXX hashes not handled yet
73 next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
74 # maybe skip unsafe settings
75 next if ! $info{safe} && ! ($config{websetup_show_unsafe} && $config{websetup_advanced});
76 # maybe skip advanced settings
77 next if $info{advanced} && ! $config{websetup_advanced};
78 # these are handled specially, so don't show
79 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
81 if ($key eq 'plugin') {
86 push @show, $key, \%info;
89 my $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
93 my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
94 (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
95 if ($plugin_forced && ! $enabled) {
96 # plugin is forced disabled, so skip its settings
101 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
102 my $name="enable.$plugin";
107 fieldset => $section,
108 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ]]
110 if (! $form->submitted) {
111 $form->field(name => $name, value => $enabled);
113 if ($plugin_forced) {
114 $form->field(name => $name, disabled => 1);
117 $enabledfields{$name}=[$name, \%plugininfo];
121 # show plugin settings
124 my %info=%{shift @show};
126 my $description=$info{description};
127 if (exists $info{link} && length $info{link}) {
128 if ($info{link} =~ /^\w+:\/\//) {
129 $description="<a href=\"$info{link}\">$description</a>";
132 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
136 # multiple plugins can have the same field
137 my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;
139 my $value=$config{$key};
141 if ($info{safe} && (ref $value eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
142 $value=[(ref $value eq 'ARRAY' ? map { Encode::encode_utf8($_) } @{$value} : ""),
143 "", ""]; # blank items for expansion
146 $value=Encode::encode_utf8($value);
149 if ($info{type} eq "string") {
152 label => $description,
153 comment => formatexample($info{example}, $value),
157 fieldset => $section,
160 elsif ($info{type} eq "pagespec") {
163 label => $description,
164 comment => formatexample($info{example}, $value),
168 validate => \&IkiWiki::pagespec_valid,
169 fieldset => $section,
172 elsif ($info{type} eq "integer") {
175 label => $description,
176 comment => formatexample($info{example}, $value),
180 validate => '/^[0-9]+$/',
181 fieldset => $section,
184 elsif ($info{type} eq "boolean") {
189 options => [ [ 1 => $description ] ],
190 fieldset => $section,
192 if (! $form->submitted) {
193 $form->field(name => $name, value => $value);
198 $form->field(name => $name, disabled => 1);
201 $enabledfields{$name}=[$key, \%info];
206 # if no fields were shown for the plugin, drop it into the
208 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
210 $form->field(name => "enable.$plugin", fieldset => "plugins");
213 return %enabledfields;
216 sub enable_plugin ($) {
219 $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
220 push @{$config{add_plugins}}, $plugin;
223 sub disable_plugin ($) {
226 if (grep { $_ eq $plugin } @{$config{add_plugins}}) {
227 $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
230 push @{$config{disable_plugins}}, $plugin;
238 if (! defined $session->param("name") ||
239 ! IkiWiki::is_admin($session->param("name"))) {
240 error(gettext("you are not logged in as an admin"));
243 if (! exists $config{setupfile}) {
244 error(gettext("setup file for this wiki is not known"));
247 eval q{use CGI::FormBuilder};
250 my $form = CGI::FormBuilder->new(
260 [main => gettext("main")],
261 [plugins => gettext("plugins")]
263 action => $config{cgiurl},
264 template => {type => 'div'},
265 stylesheet => IkiWiki::baseurl()."style.css",
268 $form->field(name => "do", type => "hidden", value => "setup",
270 $form->field(name => "rebuild_asked", type => "hidden");
272 if ($form->submitted eq 'Basic Mode') {
273 $form->field(name => "showadvanced", type => "hidden",
274 value => 0, force => 1);
276 elsif ($form->submitted eq 'Advanced Mode') {
277 $form->field(name => "showadvanced", type => "hidden",
278 value => 1, force => 1);
281 if ($form->field("showadvanced")) {
282 $config{websetup_advanced}=1;
283 $advancedtoggle="Basic Mode";
286 $config{websetup_advanced}=0;
287 $advancedtoggle="Advanced Mode";
290 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
292 IkiWiki::decode_form_utf8($form);
293 IkiWiki::run_hooks(formbuilder_setup => sub {
294 shift->(form => $form, cgi => $cgi, session => $session,
295 buttons => $buttons);
298 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
300 # record all currently enabled plugins before all are loaded
301 my %enabled_plugins=%IkiWiki::loaded_plugins;
304 require IkiWiki::Setup;
305 foreach my $pair (IkiWiki::Setup::getsetup()) {
306 my $plugin=$pair->[0];
307 my $setup=$pair->[1];
309 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
311 $fields{$_}=$shown{$_} foreach keys %shown;
315 IkiWiki::decode_form_utf8($form);
317 if ($form->submitted eq "Cancel") {
318 IkiWiki::redirect($cgi, $config{url});
321 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
322 # Push values from form into %config, avoiding unnecessary
323 # changes, and keeping track of which changes need a
326 foreach my $field (keys %fields) {
327 my %info=%{$fields{$field}->[1]};
328 my $key=$fields{$field}->[0];
329 my @value=$form->field($field);
335 error("unsafe field $key"); # should never happen
338 if (exists $info{rebuild} &&
339 ($info{rebuild} || ! defined $info{rebuild})) {
340 $rebuild{$field}=$info{rebuild};
343 if ($field=~/^enable\.(.*)/) {
345 $value[0]=0 if ! length $value[0];
346 if ($value[0] != exists $enabled_plugins{$plugin}) {
348 enable_plugin($plugin);
351 disable_plugin($plugin);
356 delete $rebuild{$field};
361 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
362 @value=sort grep { length $_ } @value;
363 my @oldvalue=sort grep { length $_ }
364 (defined $config{$key} ? @{$config{$key}} : ());
365 my $same=(@oldvalue) == (@value);
366 for (my $x=0; $same && $x < @value; $x++) {
367 $same=0 if $value[$x] ne $oldvalue[$x];
370 delete $rebuild{$field};
373 $config{$key}=\@value;
376 elsif (ref $config{$key} || ref $info{example}) {
377 error("complex field $key"); # should never happen
380 if (defined $config{$key} && $config{$key} eq $value[0]) {
381 delete $rebuild{$field};
383 elsif (! defined $config{$key} && ! length $value[0]) {
384 delete $rebuild{$field};
386 elsif ((! defined $config{$key} || ! $config{$key}) &&
387 ! $value[0] && $info{type} eq "boolean") {
388 delete $rebuild{$field};
391 $config{$key}=$value[0];
396 if (%rebuild && ! $form->field("rebuild_asked")) {
398 foreach my $field ($form->field) {
399 $required=1 if $rebuild{$field};
400 next if exists $rebuild{$field};
401 $form->field(name => $field, type => "hidden");
404 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
405 $buttons=["Rebuild Wiki", "Cancel"];
408 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
409 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
411 $form->field(name => "rebuild_asked", value => 1, force => 1);
412 $form->reset(0); # doesn't really make sense here
415 my $oldsetup=readfile($config{setupfile});
416 IkiWiki::Setup::dump($config{setupfile});
418 IkiWiki::saveindex();
419 IkiWiki::unlockwiki();
421 # Print the top part of a standard misctemplate,
422 # then show the rebuild or refresh.
424 my $html=IkiWiki::misctemplate("setup", $divider);
425 IkiWiki::printheader($session);
426 my ($head, $tail)=split($divider, $html, 2);
427 print $head."<pre>\n";
430 if ($form->submitted eq 'Rebuild Wiki') {
431 @command=("ikiwiki", "-setup", $config{setupfile},
435 @command=("ikiwiki", "-setup", $config{setupfile},
436 "-refresh", "-wrappers", "-v");
440 open(STDERR, ">&STDOUT");
441 my $ret=system(@command);
444 print '<p class="error">'.
445 sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
446 join(" ", @command), $ret).
448 open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
458 IkiWiki::showform($form, $buttons, $session, $cgi);
461 sub sessioncgi ($$) {
465 if ($cgi->param("do") eq "setup") {
466 showform($cgi, $session);
471 sub formbuilder_setup (@) {
474 my $form=$params{form};
475 if ($form->title eq "preferences") {
476 push @{$params{buttons}}, "Wiki Setup";
477 if ($form->submitted && $form->submitted eq "Wiki Setup") {
478 showform($params{cgi}, $params{session});