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