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