summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/websetup.pm
blob: 4670d69c0ad8d96b993cbfd799faabb125bc9772 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::websetup;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. sub import { #{{{
  7. hook(type => "getsetup", id => "websetup", call => \&getsetup);
  8. hook(type => "checkconfig", id => "websetup", call => \&checkconfig);
  9. hook(type => "sessioncgi", id => "websetup", call => \&sessioncgi);
  10. hook(type => "formbuilder_setup", id => "websetup",
  11. call => \&formbuilder_setup);
  12. } # }}}
  13. sub getsetup () { #{{{
  14. return
  15. websetup_force_plugins => {
  16. type => "string",
  17. example => [],
  18. description => "list of plugins that cannot be enabled/disabled via the web interface",
  19. safe => 0,
  20. rebuild => 0,
  21. },
  22. websetup_show_unsafe => {
  23. type => "boolean",
  24. example => 1,
  25. description => "show unsafe settings, read-only, in web interface?",
  26. safe => 0,
  27. rebuild => 0,
  28. },
  29. } #}}}
  30. sub checkconfig () { #{{{
  31. if (! exists $config{websetup_show_unsafe}) {
  32. $config{websetup_show_unsafe}=1;
  33. }
  34. } #}}}
  35. sub formatexample ($$) { #{{{
  36. my $example=shift;
  37. my $value=shift;
  38. if (defined $value && length $value) {
  39. return "";
  40. }
  41. elsif (defined $example && ! ref $example && length $example) {
  42. return "<br/ ><small>Example: <tt>$example</tt></small>";
  43. }
  44. else {
  45. return "";
  46. }
  47. } #}}}
  48. sub showfields ($$$@) { #{{{
  49. my $form=shift;
  50. my $plugin=shift;
  51. my $enabled=shift;
  52. my @show;
  53. my %plugininfo;
  54. while (@_) {
  55. my $key=shift;
  56. my %info=%{shift()};
  57. # skip internal settings
  58. next if defined $info{type} && $info{type} eq "internal";
  59. # XXX hashes not handled yet
  60. next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
  61. # maybe skip unsafe settings
  62. next if ! $info{safe} && ! ($config{websetup_show_unsafe} && $config{websetup_advanced});
  63. # maybe skip advanced settings
  64. next if $info{advanced} && ! $config{websetup_advanced};
  65. # these are handled specially, so don't show
  66. next if $key eq 'add_plugins' || $key eq 'disable_plugins';
  67. if ($key eq 'plugin') {
  68. %plugininfo=%info;
  69. next;
  70. }
  71. push @show, $key, \%info;
  72. }
  73. my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
  74. (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
  75. if ($plugin_forced && ! $enabled) {
  76. # plugin is forced disabled, so skip its configuration
  77. return;
  78. }
  79. my %shownfields;
  80. my $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
  81. while (@show) {
  82. my $key=shift @show;
  83. my %info=%{shift @show};
  84. my $description=$info{description};
  85. if (exists $info{link} && length $info{link}) {
  86. if ($info{link} =~ /^\w+:\/\//) {
  87. $description="<a href=\"$info{link}\">$description</a>";
  88. }
  89. else {
  90. $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
  91. }
  92. }
  93. # multiple plugins can have the same field
  94. my $name=defined $plugin ? $plugin.".".$key : $key;
  95. my $value=$config{$key};
  96. if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
  97. push @{$value}, "", ""; # blank items for expansion
  98. }
  99. if ($info{type} eq "string") {
  100. $form->field(
  101. name => $name,
  102. label => $description,
  103. comment => formatexample($info{example}, $value),
  104. type => "text",
  105. value => $value,
  106. size => 60,
  107. fieldset => $section,
  108. );
  109. }
  110. elsif ($info{type} eq "pagespec") {
  111. $form->field(
  112. name => $name,
  113. label => $description,
  114. comment => formatexample($info{example}, $value),
  115. type => "text",
  116. value => $value,
  117. size => 60,
  118. validate => \&IkiWiki::pagespec_valid,
  119. fieldset => $section,
  120. );
  121. }
  122. elsif ($info{type} eq "integer") {
  123. $form->field(
  124. name => $name,
  125. label => $description,
  126. comment => formatexample($info{example}, $value),
  127. type => "text",
  128. value => $value,
  129. size => 5,
  130. validate => '/^[0-9]+$/',
  131. fieldset => $section,
  132. );
  133. }
  134. elsif ($info{type} eq "boolean") {
  135. $form->field(
  136. name => $name,
  137. label => "",
  138. type => "checkbox",
  139. value => $value,
  140. options => [ [ 1 => $description ] ],
  141. fieldset => $section,
  142. );
  143. }
  144. if (! $info{safe}) {
  145. $form->field(name => $name, disabled => 1);
  146. }
  147. else {
  148. $shownfields{$name}=[$key, \%info];
  149. }
  150. }
  151. if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
  152. my $name="enable.$plugin";
  153. $section="plugins" unless %shownfields;
  154. $form->field(
  155. name => $name,
  156. label => "",
  157. type => "checkbox",
  158. options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
  159. value => $enabled,
  160. fieldset => $section,
  161. );
  162. if ($plugin_forced) {
  163. $form->field(name => $name, disabled => 1);
  164. }
  165. else {
  166. $shownfields{$name}=[$name, \%plugininfo];
  167. }
  168. }
  169. return %shownfields;
  170. } #}}}
  171. sub showform ($$) { #{{{
  172. my $cgi=shift;
  173. my $session=shift;
  174. if (! defined $session->param("name") ||
  175. ! IkiWiki::is_admin($session->param("name"))) {
  176. error(gettext("you are not logged in as an admin"));
  177. }
  178. eval q{use CGI::FormBuilder};
  179. error($@) if $@;
  180. my $form = CGI::FormBuilder->new(
  181. title => "setup",
  182. name => "setup",
  183. header => 0,
  184. charset => "utf-8",
  185. method => 'POST',
  186. javascript => 0,
  187. reset => 1,
  188. params => $cgi,
  189. fieldsets => [
  190. [main => gettext("main")],
  191. [plugins => gettext("plugins")]
  192. ],
  193. action => $config{cgiurl},
  194. template => {type => 'div'},
  195. stylesheet => IkiWiki::baseurl()."style.css",
  196. );
  197. if ($form->submitted eq 'Basic') {
  198. $form->field(name => "showadvanced", type => "hidden",
  199. value => 0, force => 1);
  200. }
  201. elsif ($form->submitted eq 'Advanced') {
  202. $form->field(name => "showadvanced", type => "hidden",
  203. value => 1, force => 1);
  204. }
  205. my $advancedtoggle;
  206. if ($form->field("showadvanced")) {
  207. $config{websetup_advanced}=1;
  208. $advancedtoggle="Basic";
  209. }
  210. else {
  211. $config{websetup_advanced}=0;
  212. $advancedtoggle="Advanced";
  213. }
  214. my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
  215. IkiWiki::decode_form_utf8($form);
  216. IkiWiki::run_hooks(formbuilder_setup => sub {
  217. shift->(form => $form, cgi => $cgi, session => $session,
  218. buttons => $buttons);
  219. });
  220. IkiWiki::decode_form_utf8($form);
  221. $form->field(name => "do", type => "hidden", value => "setup",
  222. force => 1);
  223. my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
  224. # record all currently enabled plugins before all are loaded
  225. my %enabled_plugins=%IkiWiki::loaded_plugins;
  226. # per-plugin setup
  227. require IkiWiki::Setup;
  228. my %plugins=map { $_ => 1 } IkiWiki::listplugins();
  229. foreach my $pair (IkiWiki::Setup::getsetup()) {
  230. my $plugin=$pair->[0];
  231. my $setup=$pair->[1];
  232. my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
  233. if (%shown) {
  234. delete $plugins{$plugin};
  235. $fields{$_}=$shown{$_} foreach keys %shown;
  236. }
  237. }
  238. if ($form->submitted eq "Cancel") {
  239. IkiWiki::redirect($cgi, $config{url});
  240. return;
  241. }
  242. elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
  243. my %rebuild;
  244. foreach my $field (keys %fields) {
  245. if ($field=~/^enable\./) {
  246. # rebuild is overkill for many plugins,
  247. # but no good way to tell which
  248. $rebuild{$field}=1; # TODO only if state changed tho
  249. # TODO plugin enable/disable
  250. next;
  251. }
  252. my %info=%{$fields{$field}->[1]};
  253. my $key=$fields{$field}->[0];
  254. my @value=$form->field($field);
  255. if (! $info{safe}) {
  256. error("unsafe field $key"); # should never happen
  257. }
  258. next unless @value;
  259. # Avoid setting fields to empty strings,
  260. # if they were not set before.
  261. next if ! defined $config{$key} && ! grep { length $_ } @value;
  262. if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
  263. if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
  264. $rebuild{$field}=1;
  265. }
  266. $config{$key}=\@value;
  267. }
  268. elsif (ref $config{$key} || ref $info{example}) {
  269. error("complex field $key"); # should never happen
  270. }
  271. else {
  272. if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
  273. $rebuild{$field}=1;
  274. }
  275. $config{$key}=$value[0];
  276. }
  277. }
  278. if (%rebuild && $form->submitted eq 'Save Setup') {
  279. $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
  280. foreach my $field ($form->field) {
  281. next if $rebuild{$field};
  282. $form->field(name => $field, type => "hidden",
  283. force => 1);
  284. }
  285. $form->reset(0); # doesn't really make sense here
  286. $buttons=["Rebuild Wiki", "Cancel"];
  287. }
  288. else {
  289. # TODO save to real path
  290. IkiWiki::Setup::dump("/tmp/s");
  291. $form->text(gettext("Setup saved."));
  292. if (%rebuild) {
  293. # TODO rebuild
  294. }
  295. }
  296. }
  297. IkiWiki::showform($form, $buttons, $session, $cgi);
  298. } #}}}
  299. sub sessioncgi ($$) { #{{{
  300. my $cgi=shift;
  301. my $session=shift;
  302. if ($cgi->param("do") eq "setup") {
  303. showform($cgi, $session);
  304. exit;
  305. }
  306. } #}}}
  307. sub formbuilder_setup (@) { #{{{
  308. my %params=@_;
  309. my $form=$params{form};
  310. if ($form->title eq "preferences") {
  311. push @{$params{buttons}}, "Wiki Setup";
  312. if ($form->submitted && $form->submitted eq "Wiki Setup") {
  313. showform($params{cgi}, $params{session});
  314. exit;
  315. }
  316. }
  317. } #}}}
  318. 1