fix syntax error with corrupt data
[ikiwiki] / IkiWiki / Plugin / websetup.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::websetup;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 my @rcs_plugins=(qw{git svn bzr mercurial monotone tla norcs});
9
10 # amazon_s3 is not something that should be enabled via the web.
11 # external is not a standalone plugin.
12 my @default_force_plugins=(qw{amazon_s3 external});
13
14 sub import { #{{{
15         hook(type => "getsetup", id => "websetup", call => \&getsetup);
16         hook(type => "checkconfig", id => "websetup", call => \&checkconfig);
17         hook(type => "sessioncgi", id => "websetup", call => \&sessioncgi);
18         hook(type => "formbuilder_setup", id => "websetup", 
19              call => \&formbuilder_setup);
20 } # }}}
21
22 sub getsetup () { #{{{
23         return
24                 websetup_force_plugins => {
25                         type => "string",
26                         example => \@default_force_plugins,
27                         description => "list of plugins that cannot be enabled/disabled via the web interface",
28                         safe => 0,
29                         rebuild => 0,
30                 },
31                 websetup_show_unsafe => {
32                         type => "boolean",
33                         example => 1,
34                         description => "show unsafe settings, read-only, in web interface?",
35                         safe => 0,
36                         rebuild => 0,
37                 },
38 } #}}}
39
40 sub checkconfig () { #{{{
41         if (! exists $config{websetup_show_unsafe}) {
42                 $config{websetup_show_unsafe}=1;
43         }
44 } #}}}
45
46 sub formatexample ($$) { #{{{
47         my $example=shift;
48         my $value=shift;
49
50         if (defined $value && length $value) {
51                 return "";
52         }
53         elsif (defined $example && ! ref $example && length $example) {
54                 return "<br/ ><small>Example: <tt>$example</tt></small>";
55         }
56         else {
57                 return "";
58         }
59 } #}}}
60
61 sub showfields ($$$@) { #{{{
62         my $form=shift;
63         my $plugin=shift;
64         my $enabled=shift;
65
66         my @show;
67         while (@_) {
68                 my $key=shift;
69                 my %info=%{shift()};
70
71                 # skip complex or internal settings
72                 next if ref $config{$key} || ref $info{example} || $info{type} eq "internal";
73                 # maybe skip unsafe settings
74                 next if ! $info{safe} && ! $config{websetup_show_unsafe};
75                 # these are handled specially, so don't show
76                 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
77                 
78                 push @show, $key, \%info;
79         }
80
81         return unless @show;
82
83         my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
84
85         my %shownfields;
86         if (defined $plugin) {
87                 if (showplugintoggle($form, $plugin, $enabled, $section)) {
88                         $shownfields{"enable.$plugin"}=$plugin;
89                 }
90                 elsif (! $enabled) {
91                     # plugin not enabled and cannot be, so skip showing
92                     # its configuration
93                     return;
94                 }
95         }
96
97         while (@show) {
98                 my $key=shift @show;
99                 my %info=%{shift @show};
100
101                 my $description=exists $info{description_html} ? $info{description_html} : $info{description};
102                 my $value=$config{$key};
103                 # multiple plugins can have the same field
104                 my $name=defined $plugin ? $plugin.".".$key : $key;
105                 
106                 if ($info{type} eq "string") {
107                         $form->field(
108                                 name => $name,
109                                 label => $description,
110                                 comment => formatexample($info{example}, $value),
111                                 type => "text",
112                                 value => $value,
113                                 size => 60,
114                                 fieldset => $section,
115                         );
116                 }
117                 elsif ($info{type} eq "pagespec") {
118                         $form->field(
119                                 name => $name,
120                                 label => $description,
121                                 comment => formatexample($info{example}, $value),
122                                 type => "text",
123                                 value => $value,
124                                 size => 60,
125                                 validate => \&IkiWiki::pagespec_valid,
126                                 fieldset => $section,
127                         );
128                 }
129                 elsif ($info{type} eq "integer") {
130                         $form->field(
131                                 name => $name,
132                                 label => $description,
133                                 comment => formatexample($info{example}, $value),
134                                 type => "text",
135                                 value => $value,
136                                 size => 5,
137                                 validate => '/^[0-9]+$/',
138                                 fieldset => $section,
139                         );
140                 }
141                 elsif ($info{type} eq "boolean") {
142                         $form->field(
143                                 name => $name,
144                                 label => "",
145                                 type => "checkbox",
146                                 value => $value,
147                                 options => [ [ 1 => $description ] ],
148                                 fieldset => $section,
149                         );
150                 }
151                 
152                 if (! $info{safe}) {
153                         $form->field(name => $name, disabled => 1);
154                         $form->text(gettext("Note: Disabled options cannot be configured here, but only by editing the setup file."));
155                 }
156                 else {
157                         $shownfields{$name}=$key;
158                 }
159         }
160
161         return %shownfields;
162 } #}}}
163
164 sub showplugintoggle ($$$$) { #{{{
165         my $form=shift;
166         my $plugin=shift;
167         my $enabled=shift;
168         my $section=shift;
169
170         if (exists $config{websetup_force_plugins} &&
171             grep { $_ eq $plugin } @{$config{websetup_force_plugins}}, @rcs_plugins) {
172                 return 0;
173         }
174         elsif (! exists $config{websetup_force_plugins} &&
175                grep { $_ eq $plugin } @default_force_plugins, @rcs_plugins) {
176                 return 0;
177         }
178
179         $form->field(
180                 ame => "enable.$plugin",
181                 label => "",
182                 type => "checkbox",
183                 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
184                 value => $enabled,
185                 fieldset => $section,
186         );
187
188         return 1;
189 } #}}}
190
191 sub showform ($$) { #{{{
192         my $cgi=shift;
193         my $session=shift;
194
195         if (! defined $session->param("name") || 
196             ! IkiWiki::is_admin($session->param("name"))) {
197                 error(gettext("you are not logged in as an admin"));
198         }
199
200         eval q{use CGI::FormBuilder};
201         error($@) if $@;
202
203         my $form = CGI::FormBuilder->new(
204                 title => "setup",
205                 name => "setup",
206                 header => 0,
207                 charset => "utf-8",
208                 method => 'POST',
209                 javascript => 0,
210                 reset => 1,
211                 params => $cgi,
212                 action => $config{cgiurl},
213                 template => {type => 'div'},
214                 stylesheet => IkiWiki::baseurl()."style.css",
215         );
216         my $buttons=["Save Setup", "Cancel"];
217
218         IkiWiki::decode_form_utf8($form);
219         IkiWiki::run_hooks(formbuilder_setup => sub {
220                 shift->(form => $form, cgi => $cgi, session => $session,
221                         buttons => $buttons);
222         });
223         IkiWiki::decode_form_utf8($form);
224
225         $form->field(name => "do", type => "hidden", value => "setup",
226                 force => 1);
227         my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
228         
229         # record all currently enabled plugins before all are loaded
230         my %enabled_plugins=%IkiWiki::loaded_plugins;
231
232         # per-plugin setup
233         require IkiWiki::Setup;
234         my %plugins=map { $_ => 1 } IkiWiki::listplugins();
235         foreach my $pair (IkiWiki::Setup::getsetup()) {
236                 my $plugin=$pair->[0];
237                 my $setup=$pair->[1];
238                 
239                 # skip all rcs plugins except for the one in use
240                 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
241
242                 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
243                 if (%shown) {
244                         delete $plugins{$plugin};
245                         $fields{$_}=$shown{$_} foreach keys %shown;
246                 }
247         }
248
249         # list all remaining plugins (with no setup options) at the end
250         foreach (sort keys %plugins) {
251                 if (showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))) {
252                         $fields{"enable.$_"}=$_;
253                 }
254         }
255         
256         if ($form->submitted eq "Cancel") {
257                 IkiWiki::redirect($cgi, $config{url});
258                 return;
259         }
260         elsif ($form->submitted eq 'Save Setup' && $form->validate) {
261                 foreach my $field (keys %fields) {
262                         # TODO plugin enable/disable
263                         next if $field=~/^enable\./; # plugin
264                         $config{$fields{$field}}=$form->field($field);
265                 }
266                 # TODO save to real path
267                 IkiWiki::Setup::dump("/tmp/s");
268                 $form->text(gettext("Setup saved."));
269         }
270
271         IkiWiki::showform($form, $buttons, $session, $cgi);
272 } #}}}
273
274 sub sessioncgi ($$) { #{{{
275         my $cgi=shift;
276         my $session=shift;
277
278         if ($cgi->param("do") eq "setup") {
279                 showform($cgi, $session);
280                 exit;
281         }
282 } #}}}
283
284 sub formbuilder_setup (@) { #{{{
285         my %params=@_;
286
287         my $form=$params{form};
288         if ($form->title eq "preferences") {
289                 push @{$params{buttons}}, "Wiki Setup";
290                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
291                         showform($params{cgi}, $params{session});
292                         exit;
293                 }
294         }
295 } #}}}
296
297 1