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 disabled and cannot be turned on,
89 # 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);
169 $shownfields{$name}=[$key, \%info];
173 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
174 my $name="enable.$plugin";
175 $section="plugins" unless %shownfields;
180 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
182 fieldset => $section,
184 if ($plugin_forced) {
185 $form->field(name => $name, disabled => 1);
188 $shownfields{$name}=[$name, \%plugininfo];
195 sub showform ($$) { #{{{
199 if (! defined $session->param("name") ||
200 ! IkiWiki::is_admin($session->param("name"))) {
201 error(gettext("you are not logged in as an admin"));
204 eval q{use CGI::FormBuilder};
207 my $form = CGI::FormBuilder->new(
217 [main => gettext("main")],
218 [plugins => gettext("plugins")]
220 action => $config{cgiurl},
221 template => {type => 'div'},
222 stylesheet => IkiWiki::baseurl()."style.css",
225 if ($form->submitted eq 'Basic') {
226 $form->field(name => "showadvanced", type => "hidden",
227 value => 0, force => 1);
229 elsif ($form->submitted eq 'Advanced') {
230 $form->field(name => "showadvanced", type => "hidden",
231 value => 1, force => 1);
234 if ($form->field("showadvanced")) {
235 $config{websetup_advanced}=1;
236 $advancedtoggle="Basic";
239 $config{websetup_advanced}=0;
240 $advancedtoggle="Advanced";
243 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
245 IkiWiki::decode_form_utf8($form);
246 IkiWiki::run_hooks(formbuilder_setup => sub {
247 shift->(form => $form, cgi => $cgi, session => $session,
248 buttons => $buttons);
250 IkiWiki::decode_form_utf8($form);
252 $form->field(name => "do", type => "hidden", value => "setup",
254 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
256 # record all currently enabled plugins before all are loaded
257 my %enabled_plugins=%IkiWiki::loaded_plugins;
260 require IkiWiki::Setup;
261 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
262 foreach my $pair (IkiWiki::Setup::getsetup()) {
263 my $plugin=$pair->[0];
264 my $setup=$pair->[1];
266 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
268 delete $plugins{$plugin};
269 $fields{$_}=$shown{$_} foreach keys %shown;
273 if ($form->submitted eq "Cancel") {
274 IkiWiki::redirect($cgi, $config{url});
277 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
279 foreach my $field (keys %fields) {
280 if ($field=~/^enable\./) {
281 # rebuild is overkill for many plugins,
282 # but no good way to tell which
283 $rebuild{$field}=1; # TODO only if state changed tho
284 # TODO plugin enable/disable
288 my %info=%{$fields{$field}->[1]};
289 my $key=$fields{$field}->[0];
290 my @value=$form->field($field);
293 error("unsafe field $key"); # should never happen
297 # Avoid setting fields to empty strings,
298 # if they were not set before.
299 next if ! defined $config{$key} && ! grep { length $_ } @value;
301 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
302 if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
305 $config{$key}=\@value;
307 elsif (ref $config{$key} || ref $info{example}) {
308 error("complex field $key"); # should never happen
311 if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
314 $config{$key}=$value[0];
318 if (%rebuild && $form->submitted eq 'Save Setup') {
319 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
320 foreach my $field ($form->field) {
321 next if $rebuild{$field};
322 $form->field(name => $field, type => "hidden",
325 $form->reset(0); # doesn't really make sense here
326 $buttons=["Rebuild Wiki", "Cancel"];
329 # TODO save to real path
330 IkiWiki::Setup::dump("/tmp/s");
331 $form->text(gettext("Setup saved."));
339 IkiWiki::showform($form, $buttons, $session, $cgi);
342 sub sessioncgi ($$) { #{{{
346 if ($cgi->param("do") eq "setup") {
347 showform($cgi, $session);
352 sub formbuilder_setup (@) { #{{{
355 my $form=$params{form};
356 if ($form->title eq "preferences") {
357 push @{$params{buttons}}, "Wiki Setup";
358 if ($form->submitted && $form->submitted eq "Wiki Setup") {
359 showform($params{cgi}, $params{session});