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=$info{description};
104 if (exists $info{link} && length $info{link}) {
105 if ($info{link} =~ /^\w+:\/\//) {
106 $description="<a href=\"$info{link}\">$description</a>";
109 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
113 my $value=$config{$key};
114 if (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY') {
115 push @{$value}, "", ""; # blank items for expansion
118 # multiple plugins can have the same field
119 my $name=defined $plugin ? $plugin.".".$key : $key;
121 if ($info{type} eq "string") {
124 label => $description,
125 comment => formatexample($info{example}, $value),
129 fieldset => $section,
132 elsif ($info{type} eq "pagespec") {
135 label => $description,
136 comment => formatexample($info{example}, $value),
140 validate => \&IkiWiki::pagespec_valid,
141 fieldset => $section,
144 elsif ($info{type} eq "integer") {
147 label => $description,
148 comment => formatexample($info{example}, $value),
152 validate => '/^[0-9]+$/',
153 fieldset => $section,
156 elsif ($info{type} eq "boolean") {
162 options => [ [ 1 => $description ] ],
163 fieldset => $section,
168 $form->field(name => $name, disabled => 1);
169 $form->text(gettext("Note: Disabled options cannot be configured here, but only by editing the setup file."));
172 $shownfields{$name}=[$key, \%info];
179 sub showplugintoggle ($$$$) { #{{{
185 if (exists $config{websetup_force_plugins} &&
186 grep { $_ eq $plugin } @{$config{websetup_force_plugins}}) {
189 if (grep { $_ eq $plugin } @force_plugins, @rcs_plugins) {
194 name => "enable.$plugin",
197 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
199 fieldset => $section,
205 sub showform ($$) { #{{{
209 if (! defined $session->param("name") ||
210 ! IkiWiki::is_admin($session->param("name"))) {
211 error(gettext("you are not logged in as an admin"));
214 eval q{use CGI::FormBuilder};
217 my $form = CGI::FormBuilder->new(
226 action => $config{cgiurl},
227 template => {type => 'div'},
228 stylesheet => IkiWiki::baseurl()."style.css",
230 my $buttons=["Save Setup", "Cancel"];
232 IkiWiki::decode_form_utf8($form);
233 IkiWiki::run_hooks(formbuilder_setup => sub {
234 shift->(form => $form, cgi => $cgi, session => $session,
235 buttons => $buttons);
237 IkiWiki::decode_form_utf8($form);
239 $form->field(name => "do", type => "hidden", value => "setup",
241 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
243 # record all currently enabled plugins before all are loaded
244 my %enabled_plugins=%IkiWiki::loaded_plugins;
247 require IkiWiki::Setup;
248 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
249 foreach my $pair (IkiWiki::Setup::getsetup()) {
250 my $plugin=$pair->[0];
251 my $setup=$pair->[1];
253 # skip all rcs plugins except for the one in use
254 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
256 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
258 delete $plugins{$plugin};
259 $fields{$_}=$shown{$_} foreach keys %shown;
263 # list all remaining plugins (with no setup options) at the end
264 foreach (sort keys %plugins) {
265 if (showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))) {
266 $fields{"enable.$_"}=[$_];
270 if ($form->submitted eq "Cancel") {
271 IkiWiki::redirect($cgi, $config{url});
274 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
276 foreach my $field (keys %fields) {
277 if ($field=~/^enable\./) {
278 # rebuild is overkill for many plugins,
279 # but no good way to tell which
280 $rebuild{$field}=1; # TODO only if state changed tho
281 # TODO plugin enable/disable
285 my %info=%{$fields{$field}->[1]};
286 my $key=$fields{$field}->[0];
287 my @value=$form->field($field);
290 error("unsafe field $key"); # should never happen
294 # Avoid setting fields to empty strings,
295 # if they were not set before.
296 next if ! defined $config{$key} && ! grep { length $_ } @value;
298 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
299 if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
302 $config{$key}=\@value;
304 elsif (ref $config{$key} || ref $info{example}) {
305 error("complex field $key"); # should never happen
308 if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
311 $config{$key}=$value[0];
315 if (%rebuild && $form->submitted eq 'Save Setup') {
316 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
317 foreach my $field ($form->field) {
318 next if $rebuild{$field};
319 $form->field(name => $field, type => "hidden",
322 $form->reset(0); # doesn't really make sense here
323 $buttons=["Rebuild Wiki", "Cancel"];
326 # TODO save to real path
327 IkiWiki::Setup::dump("/tmp/s");
328 $form->text(gettext("Setup saved."));
336 IkiWiki::showform($form, $buttons, $session, $cgi);
339 sub sessioncgi ($$) { #{{{
343 if ($cgi->param("do") eq "setup") {
344 showform($cgi, $session);
349 sub formbuilder_setup (@) { #{{{
352 my $form=$params{form};
353 if ($form->title eq "preferences") {
354 push @{$params{buttons}}, "Wiki Setup";
355 if ($form->submitted && $form->submitted eq "Wiki Setup") {
356 showform($params{cgi}, $params{session});