2 package IkiWiki::Plugin::websetup;
9 hook(type => "getsetup", id => "websetup", call => \&getsetup);
10 hook(type => "checkconfig", id => "websetup", call => \&checkconfig);
11 hook(type => "sessioncgi", id => "websetup", call => \&sessioncgi);
12 hook(type => "formbuilder_setup", id => "websetup",
13 call => \&formbuilder_setup);
16 sub getsetup () { #{{{
18 websetup_force_plugins => {
21 description => "list of plugins that cannot be enabled/disabled via the web interface",
25 websetup_show_unsafe => {
28 description => "show unsafe settings, read-only, in web interface?",
34 sub checkconfig () { #{{{
35 if (! exists $config{websetup_show_unsafe}) {
36 $config{websetup_show_unsafe}=1;
40 sub formatexample ($$) { #{{{
44 if (defined $value && length $value) {
47 elsif (defined $example && ! ref $example && length $example) {
48 return "<br/ ><small>Example: <tt>$example</tt></small>";
55 sub showfields ($$$@) { #{{{
66 # skip internal settings
67 next if defined $info{type} && $info{type} eq "internal";
68 # XXX hashes not handled yet
69 next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
70 # maybe skip unsafe settings
71 next if ! $info{safe} && ! ($config{websetup_show_unsafe} && $config{websetup_advanced});
72 # maybe skip advanced settings
73 next if $info{advanced} && ! $config{websetup_advanced};
74 # these are handled specially, so don't show
75 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
77 if ($key eq 'plugin') {
82 push @show, $key, \%info;
85 my $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
89 my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
90 (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
91 if ($plugin_forced && ! $enabled) {
92 # plugin is forced disabled, so skip its configuration
96 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
97 my $name="enable.$plugin";
102 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
104 fieldset => $section,
106 if ($plugin_forced) {
107 $form->field(name => $name, disabled => 1);
110 $enabledfields{$name}=[$name, \%plugininfo];
116 my %info=%{shift @show};
118 my $description=$info{description};
119 if (exists $info{link} && length $info{link}) {
120 if ($info{link} =~ /^\w+:\/\//) {
121 $description="<a href=\"$info{link}\">$description</a>";
124 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
128 # multiple plugins can have the same field
129 my $name=defined $plugin ? $plugin.".".$key : $key;
131 my $value=$config{$key};
133 if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
134 push @{$value}, "", ""; # blank items for expansion
137 if ($info{type} eq "string") {
140 label => $description,
141 comment => formatexample($info{example}, $value),
145 fieldset => $section,
148 elsif ($info{type} eq "pagespec") {
151 label => $description,
152 comment => formatexample($info{example}, $value),
156 validate => \&IkiWiki::pagespec_valid,
157 fieldset => $section,
160 elsif ($info{type} eq "integer") {
163 label => $description,
164 comment => formatexample($info{example}, $value),
168 validate => '/^[0-9]+$/',
169 fieldset => $section,
172 elsif ($info{type} eq "boolean") {
178 options => [ [ 1 => $description ] ],
179 fieldset => $section,
184 $form->field(name => $name, disabled => 1);
187 $enabledfields{$name}=[$key, \%info];
192 # if no fields were shown for the plugin, drop it into the
194 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
196 $form->field(name => "enable.$plugin", fieldset => "plugins");
199 return %enabledfields;
202 sub showform ($$) { #{{{
206 if (! defined $session->param("name") ||
207 ! IkiWiki::is_admin($session->param("name"))) {
208 error(gettext("you are not logged in as an admin"));
211 eval q{use CGI::FormBuilder};
214 my $form = CGI::FormBuilder->new(
224 [main => gettext("main")],
225 [plugins => gettext("plugins")]
227 action => $config{cgiurl},
228 template => {type => 'div'},
229 stylesheet => IkiWiki::baseurl()."style.css",
232 if ($form->submitted eq 'Basic Mode') {
233 $form->field(name => "showadvanced", type => "hidden",
234 value => 0, force => 1);
236 elsif ($form->submitted eq 'Advanced Mode') {
237 $form->field(name => "showadvanced", type => "hidden",
238 value => 1, force => 1);
241 if ($form->field("showadvanced")) {
242 $config{websetup_advanced}=1;
243 $advancedtoggle="Basic Mode";
246 $config{websetup_advanced}=0;
247 $advancedtoggle="Advanced Mode";
250 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
252 IkiWiki::decode_form_utf8($form);
253 IkiWiki::run_hooks(formbuilder_setup => sub {
254 shift->(form => $form, cgi => $cgi, session => $session,
255 buttons => $buttons);
257 IkiWiki::decode_form_utf8($form);
259 $form->field(name => "do", type => "hidden", value => "setup",
261 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
263 # record all currently enabled plugins before all are loaded
264 my %enabled_plugins=%IkiWiki::loaded_plugins;
267 require IkiWiki::Setup;
268 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
269 foreach my $pair (IkiWiki::Setup::getsetup()) {
270 my $plugin=$pair->[0];
271 my $setup=$pair->[1];
273 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
275 delete $plugins{$plugin};
276 $fields{$_}=$shown{$_} foreach keys %shown;
280 if ($form->submitted eq "Cancel") {
281 IkiWiki::redirect($cgi, $config{url});
284 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
286 foreach my $field (keys %fields) {
287 if ($field=~/^enable\./) {
288 # rebuild is overkill for many plugins,
289 # but no good way to tell which
290 $rebuild{$field}=1; # TODO only if state changed tho
291 # TODO plugin enable/disable
295 my %info=%{$fields{$field}->[1]};
296 my $key=$fields{$field}->[0];
297 my @value=$form->field($field);
300 error("unsafe field $key"); # should never happen
304 # Avoid setting fields to empty strings,
305 # if they were not set before.
306 next if ! defined $config{$key} && ! grep { length $_ } @value;
308 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
309 if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
312 $config{$key}=\@value;
314 elsif (ref $config{$key} || ref $info{example}) {
315 error("complex field $key"); # should never happen
318 if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
321 $config{$key}=$value[0];
325 if (%rebuild && $form->submitted eq 'Save Setup') {
326 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
327 foreach my $field ($form->field) {
328 next if $rebuild{$field};
329 $form->field(name => $field, type => "hidden",
332 $form->reset(0); # doesn't really make sense here
333 $buttons=["Rebuild Wiki", "Cancel"];
336 # TODO save to real path
337 IkiWiki::Setup::dump("/tmp/s");
338 $form->text(gettext("Setup saved."));
346 IkiWiki::showform($form, $buttons, $session, $cgi);
349 sub sessioncgi ($$) { #{{{
353 if ($cgi->param("do") eq "setup") {
354 showform($cgi, $session);
359 sub formbuilder_setup (@) { #{{{
362 my $form=$params{form};
363 if ($form->title eq "preferences") {
364 push @{$params{buttons}}, "Wiki Setup";
365 if ($form->submitted && $form->submitted eq "Wiki Setup") {
366 showform($params{cgi}, $params{session});