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;
83 my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
86 if (defined $plugin) {
87 if (showplugintoggle($form, $plugin, $enabled, $section)) {
88 $shownfields{"enable.$plugin"}=$plugin;
91 # plugin not enabled and cannot be, so skip showing
99 my %info=%{shift @show};
101 my $description=exists $info{description_html} ? $info{description_html} : $info{description};
102 my $value=$config{$key};
103 # multiple plugins can have the same field
104 my $name=defined $plugin ? $plugin.".".$key : $key;
106 if ($info{type} eq "string") {
109 label => $description,
110 comment => formatexample($info{example}, $value),
114 fieldset => $section,
117 elsif ($info{type} eq "pagespec") {
120 label => $description,
121 comment => formatexample($info{example}, $value),
125 validate => \&IkiWiki::pagespec_valid,
126 fieldset => $section,
129 elsif ($info{type} eq "integer") {
132 label => $description,
133 comment => formatexample($info{example}, $value),
137 validate => '/^[0-9]+$/',
138 fieldset => $section,
141 elsif ($info{type} eq "boolean") {
147 options => [ [ 1 => $description ] ],
148 fieldset => $section,
153 $form->field(name => $name, disabled => 1);
154 $form->text(gettext("Note: Disabled options cannot be configured here, but only by editing the setup file."));
157 $shownfields{$name}=$key;
164 sub showplugintoggle ($$$$) { #{{{
170 if (exists $config{websetup_force_plugins} &&
171 grep { $_ eq $plugin } @{$config{websetup_force_plugins}}, @rcs_plugins) {
174 elsif (! exists $config{websetup_force_plugins} &&
175 grep { $_ eq $plugin } @default_force_plugins, @rcs_plugins) {
180 ame => "enable.$plugin",
183 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
185 fieldset => $section,
191 sub showform ($$) { #{{{
195 if (! defined $session->param("name") ||
196 ! IkiWiki::is_admin($session->param("name"))) {
197 error(gettext("you are not logged in as an admin"));
200 eval q{use CGI::FormBuilder};
203 my $form = CGI::FormBuilder->new(
212 action => $config{cgiurl},
213 template => {type => 'div'},
214 stylesheet => IkiWiki::baseurl()."style.css",
216 my $buttons=["Save Setup", "Cancel"];
218 IkiWiki::decode_form_utf8($form);
219 IkiWiki::run_hooks(formbuilder_setup => sub {
220 shift->(form => $form, cgi => $cgi, session => $session,
221 buttons => $buttons);
223 IkiWiki::decode_form_utf8($form);
225 $form->field(name => "do", type => "hidden", value => "setup",
227 my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
229 # record all currently enabled plugins before all are loaded
230 my %enabled_plugins=%IkiWiki::loaded_plugins;
233 require IkiWiki::Setup;
234 my %plugins=map { $_ => 1 } IkiWiki::listplugins();
235 foreach my $pair (IkiWiki::Setup::getsetup()) {
236 my $plugin=$pair->[0];
237 my $setup=$pair->[1];
239 # skip all rcs plugins except for the one in use
240 next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
242 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
244 delete $plugins{$plugin};
245 $fields{$_}=$shown{$_} foreach keys %shown;
249 # list all remaining plugins (with no setup options) at the end
250 foreach (sort keys %plugins) {
251 if (showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))) {
252 $fields{"enable.$_"}=$_;
256 if ($form->submitted eq "Cancel") {
257 IkiWiki::redirect($cgi, $config{url});
260 elsif ($form->submitted eq 'Save Setup' && $form->validate) {
261 foreach my $field (keys %fields) {
262 # TODO plugin enable/disable
263 next if $field=~/^enable\./; # plugin
264 $config{$fields{$field}}=$form->field($field);
266 # TODO save to real path
267 IkiWiki::Setup::dump("/tmp/s");
268 $form->text(gettext("Setup saved."));
271 IkiWiki::showform($form, $buttons, $session, $cgi);
274 sub sessioncgi ($$) { #{{{
278 if ($cgi->param("do") eq "setup") {
279 showform($cgi, $session);
284 sub formbuilder_setup (@) { #{{{
287 my $form=$params{form};
288 if ($form->title eq "preferences") {
289 push @{$params{buttons}}, "Wiki Setup";
290 if ($form->submitted && $form->submitted eq "Wiki Setup") {
291 showform($params{cgi}, $params{session});