2 package IkiWiki::Plugin::websetup;
8 my @rcs_plugins=(qw{git svn bzr mercurial monotone tla norcs});
10 # amazon_s3 is not something that should be enabled via the web.
11 # external is not a standalone plugin.
12 my @force_plugins=(qw{amazon_s3 external});
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);
22 sub getsetup () { #{{{
24 websetup_force_plugins => {
27 description => "list of plugins that cannot be enabled/disabled via the web interface",
31 websetup_show_unsafe => {
34 description => "show unsafe settings, read-only, in web interface?",
40 sub checkconfig () { #{{{
41 if (! exists $config{websetup_show_unsafe}) {
42 $config{websetup_show_unsafe}=1;
46 sub formatexample ($$) { #{{{
50 if (defined $value && length $value) {
53 elsif (defined $example && ! ref $example && length $example) {
54 return "<br/ ><small>Example: <tt>$example</tt></small>";
61 sub showfields ($$$@) { #{{{
71 # skip internal settings
72 next if $info{type} eq "internal";
73 # XXX hashes not handled yet
74 next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
75 # maybe skip unsafe settings
76 next if ! $info{safe} && ! $config{websetup_show_unsafe};
77 # these are handled specially, so don't show
78 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
80 push @show, $key, \%info;
85 my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
88 if (defined $plugin) {
89 if (showplugintoggle($form, $plugin, $enabled, $section)) {
90 $shownfields{"enable.$plugin"}=[$plugin];
93 # plugin not enabled and cannot be, so skip showing
101 my %info=%{shift @show};
103 my $description=exists $info{description_html} ? $info{description_html} : $info{description};
104 my $value=$config{$key};
105 # multiple plugins can have the same field
106 my $name=defined $plugin ? $plugin.".".$key : $key;
108 if (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY') {
111 label => $description,
112 comment => formatexample($info{example}, $value),
114 value => [ref $value eq 'ARRAY' ? @{$value} : "", , "", ""],
116 fieldset => $section,
119 elsif ($info{type} eq "string") {
122 label => $description,
123 comment => formatexample($info{example}, $value),
127 fieldset => $section,
130 elsif ($info{type} eq "pagespec") {
133 label => $description,
134 comment => formatexample($info{example}, $value),
138 validate => \&IkiWiki::pagespec_valid,
139 fieldset => $section,
142 elsif ($info{type} eq "integer") {
145 label => $description,
146 comment => formatexample($info{example}, $value),
150 validate => '/^[0-9]+$/',
151 fieldset => $section,
154 elsif ($info{type} eq "boolean") {
160 options => [ [ 1 => $description ] ],
161 fieldset => $section,
166 $form->field(name => $name, disabled => 1);
167 $form->text(gettext("Note: Disabled options cannot be configured here, but only by editing the setup file."));
170 $shownfields{$name}=[$key, \%info];
177 sub showplugintoggle ($$$$) { #{{{
183 if (exists $config{websetup_force_plugins} &&
184 grep { $_ eq $plugin } @{$config{websetup_force_plugins}}) {
187 if (grep { $_ eq $plugin } @force_plugins, @rcs_plugins) {
192 name => "enable.$plugin",
195 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
197 fieldset => $section,
203 sub showform ($$) { #{{{
207 if (! defined $session->param("name") ||
208 ! IkiWiki::is_admin($session->param("name"))) {
209 error(gettext("you are not logged in as an admin"));
212 eval q{use CGI::FormBuilder};
215 my $form = CGI::FormBuilder->new(
224 action => $config{cgiurl},
225 template => {type => 'div'},
226 stylesheet => IkiWiki::baseurl()."style.css",
228 my $buttons=["Save Setup", "Cancel"];
230 IkiWiki::decode_form_utf8($form);
231 IkiWiki::run_hooks(formbuilder_setup => sub {
232 shift->(form => $form, cgi => $cgi, session => $session,
233 buttons => $buttons);
235 IkiWiki::decode_form_utf8($form);
237 $form->field(name => "do", type => "hidden", value => "setup",
239 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
241 # record all currently enabled plugins before all are loaded
242 my %enabled_plugins=%IkiWiki::loaded_plugins;
245 require IkiWiki::Setup;
246 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
247 foreach my $pair (IkiWiki::Setup::getsetup()) {
248 my $plugin=$pair->[0];
249 my $setup=$pair->[1];
251 # skip all rcs plugins except for the one in use
252 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
254 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
256 delete $plugins{$plugin};
257 $fields{$_}=$shown{$_} foreach keys %shown;
261 # list all remaining plugins (with no setup options) at the end
262 foreach (sort keys %plugins) {
263 if (showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))) {
264 $fields{"enable.$_"}=[$_];
268 if ($form->submitted eq "Cancel") {
269 IkiWiki::redirect($cgi, $config{url});
272 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
274 foreach my $field (keys %fields) {
275 if ($field=~/^enable\./) {
276 # rebuild is overkill for many plugins,
277 # but no good way to tell which
278 $rebuild{$field}=1; # TODO only if state changed tho
279 # TODO plugin enable/disable
283 my %info=%{$fields{$field}->[1]};
284 my $key=$fields{$field}->[0];
285 my @value=$form->field($field);
288 error("unsafe field $key"); # should never happen
292 # Avoid setting fields to empty strings,
293 # if they were not set before.
294 next if ! defined $config{$key} && ! grep { length $_ } @value;
296 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
297 if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
300 $config{$key}=\@value;
302 elsif (ref $config{$key} || ref $info{example}) {
303 error("complex field $key"); # should never happen
306 if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
309 $config{$key}=$value[0];
313 if (%rebuild && $form->submitted eq 'Save Setup') {
314 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
315 foreach my $field ($form->field) {
316 next if $rebuild{$field};
317 $form->field(name => $field, type => "hidden",
320 $form->reset(0); # doesn't really make sense here
321 $buttons=["Rebuild Wiki", "Cancel"];
324 # TODO save to real path
325 IkiWiki::Setup::dump("/tmp/s");
326 $form->text(gettext("Setup saved."));
334 IkiWiki::showform($form, $buttons, $session, $cgi);
337 sub sessioncgi ($$) { #{{{
341 if ($cgi->param("do") eq "setup") {
342 showform($cgi, $session);
347 sub formbuilder_setup (@) { #{{{
350 my $form=$params{form};
351 if ($form->title eq "preferences") {
352 push @{$params{buttons}}, "Wiki Setup";
353 if ($form->submitted && $form->submitted eq "Wiki Setup") {
354 showform($params{cgi}, $params{session});