avoid setting default value in websetup_force_plugins
[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 @default_force_plugins=(qw{amazon_s3 external});
13
14 sub import { #{{{
15         hook(type => "getsetup", id => "websetup", call => \&getsetup);
16         hook(type => "sessioncgi", id => "websetup", call => \&sessioncgi);
17         hook(type => "formbuilder_setup", id => "websetup", 
18              call => \&formbuilder_setup);
19 } # }}}
20
21 sub getsetup () { #{{{
22         return
23                 websetup_force_plugins => {
24                         type => "string",
25                         example => \@default_force_plugins,
26                         description => "list of plugins that cannot be enabled/disabled via the web interface",
27                         safe => 0,
28                         rebuild => 0,
29                 },
30 } #}}}
31
32 sub formatexample ($) { #{{{
33         my $example=shift;
34
35         if (defined $example && ! ref $example && length $example) {
36                 return "<br/ ><small>Example: <tt>$example</tt></small>";
37         }
38         else {
39                 return "";
40         }
41 } #}}}
42
43 sub showfields ($$$@) { #{{{
44         my $form=shift;
45         my $plugin=shift;
46         my $enabled=shift;
47
48         my @show;
49         while (@_) {
50                 my $key=shift;
51                 my %info=%{shift()};
52
53                 # skip complex, unsafe, or internal settings
54                 next if ref $config{$key} || ! $info{safe} || $info{type} eq "internal";
55                 # these are handled specially, so don't show
56                 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
57                 
58                 push @show, $key, \%info;
59         }
60
61         return 0 unless @show;
62
63         my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
64
65         if (defined $plugin) {
66                 if (! showplugintoggle($form, $plugin, $enabled, $section) && ! $enabled) {
67                     # plugin not enabled and cannot be, so skip showing
68                     # its configuration
69                     return 0;
70                 }
71         }
72
73         while (@show) {
74                 my $key=shift @show;
75                 my %info=%{shift @show};
76
77                 my $description=exists $info{description_html} ? $info{description_html} : $info{description};
78                 my $value=$config{$key};
79                 # multiple plugins can have the same field
80                 my $name=defined $plugin ? $plugin.".".$key : $key;
81                 
82                 if ($info{type} eq "string") {
83                         $form->field(
84                                 name => $name,
85                                 label => $description,
86                                 comment => defined $value && length $value ? "" : formatexample($info{example}),
87                                 type => "text",
88                                 value => $value,
89                                 size => 60,
90                                 fieldset => $section,
91                         );
92                 }
93                 elsif ($info{type} eq "pagespec") {
94                         $form->field(
95                                 name => $name,
96                                 label => $description,
97                                 comment => formatexample($info{example}),
98                                 type => "text",
99                                 value => $value,
100                                 size => 60,
101                                 validate => \&IkiWiki::pagespec_valid,
102                                 fieldset => $section,
103                         );
104                 }
105                 elsif ($info{type} eq "integer") {
106                         $form->field(
107                                 name => $name,
108                                 label => $description,
109                                 type => "text",
110                                 value => $value,
111                                 size => 5,
112                                 validate => '/^[0-9]+$/',
113                                 fieldset => $section,
114                         );
115                 }
116                 elsif ($info{type} eq "boolean") {
117                         $form->field(
118                                 name => $name,
119                                 label => "",
120                                 type => "checkbox",
121                                 value => $value,
122                                 options => [ [ 1 => $description ] ],
123                                 fieldset => $section,
124                         );
125                 }
126         }
127
128         return 1;
129 } #}}}
130
131 sub showplugintoggle ($$$$) { #{{{
132         my $form=shift;
133         my $plugin=shift;
134         my $enabled=shift;
135         my $section=shift;
136
137         if (exists $config{websetup_force_plugins} &&
138             grep { $_ eq $plugin } @{$config{websetup_force_plugins}}, @rcs_plugins) {
139                 return 0;
140         }
141         elsif (! exists $config{websetup_force_plugins} &&
142                grep { $_ eq $plugin } @default_force_plugins, @rcs_plugins) {
143                 return 0;
144         }
145
146         print STDERR ">>$plugin (@{$config{websetup_force_plugins}})\n";
147
148         $form->field(
149                 name => "enable.$plugin",
150                 label => "",
151                 type => "checkbox",
152                 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
153                 value => $enabled,
154                 fieldset => $section,
155         );
156
157         return 1;
158 } #}}}
159
160 sub showform ($$) { #{{{
161         my $cgi=shift;
162         my $session=shift;
163
164         if (! defined $session->param("name") || 
165             ! IkiWiki::is_admin($session->param("name"))) {
166                 error(gettext("you are not logged in as an admin"));
167         }
168
169         eval q{use CGI::FormBuilder};
170         error($@) if $@;
171
172         my $form = CGI::FormBuilder->new(
173                 title => "setup",
174                 name => "setup",
175                 header => 0,
176                 charset => "utf-8",
177                 method => 'POST',
178                 javascript => 0,
179                 reset => 1,
180                 params => $cgi,
181                 action => $config{cgiurl},
182                 template => {type => 'div'},
183                 stylesheet => IkiWiki::baseurl()."style.css",
184         );
185         my $buttons=["Save Setup", "Cancel"];
186
187         IkiWiki::decode_form_utf8($form);
188         IkiWiki::run_hooks(formbuilder_setup => sub {
189                 shift->(form => $form, cgi => $cgi, session => $session,
190                         buttons => $buttons);
191         });
192         IkiWiki::decode_form_utf8($form);
193
194         $form->field(name => "do", type => "hidden", value => "setup",
195                 force => 1);
196         showfields($form, undef, undef, IkiWiki::getsetup());
197         
198         # record all currently enabled plugins before all are loaded
199         my %enabled_plugins=%IkiWiki::loaded_plugins;
200
201         # per-plugin setup
202         require IkiWiki::Setup;
203         my %plugins=map { $_ => 1 } IkiWiki::listplugins();
204         foreach my $pair (IkiWiki::Setup::getsetup()) {
205                 my $plugin=$pair->[0];
206                 my $setup=$pair->[1];
207                 
208                 # skip all rcs plugins except for the one in use
209                 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
210
211                 delete $plugins{$plugin} if showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
212         }
213
214         # list all remaining plugins (with no setup options) at the end
215         showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))
216                 foreach sort keys %plugins;
217         
218         if ($form->submitted eq "Cancel") {
219                 IkiWiki::redirect($cgi, $config{url});
220                 return;
221         }
222         elsif ($form->submitted eq 'Save Setup' && $form->validate) {
223                 # TODO
224                 IkiWiki::Setup::dump("/tmp/s");
225                 $form->text(gettext("Setup saved."));
226         }
227
228         IkiWiki::showform($form, $buttons, $session, $cgi);
229 } #}}}
230
231 sub sessioncgi ($$) { #{{{
232         my $cgi=shift;
233         my $session=shift;
234
235         if ($cgi->param("do") eq "setup") {
236                 showform($cgi, $session);
237                 exit;
238         }
239 } #}}}
240
241 sub formbuilder_setup (@) { #{{{
242         my %params=@_;
243
244         my $form=$params{form};
245         if ($form->title eq "preferences") {
246                 push @{$params{buttons}}, "Wiki Setup";
247                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
248                         showform($params{cgi}, $params{session});
249                         exit;
250                 }
251         }
252 } #}}}
253
254 1