]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/websetup.pm
correcting link
[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' ? map { Encode::encode_utf8($_) }  @{$value} : ""),
143                                 "", ""]; # blank items for expansion
144                 }
145                 else {
146                         $value=Encode::encode_utf8($value);
147                 }
149                 if ($info{type} eq "string") {
150                         $form->field(
151                                 name => $name,
152                                 label => $description,
153                                 comment => formatexample($info{example}, $value),
154                                 type => "text",
155                                 value => $value,
156                                 size => 60,
157                                 fieldset => $section,
158                         );
159                 }
160                 elsif ($info{type} eq "pagespec") {
161                         $form->field(
162                                 name => $name,
163                                 label => $description,
164                                 comment => formatexample($info{example}, $value),
165                                 type => "text",
166                                 value => $value,
167                                 size => 60,
168                                 validate => \&IkiWiki::pagespec_valid,
169                                 fieldset => $section,
170                         );
171                 }
172                 elsif ($info{type} eq "integer") {
173                         $form->field(
174                                 name => $name,
175                                 label => $description,
176                                 comment => formatexample($info{example}, $value),
177                                 type => "text",
178                                 value => $value,
179                                 size => 5,
180                                 validate => '/^[0-9]+$/',
181                                 fieldset => $section,
182                         );
183                 }
184                 elsif ($info{type} eq "boolean") {
185                         $form->field(
186                                 name => $name,
187                                 label => "",
188                                 type => "checkbox",
189                                 options => [ [ 1 => $description ] ],
190                                 fieldset => $section,
191                         );
192                         if (! $form->submitted) {
193                                 $form->field(name => $name, value => $value);
194                         }
195                 }
196                 
197                 if (! $info{safe}) {
198                         $form->field(name => $name, disabled => 1);
199                 }
200                 else {
201                         $enabledfields{$name}=[$key, \%info];
202                 }
203                 $shownfields++;
204         }
205         
206         # if no fields were shown for the plugin, drop it into the
207         # plugins fieldset
208         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
209             ! $shownfields) {
210                 $form->field(name => "enable.$plugin", fieldset => "plugins");
211         }
213         return %enabledfields;
216 sub enable_plugin ($) {
217         my $plugin=shift;
219         $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
220         push @{$config{add_plugins}}, $plugin;
223 sub disable_plugin ($) {
224         my $plugin=shift;
226         if (grep { $_ eq $plugin } @{$config{add_plugins}}) {
227                 $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
228         }
229         else {
230                 push @{$config{disable_plugins}}, $plugin;
231         }
234 sub showform ($$) {
235         my $cgi=shift;
236         my $session=shift;
238         if (! defined $session->param("name") || 
239             ! IkiWiki::is_admin($session->param("name"))) {
240                 error(gettext("you are not logged in as an admin"));
241         }
243         if (! exists $config{setupfile}) {
244                 error(gettext("setup file for this wiki is not known"));
245         }
247         eval q{use CGI::FormBuilder};
248         error($@) if $@;
250         my $form = CGI::FormBuilder->new(
251                 title => "setup",
252                 name => "setup",
253                 header => 0,
254                 charset => "utf-8",
255                 method => 'POST',
256                 javascript => 0,
257                 reset => 1,
258                 params => $cgi,
259                 fieldsets => [
260                         [main => gettext("main")], 
261                         [plugins => gettext("plugins")]
262                 ],
263                 action => $config{cgiurl},
264                 template => {type => 'div'},
265                 stylesheet => IkiWiki::baseurl()."style.css",
266         );
267         
268         $form->field(name => "do", type => "hidden", value => "setup",
269                 force => 1);
270         $form->field(name => "rebuild_asked", type => "hidden");
272         if ($form->submitted eq 'Basic Mode') {
273                 $form->field(name => "showadvanced", type => "hidden", 
274                         value => 0, force => 1);
275         }
276         elsif ($form->submitted eq 'Advanced Mode') {
277                 $form->field(name => "showadvanced", type => "hidden", 
278                         value => 1, force => 1);
279         }
280         my $advancedtoggle;
281         if ($form->field("showadvanced")) {
282                 $config{websetup_advanced}=1;
283                 $advancedtoggle="Basic Mode";
284         }
285         else {
286                 $config{websetup_advanced}=0;
287                 $advancedtoggle="Advanced Mode";
288         }
290         my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
292         IkiWiki::decode_form_utf8($form);
293         IkiWiki::run_hooks(formbuilder_setup => sub {
294                 shift->(form => $form, cgi => $cgi, session => $session,
295                         buttons => $buttons);
296         });
298         my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
299         
300         # record all currently enabled plugins before all are loaded
301         my %enabled_plugins=%IkiWiki::loaded_plugins;
303         # per-plugin setup
304         require IkiWiki::Setup;
305         foreach my $pair (IkiWiki::Setup::getsetup()) {
306                 my $plugin=$pair->[0];
307                 my $setup=$pair->[1];
309                 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
310                 if (%shown) {
311                         $fields{$_}=$shown{$_} foreach keys %shown;
312                 }
313         }
315         IkiWiki::decode_form_utf8($form);
316         
317         if ($form->submitted eq "Cancel") {
318                 IkiWiki::redirect($cgi, $config{url});
319                 return;
320         }
321         elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
322                 # Push values from form into %config, avoiding unnecessary
323                 # changes, and keeping track of which changes need a
324                 # rebuild.
325                 my %rebuild;
326                 foreach my $field (keys %fields) {
327                         my %info=%{$fields{$field}->[1]};
328                         my $key=$fields{$field}->[0];
329                         my @value=$form->field($field);
330                         if (! @value) {
331                                 @value=0;
332                         }
333                 
334                         if (! $info{safe}) {
335                                 error("unsafe field $key"); # should never happen
336                         }
337                 
338                         if (exists $info{rebuild} &&
339                             ($info{rebuild} || ! defined $info{rebuild})) {
340                                 $rebuild{$field}=$info{rebuild};
341                         }
342                                         
343                         if ($field=~/^enable\.(.*)/) {
344                                 my $plugin=$1;
345                                 $value[0]=0 if ! length $value[0];
346                                 if ($value[0] != exists $enabled_plugins{$plugin}) {
347                                         if ($value[0]) {
348                                                 enable_plugin($plugin);
349                                         }
350                                         else {
351                                                 disable_plugin($plugin);
353                                         }
354                                 }
355                                 else {
356                                         delete $rebuild{$field};
357                                 }
358                                 next;
359                         }
361                         if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
362                                 @value=sort grep { length $_ } @value;
363                                 my @oldvalue=sort grep { length $_ }
364                                         (defined $config{$key} ? @{$config{$key}} : ());
365                                 my $same=(@oldvalue) == (@value);
366                                 for (my $x=0; $same && $x < @value; $x++) {
367                                         $same=0 if $value[$x] ne $oldvalue[$x];
368                                 }
369                                 if ($same) {
370                                         delete $rebuild{$field};
371                                 }
372                                 else {
373                                         $config{$key}=\@value;
374                                 }
375                         }
376                         elsif (ref $config{$key} || ref $info{example}) {
377                                 error("complex field $key"); # should never happen
378                         }
379                         else {
380                                 if (defined $config{$key} && $config{$key} eq $value[0]) {
381                                         delete $rebuild{$field};
382                                 }
383                                 elsif (! defined $config{$key} && ! length $value[0]) {
384                                         delete $rebuild{$field};
385                                 }
386                                 elsif ((! defined $config{$key} || ! $config{$key}) &&
387                                        ! $value[0] && $info{type} eq "boolean") {
388                                         delete $rebuild{$field};
389                                 }
390                                 else {
391                                         $config{$key}=$value[0];
392                                 }
393                         }
394                 }
395                 
396                 if (%rebuild && ! $form->field("rebuild_asked")) {
397                         my $required=0;
398                         foreach my $field ($form->field) {
399                                 $required=1 if $rebuild{$field};
400                                 next if exists $rebuild{$field};
401                                 $form->field(name => $field, type => "hidden");
402                         }
403                         if ($required) {
404                                 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
405                                 $buttons=["Rebuild Wiki", "Cancel"];
406                         }
407                         else {
408                                 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
409                                 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
410                         }
411                         $form->field(name => "rebuild_asked", value => 1, force => 1);
412                         $form->reset(0); # doesn't really make sense here
413                 }
414                 else {
415                         my $oldsetup=readfile($config{setupfile});
416                         IkiWiki::Setup::dump($config{setupfile});
418                         IkiWiki::saveindex();
419                         IkiWiki::unlockwiki();
421                         # Print the top part of a standard misctemplate,
422                         # then show the rebuild or refresh.
423                         my $divider="xxx";
424                         my $html=IkiWiki::misctemplate("setup", $divider);
425                         IkiWiki::printheader($session);
426                         my ($head, $tail)=split($divider, $html, 2);
427                         print $head."<pre>\n";
429                         my @command;
430                         if ($form->submitted eq 'Rebuild Wiki') {
431                                 @command=("ikiwiki", "-setup", $config{setupfile},
432                                         "-rebuild", "-v");
433                         }
434                         else {
435                                 @command=("ikiwiki", "-setup", $config{setupfile},
436                                         "-refresh", "-wrappers", "-v");
437                         }
439                         close STDERR;
440                         open(STDERR, ">&STDOUT");
441                         my $ret=system(@command);
442                         print "\n<\/pre>";
443                         if ($ret != 0) {
444                                 print '<p class="error">'.
445                                         sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
446                                                 join(" ", @command), $ret).
447                                         '</p>';
448                                 open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
449                                 print OUT $oldsetup;
450                                 close OUT;
451                         }
453                         print $tail;
454                         exit 0;
455                 }
456         }
458         IkiWiki::showform($form, $buttons, $session, $cgi);
461 sub sessioncgi ($$) {
462         my $cgi=shift;
463         my $session=shift;
465         if ($cgi->param("do") eq "setup") {
466                 showform($cgi, $session);
467                 exit;
468         }
471 sub formbuilder_setup (@) {
472         my %params=@_;
474         my $form=$params{form};
475         if ($form->title eq "preferences") {
476                 push @{$params{buttons}}, "Wiki Setup";
477                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
478                         showform($params{cgi}, $params{session});
479                         exit;
480                 }
481         }