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