2 package IkiWiki::Plugin::websetup;
8 my @rcs_plugins=(qw{git svn bzr mercurial monotone tla norcs});
9 my @default_force_plugins=(qw{amazon_s3});
12 hook(type => "checkconfig", id => "websetup", call => \&checkconfig);
13 hook(type => "getsetup", id => "websetup", call => \&getsetup);
14 hook(type => "sessioncgi", id => "websetup", call => \&sessioncgi);
15 hook(type => "formbuilder_setup", id => "websetup",
16 call => \&formbuilder_setup);
19 sub getsetup () { #{{{
21 websetup_force_plugins => {
23 example => \@default_force_plugins,
24 description => "list of plugins that cannot be enabled/disabled via the web interface",
30 sub checkconfig () { #{{{
31 if (! exists $config{websetup_force_plugins}) {
32 $config{websetup_force_plugins}=\@default_force_plugins;
36 sub formatexample ($) { #{{{
39 if (defined $example && ! ref $example && length $example) {
40 return "<br/ ><small>Example: <tt>$example</tt></small>";
47 sub showfields ($$$@) { #{{{
57 # skip complex, unsafe, or internal settings
58 next if ref $config{$key} || ! $info{safe} || $info{type} eq "internal";
59 # these are handled specially, so don't show
60 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
62 push @show, $key, \%info;
65 return 0 unless @show;
67 my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
69 if (defined $plugin) {
70 if (! showplugintoggle($form, $plugin, $enabled, $section) && ! $enabled) {
71 # plugin not enabled and cannot be, so skip showing
79 my %info=%{shift @show};
81 my $description=exists $info{description_html} ? $info{description_html} : $info{description};
82 my $value=$config{$key};
83 # multiple plugins can have the same field
84 my $name=defined $plugin ? $plugin.".".$key : $key;
86 if ($info{type} eq "string") {
89 label => $description,
90 comment => defined $value && length $value ? "" : formatexample($info{example}),
97 elsif ($info{type} eq "pagespec") {
100 label => $description,
101 comment => formatexample($info{example}),
105 validate => \&IkiWiki::pagespec_valid,
106 fieldset => $section,
109 elsif ($info{type} eq "integer") {
112 label => $description,
116 validate => '/^[0-9]+$/',
117 fieldset => $section,
120 elsif ($info{type} eq "boolean") {
126 options => [ [ 1 => $description ] ],
127 fieldset => $section,
135 sub showplugintoggle ($$$$) { #{{{
141 return 0 if (grep { $_ eq $plugin } @{$config{websetup_force_plugins}}, @rcs_plugins);
144 name => "enable.$plugin",
147 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
149 fieldset => $section,
155 sub showform ($$) { #{{{
159 if (! defined $session->param("name") ||
160 ! IkiWiki::is_admin($session->param("name"))) {
161 error(gettext("you are not logged in as an admin"));
164 eval q{use CGI::FormBuilder};
167 my $form = CGI::FormBuilder->new(
176 action => $config{cgiurl},
177 template => {type => 'div'},
178 stylesheet => IkiWiki::baseurl()."style.css",
180 my $buttons=["Save Setup", "Cancel"];
182 IkiWiki::decode_form_utf8($form);
183 IkiWiki::run_hooks(formbuilder_setup => sub {
184 shift->(form => $form, cgi => $cgi, session => $session,
185 buttons => $buttons);
187 IkiWiki::decode_form_utf8($form);
189 $form->field(name => "do", type => "hidden", value => "setup",
191 showfields($form, undef, undef, IkiWiki::getsetup());
193 # record all currently enabled plugins before all are loaded
194 my %enabled_plugins=%IkiWiki::loaded_plugins;
197 require IkiWiki::Setup;
198 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
199 foreach my $pair (IkiWiki::Setup::getsetup()) {
200 my $plugin=$pair->[0];
201 my $setup=$pair->[1];
203 # skip all rcs plugins except for the one in use
204 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
206 delete $plugins{$plugin} if showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
209 # list all remaining plugins (with no setup options) at the end
210 showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))
211 foreach sort keys %plugins;
213 if ($form->submitted eq "Cancel") {
214 IkiWiki::redirect($cgi, $config{url});
217 elsif ($form->submitted eq 'Save Setup' && $form->validate) {
220 $form->text(gettext("Setup saved."));
223 IkiWiki::showform($form, $buttons, $session, $cgi);
226 sub sessioncgi ($$) { #{{{
230 if ($cgi->param("do") eq "setup") {
231 showform($cgi, $session);
236 sub formbuilder_setup (@) { #{{{
239 my $form=$params{form};
240 if ($form->title eq "preferences") {
241 push @{$params{buttons}}, "Wiki Setup";
242 if ($form->submitted && $form->submitted eq "Wiki Setup") {
243 showform($params{cgi}, $params{session});