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
93 my $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
97 my %info=%{shift @show};
99 my $description=$info{description};
100 if (exists $info{link} && length $info{link}) {
101 if ($info{link} =~ /^\w+:\/\//) {
102 $description="<a href=\"$info{link}\">$description</a>";
105 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
109 # multiple plugins can have the same field
110 my $name=defined $plugin ? $plugin.".".$key : $key;
112 my $value=$config{$key};
114 if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
115 push @{$value}, "", ""; # blank items for expansion
118 if ($info{type} eq "string") {
121 label => $description,
122 comment => formatexample($info{example}, $value),
126 fieldset => $section,
129 elsif ($info{type} eq "pagespec") {
132 label => $description,
133 comment => formatexample($info{example}, $value),
137 validate => \&IkiWiki::pagespec_valid,
138 fieldset => $section,
141 elsif ($info{type} eq "integer") {
144 label => $description,
145 comment => formatexample($info{example}, $value),
149 validate => '/^[0-9]+$/',
150 fieldset => $section,
153 elsif ($info{type} eq "boolean") {
159 options => [ [ 1 => $description ] ],
160 fieldset => $section,
165 $form->field(name => $name, disabled => 1);
168 $shownfields{$name}=[$key, \%info];
172 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
173 my $name="enable.$plugin";
174 $section="plugins" unless %shownfields;
179 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
181 fieldset => $section,
183 if ($plugin_forced) {
184 $form->field(name => $name, disabled => 1);
187 $shownfields{$name}=[$name, \%plugininfo];
194 sub showform ($$) { #{{{
198 if (! defined $session->param("name") ||
199 ! IkiWiki::is_admin($session->param("name"))) {
200 error(gettext("you are not logged in as an admin"));
203 eval q{use CGI::FormBuilder};
206 my $form = CGI::FormBuilder->new(
216 [main => gettext("main")],
217 [plugins => gettext("plugins")]
219 action => $config{cgiurl},
220 template => {type => 'div'},
221 stylesheet => IkiWiki::baseurl()."style.css",
224 if ($form->submitted eq 'Basic') {
225 $form->field(name => "showadvanced", type => "hidden",
226 value => 0, force => 1);
228 elsif ($form->submitted eq 'Advanced') {
229 $form->field(name => "showadvanced", type => "hidden",
230 value => 1, force => 1);
233 if ($form->field("showadvanced")) {
234 $config{websetup_advanced}=1;
235 $advancedtoggle="Basic";
238 $config{websetup_advanced}=0;
239 $advancedtoggle="Advanced";
242 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
244 IkiWiki::decode_form_utf8($form);
245 IkiWiki::run_hooks(formbuilder_setup => sub {
246 shift->(form => $form, cgi => $cgi, session => $session,
247 buttons => $buttons);
249 IkiWiki::decode_form_utf8($form);
251 $form->field(name => "do", type => "hidden", value => "setup",
253 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
255 # record all currently enabled plugins before all are loaded
256 my %enabled_plugins=%IkiWiki::loaded_plugins;
259 require IkiWiki::Setup;
260 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
261 foreach my $pair (IkiWiki::Setup::getsetup()) {
262 my $plugin=$pair->[0];
263 my $setup=$pair->[1];
265 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
267 delete $plugins{$plugin};
268 $fields{$_}=$shown{$_} foreach keys %shown;
272 if ($form->submitted eq "Cancel") {
273 IkiWiki::redirect($cgi, $config{url});
276 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
278 foreach my $field (keys %fields) {
279 if ($field=~/^enable\./) {
280 # rebuild is overkill for many plugins,
281 # but no good way to tell which
282 $rebuild{$field}=1; # TODO only if state changed tho
283 # TODO plugin enable/disable
287 my %info=%{$fields{$field}->[1]};
288 my $key=$fields{$field}->[0];
289 my @value=$form->field($field);
292 error("unsafe field $key"); # should never happen
296 # Avoid setting fields to empty strings,
297 # if they were not set before.
298 next if ! defined $config{$key} && ! grep { length $_ } @value;
300 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
301 if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
304 $config{$key}=\@value;
306 elsif (ref $config{$key} || ref $info{example}) {
307 error("complex field $key"); # should never happen
310 if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
313 $config{$key}=$value[0];
317 if (%rebuild && $form->submitted eq 'Save Setup') {
318 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
319 foreach my $field ($form->field) {
320 next if $rebuild{$field};
321 $form->field(name => $field, type => "hidden",
324 $form->reset(0); # doesn't really make sense here
325 $buttons=["Rebuild Wiki", "Cancel"];
328 # TODO save to real path
329 IkiWiki::Setup::dump("/tmp/s");
330 $form->text(gettext("Setup saved."));
338 IkiWiki::showform($form, $buttons, $session, $cgi);
341 sub sessioncgi ($$) { #{{{
345 if ($cgi->param("do") eq "setup") {
346 showform($cgi, $session);
351 sub formbuilder_setup (@) { #{{{
354 my $form=$params{form};
355 if ($form->title eq "preferences") {
356 push @{$params{buttons}}, "Wiki Setup";
357 if ($form->submitted && $form->submitted eq "Wiki Setup") {
358 showform($params{cgi}, $params{session});