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