simplify formbuilder stylesheet specification
[ikiwiki.git] / IkiWiki / Plugin / websetup.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::websetup;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 sub import {
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);
14 }
15
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                         section => "web",
22                 },
23                 websetup_force_plugins => {
24                         type => "string",
25                         example => [],
26                         description => "list of plugins that cannot be enabled/disabled via the web interface",
27                         safe => 0,
28                         rebuild => 0,
29                 },
30                 websetup_unsafe => {
31                         type => "string",
32                         example => [],
33                         description => "list of additional setup field keys to treat as unsafe",
34                         safe => 0,
35                         rebuild => 0,
36                 },
37                 websetup_show_unsafe => {
38                         type => "boolean",
39                         example => 1,
40                         description => "show unsafe settings, read-only, in web interface?",
41                         safe => 0,
42                         rebuild => 0,
43                 },
44 }
45
46 sub checkconfig () {
47         if (! exists $config{websetup_show_unsafe}) {
48                 $config{websetup_show_unsafe}=1;
49         }
50 }
51
52 sub formatexample ($$) {
53         my $example=shift;
54         my $value=shift;
55
56         if (defined $value && length $value) {
57                 return "";
58         }
59         elsif (defined $example && ! ref $example && length $example) {
60                 return "<br/ ><small>Example: <tt>$example</tt></small>";
61         }
62         else {
63                 return "";
64         }
65 }
66
67 sub issafe ($) {
68         my $key=shift;
69
70         return ! grep { $_ eq $key } @{$config{websetup_unsafe}};
71 }
72
73 sub showfields ($$$@) {
74         my $form=shift;
75         my $plugin=shift;
76         my $enabled=shift;
77
78         my @show;
79         my %plugininfo;
80         while (@_) {
81                 my $key=shift;
82                 my %info=%{shift()};
83                 
84                 if ($key eq 'plugin') {
85                         %plugininfo=%info;
86                         next;
87                 }
88
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';
100
101                 push @show, $key, \%info;
102         }
103
104         my $section=defined $plugin
105                 ? sprintf(gettext("%s plugin:"), $plugininfo{section})." ".$plugin
106                 : "main";
107         my %enabledfields;
108         my $shownfields=0;
109         
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
114                 @show=();
115         }
116
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}",
123                         type => "hidden",
124                         fieldset => $section_fieldset);
125         }
126
127         # show plugin toggle
128         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
129                 my $name="enable.$plugin";
130                 $form->field(
131                         name => $name,
132                         label => "",
133                         type => "checkbox",
134                         fieldset => $section,
135                         options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ]]
136                 );
137                 if (! $form->submitted) {
138                         $form->field(name => $name, value => $enabled);
139                 }
140                 if ($plugin_forced) {
141                         $form->field(name => $name, disabled => 1);
142                 }
143                 else {
144                         $enabledfields{$name}=[$name, \%plugininfo];
145                 }
146         }
147
148         # show plugin settings
149         while (@show) {
150                 my $key=shift @show;
151                 my %info=%{shift @show};
152
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>";
157                         }
158                         else {
159                                 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
160                         }
161                 }
162
163                 # multiple plugins can have the same field
164                 my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;
165
166                 my $value=$config{$key};
167                 if (! defined $value) {
168                         $value="";
169                 }
170
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
174                 }
175                 else {
176                         $value=Encode::encode_utf8($value);
177                 }
178
179                 if ($info{type} eq "string") {
180                         $form->field(
181                                 name => $name,
182                                 label => $description,
183                                 comment => formatexample($info{example}, $value),
184                                 type => "text",
185                                 value => $value,
186                                 size => 60,
187                                 fieldset => $section,
188                         );
189                 }
190                 elsif ($info{type} eq "pagespec") {
191                         $form->field(
192                                 name => $name,
193                                 label => $description,
194                                 comment => formatexample($info{example}, $value),
195                                 type => "text",
196                                 value => $value,
197                                 size => 60,
198                                 validate => \&IkiWiki::pagespec_valid,
199                                 fieldset => $section,
200                         );
201                 }
202                 elsif ($info{type} eq "integer") {
203                         $form->field(
204                                 name => $name,
205                                 label => $description,
206                                 comment => formatexample($info{example}, $value),
207                                 type => "text",
208                                 value => $value,
209                                 size => 5,
210                                 validate => '/^[0-9]+$/',
211                                 fieldset => $section,
212                         );
213                 }
214                 elsif ($info{type} eq "boolean") {
215                         $form->field(
216                                 name => $name,
217                                 label => "",
218                                 type => "checkbox",
219                                 options => [ [ 1 => $description ] ],
220                                 fieldset => $section,
221                         );
222                         if (! $form->submitted) {
223                                 $form->field(name => $name, value => $value);
224                         }
225                 }
226                 
227                 if (! $info{safe} || ! issafe($key)) {
228                         $form->field(name => $name, disabled => 1);
229                 }
230                 else {
231                         $enabledfields{$name}=[$key, \%info];
232                 }
233                 $shownfields++;
234         }
235         
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}) &&
239             ! $shownfields) {
240                 $form->field(name => "enable.$plugin", fieldset => $section_fieldset);
241         }
242
243         return %enabledfields;
244 }
245
246 sub enable_plugin ($) {
247         my $plugin=shift;
248
249         $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
250         push @{$config{add_plugins}}, $plugin;
251 }
252
253 sub disable_plugin ($) {
254         my $plugin=shift;
255
256         if (grep { $_ eq $plugin } @{$config{add_plugins}}) {
257                 $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
258         }
259         else {
260                 push @{$config{disable_plugins}}, $plugin;
261         }
262 }
263
264 sub showform ($$) {
265         my $cgi=shift;
266         my $session=shift;
267
268         IkiWiki::needsignin($cgi, $session);
269
270         if (! defined $session->param("name") || 
271             ! IkiWiki::is_admin($session->param("name"))) {
272                 error(gettext("you are not logged in as an admin"));
273         }
274
275         if (! exists $config{setupfile}) {
276                 error(gettext("setup file for this wiki is not known"));
277         }
278
279         eval q{use CGI::FormBuilder};
280         error($@) if $@;
281
282         my $form = CGI::FormBuilder->new(
283                 title => "setup",
284                 name => "setup",
285                 header => 0,
286                 charset => "utf-8",
287                 method => 'POST',
288                 javascript => 0,
289                 reset => 1,
290                 params => $cgi,
291                 fieldsets => [
292                         [main => gettext("main")], 
293                 ],
294                 action => $config{cgiurl},
295                 template => {type => 'div'},
296                 stylesheet => 1,
297         );
298         
299         $form->field(name => "do", type => "hidden", value => "setup",
300                 force => 1);
301         $form->field(name => "rebuild_asked", type => "hidden");
302
303         if ($form->submitted eq 'Basic Mode') {
304                 $form->field(name => "showadvanced", type => "hidden", 
305                         value => 0, force => 1);
306         }
307         elsif ($form->submitted eq 'Advanced Mode') {
308                 $form->field(name => "showadvanced", type => "hidden", 
309                         value => 1, force => 1);
310         }
311         my $advancedtoggle;
312         if ($form->field("showadvanced")) {
313                 $config{websetup_advanced}=1;
314                 $advancedtoggle="Basic Mode";
315         }
316         else {
317                 $config{websetup_advanced}=0;
318                 $advancedtoggle="Advanced Mode";
319         }
320
321         my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
322
323         IkiWiki::decode_form_utf8($form);
324         IkiWiki::run_hooks(formbuilder_setup => sub {
325                 shift->(form => $form, cgi => $cgi, session => $session,
326                         buttons => $buttons);
327         });
328
329         my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
330         
331         # record all currently enabled plugins before all are loaded
332         my %enabled_plugins=%IkiWiki::loaded_plugins;
333
334         # per-plugin setup
335         require IkiWiki::Setup;
336         foreach my $pair (IkiWiki::Setup::getsetup()) {
337                 my $plugin=$pair->[0];
338                 my $setup=$pair->[1];
339
340                 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
341                 if (%shown) {
342                         $fields{$_}=$shown{$_} foreach keys %shown;
343                 }
344         }
345
346         IkiWiki::decode_form_utf8($form);
347         
348         if ($form->submitted eq "Cancel") {
349                 IkiWiki::redirect($cgi, $config{url});
350                 return;
351         }
352         elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
353                 # Push values from form into %config, avoiding unnecessary
354                 # changes, and keeping track of which changes need a
355                 # rebuild.
356                 my %rebuild;
357                 foreach my $field (keys %fields) {
358                         my %info=%{$fields{$field}->[1]};
359                         my $key=$fields{$field}->[0];
360                         my @value=$form->field($field);
361                         if (! @value) {
362                                 @value=0;
363                         }
364                 
365                         if (! $info{safe} || ! issafe($key)) {
366                                 error("unsafe field $key"); # should never happen
367                         }
368                 
369                         if (exists $info{rebuild} &&
370                             ($info{rebuild} || ! defined $info{rebuild})) {
371                                 $rebuild{$field}=$info{rebuild};
372                         }
373                                         
374                         if ($field=~/^enable\.(.*)/) {
375                                 my $plugin=$1;
376                                 $value[0]=0 if ! length $value[0];
377                                 if ($value[0] != exists $enabled_plugins{$plugin}) {
378                                         if ($value[0]) {
379                                                 enable_plugin($plugin);
380                                         }
381                                         else {
382                                                 disable_plugin($plugin);
383
384                                         }
385                                 }
386                                 else {
387                                         delete $rebuild{$field};
388                                 }
389                                 next;
390                         }
391
392                         if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
393                                 @value=sort grep { length $_ } @value;
394                                 my @oldvalue=sort grep { length $_ }
395                                         (defined $config{$key} ? @{$config{$key}} : ());
396                                 my $same=(@oldvalue) == (@value);
397                                 for (my $x=0; $same && $x < @value; $x++) {
398                                         $same=0 if $value[$x] ne $oldvalue[$x];
399                                 }
400                                 if ($same) {
401                                         delete $rebuild{$field};
402                                 }
403                                 else {
404                                         $config{$key}=\@value;
405                                 }
406                         }
407                         elsif (ref $config{$key} || ref $info{example}) {
408                                 error("complex field $key"); # should never happen
409                         }
410                         else {
411                                 if (defined $config{$key} && $config{$key} eq $value[0]) {
412                                         delete $rebuild{$field};
413                                 }
414                                 elsif (! defined $config{$key} && ! length $value[0]) {
415                                         delete $rebuild{$field};
416                                 }
417                                 elsif ((! defined $config{$key} || ! $config{$key}) &&
418                                        ! $value[0] && $info{type} eq "boolean") {
419                                         delete $rebuild{$field};
420                                 }
421                                 else {
422                                         $config{$key}=$value[0];
423                                 }
424                         }
425                 }
426                 
427                 if (%rebuild && ! $form->field("rebuild_asked")) {
428                         my $required=0;
429                         foreach my $field ($form->field) {
430                                 $required=1 if $rebuild{$field};
431                                 next if exists $rebuild{$field};
432                                 $form->field(name => $field, type => "hidden");
433                         }
434                         if ($required) {
435                                 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
436                                 $buttons=["Rebuild Wiki", "Cancel"];
437                         }
438                         else {
439                                 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
440                                 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
441                         }
442                         $form->field(name => "rebuild_asked", value => 1, force => 1);
443                         $form->reset(0); # doesn't really make sense here
444                 }
445                 else {
446                         my $oldsetup=readfile($config{setupfile});
447                         IkiWiki::Setup::dump($config{setupfile});
448
449                         IkiWiki::saveindex();
450                         IkiWiki::unlockwiki();
451
452                         # Print the top part of a standard misctemplate,
453                         # then show the rebuild or refresh.
454                         my $divider="xxx";
455                         my $html=IkiWiki::misctemplate("setup", $divider);
456                         IkiWiki::printheader($session);
457                         my ($head, $tail)=split($divider, $html, 2);
458                         print $head."<pre>\n";
459
460                         my @command;
461                         if ($form->submitted eq 'Rebuild Wiki') {
462                                 @command=("ikiwiki", "-setup", $config{setupfile},
463                                         "-rebuild", "-v");
464                         }
465                         else {
466                                 @command=("ikiwiki", "-setup", $config{setupfile},
467                                         "-refresh", "-wrappers", "-v");
468                         }
469
470                         close STDERR;
471                         open(STDERR, ">&STDOUT");
472                         my $ret=system(@command);
473                         print "\n<\/pre>";
474                         if ($ret != 0) {
475                                 print '<p class="error">'.
476                                         sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
477                                                 join(" ", @command), $ret).
478                                         '</p>';
479                                 open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
480                                 print OUT $oldsetup;
481                                 close OUT;
482                         }
483
484                         print $tail;
485                         exit 0;
486                 }
487         }
488
489         IkiWiki::showform($form, $buttons, $session, $cgi);
490 }
491
492 sub sessioncgi ($$) {
493         my $cgi=shift;
494         my $session=shift;
495
496         if ($cgi->param("do") eq "setup") {
497                 showform($cgi, $session);
498                 exit;
499         }
500 }
501
502 sub formbuilder_setup (@) {
503         my %params=@_;
504
505         my $form=$params{form};
506         if ($form->title eq "preferences" &&
507             IkiWiki::is_admin($params{session}->param("name"))) {
508                 push @{$params{buttons}}, "Setup";
509                 if ($form->submitted && $form->submitted eq "Setup") {
510                         showform($params{cgi}, $params{session});
511                         exit;
512                 }
513         }
514 }
515
516 1