shortcut: If default_pageext is set, first look for shortcuts.default_pageext
[ikiwiki] / IkiWiki / Plugin / websetup.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::websetup;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 sub import {
9         hook(type => "getsetup", id => "websetup", call => \&getsetup);
10         hook(type => "checkconfig", id => "websetup", call => \&checkconfig);
11         hook(type => "sessioncgi", id => "websetup", call => \&sessioncgi);
12         hook(type => "formbuilder_setup", id => "websetup", 
13              call => \&formbuilder_setup);
14 }
15
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                 },
22                 websetup_force_plugins => {
23                         type => "string",
24                         example => [],
25                         description => "list of plugins that cannot be enabled/disabled via the web interface",
26                         safe => 0,
27                         rebuild => 0,
28                 },
29                 websetup_show_unsafe => {
30                         type => "boolean",
31                         example => 1,
32                         description => "show unsafe settings, read-only, in web interface?",
33                         safe => 0,
34                         rebuild => 0,
35                 },
36 }
37
38 sub checkconfig () {
39         if (! exists $config{websetup_show_unsafe}) {
40                 $config{websetup_show_unsafe}=1;
41         }
42 }
43
44 sub formatexample ($$) {
45         my $example=shift;
46         my $value=shift;
47
48         if (defined $value && length $value) {
49                 return "";
50         }
51         elsif (defined $example && ! ref $example && length $example) {
52                 return "<br/ ><small>Example: <tt>$example</tt></small>";
53         }
54         else {
55                 return "";
56         }
57 }
58
59 sub showfields ($$$@) {
60         my $form=shift;
61         my $plugin=shift;
62         my $enabled=shift;
63
64         my @show;
65         my %plugininfo;
66         while (@_) {
67                 my $key=shift;
68                 my %info=%{shift()};
69
70                 # skip internal settings
71                 next if defined $info{type} && $info{type} eq "internal";
72                 # XXX hashes not handled yet
73                 next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
74                 # maybe skip unsafe settings
75                 next if ! $info{safe} && ! ($config{websetup_show_unsafe} && $config{websetup_advanced});
76                 # maybe skip advanced settings
77                 next if $info{advanced} && ! $config{websetup_advanced};
78                 # these are handled specially, so don't show
79                 next if $key eq 'add_plugins' || $key eq 'disable_plugins';
80
81                 if ($key eq 'plugin') {
82                         %plugininfo=%info;
83                         next;
84                 }
85                 
86                 push @show, $key, \%info;
87         }
88
89         my $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
90         my %enabledfields;
91         my $shownfields=0;
92         
93         my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
94                 (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
95         if ($plugin_forced && ! $enabled) {
96                 # plugin is forced disabled, so skip its settings
97                 @show=();
98         }
99
100         # show plugin toggle
101         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
102                 my $name="enable.$plugin";
103                 $form->field(
104                         name => $name,
105                         label => "",
106                         type => "checkbox",
107                         fieldset => $section,
108                         options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ]]
109                 );
110                 if (! $form->submitted) {
111                         $form->field(name => $name, value => $enabled);
112                 }
113                 if ($plugin_forced) {
114                         $form->field(name => $name, disabled => 1);
115                 }
116                 else {
117                         $enabledfields{$name}=[$name, \%plugininfo];
118                 }
119         }
120
121         # show plugin settings
122         while (@show) {
123                 my $key=shift @show;
124                 my %info=%{shift @show};
125
126                 my $description=$info{description};
127                 if (exists $info{link} && length $info{link}) {
128                         if ($info{link} =~ /^\w+:\/\//) {
129                                 $description="<a href=\"$info{link}\">$description</a>";
130                         }
131                         else {
132                                 $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
133                         }
134                 }
135
136                 # multiple plugins can have the same field
137                 my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;
138
139                 my $value=$config{$key};
140
141                 if ($info{safe} && (ref $value eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
142                         $value=[(ref $value eq 'ARRAY' ? @{$value} : ""), "", ""]; # blank items for expansion
143                 }
144
145                 if ($info{type} eq "string") {
146                         $form->field(
147                                 name => $name,
148                                 label => $description,
149                                 comment => formatexample($info{example}, $value),
150                                 type => "text",
151                                 value => $value,
152                                 size => 60,
153                                 fieldset => $section,
154                         );
155                 }
156                 elsif ($info{type} eq "pagespec") {
157                         $form->field(
158                                 name => $name,
159                                 label => $description,
160                                 comment => formatexample($info{example}, $value),
161                                 type => "text",
162                                 value => $value,
163                                 size => 60,
164                                 validate => \&IkiWiki::pagespec_valid,
165                                 fieldset => $section,
166                         );
167                 }
168                 elsif ($info{type} eq "integer") {
169                         $form->field(
170                                 name => $name,
171                                 label => $description,
172                                 comment => formatexample($info{example}, $value),
173                                 type => "text",
174                                 value => $value,
175                                 size => 5,
176                                 validate => '/^[0-9]+$/',
177                                 fieldset => $section,
178                         );
179                 }
180                 elsif ($info{type} eq "boolean") {
181                         $form->field(
182                                 name => $name,
183                                 label => "",
184                                 type => "checkbox",
185                                 options => [ [ 1 => $description ] ],
186                                 fieldset => $section,
187                         );
188                         if (! $form->submitted) {
189                                 $form->field(name => $name, value => $value);
190                         }
191                 }
192                 
193                 if (! $info{safe}) {
194                         $form->field(name => $name, disabled => 1);
195                 }
196                 else {
197                         $enabledfields{$name}=[$key, \%info];
198                 }
199                 $shownfields++;
200         }
201         
202         # if no fields were shown for the plugin, drop it into the
203         # plugins fieldset
204         if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
205             ! $shownfields) {
206                 $form->field(name => "enable.$plugin", fieldset => "plugins");
207         }
208
209         return %enabledfields;
210 }
211
212 sub enable_plugin ($) {
213         my $plugin=shift;
214
215         $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
216         push @{$config{add_plugins}}, $plugin;
217 }
218
219 sub disable_plugin ($) {
220         my $plugin=shift;
221
222         if (grep { $_ eq $plugin } @{$config{add_plugins}}) {
223                 $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
224         }
225         else {
226                 push @{$config{disable_plugins}}, $plugin;
227         }
228 }
229
230 sub showform ($$) {
231         my $cgi=shift;
232         my $session=shift;
233
234         if (! defined $session->param("name") || 
235             ! IkiWiki::is_admin($session->param("name"))) {
236                 error(gettext("you are not logged in as an admin"));
237         }
238
239         if (! exists $config{setupfile}) {
240                 error(gettext("setup file for this wiki is not known"));
241         }
242
243         eval q{use CGI::FormBuilder};
244         error($@) if $@;
245
246         my $form = CGI::FormBuilder->new(
247                 title => "setup",
248                 name => "setup",
249                 header => 0,
250                 charset => "utf-8",
251                 method => 'POST',
252                 javascript => 0,
253                 reset => 1,
254                 params => $cgi,
255                 fieldsets => [
256                         [main => gettext("main")], 
257                         [plugins => gettext("plugins")]
258                 ],
259                 action => $config{cgiurl},
260                 template => {type => 'div'},
261                 stylesheet => IkiWiki::baseurl()."style.css",
262         );
263         
264         $form->field(name => "do", type => "hidden", value => "setup",
265                 force => 1);
266         $form->field(name => "rebuild_asked", type => "hidden");
267
268         if ($form->submitted eq 'Basic Mode') {
269                 $form->field(name => "showadvanced", type => "hidden", 
270                         value => 0, force => 1);
271         }
272         elsif ($form->submitted eq 'Advanced Mode') {
273                 $form->field(name => "showadvanced", type => "hidden", 
274                         value => 1, force => 1);
275         }
276         my $advancedtoggle;
277         if ($form->field("showadvanced")) {
278                 $config{websetup_advanced}=1;
279                 $advancedtoggle="Basic Mode";
280         }
281         else {
282                 $config{websetup_advanced}=0;
283                 $advancedtoggle="Advanced Mode";
284         }
285
286         my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
287
288         IkiWiki::decode_form_utf8($form);
289         IkiWiki::run_hooks(formbuilder_setup => sub {
290                 shift->(form => $form, cgi => $cgi, session => $session,
291                         buttons => $buttons);
292         });
293         IkiWiki::decode_form_utf8($form);
294
295         my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
296         
297         # record all currently enabled plugins before all are loaded
298         my %enabled_plugins=%IkiWiki::loaded_plugins;
299
300         # per-plugin setup
301         require IkiWiki::Setup;
302         foreach my $pair (IkiWiki::Setup::getsetup()) {
303                 my $plugin=$pair->[0];
304                 my $setup=$pair->[1];
305
306                 my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
307                 if (%shown) {
308                         $fields{$_}=$shown{$_} foreach keys %shown;
309                 }
310         }
311         
312         if ($form->submitted eq "Cancel") {
313                 IkiWiki::redirect($cgi, $config{url});
314                 return;
315         }
316         elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
317                 # Push values from form into %config, avoiding unnecessary
318                 # changes, and keeping track of which changes need a
319                 # rebuild.
320                 my %rebuild;
321                 foreach my $field (keys %fields) {
322                         my %info=%{$fields{$field}->[1]};
323                         my $key=$fields{$field}->[0];
324                         my @value=$form->field($field);
325                         if (! @value) {
326                                 @value=0;
327                         }
328                 
329                         if (! $info{safe}) {
330                                 error("unsafe field $key"); # should never happen
331                         }
332                 
333                         if (exists $info{rebuild} &&
334                             ($info{rebuild} || ! defined $info{rebuild})) {
335                                 $rebuild{$field}=$info{rebuild};
336                         }
337                                         
338                         if ($field=~/^enable\.(.*)/) {
339                                 my $plugin=$1;
340                                 $value[0]=0 if ! length $value[0];
341                                 if ($value[0] != exists $enabled_plugins{$plugin}) {
342                                         if ($value[0]) {
343                                                 enable_plugin($plugin);
344                                         }
345                                         else {
346                                                 disable_plugin($plugin);
347
348                                         }
349                                 }
350                                 else {
351                                         delete $rebuild{$field};
352                                 }
353                                 next;
354                         }
355
356                         if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
357                                 @value=sort grep { length $_ } @value;
358                                 my @oldvalue=sort grep { length $_ }
359                                         (defined $config{$key} ? @{$config{$key}} : ());
360                                 if ((@oldvalue) == (@value)) {
361                                         delete $rebuild{$field};
362                                 }
363                                 else {
364                                         $config{$key}=\@value;
365                                 }
366                         }
367                         elsif (ref $config{$key} || ref $info{example}) {
368                                 error("complex field $key"); # should never happen
369                         }
370                         else {
371                                 if (defined $config{$key} && $config{$key} eq $value[0]) {
372                                         delete $rebuild{$field};
373                                 }
374                                 elsif (! defined $config{$key} && ! length $value[0]) {
375                                         delete $rebuild{$field};
376                                 }
377                                 elsif ((! defined $config{$key} || ! $config{$key}) &&
378                                        ! $value[0] && $info{type} eq "boolean") {
379                                         delete $rebuild{$field};
380                                 }
381                                 else {
382                                         $config{$key}=$value[0];
383                                 }
384                         }
385                 }
386                 
387                 if (%rebuild && ! $form->field("rebuild_asked")) {
388                         my $required=0;
389                         foreach my $field ($form->field) {
390                                 $required=1 if $rebuild{$field};
391                                 next if exists $rebuild{$field};
392                                 $form->field(name => $field, type => "hidden");
393                         }
394                         if ($required) {
395                                 $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
396                                 $buttons=["Rebuild Wiki", "Cancel"];
397                         }
398                         else {
399                                 $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
400                                 $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
401                         }
402                         $form->field(name => "rebuild_asked", value => 1, force => 1);
403                         $form->reset(0); # doesn't really make sense here
404                 }
405                 else {
406                         IkiWiki::Setup::dump($config{setupfile});
407
408                         IkiWiki::saveindex();
409                         IkiWiki::unlockwiki();
410
411                         # Print the top part of a standard misctemplate,
412                         # then show the rebuild or refresh.
413                         my $divider="xxx";
414                         my $html=IkiWiki::misctemplate("setup", $divider);
415                         IkiWiki::printheader($session);
416                         my ($head, $tail)=split($divider, $html, 2);
417                         print $head."<pre>\n";
418
419                         my @command;
420                         if ($form->submitted eq 'Rebuild Wiki') {
421                                 @command=("ikiwiki", "-setup", $config{setupfile},
422                                         "-rebuild", "-v");
423                         }
424                         else {
425                                 @command=("ikiwiki", "-setup", $config{setupfile},
426                                         "-refresh", "-wrappers", "-v");
427                         }
428
429                         my $ret=system(@command);
430                         print "\n<pre>";
431                         if ($ret != 0) {
432                                 print '<p class="error">'.
433                                         sprintf(gettext("<p class=\"error\">Error: %s exited nonzero (%s)"),
434                                                 join(" ", @command), $ret).
435                                         '</p>';
436                         }
437
438                         print $tail;                    
439                         exit 0;
440                 }
441         }
442
443         IkiWiki::showform($form, $buttons, $session, $cgi);
444 }
445
446 sub sessioncgi ($$) {
447         my $cgi=shift;
448         my $session=shift;
449
450         if ($cgi->param("do") eq "setup") {
451                 showform($cgi, $session);
452                 exit;
453         }
454 }
455
456 sub formbuilder_setup (@) {
457         my %params=@_;
458
459         my $form=$params{form};
460         if ($form->title eq "preferences") {
461                 push @{$params{buttons}}, "Wiki Setup";
462                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
463                         showform($params{cgi}, $params{session});
464                         exit;
465                 }
466         }
467 }
468
469 1