2 package IkiWiki::Plugin::websetup;
8 my @rcs_plugins=(qw{git svn bzr mercurial monotone tla norcs});
10 # amazon_s3 is not something that should be enabled via the web.
11 # external is not a standalone plugin.
12 my @default_force_plugins=(qw{amazon_s3 external});
15 hook(type => "getsetup", id => "websetup", call => \&getsetup);
16 hook(type => "sessioncgi", id => "websetup", call => \&sessioncgi);
17 hook(type => "formbuilder_setup", id => "websetup",
18 call => \&formbuilder_setup);
21 sub getsetup () { #{{{
23 websetup_force_plugins => {
25 example => \@default_force_plugins,
26 description => "list of plugins that cannot be enabled/disabled via the web interface",
32 sub formatexample ($) { #{{{
35 if (defined $example && ! ref $example && length $example) {
36 return "<br/ ><small>Example: <tt>$example</tt></small>";
43 sub showfields ($$$@) { #{{{
53 # skip complex, unsafe, or internal settings
54 next if ref $config{$key} || ! $info{safe} || $info{type} eq "internal";
55 # these are handled specially, so don't show
56 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
58 push @show, $key, \%info;
61 return 0 unless @show;
63 my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
65 if (defined $plugin) {
66 if (! showplugintoggle($form, $plugin, $enabled, $section) && ! $enabled) {
67 # plugin not enabled and cannot be, so skip showing
75 my %info=%{shift @show};
77 my $description=exists $info{description_html} ? $info{description_html} : $info{description};
78 my $value=$config{$key};
79 # multiple plugins can have the same field
80 my $name=defined $plugin ? $plugin.".".$key : $key;
82 if ($info{type} eq "string") {
85 label => $description,
86 comment => defined $value && length $value ? "" : formatexample($info{example}),
93 elsif ($info{type} eq "pagespec") {
96 label => $description,
97 comment => formatexample($info{example}),
101 validate => \&IkiWiki::pagespec_valid,
102 fieldset => $section,
105 elsif ($info{type} eq "integer") {
108 label => $description,
112 validate => '/^[0-9]+$/',
113 fieldset => $section,
116 elsif ($info{type} eq "boolean") {
122 options => [ [ 1 => $description ] ],
123 fieldset => $section,
131 sub showplugintoggle ($$$$) { #{{{
137 if (exists $config{websetup_force_plugins} &&
138 grep { $_ eq $plugin } @{$config{websetup_force_plugins}}, @rcs_plugins) {
141 elsif (! exists $config{websetup_force_plugins} &&
142 grep { $_ eq $plugin } @default_force_plugins, @rcs_plugins) {
147 name => "enable.$plugin",
150 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
152 fieldset => $section,
158 sub showform ($$) { #{{{
162 if (! defined $session->param("name") ||
163 ! IkiWiki::is_admin($session->param("name"))) {
164 error(gettext("you are not logged in as an admin"));
167 eval q{use CGI::FormBuilder};
170 my $form = CGI::FormBuilder->new(
179 action => $config{cgiurl},
180 template => {type => 'div'},
181 stylesheet => IkiWiki::baseurl()."style.css",
183 my $buttons=["Save Setup", "Cancel"];
185 IkiWiki::decode_form_utf8($form);
186 IkiWiki::run_hooks(formbuilder_setup => sub {
187 shift->(form => $form, cgi => $cgi, session => $session,
188 buttons => $buttons);
190 IkiWiki::decode_form_utf8($form);
192 $form->field(name => "do", type => "hidden", value => "setup",
194 showfields($form, undef, undef, IkiWiki::getsetup());
196 # record all currently enabled plugins before all are loaded
197 my %enabled_plugins=%IkiWiki::loaded_plugins;
200 require IkiWiki::Setup;
201 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
202 foreach my $pair (IkiWiki::Setup::getsetup()) {
203 my $plugin=$pair->[0];
204 my $setup=$pair->[1];
206 # skip all rcs plugins except for the one in use
207 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
209 delete $plugins{$plugin} if showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
212 # list all remaining plugins (with no setup options) at the end
213 showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))
214 foreach sort keys %plugins;
216 if ($form->submitted eq "Cancel") {
217 IkiWiki::redirect($cgi, $config{url});
220 elsif ($form->submitted eq 'Save Setup' && $form->validate) {
222 IkiWiki::Setup::dump("/tmp/s");
223 $form->text(gettext("Setup saved."));
226 IkiWiki::showform($form, $buttons, $session, $cgi);
229 sub sessioncgi ($$) { #{{{
233 if ($cgi->param("do") eq "setup") {
234 showform($cgi, $session);
239 sub formbuilder_setup (@) { #{{{
242 my $form=$params{form};
243 if ($form->title eq "preferences") {
244 push @{$params{buttons}}, "Wiki Setup";
245 if ($form->submitted && $form->submitted eq "Wiki Setup") {
246 showform($params{cgi}, $params{session});