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 # multiple plugins can have the same field
114 my $name=defined $plugin ? $plugin.".".$key : $key;
116 my $value=$config{$key};
118 if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
119 push @{$value}, "", ""; # blank items for expansion
122 if ($info{type} eq "string") {
125 label => $description,
126 comment => formatexample($info{example}, $value),
130 fieldset => $section,
133 elsif ($info{type} eq "pagespec") {
136 label => $description,
137 comment => formatexample($info{example}, $value),
141 validate => \&IkiWiki::pagespec_valid,
142 fieldset => $section,
145 elsif ($info{type} eq "integer") {
148 label => $description,
149 comment => formatexample($info{example}, $value),
153 validate => '/^[0-9]+$/',
154 fieldset => $section,
157 elsif ($info{type} eq "boolean") {
163 options => [ [ 1 => $description ] ],
164 fieldset => $section,
169 $form->field(name => $name, disabled => 1);
170 $form->text(gettext("Note: Disabled options cannot be configured here, but only by editing the setup file."));
173 $shownfields{$name}=[$key, \%info];
180 sub showplugintoggle ($$$$) { #{{{
186 if (exists $config{websetup_force_plugins} &&
187 grep { $_ eq $plugin } @{$config{websetup_force_plugins}}) {
190 if (grep { $_ eq $plugin } @force_plugins, @rcs_plugins) {
195 name => "enable.$plugin",
198 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
200 fieldset => $section,
206 sub showform ($$) { #{{{
210 if (! defined $session->param("name") ||
211 ! IkiWiki::is_admin($session->param("name"))) {
212 error(gettext("you are not logged in as an admin"));
215 eval q{use CGI::FormBuilder};
218 my $form = CGI::FormBuilder->new(
227 action => $config{cgiurl},
228 template => {type => 'div'},
229 stylesheet => IkiWiki::baseurl()."style.css",
231 my $buttons=["Save Setup", "Cancel"];
233 IkiWiki::decode_form_utf8($form);
234 IkiWiki::run_hooks(formbuilder_setup => sub {
235 shift->(form => $form, cgi => $cgi, session => $session,
236 buttons => $buttons);
238 IkiWiki::decode_form_utf8($form);
240 $form->field(name => "do", type => "hidden", value => "setup",
242 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
244 # record all currently enabled plugins before all are loaded
245 my %enabled_plugins=%IkiWiki::loaded_plugins;
248 require IkiWiki::Setup;
249 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
250 foreach my $pair (IkiWiki::Setup::getsetup()) {
251 my $plugin=$pair->[0];
252 my $setup=$pair->[1];
254 # skip all rcs plugins except for the one in use
255 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
257 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
259 delete $plugins{$plugin};
260 $fields{$_}=$shown{$_} foreach keys %shown;
264 # list all remaining plugins (with no setup options) at the end
265 foreach (sort keys %plugins) {
266 if (showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))) {
267 $fields{"enable.$_"}=[$_];
271 if ($form->submitted eq "Cancel") {
272 IkiWiki::redirect($cgi, $config{url});
275 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
277 foreach my $field (keys %fields) {
278 if ($field=~/^enable\./) {
279 # rebuild is overkill for many plugins,
280 # but no good way to tell which
281 $rebuild{$field}=1; # TODO only if state changed tho
282 # TODO plugin enable/disable
286 my %info=%{$fields{$field}->[1]};
287 my $key=$fields{$field}->[0];
288 my @value=$form->field($field);
291 error("unsafe field $key"); # should never happen
295 # Avoid setting fields to empty strings,
296 # if they were not set before.
297 next if ! defined $config{$key} && ! grep { length $_ } @value;
299 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
300 if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
303 $config{$key}=\@value;
305 elsif (ref $config{$key} || ref $info{example}) {
306 error("complex field $key"); # should never happen
309 if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
312 $config{$key}=$value[0];
316 if (%rebuild && $form->submitted eq 'Save Setup') {
317 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
318 foreach my $field ($form->field) {
319 next if $rebuild{$field};
320 $form->field(name => $field, type => "hidden",
323 $form->reset(0); # doesn't really make sense here
324 $buttons=["Rebuild Wiki", "Cancel"];
327 # TODO save to real path
328 IkiWiki::Setup::dump("/tmp/s");
329 $form->text(gettext("Setup saved."));
337 IkiWiki::showform($form, $buttons, $session, $cgi);
340 sub sessioncgi ($$) { #{{{
344 if ($cgi->param("do") eq "setup") {
345 showform($cgi, $session);
350 sub formbuilder_setup (@) { #{{{
353 my $form=$params{form};
354 if ($form->title eq "preferences") {
355 push @{$params{buttons}}, "Wiki Setup";
356 if ($form->submitted && $form->submitted eq "Wiki Setup") {
357 showform($params{cgi}, $params{session});