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