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 () { #{{{
22 websetup_force_plugins => {
25 description => "list of plugins that cannot be enabled/disabled via the web interface",
29 websetup_show_unsafe => {
32 description => "show unsafe settings, read-only, in web interface?",
38 sub checkconfig () { #{{{
39 if (! exists $config{websetup_show_unsafe}) {
40 $config{websetup_show_unsafe}=1;
44 sub formatexample ($$) { #{{{
48 if (defined $value && length $value) {
51 elsif (defined $example && ! ref $example && length $example) {
52 return "<br/ ><small>Example: <tt>$example</tt></small>";
59 sub showfields ($$$@) { #{{{
70 # skip internal settings
71 next if defined $info{type} && $info{type} eq "internal";
72 # XXX hashes not handled yet
73 next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
74 # maybe skip unsafe settings
75 next if ! $info{safe} && ! ($config{websetup_show_unsafe} && $config{websetup_advanced});
76 # maybe skip advanced settings
77 next if $info{advanced} && ! $config{websetup_advanced};
78 # these are handled specially, so don't show
79 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
81 if ($key eq 'plugin') {
86 push @show, $key, \%info;
89 my $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
93 my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
94 (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
95 if ($plugin_forced && ! $enabled) {
96 # plugin is forced disabled, so skip its settings
101 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
102 my $name="enable.$plugin";
105 label => sprintf(gettext("enable %s?"), $plugin),
108 fieldset => $section,
110 [ 1 => gettext("Yes") ],
111 [ 0 => gettext("No") ]
114 if ($plugin_forced) {
115 $form->field(name => $name, disabled => 1);
118 $enabledfields{$name}=[$name, \%plugininfo];
122 # show plugin settings
125 my %info=%{shift @show};
127 my $description=$info{description};
128 if (exists $info{link} && length $info{link}) {
129 if ($info{link} =~ /^\w+:\/\//) {
130 $description="<a href=\"$info{link}\">$description</a>";
133 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
137 # multiple plugins can have the same field
138 my $name=defined $plugin ? $plugin.".".$key : $key;
140 my $value=$config{$key};
142 if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
143 push @{$value}, "", ""; # blank items for expansion
146 if ($info{type} eq "string") {
149 label => $description,
150 comment => formatexample($info{example}, $value),
154 fieldset => $section,
157 elsif ($info{type} eq "pagespec") {
160 label => $description,
161 comment => formatexample($info{example}, $value),
165 validate => \&IkiWiki::pagespec_valid,
166 fieldset => $section,
169 elsif ($info{type} eq "integer") {
172 label => $description,
173 comment => formatexample($info{example}, $value),
177 validate => '/^[0-9]+$/',
178 fieldset => $section,
181 elsif ($info{type} eq "boolean") {
184 label => $description,
188 [ 1 => gettext("Yes") ],
189 [ 0 => gettext("No") ]
191 fieldset => $section,
196 $form->field(name => $name, disabled => 1);
199 $enabledfields{$name}=[$key, \%info];
204 # if no fields were shown for the plugin, drop it into the
206 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
208 $form->field(name => "enable.$plugin", fieldset => "plugins");
211 return %enabledfields;
214 sub showform ($$) { #{{{
218 if (! defined $session->param("name") ||
219 ! IkiWiki::is_admin($session->param("name"))) {
220 error(gettext("you are not logged in as an admin"));
223 eval q{use CGI::FormBuilder};
226 my $form = CGI::FormBuilder->new(
236 [main => gettext("main")],
237 [plugins => gettext("plugins")]
239 action => $config{cgiurl},
241 template => {type => 'div'},
242 stylesheet => IkiWiki::baseurl()."style.css",
245 $form->field(name => "do", type => "hidden", value => "setup",
248 if ($form->submitted eq 'Basic Mode') {
249 $form->field(name => "showadvanced", type => "hidden",
250 value => 0, force => 1);
252 elsif ($form->submitted eq 'Advanced Mode') {
253 $form->field(name => "showadvanced", type => "hidden",
254 value => 1, force => 1);
257 if ($form->field("showadvanced")) {
258 $config{websetup_advanced}=1;
259 $advancedtoggle="Basic Mode";
262 $config{websetup_advanced}=0;
263 $advancedtoggle="Advanced Mode";
266 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
268 IkiWiki::decode_form_utf8($form);
269 IkiWiki::run_hooks(formbuilder_setup => sub {
270 shift->(form => $form, cgi => $cgi, session => $session,
271 buttons => $buttons);
273 IkiWiki::decode_form_utf8($form);
275 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
277 # record all currently enabled plugins before all are loaded
278 my %enabled_plugins=%IkiWiki::loaded_plugins;
281 require IkiWiki::Setup;
282 foreach my $pair (IkiWiki::Setup::getsetup()) {
283 my $plugin=$pair->[0];
284 my $setup=$pair->[1];
286 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
288 $fields{$_}=$shown{$_} foreach keys %shown;
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) {
297 # Push values from form into %config, avoiding unnecessary
298 # changes, and keeping track of which changes need a
301 foreach my $field (keys %fields) {
302 my %info=%{$fields{$field}->[1]};
303 my $key=$fields{$field}->[0];
304 my @value=$form->field($field);
310 error("unsafe field $key"); # should never happen
313 if (exists $info{rebuild} &&
314 ($info{rebuild} || ! defined $info{rebuild})) {
315 $rebuild{$field}=$info{rebuild};
318 if ($field=~/^enable\.(.*)/) {
320 if ($value[0] != exists $enabled_plugins{$plugin}) {
321 # TODO plugin enable/disable
324 delete $rebuild{$field};
329 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
330 @value=sort grep { length $_ } @value;
331 my @oldvalue=sort grep { length $_ }
332 (defined $config{$key} ? @{$config{$key}} : ());
333 if ((@oldvalue) == (@value)) {
334 delete $rebuild{$field};
337 $config{$key}=\@value;
340 elsif (ref $config{$key} || ref $info{example}) {
341 error("complex field $key"); # should never happen
344 if (defined $config{$key} && $config{$key} eq $value[0]) {
345 delete $rebuild{$field};
347 elsif (! defined $config{$key} && ! length $value[0]) {
348 delete $rebuild{$field};
350 elsif (! defined $config{$key} && ! $value[0] &&
351 $info{type} eq "boolean") {
352 delete $rebuild{$field};
355 $config{$key}=$value[0];
360 if (%rebuild && ! $form->field("rebuild_asked")) {
362 foreach my $field ($form->field) {
363 $required=1 if $rebuild{$field};
364 next if exists $rebuild{$field};
365 $form->field(name => $field, type => "hidden",
369 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
370 $buttons=["Rebuild Wiki", "Cancel"];
373 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
374 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
376 $form->field(name => "rebuild_asked", type => "hidden",
377 value => 1, force => 1);
378 $form->reset(0); # doesn't really make sense here
381 $form->field(name => "rebuild_asked", type => "hidden",
382 value => 0, force => 1);
383 # TODO save to real path
384 IkiWiki::Setup::dump("/tmp/s");
385 $form->text(gettext("Setup saved."));
387 if ($form->submitted eq 'Rebuild Wiki') {
393 IkiWiki::showform($form, $buttons, $session, $cgi);
396 sub sessioncgi ($$) { #{{{
400 if ($cgi->param("do") eq "setup") {
401 showform($cgi, $session);
406 sub formbuilder_setup (@) { #{{{
409 my $form=$params{form};
410 if ($form->title eq "preferences") {
411 push @{$params{buttons}}, "Wiki Setup";
412 if ($form->submitted && $form->submitted eq "Wiki Setup") {
413 showform($params{cgi}, $params{session});