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 => "checkconfig", id => "websetup", call => \&checkconfig);
17 hook(type => "sessioncgi", id => "websetup", call => \&sessioncgi);
18 hook(type => "formbuilder_setup", id => "websetup",
19 call => \&formbuilder_setup);
22 sub getsetup () { #{{{
24 websetup_force_plugins => {
26 example => \@default_force_plugins,
27 description => "list of plugins that cannot be enabled/disabled via the web interface",
31 websetup_show_unsafe => {
34 description => "show unsafe settings, read-only, in web interface?",
40 sub checkconfig () { #{{{
41 if (! exists $config{websetup_show_unsafe}) {
42 $config{websetup_show_unsafe}=1;
46 sub formatexample ($$) { #{{{
50 if (defined $value && length $value) {
53 elsif (defined $example && ! ref $example && length $example) {
54 return "<br/ ><small>Example: <tt>$example</tt></small>";
61 sub showfields ($$$@) { #{{{
71 # skip complex or internal settings
72 next if ref $config{$key} || ref $info{example} || $info{type} eq "internal";
73 # maybe skip unsafe settings
74 next if ! $info{safe} && ! $config{websetup_show_unsafe};
75 # these are handled specially, so don't show
76 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
78 push @show, $key, \%info;
81 return 0 unless @show;
83 my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
85 if (defined $plugin) {
86 if (! showplugintoggle($form, $plugin, $enabled, $section) && ! $enabled) {
87 # plugin not enabled and cannot be, so skip showing
95 my %info=%{shift @show};
97 my $description=exists $info{description_html} ? $info{description_html} : $info{description};
98 my $value=$config{$key};
99 # multiple plugins can have the same field
100 my $name=defined $plugin ? $plugin.".".$key : $key;
102 if ($info{type} eq "string") {
105 label => $description,
106 comment => formatexample($info{example}, $value),
110 fieldset => $section,
113 elsif ($info{type} eq "pagespec") {
116 label => $description,
117 comment => formatexample($info{example}, $value),
121 validate => \&IkiWiki::pagespec_valid,
122 fieldset => $section,
125 elsif ($info{type} eq "integer") {
128 label => $description,
129 comment => formatexample($info{example}, $value),
133 validate => '/^[0-9]+$/',
134 fieldset => $section,
137 elsif ($info{type} eq "boolean") {
143 options => [ [ 1 => $description ] ],
144 fieldset => $section,
149 $form->field(name => $name, disabled => 1);
150 $form->text(gettext("Note: Disabled options cannot be configured here, but only by editing the setup file."));
157 sub showplugintoggle ($$$$) { #{{{
163 if (exists $config{websetup_force_plugins} &&
164 grep { $_ eq $plugin } @{$config{websetup_force_plugins}}, @rcs_plugins) {
167 elsif (! exists $config{websetup_force_plugins} &&
168 grep { $_ eq $plugin } @default_force_plugins, @rcs_plugins) {
173 name => "enable.$plugin",
176 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
178 fieldset => $section,
184 sub showform ($$) { #{{{
188 if (! defined $session->param("name") ||
189 ! IkiWiki::is_admin($session->param("name"))) {
190 error(gettext("you are not logged in as an admin"));
193 eval q{use CGI::FormBuilder};
196 my $form = CGI::FormBuilder->new(
205 action => $config{cgiurl},
206 template => {type => 'div'},
207 stylesheet => IkiWiki::baseurl()."style.css",
209 my $buttons=["Save Setup", "Cancel"];
211 IkiWiki::decode_form_utf8($form);
212 IkiWiki::run_hooks(formbuilder_setup => sub {
213 shift->(form => $form, cgi => $cgi, session => $session,
214 buttons => $buttons);
216 IkiWiki::decode_form_utf8($form);
218 $form->field(name => "do", type => "hidden", value => "setup",
220 showfields($form, undef, undef, IkiWiki::getsetup());
222 # record all currently enabled plugins before all are loaded
223 my %enabled_plugins=%IkiWiki::loaded_plugins;
226 require IkiWiki::Setup;
227 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
228 foreach my $pair (IkiWiki::Setup::getsetup()) {
229 my $plugin=$pair->[0];
230 my $setup=$pair->[1];
232 # skip all rcs plugins except for the one in use
233 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
235 delete $plugins{$plugin} if showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
238 # list all remaining plugins (with no setup options) at the end
239 showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))
240 foreach sort keys %plugins;
242 if ($form->submitted eq "Cancel") {
243 IkiWiki::redirect($cgi, $config{url});
246 elsif ($form->submitted eq 'Save Setup' && $form->validate) {
248 IkiWiki::Setup::dump("/tmp/s");
249 $form->text(gettext("Setup saved."));
252 IkiWiki::showform($form, $buttons, $session, $cgi);
255 sub sessioncgi ($$) { #{{{
259 if ($cgi->param("do") eq "setup") {
260 showform($cgi, $session);
265 sub formbuilder_setup (@) { #{{{
268 my $form=$params{form};
269 if ($form->title eq "preferences") {
270 push @{$params{buttons}}, "Wiki Setup";
271 if ($form->submitted && $form->submitted eq "Wiki Setup") {
272 showform($params{cgi}, $params{session});