show unsafe options (RO) by default
[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 => "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 => \@default_force_plugins,
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 0 unless @show;
82
83         my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
84
85         if (defined $plugin) {
86                 if (! showplugintoggle($form, $plugin, $enabled, $section) && ! $enabled) {
87                     # plugin not enabled and cannot be, so skip showing
88                     # its configuration
89                     return 0;
90                 }
91         }
92
93         while (@show) {
94                 my $key=shift @show;
95                 my %info=%{shift @show};
96
97                 my $description=exists $info{description_html} ? $info{description_html} : $info{description};
98                 my $value=$config{$key};
99                 # multiple plugins can have the same field
100                 my $name=defined $plugin ? $plugin.".".$key : $key;
101                 
102                 if ($info{type} eq "string") {
103                         $form->field(
104                                 name => $name,
105                                 label => $description,
106                                 comment => formatexample($info{example}, $value),
107                                 type => "text",
108                                 value => $value,
109                                 size => 60,
110                                 fieldset => $section,
111                         );
112                 }
113                 elsif ($info{type} eq "pagespec") {
114                         $form->field(
115                                 name => $name,
116                                 label => $description,
117                                 comment => formatexample($info{example}, $value),
118                                 type => "text",
119                                 value => $value,
120                                 size => 60,
121                                 validate => \&IkiWiki::pagespec_valid,
122                                 fieldset => $section,
123                         );
124                 }
125                 elsif ($info{type} eq "integer") {
126                         $form->field(
127                                 name => $name,
128                                 label => $description,
129                                 comment => formatexample($info{example}, $value),
130                                 type => "text",
131                                 value => $value,
132                                 size => 5,
133                                 validate => '/^[0-9]+$/',
134                                 fieldset => $section,
135                         );
136                 }
137                 elsif ($info{type} eq "boolean") {
138                         $form->field(
139                                 name => $name,
140                                 label => "",
141                                 type => "checkbox",
142                                 value => $value,
143                                 options => [ [ 1 => $description ] ],
144                                 fieldset => $section,
145                         );
146                 }
147                 
148                 if (! $info{safe}) {
149                         $form->field(name => $name, disabled => 1);
150                         $form->text(gettext("Note: Disabled options cannot be configured here, but only by editing the setup file."));
151                 }
152         }
153
154         return 1;
155 } #}}}
156
157 sub showplugintoggle ($$$$) { #{{{
158         my $form=shift;
159         my $plugin=shift;
160         my $enabled=shift;
161         my $section=shift;
162
163         if (exists $config{websetup_force_plugins} &&
164             grep { $_ eq $plugin } @{$config{websetup_force_plugins}}, @rcs_plugins) {
165                 return 0;
166         }
167         elsif (! exists $config{websetup_force_plugins} &&
168                grep { $_ eq $plugin } @default_force_plugins, @rcs_plugins) {
169                 return 0;
170         }
171
172         $form->field(
173                 name => "enable.$plugin",
174                 label => "",
175                 type => "checkbox",
176                 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
177                 value => $enabled,
178                 fieldset => $section,
179         );
180
181         return 1;
182 } #}}}
183
184 sub showform ($$) { #{{{
185         my $cgi=shift;
186         my $session=shift;
187
188         if (! defined $session->param("name") || 
189             ! IkiWiki::is_admin($session->param("name"))) {
190                 error(gettext("you are not logged in as an admin"));
191         }
192
193         eval q{use CGI::FormBuilder};
194         error($@) if $@;
195
196         my $form = CGI::FormBuilder->new(
197                 title => "setup",
198                 name => "setup",
199                 header => 0,
200                 charset => "utf-8",
201                 method => 'POST',
202                 javascript => 0,
203                 reset => 1,
204                 params => $cgi,
205                 action => $config{cgiurl},
206                 template => {type => 'div'},
207                 stylesheet => IkiWiki::baseurl()."style.css",
208         );
209         my $buttons=["Save Setup", "Cancel"];
210
211         IkiWiki::decode_form_utf8($form);
212         IkiWiki::run_hooks(formbuilder_setup => sub {
213                 shift->(form => $form, cgi => $cgi, session => $session,
214                         buttons => $buttons);
215         });
216         IkiWiki::decode_form_utf8($form);
217
218         $form->field(name => "do", type => "hidden", value => "setup",
219                 force => 1);
220         showfields($form, undef, undef, IkiWiki::getsetup());
221         
222         # record all currently enabled plugins before all are loaded
223         my %enabled_plugins=%IkiWiki::loaded_plugins;
224
225         # per-plugin setup
226         require IkiWiki::Setup;
227         my %plugins=map { $_ => 1 } IkiWiki::listplugins();
228         foreach my $pair (IkiWiki::Setup::getsetup()) {
229                 my $plugin=$pair->[0];
230                 my $setup=$pair->[1];
231                 
232                 # skip all rcs plugins except for the one in use
233                 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
234
235                 delete $plugins{$plugin} if showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
236         }
237
238         # list all remaining plugins (with no setup options) at the end
239         showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))
240                 foreach sort keys %plugins;
241         
242         if ($form->submitted eq "Cancel") {
243                 IkiWiki::redirect($cgi, $config{url});
244                 return;
245         }
246         elsif ($form->submitted eq 'Save Setup' && $form->validate) {
247                 # TODO
248                 IkiWiki::Setup::dump("/tmp/s");
249                 $form->text(gettext("Setup saved."));
250         }
251
252         IkiWiki::showform($form, $buttons, $session, $cgi);
253 } #}}}
254
255 sub sessioncgi ($$) { #{{{
256         my $cgi=shift;
257         my $session=shift;
258
259         if ($cgi->param("do") eq "setup") {
260                 showform($cgi, $session);
261                 exit;
262         }
263 } #}}}
264
265 sub formbuilder_setup (@) { #{{{
266         my %params=@_;
267
268         my $form=$params{form};
269         if ($form->title eq "preferences") {
270                 push @{$params{buttons}}, "Wiki Setup";
271                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
272                         showform($params{cgi}, $params{session});
273                         exit;
274                 }
275         }
276 } #}}}
277
278 1