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