]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/websetup.pm
properly support all types of data in arrays
[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 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};
77                 # these are handled specially, so don't show
78                 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
79                 
80                 push @show, $key, \%info;
81         }
82
83         return unless @show;
84
85         my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
86
87         my %shownfields;
88         if (defined $plugin) {
89                 if (showplugintoggle($form, $plugin, $enabled, $section)) {
90                         $shownfields{"enable.$plugin"}=[$plugin];
91                 }
92                 elsif (! $enabled) {
93                     # plugin not enabled and cannot be, so skip showing
94                     # its configuration
95                     return;
96                 }
97         }
98
99         while (@show) {
100                 my $key=shift @show;
101                 my %info=%{shift @show};
102
103                 my $description=$info{description};
104                 if (exists $info{link} && length $info{link}) {
105                         if ($info{link} =~ /^\w+:\/\//) {
106                                 $description="<a href=\"$info{link}\">$description</a>";
107                         }
108                         else {
109                                 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
110                         }
111                 }
112
113                 my $value=$config{$key};
114                 if (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY') {
115                         push @{$value}, "", ""; # blank items for expansion
116                 }
117
118                 # multiple plugins can have the same field
119                 my $name=defined $plugin ? $plugin.".".$key : $key;
120
121                 if ($info{type} eq "string") {
122                         $form->field(
123                                 name => $name,
124                                 label => $description,
125                                 comment => formatexample($info{example}, $value),
126                                 type => "text",
127                                 value => $value,
128                                 size => 60,
129                                 fieldset => $section,
130                         );
131                 }
132                 elsif ($info{type} eq "pagespec") {
133                         $form->field(
134                                 name => $name,
135                                 label => $description,
136                                 comment => formatexample($info{example}, $value),
137                                 type => "text",
138                                 value => $value,
139                                 size => 60,
140                                 validate => \&IkiWiki::pagespec_valid,
141                                 fieldset => $section,
142                         );
143                 }
144                 elsif ($info{type} eq "integer") {
145                         $form->field(
146                                 name => $name,
147                                 label => $description,
148                                 comment => formatexample($info{example}, $value),
149                                 type => "text",
150                                 value => $value,
151                                 size => 5,
152                                 validate => '/^[0-9]+$/',
153                                 fieldset => $section,
154                         );
155                 }
156                 elsif ($info{type} eq "boolean") {
157                         $form->field(
158                                 name => $name,
159                                 label => "",
160                                 type => "checkbox",
161                                 value => $value,
162                                 options => [ [ 1 => $description ] ],
163                                 fieldset => $section,
164                         );
165                 }
166                 
167                 if (! $info{safe}) {
168                         $form->field(name => $name, disabled => 1);
169                         $form->text(gettext("Note: Disabled options cannot be configured here, but only by editing the setup file."));
170                 }
171                 else {
172                         $shownfields{$name}=[$key, \%info];
173                 }
174         }
175
176         return %shownfields;
177 } #}}}
178
179 sub showplugintoggle ($$$$) { #{{{
180         my $form=shift;
181         my $plugin=shift;
182         my $enabled=shift;
183         my $section=shift;
184
185         if (exists $config{websetup_force_plugins} &&
186             grep { $_ eq $plugin } @{$config{websetup_force_plugins}}) {
187                 return 0;
188         }
189         if (grep { $_ eq $plugin } @force_plugins, @rcs_plugins) {
190                 return 0;
191         }
192
193         $form->field(
194                 name => "enable.$plugin",
195                 label => "",
196                 type => "checkbox",
197                 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
198                 value => $enabled,
199                 fieldset => $section,
200         );
201
202         return 1;
203 } #}}}
204
205 sub showform ($$) { #{{{
206         my $cgi=shift;
207         my $session=shift;
208
209         if (! defined $session->param("name") || 
210             ! IkiWiki::is_admin($session->param("name"))) {
211                 error(gettext("you are not logged in as an admin"));
212         }
213
214         eval q{use CGI::FormBuilder};
215         error($@) if $@;
216
217         my $form = CGI::FormBuilder->new(
218                 title => "setup",
219                 name => "setup",
220                 header => 0,
221                 charset => "utf-8",
222                 method => 'POST',
223                 javascript => 0,
224                 reset => 1,
225                 params => $cgi,
226                 action => $config{cgiurl},
227                 template => {type => 'div'},
228                 stylesheet => IkiWiki::baseurl()."style.css",
229         );
230         my $buttons=["Save Setup", "Cancel"];
231
232         IkiWiki::decode_form_utf8($form);
233         IkiWiki::run_hooks(formbuilder_setup => sub {
234                 shift->(form => $form, cgi => $cgi, session => $session,
235                         buttons => $buttons);
236         });
237         IkiWiki::decode_form_utf8($form);
238
239         $form->field(name => "do", type => "hidden", value => "setup",
240                 force => 1);
241         my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
242         
243         # record all currently enabled plugins before all are loaded
244         my %enabled_plugins=%IkiWiki::loaded_plugins;
245
246         # per-plugin setup
247         require IkiWiki::Setup;
248         my %plugins=map { $_ => 1 } IkiWiki::listplugins();
249         foreach my $pair (IkiWiki::Setup::getsetup()) {
250                 my $plugin=$pair->[0];
251                 my $setup=$pair->[1];
252                 
253                 # skip all rcs plugins except for the one in use
254                 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
255
256                 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
257                 if (%shown) {
258                         delete $plugins{$plugin};
259                         $fields{$_}=$shown{$_} foreach keys %shown;
260                 }
261         }
262
263         # list all remaining plugins (with no setup options) at the end
264         foreach (sort keys %plugins) {
265                 if (showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))) {
266                         $fields{"enable.$_"}=[$_];
267                 }
268         }
269         
270         if ($form->submitted eq "Cancel") {
271                 IkiWiki::redirect($cgi, $config{url});
272                 return;
273         }
274         elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
275                 my %rebuild;
276                 foreach my $field (keys %fields) {
277                         if ($field=~/^enable\./) {
278                                 # rebuild is overkill for many plugins,
279                                 # but no good way to tell which
280                                 $rebuild{$field}=1; # TODO only if state changed tho
281                                 # TODO plugin enable/disable
282                                 next;
283                         }
284                         
285                         my %info=%{$fields{$field}->[1]};
286                         my $key=$fields{$field}->[0];
287                         my @value=$form->field($field);
288                         
289                         if (! $info{safe}) {
290                                 error("unsafe field $key"); # should never happen
291                         }
292
293                         next unless @value;
294                         # Avoid setting fields to empty strings,
295                         # if they were not set before.
296                         next if ! defined $config{$key} && ! grep { length $_ } @value;
297
298                         if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
299                                 if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
300                                         $rebuild{$field}=1;
301                                 }
302                                 $config{$key}=\@value;
303                         }
304                         elsif (ref $config{$key} || ref $info{example}) {
305                                 error("complex field $key"); # should never happen
306                         }
307                         else {
308                                 if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
309                                         $rebuild{$field}=1;
310                                 }
311                                 $config{$key}=$value[0];
312                         }               
313                 }
314
315                 if (%rebuild && $form->submitted eq 'Save Setup') {
316                         $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
317                         foreach my $field ($form->field) {
318                                 next if $rebuild{$field};
319                                 $form->field(name => $field, type => "hidden",
320                                         force => 1);
321                         }
322                         $form->reset(0); # doesn't really make sense here
323                         $buttons=["Rebuild Wiki", "Cancel"];
324                 }
325                 else {
326                         # TODO save to real path
327                         IkiWiki::Setup::dump("/tmp/s");
328                         $form->text(gettext("Setup saved."));
329
330                         if (%rebuild) {
331                                 # TODO rebuild
332                         }
333                 }
334         }
335
336         IkiWiki::showform($form, $buttons, $session, $cgi);
337 } #}}}
338
339 sub sessioncgi ($$) { #{{{
340         my $cgi=shift;
341         my $session=shift;
342
343         if ($cgi->param("do") eq "setup") {
344                 showform($cgi, $session);
345                 exit;
346         }
347 } #}}}
348
349 sub formbuilder_setup (@) { #{{{
350         my %params=@_;
351
352         my $form=$params{form};
353         if ($form->title eq "preferences") {
354                 push @{$params{buttons}}, "Wiki Setup";
355                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
356                         showform($params{cgi}, $params{session});
357                         exit;
358                 }
359         }
360 } #}}}
361
362 1