summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/websetup.pm
blob: dec360d68d21f903049e7f45765f0f9338c714bc (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 $section=defined $plugin ? $plugin." ".gettext("plugin") : "main";
  74. my %enabledfields;
  75. my $shownfields=0;
  76. my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
  77. (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
  78. if ($plugin_forced && ! $enabled) {
  79. # plugin is forced disabled, so skip its configuration
  80. @show=();
  81. }
  82. # show plugin toggle
  83. if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
  84. my $name="enable.$plugin";
  85. $form->field(
  86. name => $name,
  87. label => "",
  88. type => "checkbox",
  89. options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ] ],
  90. value => $enabled,
  91. fieldset => $section,
  92. );
  93. if ($plugin_forced) {
  94. $form->field(name => $name, disabled => 1);
  95. }
  96. else {
  97. $enabledfields{$name}=[$name, \%plugininfo];
  98. }
  99. }
  100. while (@show) {
  101. my $key=shift @show;
  102. my %info=%{shift @show};
  103. my $description=$info{description};
  104. if (exists $info{link} && length $info{link}) {
  105. if ($info{link} =~ /^\w+:\/\//) {
  106. $description="<a href=\"$info{link}\">$description</a>";
  107. }
  108. else {
  109. $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
  110. }
  111. }
  112. # multiple plugins can have the same field
  113. my $name=defined $plugin ? $plugin.".".$key : $key;
  114. my $value=$config{$key};
  115. if ($info{safe} && (ref $config{$key} eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
  116. push @{$value}, "", ""; # blank items for expansion
  117. }
  118. if ($info{type} eq "string") {
  119. $form->field(
  120. name => $name,
  121. label => $description,
  122. comment => formatexample($info{example}, $value),
  123. type => "text",
  124. value => $value,
  125. size => 60,
  126. fieldset => $section,
  127. );
  128. }
  129. elsif ($info{type} eq "pagespec") {
  130. $form->field(
  131. name => $name,
  132. label => $description,
  133. comment => formatexample($info{example}, $value),
  134. type => "text",
  135. value => $value,
  136. size => 60,
  137. validate => \&IkiWiki::pagespec_valid,
  138. fieldset => $section,
  139. );
  140. }
  141. elsif ($info{type} eq "integer") {
  142. $form->field(
  143. name => $name,
  144. label => $description,
  145. comment => formatexample($info{example}, $value),
  146. type => "text",
  147. value => $value,
  148. size => 5,
  149. validate => '/^[0-9]+$/',
  150. fieldset => $section,
  151. );
  152. }
  153. elsif ($info{type} eq "boolean") {
  154. $form->field(
  155. name => $name,
  156. label => "",
  157. type => "checkbox",
  158. value => $value,
  159. options => [ [ 1 => $description ] ],
  160. fieldset => $section,
  161. );
  162. }
  163. if (! $info{safe}) {
  164. $form->field(name => $name, disabled => 1);
  165. }
  166. else {
  167. $enabledfields{$name}=[$key, \%info];
  168. }
  169. $shownfields++;
  170. }
  171. # if no fields were shown for the plugin, drop it into the
  172. # plugins fieldset
  173. if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
  174. ! $shownfields) {
  175. $form->field(name => "enable.$plugin", fieldset => "plugins");
  176. }
  177. return %enabledfields;
  178. } #}}}
  179. sub showform ($$) { #{{{
  180. my $cgi=shift;
  181. my $session=shift;
  182. if (! defined $session->param("name") ||
  183. ! IkiWiki::is_admin($session->param("name"))) {
  184. error(gettext("you are not logged in as an admin"));
  185. }
  186. eval q{use CGI::FormBuilder};
  187. error($@) if $@;
  188. my $form = CGI::FormBuilder->new(
  189. title => "setup",
  190. name => "setup",
  191. header => 0,
  192. charset => "utf-8",
  193. method => 'POST',
  194. javascript => 0,
  195. reset => 1,
  196. params => $cgi,
  197. fieldsets => [
  198. [main => gettext("main")],
  199. [plugins => gettext("plugins")]
  200. ],
  201. action => $config{cgiurl},
  202. template => {type => 'div'},
  203. stylesheet => IkiWiki::baseurl()."style.css",
  204. );
  205. if ($form->submitted eq 'Basic Mode') {
  206. $form->field(name => "showadvanced", type => "hidden",
  207. value => 0, force => 1);
  208. }
  209. elsif ($form->submitted eq 'Advanced Mode') {
  210. $form->field(name => "showadvanced", type => "hidden",
  211. value => 1, force => 1);
  212. }
  213. my $advancedtoggle;
  214. if ($form->field("showadvanced")) {
  215. $config{websetup_advanced}=1;
  216. $advancedtoggle="Basic Mode";
  217. }
  218. else {
  219. $config{websetup_advanced}=0;
  220. $advancedtoggle="Advanced Mode";
  221. }
  222. my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
  223. IkiWiki::decode_form_utf8($form);
  224. IkiWiki::run_hooks(formbuilder_setup => sub {
  225. shift->(form => $form, cgi => $cgi, session => $session,
  226. buttons => $buttons);
  227. });
  228. IkiWiki::decode_form_utf8($form);
  229. $form->field(name => "do", type => "hidden", value => "setup",
  230. force => 1);
  231. my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
  232. # record all currently enabled plugins before all are loaded
  233. my %enabled_plugins=%IkiWiki::loaded_plugins;
  234. # per-plugin setup
  235. require IkiWiki::Setup;
  236. my %plugins=map { $_ => 1 } IkiWiki::listplugins();
  237. foreach my $pair (IkiWiki::Setup::getsetup()) {
  238. my $plugin=$pair->[0];
  239. my $setup=$pair->[1];
  240. my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
  241. if (%shown) {
  242. delete $plugins{$plugin};
  243. $fields{$_}=$shown{$_} foreach keys %shown;
  244. }
  245. }
  246. if ($form->submitted eq "Cancel") {
  247. IkiWiki::redirect($cgi, $config{url});
  248. return;
  249. }
  250. elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
  251. my %rebuild;
  252. foreach my $field (keys %fields) {
  253. if ($field=~/^enable\./) {
  254. # rebuild is overkill for many plugins,
  255. # but no good way to tell which
  256. $rebuild{$field}=1; # TODO only if state changed tho
  257. # TODO plugin enable/disable
  258. next;
  259. }
  260. my %info=%{$fields{$field}->[1]};
  261. my $key=$fields{$field}->[0];
  262. my @value=$form->field($field);
  263. if (! $info{safe}) {
  264. error("unsafe field $key"); # should never happen
  265. }
  266. next unless @value;
  267. # Avoid setting fields to empty strings,
  268. # if they were not set before.
  269. next if ! defined $config{$key} && ! grep { length $_ } @value;
  270. if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
  271. if ($info{rebuild} && (! defined $config{$key} || (@{$config{$key}}) != (@value))) {
  272. $rebuild{$field}=1;
  273. }
  274. $config{$key}=\@value;
  275. }
  276. elsif (ref $config{$key} || ref $info{example}) {
  277. error("complex field $key"); # should never happen
  278. }
  279. else {
  280. if ($info{rebuild} && (! defined $config{$key} || $config{$key} ne $value[0])) {
  281. $rebuild{$field}=1;
  282. }
  283. $config{$key}=$value[0];
  284. }
  285. }
  286. if (%rebuild && $form->submitted eq 'Save Setup') {
  287. $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
  288. foreach my $field ($form->field) {
  289. next if $rebuild{$field};
  290. $form->field(name => $field, type => "hidden",
  291. force => 1);
  292. }
  293. $form->reset(0); # doesn't really make sense here
  294. $buttons=["Rebuild Wiki", "Cancel"];
  295. }
  296. else {
  297. # TODO save to real path
  298. IkiWiki::Setup::dump("/tmp/s");
  299. $form->text(gettext("Setup saved."));
  300. if (%rebuild) {
  301. # TODO rebuild
  302. }
  303. }
  304. }
  305. IkiWiki::showform($form, $buttons, $session, $cgi);
  306. } #}}}
  307. sub sessioncgi ($$) { #{{{
  308. my $cgi=shift;
  309. my $session=shift;
  310. if ($cgi->param("do") eq "setup") {
  311. showform($cgi, $session);
  312. exit;
  313. }
  314. } #}}}
  315. sub formbuilder_setup (@) { #{{{
  316. my %params=@_;
  317. my $form=$params{form};
  318. if ($form->title eq "preferences") {
  319. push @{$params{buttons}}, "Wiki Setup";
  320. if ($form->submitted && $form->submitted eq "Wiki Setup") {
  321. showform($params{cgi}, $params{session});
  322. exit;
  323. }
  324. }
  325. } #}}}
  326. 1