]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/websetup.pm
6aab04e320fe21d02a36a9334ab0f3ab068bd191
[git.ikiwiki.info.git] / IkiWiki / Plugin / websetup.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::websetup;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
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});
14 sub import { #{{{
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);
19 } # }}}
21 sub getsetup () { #{{{
22         return
23                 websetup_force_plugins => {
24                         type => "string",
25                         example => \@default_force_plugins,
26                         description => "list of plugins that cannot be enabled/disabled via the web interface",
27                         safe => 0,
28                         rebuild => 0,
29                 },
30 } #}}}
32 sub formatexample ($) { #{{{
33         my $example=shift;
35         if (defined $example && ! ref $example && length $example) {
36                 return "<br/ ><small>Example: <tt>$example</tt></small>";
37         }
38         else {
39                 return "";
40         }
41 } #}}}
43 sub showfields ($$$@) { #{{{
44         my $form=shift;
45         my $plugin=shift;
46         my $enabled=shift;
48         my @show;
49         while (@_) {
50                 my $key=shift;
51                 my %info=%{shift()};
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';
57                 
58                 push @show, $key, \%info;
59         }
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
68                     # its configuration
69                     return 0;
70                 }
71         }
73         while (@show) {
74                 my $key=shift @show;
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;
81                 
82                 if ($info{type} eq "string") {
83                         $form->field(
84                                 name => $name,
85                                 label => $description,
86                                 comment => defined $value && length $value ? "" : formatexample($info{example}),
87                                 type => "text",
88                                 value => $value,
89                                 size => 60,
90                                 fieldset => $section,
91                         );
92                 }
93                 elsif ($info{type} eq "pagespec") {
94                         $form->field(
95                                 name => $name,
96                                 label => $description,
97                                 comment => formatexample($info{example}),
98                                 type => "text",
99                                 value => $value,
100                                 size => 60,
101                                 validate => \&IkiWiki::pagespec_valid,
102                                 fieldset => $section,
103                         );
104                 }
105                 elsif ($info{type} eq "integer") {
106                         $form->field(
107                                 name => $name,
108                                 label => $description,
109                                 type => "text",
110                                 value => $value,
111                                 size => 5,
112                                 validate => '/^[0-9]+$/',
113                                 fieldset => $section,
114                         );
115                 }
116                 elsif ($info{type} eq "boolean") {
117                         $form->field(
118                                 name => $name,
119                                 label => "",
120                                 type => "checkbox",
121                                 value => $value,
122                                 options => [ [ 1 => $description ] ],
123                                 fieldset => $section,
124                         );
125                 }
126         }
128         return 1;
129 } #}}}
131 sub showplugintoggle ($$$$) { #{{{
132         my $form=shift;
133         my $plugin=shift;
134         my $enabled=shift;
135         my $section=shift;
137         if (exists $config{websetup_force_plugins} &&
138             grep { $_ eq $plugin } @{$config{websetup_force_plugins}}, @rcs_plugins) {
139                 return 0;
140         }
141         elsif (! exists $config{websetup_force_plugins} &&
142                grep { $_ eq $plugin } @default_force_plugins, @rcs_plugins) {
143                 return 0;
144         }
146         $form->field(
147                 name => "enable.$plugin",
148                 label => "",
149                 type => "checkbox",
150                 options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
151                 value => $enabled,
152                 fieldset => $section,
153         );
155         return 1;
156 } #}}}
158 sub showform ($$) { #{{{
159         my $cgi=shift;
160         my $session=shift;
162         if (! defined $session->param("name") || 
163             ! IkiWiki::is_admin($session->param("name"))) {
164                 error(gettext("you are not logged in as an admin"));
165         }
167         eval q{use CGI::FormBuilder};
168         error($@) if $@;
170         my $form = CGI::FormBuilder->new(
171                 title => "setup",
172                 name => "setup",
173                 header => 0,
174                 charset => "utf-8",
175                 method => 'POST',
176                 javascript => 0,
177                 reset => 1,
178                 params => $cgi,
179                 action => $config{cgiurl},
180                 template => {type => 'div'},
181                 stylesheet => IkiWiki::baseurl()."style.css",
182         );
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);
189         });
190         IkiWiki::decode_form_utf8($form);
192         $form->field(name => "do", type => "hidden", value => "setup",
193                 force => 1);
194         showfields($form, undef, undef, IkiWiki::getsetup());
195         
196         # record all currently enabled plugins before all are loaded
197         my %enabled_plugins=%IkiWiki::loaded_plugins;
199         # per-plugin setup
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];
205                 
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});
210         }
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;
215         
216         if ($form->submitted eq "Cancel") {
217                 IkiWiki::redirect($cgi, $config{url});
218                 return;
219         }
220         elsif ($form->submitted eq 'Save Setup' && $form->validate) {
221                 # TODO
222                 IkiWiki::Setup::dump("/tmp/s");
223                 $form->text(gettext("Setup saved."));
224         }
226         IkiWiki::showform($form, $buttons, $session, $cgi);
227 } #}}}
229 sub sessioncgi ($$) { #{{{
230         my $cgi=shift;
231         my $session=shift;
233         if ($cgi->param("do") eq "setup") {
234                 showform($cgi, $session);
235                 exit;
236         }
237 } #}}}
239 sub formbuilder_setup (@) { #{{{
240         my %params=@_;
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});
247                         exit;
248                 }
249         }
250 } #}}}