plugin enable/disable working and bugfixes
[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                         fieldset => $section,
108                         options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ]]
109                 );
110                 if (! $form->submitted) {
111                         $form->field(name => $name, value => $enabled);
112                 }
113                 if ($plugin_forced) {
114                         $form->field(name => $name, disabled => 1);
115                 }
116                 else {
117                         $enabledfields{$name}=[$name, \%plugininfo];
118                 }
119         }
120
121         # show plugin settings
122         while (@show) {
123                 my $key=shift @show;
124                 my %info=%{shift @show};
125
126                 my $description=$info{description};
127                 if (exists $info{link} && length $info{link}) {
128                         if ($info{link} =~ /^\w+:\/\//) {
129                                 $description="<a href=\"$info{link}\">$description</a>";
130                         }
131                         else {
132                                 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
133                         }
134                 }
135
136                 # multiple plugins can have the same field
137                 my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;
138
139                 my $value=$config{$key};
140
141                 if ($info{safe} && (ref $value eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
142                         $value=[@{$value}, "", ""]; # blank items for expansion
143                 }
144
145                 if ($info{type} eq "string") {
146                         $form->field(
147                                 name => $name,
148                                 label => $description,
149                                 comment => formatexample($info{example}, $value),
150                                 type => "text",
151                                 value => $value,
152                                 size => 60,
153                                 fieldset => $section,
154                         );
155                 }
156                 elsif ($info{type} eq "pagespec") {
157                         $form->field(
158                                 name => $name,
159                                 label => $description,
160                                 comment => formatexample($info{example}, $value),
161                                 type => "text",
162                                 value => $value,
163                                 size => 60,
164                                 validate => \&IkiWiki::pagespec_valid,
165                                 fieldset => $section,
166                         );
167                 }
168                 elsif ($info{type} eq "integer") {
169                         $form->field(
170                                 name => $name,
171                                 label => $description,
172                                 comment => formatexample($info{example}, $value),
173                                 type => "text",
174                                 value => $value,
175                                 size => 5,
176                                 validate => '/^[0-9]+$/',
177                                 fieldset => $section,
178                         );
179                 }
180                 elsif ($info{type} eq "boolean") {
181                         $form->field(
182                                 name => $name,
183                                 label => "",
184                                 type => "checkbox",
185                                 options => [ [ 1 => $description ] ],
186                                 fieldset => $section,
187                         );
188                         if (! $form->submitted) {
189                                 $form->field(name => $name, value => $value);
190                         }
191                 }
192                 
193                 if (! $info{safe}) {
194                         $form->field(name => $name, disabled => 1);
195                 }
196                 else {
197                         $enabledfields{$name}=[$key, \%info];
198                 }
199                 $shownfields++;
200         }
201         
202         # if no fields were shown for the plugin, drop it into the
203         # plugins fieldset
204         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
205             ! $shownfields) {
206                 $form->field(name => "enable.$plugin", fieldset => "plugins");
207         }
208
209         return %enabledfields;
210 } #}}}
211
212 sub enable_plugin ($) { #{{{
213         my $plugin=shift;
214
215         $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
216         push @{$config{add_plugins}}, $plugin;
217 }
218
219 sub disable_plugin ($) { #{{{
220         my $plugin=shift;
221
222         if (grep { $_ eq $plugin } @{$config{add_plugins}}) {
223                 $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
224         }
225         else {
226                 push @{$config{disable_plugins}}, $plugin;
227         }
228 }
229
230 sub showform ($$) { #{{{
231         my $cgi=shift;
232         my $session=shift;
233
234         if (! defined $session->param("name") || 
235             ! IkiWiki::is_admin($session->param("name"))) {
236                 error(gettext("you are not logged in as an admin"));
237         }
238
239         eval q{use CGI::FormBuilder};
240         error($@) if $@;
241
242         my $form = CGI::FormBuilder->new(
243                 title => "setup",
244                 name => "setup",
245                 header => 0,
246                 charset => "utf-8",
247                 method => 'POST',
248                 javascript => 0,
249                 reset => 1,
250                 params => $cgi,
251                 fieldsets => [
252                         [main => gettext("main")], 
253                         [plugins => gettext("plugins")]
254                 ],
255                 action => $config{cgiurl},
256                 template => {type => 'div'},
257                 stylesheet => IkiWiki::baseurl()."style.css",
258         );
259         
260         $form->field(name => "do", type => "hidden", value => "setup",
261                 force => 1);
262         $form->field(name => "rebuild_asked", type => "hidden");
263
264         if ($form->submitted eq 'Basic Mode') {
265                 $form->field(name => "showadvanced", type => "hidden", 
266                         value => 0, force => 1);
267         }
268         elsif ($form->submitted eq 'Advanced Mode') {
269                 $form->field(name => "showadvanced", type => "hidden", 
270                         value => 1, force => 1);
271         }
272         my $advancedtoggle;
273         if ($form->field("showadvanced")) {
274                 $config{websetup_advanced}=1;
275                 $advancedtoggle="Basic Mode";
276         }
277         else {
278                 $config{websetup_advanced}=0;
279                 $advancedtoggle="Advanced Mode";
280         }
281
282         my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
283
284         IkiWiki::decode_form_utf8($form);
285         IkiWiki::run_hooks(formbuilder_setup => sub {
286                 shift->(form => $form, cgi => $cgi, session => $session,
287                         buttons => $buttons);
288         });
289         IkiWiki::decode_form_utf8($form);
290
291         my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
292         
293         # record all currently enabled plugins before all are loaded
294         my %enabled_plugins=%IkiWiki::loaded_plugins;
295
296         # per-plugin setup
297         require IkiWiki::Setup;
298         foreach my $pair (IkiWiki::Setup::getsetup()) {
299                 my $plugin=$pair->[0];
300                 my $setup=$pair->[1];
301
302                 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
303                 if (%shown) {
304                         $fields{$_}=$shown{$_} foreach keys %shown;
305                 }
306         }
307         
308         if ($form->submitted eq "Cancel") {
309                 IkiWiki::redirect($cgi, $config{url});
310                 return;
311         }
312         elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
313                 # Push values from form into %config, avoiding unnecessary
314                 # changes, and keeping track of which changes need a
315                 # rebuild.
316                 my %rebuild;
317                 foreach my $field (keys %fields) {
318                         my %info=%{$fields{$field}->[1]};
319                         my $key=$fields{$field}->[0];
320                         my @value=$form->field($field);
321                         if (! @value) {
322                                 @value=0;
323                         }
324                 
325                         if (! $info{safe}) {
326                                 error("unsafe field $key"); # should never happen
327                         }
328                 
329                         if (exists $info{rebuild} &&
330                             ($info{rebuild} || ! defined $info{rebuild})) {
331                                 $rebuild{$field}=$info{rebuild};
332                         }
333                                         
334                         if ($field=~/^enable\.(.*)/) {
335                                 my $plugin=$1;
336                                 $value[0]=0 if ! length $value[0];
337                                 if ($value[0] != exists $enabled_plugins{$plugin}) {
338                                         if ($value[0]) {
339                                                 enable_plugin($plugin);
340                                         }
341                                         else {
342                                                 disable_plugin($plugin);
343
344                                         }
345                                 }
346                                 else {
347                                         delete $rebuild{$field};
348                                 }
349                                 next;
350                         }
351
352                         if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
353                                 @value=sort grep { length $_ } @value;
354                                 my @oldvalue=sort grep { length $_ }
355                                         (defined $config{$key} ? @{$config{$key}} : ());
356                                 if ((@oldvalue) == (@value)) {
357                                         delete $rebuild{$field};
358                                 }
359                                 else {
360                                         $config{$key}=\@value;
361                                 }
362                         }
363                         elsif (ref $config{$key} || ref $info{example}) {
364                                 error("complex field $key"); # should never happen
365                         }
366                         else {
367                                 if (defined $config{$key} && $config{$key} eq $value[0]) {
368                                         delete $rebuild{$field};
369                                 }
370                                 elsif (! defined $config{$key} && ! length $value[0]) {
371                                         delete $rebuild{$field};
372                                 }
373                                 elsif ((! defined $config{$key} || ! $config{$key}) &&
374                                        ! $value[0] && $info{type} eq "boolean") {
375                                         delete $rebuild{$field};
376                                 }
377                                 else {
378                                         $config{$key}=$value[0];
379                                 }
380                         }
381                 }
382                 
383                 if (%rebuild && ! $form->field("rebuild_asked")) {
384                         my $required=0;
385                         foreach my $field ($form->field) {
386                                 $required=1 if $rebuild{$field};
387                                 next if exists $rebuild{$field};
388                                 $form->field(name => $field, type => "hidden");
389                         }
390                         if ($required) {
391                                 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
392                                 $buttons=["Rebuild Wiki", "Cancel"];
393                         }
394                         else {
395                                 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
396                                 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
397                         }
398                         $form->field(name => "rebuild_asked", value => 1, force => 1);
399                         $form->reset(0); # doesn't really make sense here
400                 }
401                 else {
402                         $form->field(name => "rebuild_asked", type => "hidden",
403                                 value => 0, force => 1);
404                         # TODO save to real path
405                         IkiWiki::Setup::dump("/tmp/s");
406                         $form->text(gettext("Setup saved."));
407         
408                         if ($form->submitted eq 'Rebuild Wiki') {
409                                 # TODO rebuild
410                         }
411                 }
412         }
413
414         IkiWiki::showform($form, $buttons, $session, $cgi);
415 } #}}}
416
417 sub sessioncgi ($$) { #{{{
418         my $cgi=shift;
419         my $session=shift;
420
421         if ($cgi->param("do") eq "setup") {
422                 showform($cgi, $session);
423                 exit;
424         }
425 } #}}}
426
427 sub formbuilder_setup (@) { #{{{
428         my %params=@_;
429
430         my $form=$params{form};
431         if ($form->title eq "preferences") {
432                 push @{$params{buttons}}, "Wiki Setup";
433                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
434                         showform($params{cgi}, $params{session});
435                         exit;
436                 }
437         }
438 } #}}}
439
440 1