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