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} && $config{websetup_advanced});
77 # maybe skip advanced settings
78 next if $info{advanced} && ! $config{websetup_advanced};
79 # these are handled specially, so don't show
80 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
82 push @show, $key, \%info;
87 my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
90 if (defined $plugin) {
91 if (showplugintoggle($form, $plugin, $enabled, $section)) {
92 $shownfields{"enable.$plugin"}=[$plugin];
95 # plugin not enabled and cannot be, so skip showing
103 my %info=%{shift @show};
105 my $description=$info{description};
106 if (exists $info{link} && length $info{link}) {
107 if ($info{link} =~ /^\w+:\/\//) {
108 $description="<a href=\"$info{link}\">$description</a>";
111 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
115 # multiple plugins can have the same field
116 my $name=defined $plugin ? $plugin.".".$key : $key;
118 my $value=$config{$key};
120 if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
121 push @{$value}, "", ""; # blank items for expansion
124 if ($info{type} eq "string") {
127 label => $description,
128 comment => formatexample($info{example}, $value),
132 fieldset => $section,
135 elsif ($info{type} eq "pagespec") {
138 label => $description,
139 comment => formatexample($info{example}, $value),
143 validate => \&IkiWiki::pagespec_valid,
144 fieldset => $section,
147 elsif ($info{type} eq "integer") {
150 label => $description,
151 comment => formatexample($info{example}, $value),
155 validate => '/^[0-9]+$/',
156 fieldset => $section,
159 elsif ($info{type} eq "boolean") {
165 options => [ [ 1 => $description ] ],
166 fieldset => $section,
171 $form->field(name => $name, disabled => 1);
172 $form->text(gettext("Note: Disabled options cannot be configured here, but only by editing the setup file."));
175 $shownfields{$name}=[$key, \%info];
182 sub showplugintoggle ($$$$) { #{{{
188 if (exists $config{websetup_force_plugins} &&
189 grep { $_ eq $plugin } @{$config{websetup_force_plugins}}) {
192 if (grep { $_ eq $plugin } @force_plugins, @rcs_plugins) {
197 name => "enable.$plugin",
200 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
202 fieldset => $section,
208 sub showform ($$) { #{{{
212 if (! defined $session->param("name") ||
213 ! IkiWiki::is_admin($session->param("name"))) {
214 error(gettext("you are not logged in as an admin"));
217 eval q{use CGI::FormBuilder};
220 my $form = CGI::FormBuilder->new(
229 action => $config{cgiurl},
230 template => {type => 'div'},
231 stylesheet => IkiWiki::baseurl()."style.css",
234 if ($form->submitted eq 'Basic') {
235 $form->field(name => "showadvanced", type => "hidden",
236 value => 0, force => 1);
238 elsif ($form->submitted eq 'Advanced') {
239 $form->field(name => "showadvanced", type => "hidden",
240 value => 1, force => 1);
243 if ($form->field("showadvanced")) {
244 $config{websetup_advanced}=1;
245 $advancedtoggle="Basic";
248 $config{websetup_advanced}=0;
249 $advancedtoggle="Advanced";
252 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
254 IkiWiki::decode_form_utf8($form);
255 IkiWiki::run_hooks(formbuilder_setup => sub {
256 shift->(form => $form, cgi => $cgi, session => $session,
257 buttons => $buttons);
259 IkiWiki::decode_form_utf8($form);
261 $form->field(name => "do", type => "hidden", value => "setup",
263 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
265 # record all currently enabled plugins before all are loaded
266 my %enabled_plugins=%IkiWiki::loaded_plugins;
269 require IkiWiki::Setup;
270 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
271 foreach my $pair (IkiWiki::Setup::getsetup()) {
272 my $plugin=$pair->[0];
273 my $setup=$pair->[1];
275 # skip all rcs plugins except for the one in use
276 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
278 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
280 delete $plugins{$plugin};
281 $fields{$_}=$shown{$_} foreach keys %shown;
285 # list all remaining plugins (with no setup options) at the end
286 foreach (sort keys %plugins) {
287 if (showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))) {
288 $fields{"enable.$_"}=[$_];
292 if ($form->submitted eq "Cancel") {
293 IkiWiki::redirect($cgi, $config{url});
296 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
298 foreach my $field (keys %fields) {
299 if ($field=~/^enable\./) {
300 # rebuild is overkill for many plugins,
301 # but no good way to tell which
302 $rebuild{$field}=1; # TODO only if state changed tho
303 # TODO plugin enable/disable
307 my %info=%{$fields{$field}->[1]};
308 my $key=$fields{$field}->[0];
309 my @value=$form->field($field);
312 error("unsafe field $key"); # should never happen
316 # Avoid setting fields to empty strings,
317 # if they were not set before.
318 next if ! defined $config{$key} && ! grep { length $_ } @value;
320 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
321 if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
324 $config{$key}=\@value;
326 elsif (ref $config{$key} || ref $info{example}) {
327 error("complex field $key"); # should never happen
330 if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
333 $config{$key}=$value[0];
337 if (%rebuild && $form->submitted eq 'Save Setup') {
338 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
339 foreach my $field ($form->field) {
340 next if $rebuild{$field};
341 $form->field(name => $field, type => "hidden",
344 $form->reset(0); # doesn't really make sense here
345 $buttons=["Rebuild Wiki", "Cancel"];
348 # TODO save to real path
349 IkiWiki::Setup::dump("/tmp/s");
350 $form->text(gettext("Setup saved."));
358 IkiWiki::showform($form, $buttons, $session, $cgi);
361 sub sessioncgi ($$) { #{{{
365 if ($cgi->param("do") eq "setup") {
366 showform($cgi, $session);
371 sub formbuilder_setup (@) { #{{{
374 my $form=$params{form};
375 if ($form->title eq "preferences") {
376 push @{$params{buttons}}, "Wiki Setup";
377 if ($form->submitted && $form->submitted eq "Wiki Setup") {
378 showform($params{cgi}, $params{session});