]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/websetup.pm
don't install demo external plugins by default
[ikiwiki.git] / IkiWiki / Plugin / websetup.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::websetup;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 my @rcs_plugins=(qw{git svn bzr mercurial monotone tla norcs});
9
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});
13
14 sub import { #{{{
15         hook(type => "checkconfig", id => "websetup", call => \&checkconfig);
16         hook(type => "getsetup", id => "websetup", call => \&getsetup);
17         hook(type => "sessioncgi", id => "websetup", call => \&sessioncgi);
18         hook(type => "formbuilder_setup", id => "websetup", 
19              call => \&formbuilder_setup);
20 } # }}}
21
22 sub getsetup () { #{{{
23         return
24                 websetup_force_plugins => {
25                         type => "string",
26                         example => \@default_force_plugins,
27                         description => "list of plugins that cannot be enabled/disabled via the web interface",
28                         safe => 0,
29                         rebuild => 0,
30                 },
31 } #}}}
32
33 sub checkconfig () { #{{{
34         if (! exists $config{websetup_force_plugins}) {
35                 $config{websetup_force_plugins}=\@default_force_plugins;
36         }
37 } #}}}
38
39 sub formatexample ($) { #{{{
40         my $example=shift;
41
42         if (defined $example && ! ref $example && length $example) {
43                 return "<br/ ><small>Example: <tt>$example</tt></small>";
44         }
45         else {
46                 return "";
47         }
48 } #}}}
49
50 sub showfields ($$$@) { #{{{
51         my $form=shift;
52         my $plugin=shift;
53         my $enabled=shift;
54
55         my @show;
56         while (@_) {
57                 my $key=shift;
58                 my %info=%{shift()};
59
60                 # skip complex, unsafe, or internal settings
61                 next if ref $config{$key} || ! $info{safe} || $info{type} eq "internal";
62                 # these are handled specially, so don't show
63                 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
64                 
65                 push @show, $key, \%info;
66         }
67
68         return 0 unless @show;
69
70         my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
71
72         if (defined $plugin) {
73                 if (! showplugintoggle($form, $plugin, $enabled, $section) && ! $enabled) {
74                     # plugin not enabled and cannot be, so skip showing
75                     # its configuration
76                     return 0;
77                 }
78         }
79
80         while (@show) {
81                 my $key=shift @show;
82                 my %info=%{shift @show};
83
84                 my $description=exists $info{description_html} ? $info{description_html} : $info{description};
85                 my $value=$config{$key};
86                 # multiple plugins can have the same field
87                 my $name=defined $plugin ? $plugin.".".$key : $key;
88                 
89                 if ($info{type} eq "string") {
90                         $form->field(
91                                 name => $name,
92                                 label => $description,
93                                 comment => defined $value && length $value ? "" : formatexample($info{example}),
94                                 type => "text",
95                                 value => $value,
96                                 size => 60,
97                                 fieldset => $section,
98                         );
99                 }
100                 elsif ($info{type} eq "pagespec") {
101                         $form->field(
102                                 name => $name,
103                                 label => $description,
104                                 comment => formatexample($info{example}),
105                                 type => "text",
106                                 value => $value,
107                                 size => 60,
108                                 validate => \&IkiWiki::pagespec_valid,
109                                 fieldset => $section,
110                         );
111                 }
112                 elsif ($info{type} eq "integer") {
113                         $form->field(
114                                 name => $name,
115                                 label => $description,
116                                 type => "text",
117                                 value => $value,
118                                 size => 5,
119                                 validate => '/^[0-9]+$/',
120                                 fieldset => $section,
121                         );
122                 }
123                 elsif ($info{type} eq "boolean") {
124                         $form->field(
125                                 name => $name,
126                                 label => "",
127                                 type => "checkbox",
128                                 value => $value,
129                                 options => [ [ 1 => $description ] ],
130                                 fieldset => $section,
131                         );
132                 }
133         }
134
135         return 1;
136 } #}}}
137
138 sub showplugintoggle ($$$$) { #{{{
139         my $form=shift;
140         my $plugin=shift;
141         my $enabled=shift;
142         my $section=shift;
143
144         return 0 if (grep { $_ eq $plugin } @{$config{websetup_force_plugins}}, @rcs_plugins);
145
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         );
154
155         return 1;
156 } #}}}
157
158 sub showform ($$) { #{{{
159         my $cgi=shift;
160         my $session=shift;
161
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         }
166
167         eval q{use CGI::FormBuilder};
168         error($@) if $@;
169
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"];
184
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);
191
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;
198
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;
208
209                 delete $plugins{$plugin} if showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
210         }
211
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
223                 $form->text(gettext("Setup saved."));
224         }
225
226         IkiWiki::showform($form, $buttons, $session, $cgi);
227 } #}}}
228
229 sub sessioncgi ($$) { #{{{
230         my $cgi=shift;
231         my $session=shift;
232
233         if ($cgi->param("do") eq "setup") {
234                 showform($cgi, $session);
235                 exit;
236         }
237 } #}}}
238
239 sub formbuilder_setup (@) { #{{{
240         my %params=@_;
241
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 } #}}}
251
252 1