]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/websetup.pm
4c936741066d90fcad61b42b6d6d3020e7eeac71
[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 2.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                 websetup_force_plugins => {
19                         type => "string",
20                         example => [],
21                         description => "list of plugins that cannot be enabled/disabled via the web interface",
22                         safe => 0,
23                         rebuild => 0,
24                 },
25                 websetup_show_unsafe => {
26                         type => "boolean",
27                         example => 1,
28                         description => "show unsafe settings, read-only, in web interface?",
29                         safe => 0,
30                         rebuild => 0,
31                 },
32 } #}}}
34 sub checkconfig () { #{{{
35         if (! exists $config{websetup_show_unsafe}) {
36                 $config{websetup_show_unsafe}=1;
37         }
38 } #}}}
40 sub formatexample ($$) { #{{{
41         my $example=shift;
42         my $value=shift;
44         if (defined $value && length $value) {
45                 return "";
46         }
47         elsif (defined $example && ! ref $example && length $example) {
48                 return "<br/ ><small>Example: <tt>$example</tt></small>";
49         }
50         else {
51                 return "";
52         }
53 } #}}}
55 sub showfields ($$$@) { #{{{
56         my $form=shift;
57         my $plugin=shift;
58         my $enabled=shift;
60         my @show;
61         my %plugininfo;
62         while (@_) {
63                 my $key=shift;
64                 my %info=%{shift()};
66                 # skip internal settings
67                 next if defined $info{type} && $info{type} eq "internal";
68                 # XXX hashes not handled yet
69                 next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
70                 # maybe skip unsafe settings
71                 next if ! $info{safe} && ! ($config{websetup_show_unsafe} && $config{websetup_advanced});
72                 # maybe skip advanced settings
73                 next if $info{advanced} && ! $config{websetup_advanced};
74                 # these are handled specially, so don't show
75                 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
77                 if ($key eq 'plugin') {
78                         %plugininfo=%info;
79                         next;
80                 }
81                 
82                 push @show, $key, \%info;
83         }
85         my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
86                 (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
87         if ($plugin_forced && ! $enabled) {
88                 # plugin is forced disabled, so skip its configuration
89                 @show=();
90         }
92         my %shownfields;
93         my %skippedfields;
94         my $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
95         
96         while (@show) {
97                 my $key=shift @show;
98                 my %info=%{shift @show};
100                 my $description=$info{description};
101                 if (exists $info{link} && length $info{link}) {
102                         if ($info{link} =~ /^\w+:\/\//) {
103                                 $description="<a href=\"$info{link}\">$description</a>";
104                         }
105                         else {
106                                 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
107                         }
108                 }
110                 # multiple plugins can have the same field
111                 my $name=defined $plugin ? $plugin.".".$key : $key;
113                 my $value=$config{$key};
115                 if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
116                         push @{$value}, "", ""; # blank items for expansion
117                 }
119                 if ($info{type} eq "string") {
120                         $form->field(
121                                 name => $name,
122                                 label => $description,
123                                 comment => formatexample($info{example}, $value),
124                                 type => "text",
125                                 value => $value,
126                                 size => 60,
127                                 fieldset => $section,
128                         );
129                 }
130                 elsif ($info{type} eq "pagespec") {
131                         $form->field(
132                                 name => $name,
133                                 label => $description,
134                                 comment => formatexample($info{example}, $value),
135                                 type => "text",
136                                 value => $value,
137                                 size => 60,
138                                 validate => \&IkiWiki::pagespec_valid,
139                                 fieldset => $section,
140                         );
141                 }
142                 elsif ($info{type} eq "integer") {
143                         $form->field(
144                                 name => $name,
145                                 label => $description,
146                                 comment => formatexample($info{example}, $value),
147                                 type => "text",
148                                 value => $value,
149                                 size => 5,
150                                 validate => '/^[0-9]+$/',
151                                 fieldset => $section,
152                         );
153                 }
154                 elsif ($info{type} eq "boolean") {
155                         $form->field(
156                                 name => $name,
157                                 label => "",
158                                 type => "checkbox",
159                                 value => $value,
160                                 options => [ [ 1 => $description ] ],
161                                 fieldset => $section,
162                         );
163                 }
164                 
165                 if (! $info{safe}) {
166                         $form->field(name => $name, disabled => 1);
167                         $skippedfields{$name}=1;
168                 }
169                 else {
170                         $shownfields{$name}=[$key, \%info];
171                 }
172         }
174         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
175                 my $name="enable.$plugin";
176                 $section="plugins" unless %shownfields || (%skippedfields && $config{websetup_advanced});
177                 $form->field(
178                         name => $name,
179                         label => "",
180                         type => "checkbox",
181                         options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
182                         value => $enabled,
183                         fieldset => $section,
184                 );
185                 if ($plugin_forced) {
186                         $form->field(name => $name, disabled => 1);
187                 }
188                 else {
189                         $shownfields{$name}=[$name, \%plugininfo];
190                 }
191         }
193         return %shownfields;
194 } #}}}
196 sub showform ($$) { #{{{
197         my $cgi=shift;
198         my $session=shift;
200         if (! defined $session->param("name") || 
201             ! IkiWiki::is_admin($session->param("name"))) {
202                 error(gettext("you are not logged in as an admin"));
203         }
205         eval q{use CGI::FormBuilder};
206         error($@) if $@;
208         my $form = CGI::FormBuilder->new(
209                 title => "setup",
210                 name => "setup",
211                 header => 0,
212                 charset => "utf-8",
213                 method => 'POST',
214                 javascript => 0,
215                 reset => 1,
216                 params => $cgi,
217                 fieldsets => [
218                         [main => gettext("main")], 
219                         [plugins => gettext("plugins")]
220                 ],
221                 action => $config{cgiurl},
222                 template => {type => 'div'},
223                 stylesheet => IkiWiki::baseurl()."style.css",
224         );
226         if ($form->submitted eq 'Basic Mode') {
227                 $form->field(name => "showadvanced", type => "hidden", 
228                         value => 0, force => 1);
229         }
230         elsif ($form->submitted eq 'Advanced Mode') {
231                 $form->field(name => "showadvanced", type => "hidden", 
232                         value => 1, force => 1);
233         }
234         my $advancedtoggle;
235         if ($form->field("showadvanced")) {
236                 $config{websetup_advanced}=1;
237                 $advancedtoggle="Basic Mode";
238         }
239         else {
240                 $config{websetup_advanced}=0;
241                 $advancedtoggle="Advanced Mode";
242         }
244         my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
246         IkiWiki::decode_form_utf8($form);
247         IkiWiki::run_hooks(formbuilder_setup => sub {
248                 shift->(form => $form, cgi => $cgi, session => $session,
249                         buttons => $buttons);
250         });
251         IkiWiki::decode_form_utf8($form);
253         $form->field(name => "do", type => "hidden", value => "setup",
254                 force => 1);
255         my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
256         
257         # record all currently enabled plugins before all are loaded
258         my %enabled_plugins=%IkiWiki::loaded_plugins;
260         # per-plugin setup
261         require IkiWiki::Setup;
262         my %plugins=map { $_ => 1 } IkiWiki::listplugins();
263         foreach my $pair (IkiWiki::Setup::getsetup()) {
264                 my $plugin=$pair->[0];
265                 my $setup=$pair->[1];
267                 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
268                 if (%shown) {
269                         delete $plugins{$plugin};
270                         $fields{$_}=$shown{$_} foreach keys %shown;
271                 }
272         }
273         
274         if ($form->submitted eq "Cancel") {
275                 IkiWiki::redirect($cgi, $config{url});
276                 return;
277         }
278         elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
279                 my %rebuild;
280                 foreach my $field (keys %fields) {
281                         if ($field=~/^enable\./) {
282                                 # rebuild is overkill for many plugins,
283                                 # but no good way to tell which
284                                 $rebuild{$field}=1; # TODO only if state changed tho
285                                 # TODO plugin enable/disable
286                                 next;
287                         }
288                         
289                         my %info=%{$fields{$field}->[1]};
290                         my $key=$fields{$field}->[0];
291                         my @value=$form->field($field);
292                         
293                         if (! $info{safe}) {
294                                 error("unsafe field $key"); # should never happen
295                         }
297                         next unless @value;
298                         # Avoid setting fields to empty strings,
299                         # if they were not set before.
300                         next if ! defined $config{$key} && ! grep { length $_ } @value;
302                         if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
303                                 if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
304                                         $rebuild{$field}=1;
305                                 }
306                                 $config{$key}=\@value;
307                         }
308                         elsif (ref $config{$key} || ref $info{example}) {
309                                 error("complex field $key"); # should never happen
310                         }
311                         else {
312                                 if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
313                                         $rebuild{$field}=1;
314                                 }
315                                 $config{$key}=$value[0];
316                         }               
317                 }
319                 if (%rebuild && $form->submitted eq 'Save Setup') {
320                         $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
321                         foreach my $field ($form->field) {
322                                 next if $rebuild{$field};
323                                 $form->field(name => $field, type => "hidden",
324                                         force => 1);
325                         }
326                         $form->reset(0); # doesn't really make sense here
327                         $buttons=["Rebuild Wiki", "Cancel"];
328                 }
329                 else {
330                         # TODO save to real path
331                         IkiWiki::Setup::dump("/tmp/s");
332                         $form->text(gettext("Setup saved."));
334                         if (%rebuild) {
335                                 # TODO rebuild
336                         }
337                 }
338         }
340         IkiWiki::showform($form, $buttons, $session, $cgi);
341 } #}}}
343 sub sessioncgi ($$) { #{{{
344         my $cgi=shift;
345         my $session=shift;
347         if ($cgi->param("do") eq "setup") {
348                 showform($cgi, $session);
349                 exit;
350         }
351 } #}}}
353 sub formbuilder_setup (@) { #{{{
354         my %params=@_;
356         my $form=$params{form};
357         if ($form->title eq "preferences") {
358                 push @{$params{buttons}}, "Wiki Setup";
359                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
360                         showform($params{cgi}, $params{session});
361                         exit;
362                 }
363         }
364 } #}}}