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