]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/websetup.pm
fix further places where translated discussion case was assumed
[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()};
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';
81                 if ($key eq 'plugin') {
82                         %plugininfo=%info;
83                         next;
84                 }
85                 
86                 push @show, $key, \%info;
87         }
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         }
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         }
121         # show plugin settings
122         while (@show) {
123                 my $key=shift @show;
124                 my %info=%{shift @show};
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                 }
136                 # multiple plugins can have the same field
137                 my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;
139                 my $value=$config{$key};
141                 if ($info{safe} && (ref $value eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
142                         $value=[(ref $value eq 'ARRAY' ? @{$value} : ""), "", ""]; # blank items for expansion
143                 }
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         }
209         return %enabledfields;
212 sub enable_plugin ($) {
213         my $plugin=shift;
215         $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
216         push @{$config{add_plugins}}, $plugin;
219 sub disable_plugin ($) {
220         my $plugin=shift;
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         }
230 sub showform ($$) {
231         my $cgi=shift;
232         my $session=shift;
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         }
239         if (! exists $config{setupfile}) {
240                 error(gettext("setup file for this wiki is not known"));
241         }
243         eval q{use CGI::FormBuilder};
244         error($@) if $@;
246         my $form = CGI::FormBuilder->new(
247                 title => "setup",
248                 name => "setup",
249                 header => 0,
250                 charset => "utf-8",
251                 method => 'POST',
252                 javascript => 0,
253                 reset => 1,
254                 params => $cgi,
255                 fieldsets => [
256                         [main => gettext("main")], 
257                         [plugins => gettext("plugins")]
258                 ],
259                 action => $config{cgiurl},
260                 template => {type => 'div'},
261                 stylesheet => IkiWiki::baseurl()."style.css",
262         );
263         
264         $form->field(name => "do", type => "hidden", value => "setup",
265                 force => 1);
266         $form->field(name => "rebuild_asked", type => "hidden");
268         if ($form->submitted eq 'Basic Mode') {
269                 $form->field(name => "showadvanced", type => "hidden", 
270                         value => 0, force => 1);
271         }
272         elsif ($form->submitted eq 'Advanced Mode') {
273                 $form->field(name => "showadvanced", type => "hidden", 
274                         value => 1, force => 1);
275         }
276         my $advancedtoggle;
277         if ($form->field("showadvanced")) {
278                 $config{websetup_advanced}=1;
279                 $advancedtoggle="Basic Mode";
280         }
281         else {
282                 $config{websetup_advanced}=0;
283                 $advancedtoggle="Advanced Mode";
284         }
286         my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
288         IkiWiki::decode_form_utf8($form);
289         IkiWiki::run_hooks(formbuilder_setup => sub {
290                 shift->(form => $form, cgi => $cgi, session => $session,
291                         buttons => $buttons);
292         });
293         IkiWiki::decode_form_utf8($form);
295         my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
296         
297         # record all currently enabled plugins before all are loaded
298         my %enabled_plugins=%IkiWiki::loaded_plugins;
300         # per-plugin setup
301         require IkiWiki::Setup;
302         foreach my $pair (IkiWiki::Setup::getsetup()) {
303                 my $plugin=$pair->[0];
304                 my $setup=$pair->[1];
306                 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
307                 if (%shown) {
308                         $fields{$_}=$shown{$_} foreach keys %shown;
309                 }
310         }
311         
312         if ($form->submitted eq "Cancel") {
313                 IkiWiki::redirect($cgi, $config{url});
314                 return;
315         }
316         elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
317                 # Push values from form into %config, avoiding unnecessary
318                 # changes, and keeping track of which changes need a
319                 # rebuild.
320                 my %rebuild;
321                 foreach my $field (keys %fields) {
322                         my %info=%{$fields{$field}->[1]};
323                         my $key=$fields{$field}->[0];
324                         my @value=$form->field($field);
325                         if (! @value) {
326                                 @value=0;
327                         }
328                 
329                         if (! $info{safe}) {
330                                 error("unsafe field $key"); # should never happen
331                         }
332                 
333                         if (exists $info{rebuild} &&
334                             ($info{rebuild} || ! defined $info{rebuild})) {
335                                 $rebuild{$field}=$info{rebuild};
336                         }
337                                         
338                         if ($field=~/^enable\.(.*)/) {
339                                 my $plugin=$1;
340                                 $value[0]=0 if ! length $value[0];
341                                 if ($value[0] != exists $enabled_plugins{$plugin}) {
342                                         if ($value[0]) {
343                                                 enable_plugin($plugin);
344                                         }
345                                         else {
346                                                 disable_plugin($plugin);
348                                         }
349                                 }
350                                 else {
351                                         delete $rebuild{$field};
352                                 }
353                                 next;
354                         }
356                         if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
357                                 @value=sort grep { length $_ } @value;
358                                 my @oldvalue=sort grep { length $_ }
359                                         (defined $config{$key} ? @{$config{$key}} : ());
360                                 if ((@oldvalue) == (@value)) {
361                                         delete $rebuild{$field};
362                                 }
363                                 else {
364                                         $config{$key}=\@value;
365                                 }
366                         }
367                         elsif (ref $config{$key} || ref $info{example}) {
368                                 error("complex field $key"); # should never happen
369                         }
370                         else {
371                                 if (defined $config{$key} && $config{$key} eq $value[0]) {
372                                         delete $rebuild{$field};
373                                 }
374                                 elsif (! defined $config{$key} && ! length $value[0]) {
375                                         delete $rebuild{$field};
376                                 }
377                                 elsif ((! defined $config{$key} || ! $config{$key}) &&
378                                        ! $value[0] && $info{type} eq "boolean") {
379                                         delete $rebuild{$field};
380                                 }
381                                 else {
382                                         $config{$key}=$value[0];
383                                 }
384                         }
385                 }
386                 
387                 if (%rebuild && ! $form->field("rebuild_asked")) {
388                         my $required=0;
389                         foreach my $field ($form->field) {
390                                 $required=1 if $rebuild{$field};
391                                 next if exists $rebuild{$field};
392                                 $form->field(name => $field, type => "hidden");
393                         }
394                         if ($required) {
395                                 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
396                                 $buttons=["Rebuild Wiki", "Cancel"];
397                         }
398                         else {
399                                 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
400                                 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
401                         }
402                         $form->field(name => "rebuild_asked", value => 1, force => 1);
403                         $form->reset(0); # doesn't really make sense here
404                 }
405                 else {
406                         my $oldsetup=readfile($config{setupfile});
407                         IkiWiki::Setup::dump($config{setupfile});
409                         IkiWiki::saveindex();
410                         IkiWiki::unlockwiki();
412                         # Print the top part of a standard misctemplate,
413                         # then show the rebuild or refresh.
414                         my $divider="xxx";
415                         my $html=IkiWiki::misctemplate("setup", $divider);
416                         IkiWiki::printheader($session);
417                         my ($head, $tail)=split($divider, $html, 2);
418                         print $head."<pre>\n";
420                         my @command;
421                         if ($form->submitted eq 'Rebuild Wiki') {
422                                 @command=("ikiwiki", "-setup", $config{setupfile},
423                                         "-rebuild", "-v");
424                         }
425                         else {
426                                 @command=("ikiwiki", "-setup", $config{setupfile},
427                                         "-refresh", "-wrappers", "-v");
428                         }
430                         close STDERR;
431                         open(STDERR, ">&STDOUT");
432                         my $ret=system(@command);
433                         print "\n<\/pre>";
434                         if ($ret != 0) {
435                                 print '<p class="error">'.
436                                         sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
437                                                 join(" ", @command), $ret).
438                                         '</p>';
439                                 open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
440                                 print OUT $oldsetup;
441                                 close OUT;
442                         }
444                         print $tail;
445                         exit 0;
446                 }
447         }
449         IkiWiki::showform($form, $buttons, $session, $cgi);
452 sub sessioncgi ($$) {
453         my $cgi=shift;
454         my $session=shift;
456         if ($cgi->param("do") eq "setup") {
457                 showform($cgi, $session);
458                 exit;
459         }
462 sub formbuilder_setup (@) {
463         my %params=@_;
465         my $form=$params{form};
466         if ($form->title eq "preferences") {
467                 push @{$params{buttons}}, "Wiki Setup";
468                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
469                         showform($params{cgi}, $params{session});
470                         exit;
471                 }
472         }