]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/websetup.pm
websetup: Fix saving of advanced mode changes.
[git.ikiwiki.info.git] / IkiWiki / Plugin / websetup.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::websetup;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
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 }
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                         section => "web",
22                 },
23                 websetup_force_plugins => {
24                         type => "string",
25                         example => [],
26                         description => "list of plugins that cannot be enabled/disabled via the web interface",
27                         safe => 0,
28                         rebuild => 0,
29                 },
30                 websetup_unsafe => {
31                         type => "string",
32                         example => [],
33                         description => "list of additional setup field keys to treat as unsafe",
34                         safe => 0,
35                         rebuild => 0,
36                 },
37                 websetup_show_unsafe => {
38                         type => "boolean",
39                         example => 1,
40                         description => "show unsafe settings, read-only, in web interface?",
41                         safe => 0,
42                         rebuild => 0,
43                 },
44 }
46 sub checkconfig () {
47         if (! exists $config{websetup_show_unsafe}) {
48                 $config{websetup_show_unsafe}=1;
49         }
50 }
52 sub formatexample ($$) {
53         my $example=shift;
54         my $value=shift;
56         if (defined $value && length $value) {
57                 return "";
58         }
59         elsif (defined $example && ! ref $example && length $example) {
60                 return "<br/ ><small>Example: <tt>$example</tt></small>";
61         }
62         else {
63                 return "";
64         }
65 }
67 sub issafe ($) {
68         my $key=shift;
70         return ! grep { $_ eq $key } @{$config{websetup_unsafe}};
71 }
73 sub showfields ($$$@) {
74         my $form=shift;
75         my $plugin=shift;
76         my $enabled=shift;
78         my @show;
79         my %plugininfo;
80         while (@_) {
81                 my $key=shift;
82                 my %info=%{shift()};
83                 
84                 if ($key eq 'plugin') {
85                         %plugininfo=%info;
86                         next;
87                 }
89                 # skip internal settings
90                 next if defined $info{type} && $info{type} eq "internal";
91                 # XXX hashes not handled yet
92                 next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
93                 # maybe skip unsafe settings
94                 next if ! ($config{websetup_show_unsafe} && $config{websetup_advanced}) &&
95                         (! $info{safe} || ! issafe($key));
96                 # maybe skip advanced settings
97                 next if $info{advanced} && ! $config{websetup_advanced};
98                 # these are handled specially, so don't show
99                 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
101                 push @show, $key, \%info;
102         }
104         my $section=defined $plugin
105                 ? sprintf(gettext("%s plugin:"), $plugininfo{section})." ".$plugin
106                 : "main";
107         my %enabledfields;
108         my $shownfields=0;
109         
110         my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
111                 (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
112         if ($plugin_forced && ! $enabled) {
113                 # plugin is forced disabled, so skip its settings
114                 @show=();
115         }
117         my $section_fieldset;
118         if (defined $plugin) {
119                 # Define the combined fieldset for the plugin's section.
120                 # This ensures that this fieldset comes first.
121                 $section_fieldset=sprintf(gettext("%s plugins"), $plugininfo{section});
122                 $form->field(name => "placeholder.$plugininfo{section}",
123                         type => "hidden",
124                         fieldset => $section_fieldset);
125         }
127         # show plugin toggle
128         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
129                 my $name="enable.$plugin";
130                 $form->field(
131                         name => $name,
132                         label => "",
133                         type => "checkbox",
134                         fieldset => $section,
135                         options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ]]
136                 );
137                 if (! $form->submitted) {
138                         $form->field(name => $name, value => $enabled);
139                 }
140                 if ($plugin_forced) {
141                         $form->field(name => $name, disabled => 1);
142                 }
143                 else {
144                         $enabledfields{$name}=[$name, \%plugininfo];
145                 }
146         }
148         # show plugin settings
149         while (@show) {
150                 my $key=shift @show;
151                 my %info=%{shift @show};
153                 my $description=$info{description};
154                 if (exists $info{link} && length $info{link}) {
155                         if ($info{link} =~ /^\w+:\/\//) {
156                                 $description="<a href=\"$info{link}\">$description</a>";
157                         }
158                         else {
159                                 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
160                         }
161                 }
163                 # multiple plugins can have the same field
164                 my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;
166                 my $value=$config{$key};
167                 if (! defined $value) {
168                         $value="";
169                 }
171                 if (ref $value eq 'ARRAY' || ref $info{example} eq 'ARRAY') {
172                         $value=[(ref $value eq 'ARRAY' ? map { Encode::encode_utf8($_) }  @{$value} : "")];
173                         push @$value, "", "" if $info{safe} && issafe($key); # blank items for expansion
174                 }
175                 else {
176                         $value=Encode::encode_utf8($value);
177                 }
179                 if ($info{type} eq "string") {
180                         $form->field(
181                                 name => $name,
182                                 label => $description,
183                                 comment => formatexample($info{example}, $value),
184                                 type => "text",
185                                 value => $value,
186                                 size => 60,
187                                 fieldset => $section,
188                         );
189                 }
190                 elsif ($info{type} eq "pagespec") {
191                         $form->field(
192                                 name => $name,
193                                 label => $description,
194                                 comment => formatexample($info{example}, $value),
195                                 type => "text",
196                                 value => $value,
197                                 size => 60,
198                                 validate => \&IkiWiki::pagespec_valid,
199                                 fieldset => $section,
200                         );
201                 }
202                 elsif ($info{type} eq "integer") {
203                         $form->field(
204                                 name => $name,
205                                 label => $description,
206                                 comment => formatexample($info{example}, $value),
207                                 type => "text",
208                                 value => $value,
209                                 size => 5,
210                                 validate => '/^[0-9]+$/',
211                                 fieldset => $section,
212                         );
213                 }
214                 elsif ($info{type} eq "boolean") {
215                         $form->field(
216                                 name => $name,
217                                 label => "",
218                                 type => "checkbox",
219                                 options => [ [ 1 => $description ] ],
220                                 fieldset => $section,
221                         );
222                         if (! $form->submitted) {
223                                 $form->field(name => $name, value => $value);
224                         }
225                 }
226                 
227                 if (! $info{safe} || ! issafe($key)) {
228                         $form->field(name => $name, disabled => 1);
229                 }
230                 else {
231                         $enabledfields{$name}=[$key, \%info];
232                 }
233                 $shownfields++;
234         }
235         
236         # if no fields were shown for the plugin, drop it into a combined
237         # fieldset for its section
238         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
239             ! $shownfields) {
240                 $form->field(name => "enable.$plugin", fieldset => $section_fieldset);
241         }
243         return %enabledfields;
246 sub enable_plugin ($) {
247         my $plugin=shift;
249         $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
250         push @{$config{add_plugins}}, $plugin;
253 sub disable_plugin ($) {
254         my $plugin=shift;
256         $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
257         push @{$config{disable_plugins}}, $plugin;
260 sub showform ($$) {
261         my $cgi=shift;
262         my $session=shift;
264         IkiWiki::needsignin($cgi, $session);
266         if (! defined $session->param("name") || 
267             ! IkiWiki::is_admin($session->param("name"))) {
268                 error(gettext("you are not logged in as an admin"));
269         }
271         if (! exists $config{setupfile}) {
272                 error(gettext("setup file for this wiki is not known"));
273         }
275         eval q{use CGI::FormBuilder};
276         error($@) if $@;
278         my $form = CGI::FormBuilder->new(
279                 title => "setup",
280                 name => "setup",
281                 header => 0,
282                 charset => "utf-8",
283                 method => 'POST',
284                 javascript => 0,
285                 reset => 1,
286                 params => $cgi,
287                 fieldsets => [
288                         [main => gettext("main")], 
289                 ],
290                 action => $config{cgiurl},
291                 template => {type => 'div'},
292                 stylesheet => 1,
293         );
294         
295         $form->field(name => "do", type => "hidden", value => "setup",
296                 force => 1);
297         $form->field(name => "rebuild_asked", type => "hidden");
298         $form->field(name => "showadvanced", type => "hidden");
300         if ($form->submitted eq 'Basic Mode') {
301                 $form->field(name => "showadvanced", type => "hidden", 
302                         value => 0, force => 1);
303         }
304         elsif ($form->submitted eq 'Advanced Mode') {
305                 $form->field(name => "showadvanced", type => "hidden", 
306                         value => 1, force => 1);
307         }
308         my $advancedtoggle;
309         if ($form->field("showadvanced")) {
310                 $config{websetup_advanced}=1;
311                 $advancedtoggle="Basic Mode";
312         }
313         else {
314                 $config{websetup_advanced}=0;
315                 $advancedtoggle="Advanced Mode";
316         }
318         my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
320         IkiWiki::decode_form_utf8($form);
321         IkiWiki::run_hooks(formbuilder_setup => sub {
322                 shift->(form => $form, cgi => $cgi, session => $session,
323                         buttons => $buttons);
324         });
326         my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
327         
328         # record all currently enabled plugins before all are loaded
329         my %enabled_plugins=%IkiWiki::loaded_plugins;
331         # per-plugin setup
332         require IkiWiki::Setup;
333         foreach my $pair (IkiWiki::Setup::getsetup()) {
334                 my $plugin=$pair->[0];
335                 my $setup=$pair->[1];
337                 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
338                 if (%shown) {
339                         $fields{$_}=$shown{$_} foreach keys %shown;
340                 }
341         }
343         IkiWiki::decode_form_utf8($form);
344         
345         if ($form->submitted eq "Cancel") {
346                 IkiWiki::redirect($cgi, $config{url});
347                 return;
348         }
349         elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
350                 # Push values from form into %config, avoiding unnecessary
351                 # changes, and keeping track of which changes need a
352                 # rebuild.
353                 my %rebuild;
354                 foreach my $field (keys %fields) {
355                         my %info=%{$fields{$field}->[1]};
356                         my $key=$fields{$field}->[0];
357                         my @value=$form->field($field);
358                         if (! @value) {
359                                 @value=0;
360                         }
361                 
362                         if (! $info{safe} || ! issafe($key)) {
363                                 error("unsafe field $key"); # should never happen
364                         }
365                 
366                         if (exists $info{rebuild} &&
367                             ($info{rebuild} || ! defined $info{rebuild})) {
368                                 $rebuild{$field}=$info{rebuild};
369                         }
370                                         
371                         if ($field=~/^enable\.(.*)/) {
372                                 my $plugin=$1;
373                                 $value[0]=0 if ! length $value[0];
374                                 if ($value[0] != exists $enabled_plugins{$plugin}) {
375                                         if ($value[0]) {
376                                                 enable_plugin($plugin);
377                                         }
378                                         else {
379                                                 disable_plugin($plugin);
381                                         }
382                                 }
383                                 else {
384                                         delete $rebuild{$field};
385                                 }
386                                 next;
387                         }
389                         if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
390                                 @value=sort grep { length $_ } @value;
391                                 my @oldvalue=sort grep { length $_ }
392                                         (defined $config{$key} ? @{$config{$key}} : ());
393                                 my $same=(@oldvalue) == (@value);
394                                 for (my $x=0; $same && $x < @value; $x++) {
395                                         $same=0 if $value[$x] ne $oldvalue[$x];
396                                 }
397                                 if ($same) {
398                                         delete $rebuild{$field};
399                                 }
400                                 else {
401                                         $config{$key}=\@value;
402                                 }
403                         }
404                         elsif (ref $config{$key} || ref $info{example}) {
405                                 error("complex field $key"); # should never happen
406                         }
407                         else {
408                                 if (defined $config{$key} && $config{$key} eq $value[0]) {
409                                         delete $rebuild{$field};
410                                 }
411                                 elsif (! defined $config{$key} && ! length $value[0]) {
412                                         delete $rebuild{$field};
413                                 }
414                                 elsif ((! defined $config{$key} || ! $config{$key}) &&
415                                        ! $value[0] && $info{type} eq "boolean") {
416                                         delete $rebuild{$field};
417                                 }
418                                 else {
419                                         $config{$key}=$value[0];
420                                 }
421                         }
422                 }
423                 
424                 if (%rebuild && ! $form->field("rebuild_asked")) {
425                         my $required=0;
426                         foreach my $field ($form->field) {
427                                 $required=1 if $rebuild{$field};
428                                 next if exists $rebuild{$field};
429                                 $form->field(name => $field, type => "hidden");
430                         }
431                         if ($required) {
432                                 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
433                                 $buttons=["Rebuild Wiki", "Cancel"];
434                         }
435                         else {
436                                 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
437                                 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
438                         }
439                         $form->field(name => "rebuild_asked", value => 1, force => 1);
440                         $form->reset(0); # doesn't really make sense here
441                 }
442                 else {
443                         my $oldsetup=readfile($config{setupfile});
444                         IkiWiki::Setup::dump($config{setupfile});
446                         IkiWiki::saveindex();
447                         IkiWiki::unlockwiki();
449                         # Print the top part of a standard misctemplate,
450                         # then show the rebuild or refresh, live.
451                         my $divider="\0";
452                         my $html=IkiWiki::misctemplate("setup", $divider);
453                         IkiWiki::printheader($session);
454                         my ($head, $tail)=split($divider, $html, 2);
455                         print $head."<pre>\n";
457                         my @command;
458                         if ($form->submitted eq 'Rebuild Wiki') {
459                                 @command=("ikiwiki", "-setup", $config{setupfile},
460                                         "-rebuild", "-v");
461                         }
462                         else {
463                                 @command=("ikiwiki", "-setup", $config{setupfile},
464                                         "-refresh", "-wrappers", "-v");
465                         }
467                         close STDERR;
468                         open(STDERR, ">&STDOUT");
469                         my $ret=system(@command);
470                         print "\n<\/pre>";
471                         if ($ret != 0) {
472                                 print '<p class="error">'.
473                                         sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
474                                                 join(" ", @command), $ret).
475                                         '</p>';
476                                 open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
477                                 print OUT $oldsetup;
478                                 close OUT;
479                         }
481                         print $tail;
482                         exit 0;
483                 }
484         }
486         IkiWiki::showform($form, $buttons, $session, $cgi);
489 sub sessioncgi ($$) {
490         my $cgi=shift;
491         my $session=shift;
493         if ($cgi->param("do") eq "setup") {
494                 showform($cgi, $session);
495                 exit;
496         }
499 sub formbuilder_setup (@) {
500         my %params=@_;
502         my $form=$params{form};
503         if ($form->title eq "preferences" &&
504             IkiWiki::is_admin($params{session}->param("name"))) {
505                 push @{$params{buttons}}, "Setup";
506                 if ($form->submitted && $form->submitted eq "Setup") {
507                         showform($params{cgi}, $params{session});
508                         exit;
509                 }
510         }