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 $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
89 my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
90 (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
91 if ($plugin_forced && ! $enabled) {
92 # plugin is forced disabled, so skip its settings
97 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
98 my $name="enable.$plugin";
103 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
105 fieldset => $section,
107 if ($plugin_forced) {
108 $form->field(name => $name, disabled => 1);
111 $enabledfields{$name}=[$name, \%plugininfo];
115 # show plugin settings
118 my %info=%{shift @show};
120 my $description=$info{description};
121 if (exists $info{link} && length $info{link}) {
122 if ($info{link} =~ /^\w+:\/\//) {
123 $description="<a href=\"$info{link}\">$description</a>";
126 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
130 # multiple plugins can have the same field
131 my $name=defined $plugin ? $plugin.".".$key : $key;
133 my $value=$config{$key};
135 if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
136 push @{$value}, "", ""; # blank items for expansion
139 if ($info{type} eq "string") {
142 label => $description,
143 comment => formatexample($info{example}, $value),
147 fieldset => $section,
150 elsif ($info{type} eq "pagespec") {
153 label => $description,
154 comment => formatexample($info{example}, $value),
158 validate => \&IkiWiki::pagespec_valid,
159 fieldset => $section,
162 elsif ($info{type} eq "integer") {
165 label => $description,
166 comment => formatexample($info{example}, $value),
170 validate => '/^[0-9]+$/',
171 fieldset => $section,
174 elsif ($info{type} eq "boolean") {
180 options => [ [ 1 => $description ] ],
181 fieldset => $section,
186 $form->field(name => $name, disabled => 1);
189 $enabledfields{$name}=[$key, \%info];
194 # if no fields were shown for the plugin, drop it into the
196 if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
198 $form->field(name => "enable.$plugin", fieldset => "plugins");
201 return %enabledfields;
204 sub showform ($$) { #{{{
208 if (! defined $session->param("name") ||
209 ! IkiWiki::is_admin($session->param("name"))) {
210 error(gettext("you are not logged in as an admin"));
213 eval q{use CGI::FormBuilder};
216 my $form = CGI::FormBuilder->new(
226 [main => gettext("main")],
227 [plugins => gettext("plugins")]
229 action => $config{cgiurl},
230 template => {type => 'div'},
231 stylesheet => IkiWiki::baseurl()."style.css",
234 $form->field(name => "do", type => "hidden", value => "setup",
237 if ($form->submitted eq 'Basic Mode') {
238 $form->field(name => "showadvanced", type => "hidden",
239 value => 0, force => 1);
241 elsif ($form->submitted eq 'Advanced Mode') {
242 $form->field(name => "showadvanced", type => "hidden",
243 value => 1, force => 1);
246 if ($form->field("showadvanced")) {
247 $config{websetup_advanced}=1;
248 $advancedtoggle="Basic Mode";
251 $config{websetup_advanced}=0;
252 $advancedtoggle="Advanced Mode";
255 my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
257 IkiWiki::decode_form_utf8($form);
258 IkiWiki::run_hooks(formbuilder_setup => sub {
259 shift->(form => $form, cgi => $cgi, session => $session,
260 buttons => $buttons);
262 IkiWiki::decode_form_utf8($form);
264 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
266 # record all currently enabled plugins before all are loaded
267 my %enabled_plugins=%IkiWiki::loaded_plugins;
270 require IkiWiki::Setup;
271 foreach my $pair (IkiWiki::Setup::getsetup()) {
272 my $plugin=$pair->[0];
273 my $setup=$pair->[1];
275 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
277 $fields{$_}=$shown{$_} foreach keys %shown;
281 if ($form->submitted eq "Cancel") {
282 IkiWiki::redirect($cgi, $config{url});
285 elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
286 # Push values from form into %config, avoiding unnecessary
287 # changes, and keeping track of which changes need a
290 foreach my $field (keys %fields) {
291 my %info=%{$fields{$field}->[1]};
292 my $key=$fields{$field}->[0];
293 my @value=$form->field($field);
299 error("unsafe field $key"); # should never happen
302 if (exists $info{rebuild} &&
303 ($info{rebuild} || ! defined $info{rebuild})) {
304 $rebuild{$field}=$info{rebuild};
307 if ($field=~/^enable\.(.*)/) {
309 if ($value[0] != exists $enabled_plugins{$plugin}) {
310 # TODO plugin enable/disable
313 delete $rebuild{$field};
318 if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
319 @value=sort grep { length $_ } @value;
320 my @oldvalue=sort grep { length $_ }
321 (defined $config{$key} ? @{$config{$key}} : ());
322 if ((@oldvalue) == (@value)) {
323 delete $rebuild{$field};
326 $config{$key}=\@value;
329 elsif (ref $config{$key} || ref $info{example}) {
330 error("complex field $key"); # should never happen
333 if (defined $config{$key} && $config{$key} eq $value[0]) {
334 delete $rebuild{$field};
336 elsif (! defined $config{$key} && ! length $value[0]) {
337 delete $rebuild{$field};
339 elsif (! defined $config{$key} && ! $value[0] &&
340 $info{type} eq "boolean") {
341 delete $rebuild{$field};
344 print STDERR ">>$key (@value) ($config{$key})\n";
345 $config{$key}=$value[0];
350 if (%rebuild && ! $form->field("rebuild_asked")) {
351 print STDERR ">>".(join "," , keys %rebuild)."\n";
353 foreach my $field ($form->field) {
354 $required=1 if $rebuild{$field};
355 next if exists $rebuild{$field};
356 $form->field(name => $field, type => "hidden",
360 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
361 $buttons=["Rebuild Wiki", "Cancel"];
364 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
365 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
367 $form->field(name => "rebuild_asked", type => "hidden",
368 value => 1, force => 1);
369 $form->reset(0); # doesn't really make sense here
372 $form->field(name => "rebuild_asked", type => "hidden",
373 value => 0, force => 1);
374 # TODO save to real path
375 IkiWiki::Setup::dump("/tmp/s");
376 $form->text(gettext("Setup saved."));
378 if ($form->submitted eq 'Rebuild Wiki') {
384 IkiWiki::showform($form, $buttons, $session, $cgi);
387 sub sessioncgi ($$) { #{{{
391 if ($cgi->param("do") eq "setup") {
392 showform($cgi, $session);
397 sub formbuilder_setup (@) { #{{{
400 my $form=$params{form};
401 if ($form->title eq "preferences") {
402 push @{$params{buttons}}, "Wiki Setup";
403 if ($form->submitted && $form->submitted eq "Wiki Setup") {
404 showform($params{cgi}, $params{session});