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