use radio, not checkbox, for booleans
[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 2.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                 },
22                 websetup_force_plugins => {
23                         type => "string",
24                         example => [],
25                         description => "list of plugins that cannot be enabled/disabled via the web interface",
26                         safe => 0,
27                         rebuild => 0,
28                 },
29                 websetup_show_unsafe => {
30                         type => "boolean",
31                         example => 1,
32                         description => "show unsafe settings, read-only, in web interface?",
33                         safe => 0,
34                         rebuild => 0,
35                 },
36 } #}}}
37
38 sub checkconfig () { #{{{
39         if (! exists $config{websetup_show_unsafe}) {
40                 $config{websetup_show_unsafe}=1;
41         }
42 } #}}}
43
44 sub formatexample ($$) { #{{{
45         my $example=shift;
46         my $value=shift;
47
48         if (defined $value && length $value) {
49                 return "";
50         }
51         elsif (defined $example && ! ref $example && length $example) {
52                 return "<br/ ><small>Example: <tt>$example</tt></small>";
53         }
54         else {
55                 return "";
56         }
57 } #}}}
58
59 sub showfields ($$$@) { #{{{
60         my $form=shift;
61         my $plugin=shift;
62         my $enabled=shift;
63
64         my @show;
65         my %plugininfo;
66         while (@_) {
67                 my $key=shift;
68                 my %info=%{shift()};
69
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';
80
81                 if ($key eq 'plugin') {
82                         %plugininfo=%info;
83                         next;
84                 }
85                 
86                 push @show, $key, \%info;
87         }
88
89         my $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
90         my %enabledfields;
91         my $shownfields=0;
92         
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
97                 @show=();
98         }
99
100         # show plugin toggle
101         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
102                 my $name="enable.$plugin";
103                 $form->field(
104                         name => $name,
105                         label => sprintf(gettext("enable %s?"), $plugin),
106                         type => "radio",
107                         value => $enabled,
108                         fieldset => $section,
109                         options => [
110                                 [ 1 => gettext("Yes") ],
111                                 [ 0 => gettext("No") ]
112                         ],
113                 );
114                 if ($plugin_forced) {
115                         $form->field(name => $name, disabled => 1);
116                 }
117                 else {
118                         $enabledfields{$name}=[$name, \%plugininfo];
119                 }
120         }
121
122         # show plugin settings
123         while (@show) {
124                 my $key=shift @show;
125                 my %info=%{shift @show};
126
127                 my $description=$info{description};
128                 if (exists $info{link} && length $info{link}) {
129                         if ($info{link} =~ /^\w+:\/\//) {
130                                 $description="<a href=\"$info{link}\">$description</a>";
131                         }
132                         else {
133                                 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
134                         }
135                 }
136
137                 # multiple plugins can have the same field
138                 my $name=defined $plugin ? $plugin.".".$key : $key;
139
140                 my $value=$config{$key};
141
142                 if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
143                         push @{$value}, "", ""; # blank items for expansion
144                 }
145
146                 if ($info{type} eq "string") {
147                         $form->field(
148                                 name => $name,
149                                 label => $description,
150                                 comment => formatexample($info{example}, $value),
151                                 type => "text",
152                                 value => $value,
153                                 size => 60,
154                                 fieldset => $section,
155                         );
156                 }
157                 elsif ($info{type} eq "pagespec") {
158                         $form->field(
159                                 name => $name,
160                                 label => $description,
161                                 comment => formatexample($info{example}, $value),
162                                 type => "text",
163                                 value => $value,
164                                 size => 60,
165                                 validate => \&IkiWiki::pagespec_valid,
166                                 fieldset => $section,
167                         );
168                 }
169                 elsif ($info{type} eq "integer") {
170                         $form->field(
171                                 name => $name,
172                                 label => $description,
173                                 comment => formatexample($info{example}, $value),
174                                 type => "text",
175                                 value => $value,
176                                 size => 5,
177                                 validate => '/^[0-9]+$/',
178                                 fieldset => $section,
179                         );
180                 }
181                 elsif ($info{type} eq "boolean") {
182                         $form->field(
183                                 name => $name,
184                                 label => $description,
185                                 type => "radio",
186                                 value => $value,
187                                 options => [
188                                         [ 1 => gettext("Yes") ],
189                                         [ 0 => gettext("No") ]
190                                 ],
191                                 fieldset => $section,
192                         );
193                 }
194                 
195                 if (! $info{safe}) {
196                         $form->field(name => $name, disabled => 1);
197                 }
198                 else {
199                         $enabledfields{$name}=[$key, \%info];
200                 }
201                 $shownfields++;
202         }
203         
204         # if no fields were shown for the plugin, drop it into the
205         # plugins fieldset
206         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
207             ! $shownfields) {
208                 $form->field(name => "enable.$plugin", fieldset => "plugins");
209         }
210
211         return %enabledfields;
212 } #}}}
213
214 sub showform ($$) { #{{{
215         my $cgi=shift;
216         my $session=shift;
217
218         if (! defined $session->param("name") || 
219             ! IkiWiki::is_admin($session->param("name"))) {
220                 error(gettext("you are not logged in as an admin"));
221         }
222
223         eval q{use CGI::FormBuilder};
224         error($@) if $@;
225
226         my $form = CGI::FormBuilder->new(
227                 title => "setup",
228                 name => "setup",
229                 header => 0,
230                 charset => "utf-8",
231                 method => 'POST',
232                 javascript => 0,
233                 reset => 1,
234                 params => $cgi,
235                 fieldsets => [
236                         [main => gettext("main")], 
237                         [plugins => gettext("plugins")]
238                 ],
239                 action => $config{cgiurl},
240                 table => 0,
241                 template => {type => 'div'},
242                 stylesheet => IkiWiki::baseurl()."style.css",
243         );
244         
245         $form->field(name => "do", type => "hidden", value => "setup",
246                 force => 1);
247
248         if ($form->submitted eq 'Basic Mode') {
249                 $form->field(name => "showadvanced", type => "hidden", 
250                         value => 0, force => 1);
251         }
252         elsif ($form->submitted eq 'Advanced Mode') {
253                 $form->field(name => "showadvanced", type => "hidden", 
254                         value => 1, force => 1);
255         }
256         my $advancedtoggle;
257         if ($form->field("showadvanced")) {
258                 $config{websetup_advanced}=1;
259                 $advancedtoggle="Basic Mode";
260         }
261         else {
262                 $config{websetup_advanced}=0;
263                 $advancedtoggle="Advanced Mode";
264         }
265
266         my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
267
268         IkiWiki::decode_form_utf8($form);
269         IkiWiki::run_hooks(formbuilder_setup => sub {
270                 shift->(form => $form, cgi => $cgi, session => $session,
271                         buttons => $buttons);
272         });
273         IkiWiki::decode_form_utf8($form);
274
275         my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
276         
277         # record all currently enabled plugins before all are loaded
278         my %enabled_plugins=%IkiWiki::loaded_plugins;
279
280         # per-plugin setup
281         require IkiWiki::Setup;
282         foreach my $pair (IkiWiki::Setup::getsetup()) {
283                 my $plugin=$pair->[0];
284                 my $setup=$pair->[1];
285
286                 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
287                 if (%shown) {
288                         $fields{$_}=$shown{$_} foreach keys %shown;
289                 }
290         }
291         
292         if ($form->submitted eq "Cancel") {
293                 IkiWiki::redirect($cgi, $config{url});
294                 return;
295         }
296         elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
297                 # Push values from form into %config, avoiding unnecessary
298                 # changes, and keeping track of which changes need a
299                 # rebuild.
300                 my %rebuild;
301                 foreach my $field (keys %fields) {
302                         my %info=%{$fields{$field}->[1]};
303                         my $key=$fields{$field}->[0];
304                         my @value=$form->field($field);
305                         if (! @value) {
306                                 @value=0;
307                         }
308                 
309                         if (! $info{safe}) {
310                                 error("unsafe field $key"); # should never happen
311                         }
312                 
313                         if (exists $info{rebuild} &&
314                             ($info{rebuild} || ! defined $info{rebuild})) {
315                                 $rebuild{$field}=$info{rebuild};
316                         }
317                                         
318                         if ($field=~/^enable\.(.*)/) {
319                                 my $plugin=$1;
320                                 if ($value[0] != exists $enabled_plugins{$plugin}) {
321                                         # TODO plugin enable/disable
322                                 }
323                                 else {
324                                         delete $rebuild{$field};
325                                 }
326                                 next;
327                         }
328
329                         if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
330                                 @value=sort grep { length $_ } @value;
331                                 my @oldvalue=sort grep { length $_ }
332                                         (defined $config{$key} ? @{$config{$key}} : ());
333                                 if ((@oldvalue) == (@value)) {
334                                         delete $rebuild{$field};
335                                 }
336                                 else {
337                                         $config{$key}=\@value;
338                                 }
339                         }
340                         elsif (ref $config{$key} || ref $info{example}) {
341                                 error("complex field $key"); # should never happen
342                         }
343                         else {
344                                 if (defined $config{$key} && $config{$key} eq $value[0]) {
345                                         delete $rebuild{$field};
346                                 }
347                                 elsif (! defined $config{$key} && ! length $value[0]) {
348                                         delete $rebuild{$field};
349                                 }
350                                 elsif (! defined $config{$key} && ! $value[0] &&
351                                        $info{type} eq "boolean") {
352                                         delete $rebuild{$field};
353                                 }
354                                 else {
355                                         $config{$key}=$value[0];
356                                 }
357                         }
358                 }
359                 
360                 if (%rebuild && ! $form->field("rebuild_asked")) {
361                         my $required=0;
362                         foreach my $field ($form->field) {
363                                 $required=1 if $rebuild{$field};
364                                 next if exists $rebuild{$field};
365                                 $form->field(name => $field, type => "hidden",
366                                         force => 1);
367                         }
368                         if ($required) {
369                                 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
370                                 $buttons=["Rebuild Wiki", "Cancel"];
371                         }
372                         else {
373                                 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
374                                 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
375                         }
376                         $form->field(name => "rebuild_asked", type => "hidden",
377                                 value => 1, force => 1);
378                         $form->reset(0); # doesn't really make sense here
379                 }
380                 else {
381                         $form->field(name => "rebuild_asked", type => "hidden",
382                                 value => 0, force => 1);
383                         # TODO save to real path
384                         IkiWiki::Setup::dump("/tmp/s");
385                         $form->text(gettext("Setup saved."));
386         
387                         if ($form->submitted eq 'Rebuild Wiki') {
388                                 # TODO rebuild
389                         }
390                 }
391         }
392
393         IkiWiki::showform($form, $buttons, $session, $cgi);
394 } #}}}
395
396 sub sessioncgi ($$) { #{{{
397         my $cgi=shift;
398         my $session=shift;
399
400         if ($cgi->param("do") eq "setup") {
401                 showform($cgi, $session);
402                 exit;
403         }
404 } #}}}
405
406 sub formbuilder_setup (@) { #{{{
407         my %params=@_;
408
409         my $form=$params{form};
410         if ($form->title eq "preferences") {
411                 push @{$params{buttons}}, "Wiki Setup";
412                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
413                         showform($params{cgi}, $params{session});
414                         exit;
415                 }
416         }
417 } #}}}
418
419 1