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 $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
86 (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
87 if ($plugin_forced && ! $enabled) {
88 # plugin is forced disabled, so skip its configuration
94 my $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
98 my %info=%{shift @show};
100 my $description=$info{description};
101 if (exists $info{link} && length $info{link}) {
102 if ($info{link} =~ /^\w+:\/\//) {
103 $description="<a href=\"$info{link}\">$description</a>";
106 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
110 # multiple plugins can have the same field
111 my $name=defined $plugin ? $plugin.".".$key : $key;
113 my $value=$config{$key};
115 if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
116 push @{$value}, "", ""; # blank items for expansion
119 if ($info{type} eq "string") {
122 label => $description,
123 comment => formatexample($info{example}, $value),
127 fieldset => $section,
130 elsif ($info{type} eq "pagespec") {
133 label => $description,
134 comment => formatexample($info{example}, $value),
138 validate => \&IkiWiki::pagespec_valid,
139 fieldset => $section,
142 elsif ($info{type} eq "integer") {
145 label => $description,
146 comment => formatexample($info{example}, $value),
150 validate => '/^[0-9]+$/',
151 fieldset => $section,
154 elsif ($info{type} eq "boolean") {
160 options => [ [ 1 => $description ] ],
161 fieldset => $section,
166 $form->field(name => $name, disabled => 1);
167 $skippedfields{$name}=1;
170 $shownfields{$name}=[$key, \%info];
174 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
175 my $name="enable.$plugin";
176 $section="plugins" unless %shownfields || (%skippedfields && $config{websetup_advanced});
181 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
183 fieldset => $section,
185 if ($plugin_forced) {
186 $form->field(name => $name, disabled => 1);
189 $shownfields{$name}=[$name, \%plugininfo];
196 sub showform ($$) { #{{{
200 if (! defined $session->param("name") ||
201 ! IkiWiki::is_admin($session->param("name"))) {
202 error(gettext("you are not logged in as an admin"));
205 eval q{use CGI::FormBuilder};
208 my $form = CGI::FormBuilder->new(
218 [main => gettext("main")],
219 [plugins => gettext("plugins")]
221 action => $config{cgiurl},
222 template => {type => 'div'},
223 stylesheet => IkiWiki::baseurl()."style.css",
226 if ($form->submitted eq 'Basic Mode') {
227 $form->field(name => "showadvanced", type => "hidden",
228 value => 0, force => 1);
230 elsif ($form->submitted eq 'Advanced Mode') {
231 $form->field(name => "showadvanced", type => "hidden",
232 value => 1, force => 1);
235 if ($form->field("showadvanced")) {
236 $config{websetup_advanced}=1;
237 $advancedtoggle="Basic Mode";
240 $config{websetup_advanced}=0;
241 $advancedtoggle="Advanced Mode";
244 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
246 IkiWiki::decode_form_utf8($form);
247 IkiWiki::run_hooks(formbuilder_setup => sub {
248 shift->(form => $form, cgi => $cgi, session => $session,
249 buttons => $buttons);
251 IkiWiki::decode_form_utf8($form);
253 $form->field(name => "do", type => "hidden", value => "setup",
255 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
257 # record all currently enabled plugins before all are loaded
258 my %enabled_plugins=%IkiWiki::loaded_plugins;
261 require IkiWiki::Setup;
262 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
263 foreach my $pair (IkiWiki::Setup::getsetup()) {
264 my $plugin=$pair->[0];
265 my $setup=$pair->[1];
267 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
269 delete $plugins{$plugin};
270 $fields{$_}=$shown{$_} foreach keys %shown;
274 if ($form->submitted eq "Cancel") {
275 IkiWiki::redirect($cgi, $config{url});
278 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
280 foreach my $field (keys %fields) {
281 if ($field=~/^enable\./) {
282 # rebuild is overkill for many plugins,
283 # but no good way to tell which
284 $rebuild{$field}=1; # TODO only if state changed tho
285 # TODO plugin enable/disable
289 my %info=%{$fields{$field}->[1]};
290 my $key=$fields{$field}->[0];
291 my @value=$form->field($field);
294 error("unsafe field $key"); # should never happen
298 # Avoid setting fields to empty strings,
299 # if they were not set before.
300 next if ! defined $config{$key} && ! grep { length $_ } @value;
302 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
303 if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
306 $config{$key}=\@value;
308 elsif (ref $config{$key} || ref $info{example}) {
309 error("complex field $key"); # should never happen
312 if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
315 $config{$key}=$value[0];
319 if (%rebuild && $form->submitted eq 'Save Setup') {
320 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
321 foreach my $field ($form->field) {
322 next if $rebuild{$field};
323 $form->field(name => $field, type => "hidden",
326 $form->reset(0); # doesn't really make sense here
327 $buttons=["Rebuild Wiki", "Cancel"];
330 # TODO save to real path
331 IkiWiki::Setup::dump("/tmp/s");
332 $form->text(gettext("Setup saved."));
340 IkiWiki::showform($form, $buttons, $session, $cgi);
343 sub sessioncgi ($$) { #{{{
347 if ($cgi->param("do") eq "setup") {
348 showform($cgi, $session);
353 sub formbuilder_setup (@) { #{{{
356 my $form=$params{form};
357 if ($form->title eq "preferences") {
358 push @{$params{buttons}}, "Wiki Setup";
359 if ($form->submitted && $form->submitted eq "Wiki Setup") {
360 showform($params{cgi}, $params{session});