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 # multiple plugins can have the same field
115 my $name=defined $plugin ? $plugin.".".$key : $key;
117 if (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY') {
120 label => $description,
121 comment => formatexample($info{example}, $value),
123 value => [ref $value eq 'ARRAY' ? @{$value} : "", , "", ""],
125 fieldset => $section,
128 elsif ($info{type} eq "string") {
131 label => $description,
132 comment => formatexample($info{example}, $value),
136 fieldset => $section,
139 elsif ($info{type} eq "pagespec") {
142 label => $description,
143 comment => formatexample($info{example}, $value),
147 validate => \&IkiWiki::pagespec_valid,
148 fieldset => $section,
151 elsif ($info{type} eq "integer") {
154 label => $description,
155 comment => formatexample($info{example}, $value),
159 validate => '/^[0-9]+$/',
160 fieldset => $section,
163 elsif ($info{type} eq "boolean") {
169 options => [ [ 1 => $description ] ],
170 fieldset => $section,
175 $form->field(name => $name, disabled => 1);
176 $form->text(gettext("Note: Disabled options cannot be configured here, but only by editing the setup file."));
179 $shownfields{$name}=[$key, \%info];
186 sub showplugintoggle ($$$$) { #{{{
192 if (exists $config{websetup_force_plugins} &&
193 grep { $_ eq $plugin } @{$config{websetup_force_plugins}}) {
196 if (grep { $_ eq $plugin } @force_plugins, @rcs_plugins) {
201 name => "enable.$plugin",
204 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
206 fieldset => $section,
212 sub showform ($$) { #{{{
216 if (! defined $session->param("name") ||
217 ! IkiWiki::is_admin($session->param("name"))) {
218 error(gettext("you are not logged in as an admin"));
221 eval q{use CGI::FormBuilder};
224 my $form = CGI::FormBuilder->new(
233 action => $config{cgiurl},
234 template => {type => 'div'},
235 stylesheet => IkiWiki::baseurl()."style.css",
237 my $buttons=["Save Setup", "Cancel"];
239 IkiWiki::decode_form_utf8($form);
240 IkiWiki::run_hooks(formbuilder_setup => sub {
241 shift->(form => $form, cgi => $cgi, session => $session,
242 buttons => $buttons);
244 IkiWiki::decode_form_utf8($form);
246 $form->field(name => "do", type => "hidden", value => "setup",
248 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
250 # record all currently enabled plugins before all are loaded
251 my %enabled_plugins=%IkiWiki::loaded_plugins;
254 require IkiWiki::Setup;
255 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
256 foreach my $pair (IkiWiki::Setup::getsetup()) {
257 my $plugin=$pair->[0];
258 my $setup=$pair->[1];
260 # skip all rcs plugins except for the one in use
261 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
263 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
265 delete $plugins{$plugin};
266 $fields{$_}=$shown{$_} foreach keys %shown;
270 # list all remaining plugins (with no setup options) at the end
271 foreach (sort keys %plugins) {
272 if (showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))) {
273 $fields{"enable.$_"}=[$_];
277 if ($form->submitted eq "Cancel") {
278 IkiWiki::redirect($cgi, $config{url});
281 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
283 foreach my $field (keys %fields) {
284 if ($field=~/^enable\./) {
285 # rebuild is overkill for many plugins,
286 # but no good way to tell which
287 $rebuild{$field}=1; # TODO only if state changed tho
288 # TODO plugin enable/disable
292 my %info=%{$fields{$field}->[1]};
293 my $key=$fields{$field}->[0];
294 my @value=$form->field($field);
297 error("unsafe field $key"); # should never happen
301 # Avoid setting fields to empty strings,
302 # if they were not set before.
303 next if ! defined $config{$key} && ! grep { length $_ } @value;
305 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
306 if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
309 $config{$key}=\@value;
311 elsif (ref $config{$key} || ref $info{example}) {
312 error("complex field $key"); # should never happen
315 if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
318 $config{$key}=$value[0];
322 if (%rebuild && $form->submitted eq 'Save Setup') {
323 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
324 foreach my $field ($form->field) {
325 next if $rebuild{$field};
326 $form->field(name => $field, type => "hidden",
329 $form->reset(0); # doesn't really make sense here
330 $buttons=["Rebuild Wiki", "Cancel"];
333 # TODO save to real path
334 IkiWiki::Setup::dump("/tmp/s");
335 $form->text(gettext("Setup saved."));
343 IkiWiki::showform($form, $buttons, $session, $cgi);
346 sub sessioncgi ($$) { #{{{
350 if ($cgi->param("do") eq "setup") {
351 showform($cgi, $session);
356 sub formbuilder_setup (@) { #{{{
359 my $form=$params{form};
360 if ($form->title eq "preferences") {
361 push @{$params{buttons}}, "Wiki Setup";
362 if ($form->submitted && $form->submitted eq "Wiki Setup") {
363 showform($params{cgi}, $params{session});