add plugin safe/rebuild info (part 3 of 3)
[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 sub import { #{{{
9         hook(type => "getsetup", id => "websetup", call => \&getsetup);
10         hook(type => "checkconfig", id => "websetup", call => \&checkconfig);
11         hook(type => "sessioncgi", id => "websetup", call => \&sessioncgi);
12         hook(type => "formbuilder_setup", id => "websetup", 
13              call => \&formbuilder_setup);
14 } # }}}
15
16 sub getsetup () { #{{{
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                 },
22                 websetup_force_plugins => {
23                         type => "string",
24                         example => [],
25                         description => "list of plugins that cannot be enabled/disabled via the web interface",
26                         safe => 0,
27                         rebuild => 0,
28                 },
29                 websetup_show_unsafe => {
30                         type => "boolean",
31                         example => 1,
32                         description => "show unsafe settings, read-only, in web interface?",
33                         safe => 0,
34                         rebuild => 0,
35                 },
36 } #}}}
37
38 sub checkconfig () { #{{{
39         if (! exists $config{websetup_show_unsafe}) {
40                 $config{websetup_show_unsafe}=1;
41         }
42 } #}}}
43
44 sub formatexample ($$) { #{{{
45         my $example=shift;
46         my $value=shift;
47
48         if (defined $value && length $value) {
49                 return "";
50         }
51         elsif (defined $example && ! ref $example && length $example) {
52                 return "<br/ ><small>Example: <tt>$example</tt></small>";
53         }
54         else {
55                 return "";
56         }
57 } #}}}
58
59 sub showfields ($$$@) { #{{{
60         my $form=shift;
61         my $plugin=shift;
62         my $enabled=shift;
63
64         my @show;
65         my %plugininfo;
66         while (@_) {
67                 my $key=shift;
68                 my %info=%{shift()};
69
70                 # skip internal settings
71                 next if defined $info{type} && $info{type} eq "internal";
72                 # XXX hashes not handled yet
73                 next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
74                 # maybe skip unsafe settings
75                 next if ! $info{safe} && ! ($config{websetup_show_unsafe} && $config{websetup_advanced});
76                 # maybe skip advanced settings
77                 next if $info{advanced} && ! $config{websetup_advanced};
78                 # these are handled specially, so don't show
79                 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
80
81                 if ($key eq 'plugin') {
82                         %plugininfo=%info;
83                         next;
84                 }
85                 
86                 push @show, $key, \%info;
87         }
88
89         my $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
90         my %enabledfields;
91         my $shownfields=0;
92         
93         my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
94                 (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
95         if ($plugin_forced && ! $enabled) {
96                 # plugin is forced disabled, so skip its settings
97                 @show=();
98         }
99
100         # show plugin toggle
101         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
102                 my $name="enable.$plugin";
103                 $form->field(
104                         name => $name,
105                         label => "",
106                         type => "checkbox",
107                         options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
108                         value => $enabled,
109                         fieldset => $section,
110                 );
111                 if ($plugin_forced) {
112                         $form->field(name => $name, disabled => 1);
113                 }
114                 else {
115                         $enabledfields{$name}=[$name, \%plugininfo];
116                 }
117         }
118
119         # show plugin settings
120         while (@show) {
121                 my $key=shift @show;
122                 my %info=%{shift @show};
123
124                 my $description=$info{description};
125                 if (exists $info{link} && length $info{link}) {
126                         if ($info{link} =~ /^\w+:\/\//) {
127                                 $description="<a href=\"$info{link}\">$description</a>";
128                         }
129                         else {
130                                 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
131                         }
132                 }
133
134                 # multiple plugins can have the same field
135                 my $name=defined $plugin ? $plugin.".".$key : $key;
136
137                 my $value=$config{$key};
138
139                 if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
140                         push @{$value}, "", ""; # blank items for expansion
141                 }
142
143                 if ($info{type} eq "string") {
144                         $form->field(
145                                 name => $name,
146                                 label => $description,
147                                 comment => formatexample($info{example}, $value),
148                                 type => "text",
149                                 value => $value,
150                                 size => 60,
151                                 fieldset => $section,
152                         );
153                 }
154                 elsif ($info{type} eq "pagespec") {
155                         $form->field(
156                                 name => $name,
157                                 label => $description,
158                                 comment => formatexample($info{example}, $value),
159                                 type => "text",
160                                 value => $value,
161                                 size => 60,
162                                 validate => \&IkiWiki::pagespec_valid,
163                                 fieldset => $section,
164                         );
165                 }
166                 elsif ($info{type} eq "integer") {
167                         $form->field(
168                                 name => $name,
169                                 label => $description,
170                                 comment => formatexample($info{example}, $value),
171                                 type => "text",
172                                 value => $value,
173                                 size => 5,
174                                 validate => '/^[0-9]+$/',
175                                 fieldset => $section,
176                         );
177                 }
178                 elsif ($info{type} eq "boolean") {
179                         $form->field(
180                                 name => $name,
181                                 label => "",
182                                 type => "checkbox",
183                                 value => $value,
184                                 options => [ [ 1 => $description ] ],
185                                 fieldset => $section,
186                         );
187                 }
188                 
189                 if (! $info{safe}) {
190                         $form->field(name => $name, disabled => 1);
191                 }
192                 else {
193                         $enabledfields{$name}=[$key, \%info];
194                 }
195                 $shownfields++;
196         }
197         
198         # if no fields were shown for the plugin, drop it into the
199         # plugins fieldset
200         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
201             ! $shownfields) {
202                 $form->field(name => "enable.$plugin", fieldset => "plugins");
203         }
204
205         return %enabledfields;
206 } #}}}
207
208 sub showform ($$) { #{{{
209         my $cgi=shift;
210         my $session=shift;
211
212         if (! defined $session->param("name") || 
213             ! IkiWiki::is_admin($session->param("name"))) {
214                 error(gettext("you are not logged in as an admin"));
215         }
216
217         eval q{use CGI::FormBuilder};
218         error($@) if $@;
219
220         my $form = CGI::FormBuilder->new(
221                 title => "setup",
222                 name => "setup",
223                 header => 0,
224                 charset => "utf-8",
225                 method => 'POST',
226                 javascript => 0,
227                 reset => 1,
228                 params => $cgi,
229                 fieldsets => [
230                         [main => gettext("main")], 
231                         [plugins => gettext("plugins")]
232                 ],
233                 action => $config{cgiurl},
234                 template => {type => 'div'},
235                 stylesheet => IkiWiki::baseurl()."style.css",
236         );
237         
238         $form->field(name => "do", type => "hidden", value => "setup",
239                 force => 1);
240
241         if ($form->submitted eq 'Basic Mode') {
242                 $form->field(name => "showadvanced", type => "hidden", 
243                         value => 0, force => 1);
244         }
245         elsif ($form->submitted eq 'Advanced Mode') {
246                 $form->field(name => "showadvanced", type => "hidden", 
247                         value => 1, force => 1);
248         }
249         my $advancedtoggle;
250         if ($form->field("showadvanced")) {
251                 $config{websetup_advanced}=1;
252                 $advancedtoggle="Basic Mode";
253         }
254         else {
255                 $config{websetup_advanced}=0;
256                 $advancedtoggle="Advanced Mode";
257         }
258
259         my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
260
261         IkiWiki::decode_form_utf8($form);
262         IkiWiki::run_hooks(formbuilder_setup => sub {
263                 shift->(form => $form, cgi => $cgi, session => $session,
264                         buttons => $buttons);
265         });
266         IkiWiki::decode_form_utf8($form);
267
268         my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
269         
270         # record all currently enabled plugins before all are loaded
271         my %enabled_plugins=%IkiWiki::loaded_plugins;
272
273         # per-plugin setup
274         require IkiWiki::Setup;
275         foreach my $pair (IkiWiki::Setup::getsetup()) {
276                 my $plugin=$pair->[0];
277                 my $setup=$pair->[1];
278
279                 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
280                 if (%shown) {
281                         $fields{$_}=$shown{$_} foreach keys %shown;
282                 }
283         }
284         
285         if ($form->submitted eq "Cancel") {
286                 IkiWiki::redirect($cgi, $config{url});
287                 return;
288         }
289         elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
290                 # Push values from form into %config, avoiding unnecessary
291                 # changes, and keeping track of which changes need a
292                 # rebuild.
293                 my %rebuild;
294                 foreach my $field (keys %fields) {
295                         my %info=%{$fields{$field}->[1]};
296                         my $key=$fields{$field}->[0];
297                         my @value=$form->field($field);
298                         if (! @value) {
299                                 @value=0;
300                         }
301                 
302                         if (! $info{safe}) {
303                                 error("unsafe field $key"); # should never happen
304                         }
305                 
306                         if (exists $info{rebuild} &&
307                             ($info{rebuild} || ! defined $info{rebuild})) {
308                                 $rebuild{$field}=$info{rebuild};
309                         }
310                                         
311                         if ($field=~/^enable\.(.*)/) {
312                                 my $plugin=$1;
313                                 if ($value[0] != exists $enabled_plugins{$plugin}) {
314                                         # TODO plugin enable/disable
315                                 }
316                                 else {
317                                         delete $rebuild{$field};
318                                 }
319                                 next;
320                         }
321
322                         if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
323                                 @value=sort grep { length $_ } @value;
324                                 my @oldvalue=sort grep { length $_ }
325                                         (defined $config{$key} ? @{$config{$key}} : ());
326                                 if ((@oldvalue) == (@value)) {
327                                         delete $rebuild{$field};
328                                 }
329                                 else {
330                                         $config{$key}=\@value;
331                                 }
332                         }
333                         elsif (ref $config{$key} || ref $info{example}) {
334                                 error("complex field $key"); # should never happen
335                         }
336                         else {
337                                 if (defined $config{$key} && $config{$key} eq $value[0]) {
338                                         delete $rebuild{$field};
339                                 }
340                                 elsif (! defined $config{$key} && ! length $value[0]) {
341                                         delete $rebuild{$field};
342                                 }
343                                 elsif (! defined $config{$key} && ! $value[0] &&
344                                        $info{type} eq "boolean") {
345                                         delete $rebuild{$field};
346                                 }
347                                 else {
348                                         print STDERR ">>$key (@value) ($config{$key})\n";
349                                         $config{$key}=$value[0];
350                                 }
351                         }
352                 }
353                 
354                 if (%rebuild && ! $form->field("rebuild_asked")) {
355                         print STDERR ">>".(join "," , keys %rebuild)."\n";
356                         my $required=0;
357                         foreach my $field ($form->field) {
358                                 $required=1 if $rebuild{$field};
359                                 next if exists $rebuild{$field};
360                                 $form->field(name => $field, type => "hidden",
361                                         force => 1);
362                         }
363                         if ($required) {
364                                 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
365                                 $buttons=["Rebuild Wiki", "Cancel"];
366                         }
367                         else {
368                                 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
369                                 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
370                         }
371                         $form->field(name => "rebuild_asked", type => "hidden",
372                                 value => 1, force => 1);
373                         $form->reset(0); # doesn't really make sense here
374                 }
375                 else {
376                         $form->field(name => "rebuild_asked", type => "hidden",
377                                 value => 0, force => 1);
378                         # TODO save to real path
379                         IkiWiki::Setup::dump("/tmp/s");
380                         $form->text(gettext("Setup saved."));
381         
382                         if ($form->submitted eq 'Rebuild Wiki') {
383                                 # TODO rebuild
384                         }
385                 }
386         }
387
388         IkiWiki::showform($form, $buttons, $session, $cgi);
389 } #}}}
390
391 sub sessioncgi ($$) { #{{{
392         my $cgi=shift;
393         my $session=shift;
394
395         if ($cgi->param("do") eq "setup") {
396                 showform($cgi, $session);
397                 exit;
398         }
399 } #}}}
400
401 sub formbuilder_setup (@) { #{{{
402         my %params=@_;
403
404         my $form=$params{form};
405         if ($form->title eq "preferences") {
406                 push @{$params{buttons}}, "Wiki Setup";
407                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
408                         showform($params{cgi}, $params{session});
409                         exit;
410                 }
411         }
412 } #}}}
413
414 1