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";
107 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
109 fieldset => $section,
111 if ($plugin_forced) {
112 $form->field(name => $name, disabled => 1);
115 $enabledfields{$name}=[$name, \%plugininfo];
119 # show plugin settings
122 my %info=%{shift @show};
124 my $description=$info{description};
125 if (exists $info{link} && length $info{link}) {
126 if ($info{link} =~ /^\w+:\/\//) {
127 $description="<a href=\"$info{link}\">$description</a>";
130 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
134 # multiple plugins can have the same field
135 my $name=defined $plugin ? $plugin.".".$key : $key;
137 my $value=$config{$key};
139 if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
140 push @{$value}, "", ""; # blank items for expansion
143 if ($info{type} eq "string") {
146 label => $description,
147 comment => formatexample($info{example}, $value),
151 fieldset => $section,
154 elsif ($info{type} eq "pagespec") {
157 label => $description,
158 comment => formatexample($info{example}, $value),
162 validate => \&IkiWiki::pagespec_valid,
163 fieldset => $section,
166 elsif ($info{type} eq "integer") {
169 label => $description,
170 comment => formatexample($info{example}, $value),
174 validate => '/^[0-9]+$/',
175 fieldset => $section,
178 elsif ($info{type} eq "boolean") {
184 options => [ [ 1 => $description ] ],
185 fieldset => $section,
190 $form->field(name => $name, disabled => 1);
193 $enabledfields{$name}=[$key, \%info];
198 # if no fields were shown for the plugin, drop it into the
200 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
202 $form->field(name => "enable.$plugin", fieldset => "plugins");
205 return %enabledfields;
208 sub showform ($$) { #{{{
212 if (! defined $session->param("name") ||
213 ! IkiWiki::is_admin($session->param("name"))) {
214 error(gettext("you are not logged in as an admin"));
217 eval q{use CGI::FormBuilder};
220 my $form = CGI::FormBuilder->new(
230 [main => gettext("main")],
231 [plugins => gettext("plugins")]
233 action => $config{cgiurl},
234 template => {type => 'div'},
235 stylesheet => IkiWiki::baseurl()."style.css",
238 $form->field(name => "do", type => "hidden", value => "setup",
241 if ($form->submitted eq 'Basic Mode') {
242 $form->field(name => "showadvanced", type => "hidden",
243 value => 0, force => 1);
245 elsif ($form->submitted eq 'Advanced Mode') {
246 $form->field(name => "showadvanced", type => "hidden",
247 value => 1, force => 1);
250 if ($form->field("showadvanced")) {
251 $config{websetup_advanced}=1;
252 $advancedtoggle="Basic Mode";
255 $config{websetup_advanced}=0;
256 $advancedtoggle="Advanced Mode";
259 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
261 IkiWiki::decode_form_utf8($form);
262 IkiWiki::run_hooks(formbuilder_setup => sub {
263 shift->(form => $form, cgi => $cgi, session => $session,
264 buttons => $buttons);
266 IkiWiki::decode_form_utf8($form);
268 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
270 # record all currently enabled plugins before all are loaded
271 my %enabled_plugins=%IkiWiki::loaded_plugins;
274 require IkiWiki::Setup;
275 foreach my $pair (IkiWiki::Setup::getsetup()) {
276 my $plugin=$pair->[0];
277 my $setup=$pair->[1];
279 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
281 $fields{$_}=$shown{$_} foreach keys %shown;
285 if ($form->submitted eq "Cancel") {
286 IkiWiki::redirect($cgi, $config{url});
289 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
290 # Push values from form into %config, avoiding unnecessary
291 # changes, and keeping track of which changes need a
294 foreach my $field (keys %fields) {
295 my %info=%{$fields{$field}->[1]};
296 my $key=$fields{$field}->[0];
297 my @value=$form->field($field);
303 error("unsafe field $key"); # should never happen
306 if (exists $info{rebuild} &&
307 ($info{rebuild} || ! defined $info{rebuild})) {
308 $rebuild{$field}=$info{rebuild};
311 if ($field=~/^enable\.(.*)/) {
313 if ($value[0] != exists $enabled_plugins{$plugin}) {
314 # TODO plugin enable/disable
317 delete $rebuild{$field};
322 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
323 @value=sort grep { length $_ } @value;
324 my @oldvalue=sort grep { length $_ }
325 (defined $config{$key} ? @{$config{$key}} : ());
326 if ((@oldvalue) == (@value)) {
327 delete $rebuild{$field};
330 $config{$key}=\@value;
333 elsif (ref $config{$key} || ref $info{example}) {
334 error("complex field $key"); # should never happen
337 if (defined $config{$key} && $config{$key} eq $value[0]) {
338 delete $rebuild{$field};
340 elsif (! defined $config{$key} && ! length $value[0]) {
341 delete $rebuild{$field};
343 elsif (! defined $config{$key} && ! $value[0] &&
344 $info{type} eq "boolean") {
345 delete $rebuild{$field};
348 print STDERR ">>$key (@value) ($config{$key})\n";
349 $config{$key}=$value[0];
354 if (%rebuild && ! $form->field("rebuild_asked")) {
355 print STDERR ">>".(join "," , keys %rebuild)."\n";
357 foreach my $field ($form->field) {
358 $required=1 if $rebuild{$field};
359 next if exists $rebuild{$field};
360 $form->field(name => $field, type => "hidden",
364 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
365 $buttons=["Rebuild Wiki", "Cancel"];
368 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
369 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
371 $form->field(name => "rebuild_asked", type => "hidden",
372 value => 1, force => 1);
373 $form->reset(0); # doesn't really make sense here
376 $form->field(name => "rebuild_asked", type => "hidden",
377 value => 0, force => 1);
378 # TODO save to real path
379 IkiWiki::Setup::dump("/tmp/s");
380 $form->text(gettext("Setup saved."));
382 if ($form->submitted eq 'Rebuild Wiki') {
388 IkiWiki::showform($form, $buttons, $session, $cgi);
391 sub sessioncgi ($$) { #{{{
395 if ($cgi->param("do") eq "setup") {
396 showform($cgi, $session);
401 sub formbuilder_setup (@) { #{{{
404 my $form=$params{form};
405 if ($form->title eq "preferences") {
406 push @{$params{buttons}}, "Wiki Setup";
407 if ($form->submitted && $form->submitted eq "Wiki Setup") {
408 showform($params{cgi}, $params{session});