summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/websetup.pm
blob: 0a0d4480f92458c3798f8b269293eec1469d44f7 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::websetup;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. my @rcs_plugins=(qw{git svn bzr mercurial monotone tla norcs});
  7. my @default_force_plugins=(qw{amazon_s3});
  8. sub import { #{{{
  9. hook(type => "checkconfig", id => "websetup", call => \&checkconfig);
  10. hook(type => "getsetup", id => "websetup", call => \&getsetup);
  11. hook(type => "sessioncgi", id => "websetup", call => \&sessioncgi);
  12. hook(type => "formbuilder_setup", id => "websetup",
  13. call => \&formbuilder_setup);
  14. } # }}}
  15. sub getsetup () { #{{{
  16. return
  17. websetup_force_plugins => {
  18. type => "string",
  19. example => \@default_force_plugins,
  20. description => "list of plugins that cannot be enabled/disabled via the web interface",
  21. safe => 0,
  22. rebuild => 0,
  23. },
  24. } #}}}
  25. sub checkconfig () { #{{{
  26. if (! exists $config{websetup_force_plugins}) {
  27. $config{websetup_force_plugins}=\@default_force_plugins;
  28. }
  29. } #}}}
  30. sub formatexample ($) { #{{{
  31. my $example=shift;
  32. if (defined $example && ! ref $example && length $example) {
  33. return "<br/ ><small>Example: <tt>$example</tt></small>";
  34. }
  35. else {
  36. return "";
  37. }
  38. } #}}}
  39. sub showfields ($$$@) { #{{{
  40. my $form=shift;
  41. my $plugin=shift;
  42. my $enabled=shift;
  43. my @show;
  44. while (@_) {
  45. my $key=shift;
  46. my %info=%{shift()};
  47. # skip complex, unsafe, or internal settings
  48. next if ref $config{$key} || ! $info{safe} || $info{type} eq "internal";
  49. # these are handled specially, so don't show
  50. next if $key eq 'add_plugins' || $key eq 'disable_plugins';
  51. push @show, $key, \%info;
  52. }
  53. return 0 unless @show;
  54. my $section=defined $plugin ? $plugin." ".gettext("plugin") : gettext("main");
  55. if (defined $plugin) {
  56. if (! showplugintoggle($form, $plugin, $enabled, $section) && ! $enabled) {
  57. # plugin not enabled and cannot be, so skip showing
  58. # its configuration
  59. return 0;
  60. }
  61. }
  62. while (@show) {
  63. my $key=shift @show;
  64. my %info=%{shift @show};
  65. my $description=exists $info{description_html} ? $info{description_html} : $info{description};
  66. my $value=$config{$key};
  67. # multiple plugins can have the same field
  68. my $name=defined $plugin ? $plugin.".".$key : $key;
  69. if ($info{type} eq "string") {
  70. $form->field(
  71. name => $name,
  72. label => $description,
  73. comment => defined $value && length $value ? "" : formatexample($info{example}),
  74. type => "text",
  75. value => $value,
  76. size => 60,
  77. fieldset => $section,
  78. );
  79. }
  80. elsif ($info{type} eq "pagespec") {
  81. $form->field(
  82. name => $name,
  83. label => $description,
  84. comment => formatexample($info{example}),
  85. type => "text",
  86. value => $value,
  87. size => 60,
  88. validate => \&IkiWiki::pagespec_valid,
  89. fieldset => $section,
  90. );
  91. }
  92. elsif ($info{type} eq "integer") {
  93. $form->field(
  94. name => $name,
  95. label => $description,
  96. type => "text",
  97. value => $value,
  98. size => 5,
  99. validate => '/^[0-9]+$/',
  100. fieldset => $section,
  101. );
  102. }
  103. elsif ($info{type} eq "boolean") {
  104. $form->field(
  105. name => $name,
  106. label => "",
  107. type => "checkbox",
  108. value => $value,
  109. options => [ [ 1 => $description ] ],
  110. fieldset => $section,
  111. );
  112. }
  113. }
  114. return 1;
  115. } #}}}
  116. sub showplugintoggle ($$$$) { #{{{
  117. my $form=shift;
  118. my $plugin=shift;
  119. my $enabled=shift;
  120. my $section=shift;
  121. return 0 if (grep { $_ eq $plugin } @{$config{websetup_force_plugins}}, @rcs_plugins);
  122. $form->field(
  123. name => "enable.$plugin",
  124. label => "",
  125. type => "checkbox",
  126. options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
  127. value => $enabled,
  128. fieldset => $section,
  129. );
  130. return 1;
  131. } #}}}
  132. sub showform ($$) { #{{{
  133. my $cgi=shift;
  134. my $session=shift;
  135. if (! defined $session->param("name") ||
  136. ! IkiWiki::is_admin($session->param("name"))) {
  137. error(gettext("you are not logged in as an admin"));
  138. }
  139. eval q{use CGI::FormBuilder};
  140. error($@) if $@;
  141. my $form = CGI::FormBuilder->new(
  142. title => "setup",
  143. name => "setup",
  144. header => 0,
  145. charset => "utf-8",
  146. method => 'POST',
  147. javascript => 0,
  148. reset => 1,
  149. params => $cgi,
  150. action => $config{cgiurl},
  151. template => {type => 'div'},
  152. stylesheet => IkiWiki::baseurl()."style.css",
  153. );
  154. my $buttons=["Save Setup", "Cancel"];
  155. IkiWiki::decode_form_utf8($form);
  156. IkiWiki::run_hooks(formbuilder_setup => sub {
  157. shift->(form => $form, cgi => $cgi, session => $session,
  158. buttons => $buttons);
  159. });
  160. IkiWiki::decode_form_utf8($form);
  161. $form->field(name => "do", type => "hidden", value => "setup",
  162. force => 1);
  163. showfields($form, undef, undef, IkiWiki::getsetup());
  164. # record all currently enabled plugins before all are loaded
  165. my %enabled_plugins=%IkiWiki::loaded_plugins;
  166. # per-plugin setup
  167. require IkiWiki::Setup;
  168. my %plugins=map { $_ => 1 } IkiWiki::listplugins();
  169. foreach my $pair (IkiWiki::Setup::getsetup()) {
  170. my $plugin=$pair->[0];
  171. my $setup=$pair->[1];
  172. # skip all rcs plugins except for the one in use
  173. next if $plugin ne $config{rcs} && grep { $_ eq $plugin } @rcs_plugins;
  174. delete $plugins{$plugin} if showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
  175. }
  176. # list all remaining plugins (with no setup options) at the end
  177. showplugintoggle($form, $_, $enabled_plugins{$_}, gettext("other plugins"))
  178. foreach sort keys %plugins;
  179. if ($form->submitted eq "Cancel") {
  180. IkiWiki::redirect($cgi, $config{url});
  181. return;
  182. }
  183. elsif ($form->submitted eq 'Save Setup' && $form->validate) {
  184. # TODO
  185. $form->text(gettext("Setup saved."));
  186. }
  187. IkiWiki::showform($form, $buttons, $session, $cgi);
  188. } #}}}
  189. sub sessioncgi ($$) { #{{{
  190. my $cgi=shift;
  191. my $session=shift;
  192. if ($cgi->param("do") eq "setup") {
  193. showform($cgi, $session);
  194. exit;
  195. }
  196. } #}}}
  197. sub formbuilder_setup (@) { #{{{
  198. my %params=@_;
  199. my $form=$params{form};
  200. if ($form->title eq "preferences") {
  201. push @{$params{buttons}}, "Wiki Setup";
  202. if ($form->submitted && $form->submitted eq "Wiki Setup") {
  203. showform($params{cgi}, $params{session});
  204. exit;
  205. }
  206. }
  207. } #}}}
  208. 1