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 IkiWiki::Setup::dump($config{setupfile});
408 IkiWiki::saveindex();
409 IkiWiki::unlockwiki();
411 # Print the top part of a standard misctemplate,
412 # then show the rebuild or refresh.
414 my $html=IkiWiki::misctemplate("setup", $divider);
415 IkiWiki::printheader($session);
416 my ($head, $tail)=split($divider, $html, 2);
417 print $head."<pre>\n";
420 if ($form->submitted eq 'Rebuild Wiki') {
421 @command=("ikiwiki", "-setup", $config{setupfile},
425 @command=("ikiwiki", "-setup", $config{setupfile},
426 "-refresh", "-wrappers", "-v");
429 my $ret=system(@command);
432 print '<p class="error">'.
433 sprintf(gettext("<p class=\"error\">Error: %s exited nonzero (%s)"),
434 join(" ", @command), $ret).
443 IkiWiki::showform($form, $buttons, $session, $cgi);
446 sub sessioncgi ($$) {
450 if ($cgi->param("do") eq "setup") {
451 showform($cgi, $session);
456 sub formbuilder_setup (@) {
459 my $form=$params{form};
460 if ($form->title eq "preferences") {
461 push @{$params{buttons}}, "Wiki Setup";
462 if ($form->submitted && $form->submitted eq "Wiki Setup") {
463 showform($params{cgi}, $params{session});