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' ? @{$value} : ""), "", ""]; # blank items for expansion
145 if ($info{type} eq "string") {
148 label => $description,
149 comment => formatexample($info{example}, $value),
153 fieldset => $section,
156 elsif ($info{type} eq "pagespec") {
159 label => $description,
160 comment => formatexample($info{example}, $value),
164 validate => \&IkiWiki::pagespec_valid,
165 fieldset => $section,
168 elsif ($info{type} eq "integer") {
171 label => $description,
172 comment => formatexample($info{example}, $value),
176 validate => '/^[0-9]+$/',
177 fieldset => $section,
180 elsif ($info{type} eq "boolean") {
185 options => [ [ 1 => $description ] ],
186 fieldset => $section,
188 if (! $form->submitted) {
189 $form->field(name => $name, value => $value);
194 $form->field(name => $name, disabled => 1);
197 $enabledfields{$name}=[$key, \%info];
202 # if no fields were shown for the plugin, drop it into the
204 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
206 $form->field(name => "enable.$plugin", fieldset => "plugins");
209 return %enabledfields;
212 sub enable_plugin ($) {
215 $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
216 push @{$config{add_plugins}}, $plugin;
219 sub disable_plugin ($) {
222 if (grep { $_ eq $plugin } @{$config{add_plugins}}) {
223 $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
226 push @{$config{disable_plugins}}, $plugin;
234 if (! defined $session->param("name") ||
235 ! IkiWiki::is_admin($session->param("name"))) {
236 error(gettext("you are not logged in as an admin"));
239 if (! exists $config{setupfile}) {
240 error(gettext("setup file for this wiki is not known"));
243 eval q{use CGI::FormBuilder};
246 my $form = CGI::FormBuilder->new(
256 [main => gettext("main")],
257 [plugins => gettext("plugins")]
259 action => $config{cgiurl},
260 template => {type => 'div'},
261 stylesheet => IkiWiki::baseurl()."style.css",
264 $form->field(name => "do", type => "hidden", value => "setup",
266 $form->field(name => "rebuild_asked", type => "hidden");
268 if ($form->submitted eq 'Basic Mode') {
269 $form->field(name => "showadvanced", type => "hidden",
270 value => 0, force => 1);
272 elsif ($form->submitted eq 'Advanced Mode') {
273 $form->field(name => "showadvanced", type => "hidden",
274 value => 1, force => 1);
277 if ($form->field("showadvanced")) {
278 $config{websetup_advanced}=1;
279 $advancedtoggle="Basic Mode";
282 $config{websetup_advanced}=0;
283 $advancedtoggle="Advanced Mode";
286 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
288 IkiWiki::decode_form_utf8($form);
289 IkiWiki::run_hooks(formbuilder_setup => sub {
290 shift->(form => $form, cgi => $cgi, session => $session,
291 buttons => $buttons);
293 IkiWiki::decode_form_utf8($form);
295 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
297 # record all currently enabled plugins before all are loaded
298 my %enabled_plugins=%IkiWiki::loaded_plugins;
301 require IkiWiki::Setup;
302 foreach my $pair (IkiWiki::Setup::getsetup()) {
303 my $plugin=$pair->[0];
304 my $setup=$pair->[1];
306 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
308 $fields{$_}=$shown{$_} foreach keys %shown;
312 if ($form->submitted eq "Cancel") {
313 IkiWiki::redirect($cgi, $config{url});
316 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
317 # Push values from form into %config, avoiding unnecessary
318 # changes, and keeping track of which changes need a
321 foreach my $field (keys %fields) {
322 my %info=%{$fields{$field}->[1]};
323 my $key=$fields{$field}->[0];
324 my @value=$form->field($field);
330 error("unsafe field $key"); # should never happen
333 if (exists $info{rebuild} &&
334 ($info{rebuild} || ! defined $info{rebuild})) {
335 $rebuild{$field}=$info{rebuild};
338 if ($field=~/^enable\.(.*)/) {
340 $value[0]=0 if ! length $value[0];
341 if ($value[0] != exists $enabled_plugins{$plugin}) {
343 enable_plugin($plugin);
346 disable_plugin($plugin);
351 delete $rebuild{$field};
356 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
357 @value=sort grep { length $_ } @value;
358 my @oldvalue=sort grep { length $_ }
359 (defined $config{$key} ? @{$config{$key}} : ());
360 if ((@oldvalue) == (@value)) {
361 delete $rebuild{$field};
364 $config{$key}=\@value;
367 elsif (ref $config{$key} || ref $info{example}) {
368 error("complex field $key"); # should never happen
371 if (defined $config{$key} && $config{$key} eq $value[0]) {
372 delete $rebuild{$field};
374 elsif (! defined $config{$key} && ! length $value[0]) {
375 delete $rebuild{$field};
377 elsif ((! defined $config{$key} || ! $config{$key}) &&
378 ! $value[0] && $info{type} eq "boolean") {
379 delete $rebuild{$field};
382 $config{$key}=$value[0];
387 if (%rebuild && ! $form->field("rebuild_asked")) {
389 foreach my $field ($form->field) {
390 $required=1 if $rebuild{$field};
391 next if exists $rebuild{$field};
392 $form->field(name => $field, type => "hidden");
395 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
396 $buttons=["Rebuild Wiki", "Cancel"];
399 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
400 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
402 $form->field(name => "rebuild_asked", value => 1, force => 1);
403 $form->reset(0); # doesn't really make sense here
406 my $oldsetup=readfile($config{setupfile});
407 IkiWiki::Setup::dump($config{setupfile});
409 IkiWiki::saveindex();
410 IkiWiki::unlockwiki();
412 # Print the top part of a standard misctemplate,
413 # then show the rebuild or refresh.
415 my $html=IkiWiki::misctemplate("setup", $divider);
416 IkiWiki::printheader($session);
417 my ($head, $tail)=split($divider, $html, 2);
418 print $head."<pre>\n";
421 if ($form->submitted eq 'Rebuild Wiki') {
422 @command=("ikiwiki", "-setup", $config{setupfile},
426 @command=("ikiwiki", "-setup", $config{setupfile},
427 "-refresh", "-wrappers", "-v");
431 open(STDERR, ">&STDOUT");
432 my $ret=system(@command);
435 print '<p class="error">'.
436 sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
437 join(" ", @command), $ret).
439 open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
449 IkiWiki::showform($form, $buttons, $session, $cgi);
452 sub sessioncgi ($$) {
456 if ($cgi->param("do") eq "setup") {
457 showform($cgi, $session);
462 sub formbuilder_setup (@) {
465 my $form=$params{form};
466 if ($form->title eq "preferences") {
467 push @{$params{buttons}}, "Wiki Setup";
468 if ($form->submitted && $form->submitted eq "Wiki Setup") {
469 showform($params{cgi}, $params{session});