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