summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/websetup.pm
blob: 95d044c08a5d46ada18308d6448efebede1732af (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::websetup;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.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. fieldset => $section,
  94. options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ]]
  95. );
  96. if (! $form->submitted) {
  97. $form->field(name => $name, value => $enabled);
  98. }
  99. if ($plugin_forced) {
  100. $form->field(name => $name, disabled => 1);
  101. }
  102. else {
  103. $enabledfields{$name}=[$name, \%plugininfo];
  104. }
  105. }
  106. # show plugin settings
  107. while (@show) {
  108. my $key=shift @show;
  109. my %info=%{shift @show};
  110. my $description=$info{description};
  111. if (exists $info{link} && length $info{link}) {
  112. if ($info{link} =~ /^\w+:\/\//) {
  113. $description="<a href=\"$info{link}\">$description</a>";
  114. }
  115. else {
  116. $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
  117. }
  118. }
  119. # multiple plugins can have the same field
  120. my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;
  121. my $value=$config{$key};
  122. if ($info{safe} && (ref $value eq 'ARRAY' || ref $info{example} eq 'ARRAY')) {
  123. $value=[(ref $value eq 'ARRAY' ? @{$value} : ""), "", ""]; # blank items for expansion
  124. }
  125. if ($info{type} eq "string") {
  126. $form->field(
  127. name => $name,
  128. label => $description,
  129. comment => formatexample($info{example}, $value),
  130. type => "text",
  131. value => $value,
  132. size => 60,
  133. fieldset => $section,
  134. );
  135. }
  136. elsif ($info{type} eq "pagespec") {
  137. $form->field(
  138. name => $name,
  139. label => $description,
  140. comment => formatexample($info{example}, $value),
  141. type => "text",
  142. value => $value,
  143. size => 60,
  144. validate => \&IkiWiki::pagespec_valid,
  145. fieldset => $section,
  146. );
  147. }
  148. elsif ($info{type} eq "integer") {
  149. $form->field(
  150. name => $name,
  151. label => $description,
  152. comment => formatexample($info{example}, $value),
  153. type => "text",
  154. value => $value,
  155. size => 5,
  156. validate => '/^[0-9]+$/',
  157. fieldset => $section,
  158. );
  159. }
  160. elsif ($info{type} eq "boolean") {
  161. $form->field(
  162. name => $name,
  163. label => "",
  164. type => "checkbox",
  165. options => [ [ 1 => $description ] ],
  166. fieldset => $section,
  167. );
  168. if (! $form->submitted) {
  169. $form->field(name => $name, value => $value);
  170. }
  171. }
  172. if (! $info{safe}) {
  173. $form->field(name => $name, disabled => 1);
  174. }
  175. else {
  176. $enabledfields{$name}=[$key, \%info];
  177. }
  178. $shownfields++;
  179. }
  180. # if no fields were shown for the plugin, drop it into the
  181. # plugins fieldset
  182. if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
  183. ! $shownfields) {
  184. $form->field(name => "enable.$plugin", fieldset => "plugins");
  185. }
  186. return %enabledfields;
  187. }
  188. sub enable_plugin ($) {
  189. my $plugin=shift;
  190. $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
  191. push @{$config{add_plugins}}, $plugin;
  192. }
  193. sub disable_plugin ($) {
  194. my $plugin=shift;
  195. if (grep { $_ eq $plugin } @{$config{add_plugins}}) {
  196. $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
  197. }
  198. else {
  199. push @{$config{disable_plugins}}, $plugin;
  200. }
  201. }
  202. sub showform ($$) {
  203. my $cgi=shift;
  204. my $session=shift;
  205. if (! defined $session->param("name") ||
  206. ! IkiWiki::is_admin($session->param("name"))) {
  207. error(gettext("you are not logged in as an admin"));
  208. }
  209. if (! exists $config{setupfile}) {
  210. error(gettext("setup file for this wiki is not known"));
  211. }
  212. eval q{use CGI::FormBuilder};
  213. error($@) if $@;
  214. my $form = CGI::FormBuilder->new(
  215. title => "setup",
  216. name => "setup",
  217. header => 0,
  218. charset => "utf-8",
  219. method => 'POST',
  220. javascript => 0,
  221. reset => 1,
  222. params => $cgi,
  223. fieldsets => [
  224. [main => gettext("main")],
  225. [plugins => gettext("plugins")]
  226. ],
  227. action => $config{cgiurl},
  228. template => {type => 'div'},
  229. stylesheet => IkiWiki::baseurl()."style.css",
  230. );
  231. $form->field(name => "do", type => "hidden", value => "setup",
  232. force => 1);
  233. $form->field(name => "rebuild_asked", type => "hidden");
  234. if ($form->submitted eq 'Basic Mode') {
  235. $form->field(name => "showadvanced", type => "hidden",
  236. value => 0, force => 1);
  237. }
  238. elsif ($form->submitted eq 'Advanced Mode') {
  239. $form->field(name => "showadvanced", type => "hidden",
  240. value => 1, force => 1);
  241. }
  242. my $advancedtoggle;
  243. if ($form->field("showadvanced")) {
  244. $config{websetup_advanced}=1;
  245. $advancedtoggle="Basic Mode";
  246. }
  247. else {
  248. $config{websetup_advanced}=0;
  249. $advancedtoggle="Advanced Mode";
  250. }
  251. my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
  252. IkiWiki::decode_form_utf8($form);
  253. IkiWiki::run_hooks(formbuilder_setup => sub {
  254. shift->(form => $form, cgi => $cgi, session => $session,
  255. buttons => $buttons);
  256. });
  257. IkiWiki::decode_form_utf8($form);
  258. my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
  259. # record all currently enabled plugins before all are loaded
  260. my %enabled_plugins=%IkiWiki::loaded_plugins;
  261. # per-plugin setup
  262. require IkiWiki::Setup;
  263. foreach my $pair (IkiWiki::Setup::getsetup()) {
  264. my $plugin=$pair->[0];
  265. my $setup=$pair->[1];
  266. my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
  267. if (%shown) {
  268. $fields{$_}=$shown{$_} foreach keys %shown;
  269. }
  270. }
  271. if ($form->submitted eq "Cancel") {
  272. IkiWiki::redirect($cgi, $config{url});
  273. return;
  274. }
  275. elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
  276. # Push values from form into %config, avoiding unnecessary
  277. # changes, and keeping track of which changes need a
  278. # rebuild.
  279. my %rebuild;
  280. foreach my $field (keys %fields) {
  281. my %info=%{$fields{$field}->[1]};
  282. my $key=$fields{$field}->[0];
  283. my @value=$form->field($field);
  284. if (! @value) {
  285. @value=0;
  286. }
  287. if (! $info{safe}) {
  288. error("unsafe field $key"); # should never happen
  289. }
  290. if (exists $info{rebuild} &&
  291. ($info{rebuild} || ! defined $info{rebuild})) {
  292. $rebuild{$field}=$info{rebuild};
  293. }
  294. if ($field=~/^enable\.(.*)/) {
  295. my $plugin=$1;
  296. $value[0]=0 if ! length $value[0];
  297. if ($value[0] != exists $enabled_plugins{$plugin}) {
  298. if ($value[0]) {
  299. enable_plugin($plugin);
  300. }
  301. else {
  302. disable_plugin($plugin);
  303. }
  304. }
  305. else {
  306. delete $rebuild{$field};
  307. }
  308. next;
  309. }
  310. if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
  311. @value=sort grep { length $_ } @value;
  312. my @oldvalue=sort grep { length $_ }
  313. (defined $config{$key} ? @{$config{$key}} : ());
  314. if ((@oldvalue) == (@value)) {
  315. delete $rebuild{$field};
  316. }
  317. else {
  318. $config{$key}=\@value;
  319. }
  320. }
  321. elsif (ref $config{$key} || ref $info{example}) {
  322. error("complex field $key"); # should never happen
  323. }
  324. else {
  325. if (defined $config{$key} && $config{$key} eq $value[0]) {
  326. delete $rebuild{$field};
  327. }
  328. elsif (! defined $config{$key} && ! length $value[0]) {
  329. delete $rebuild{$field};
  330. }
  331. elsif ((! defined $config{$key} || ! $config{$key}) &&
  332. ! $value[0] && $info{type} eq "boolean") {
  333. delete $rebuild{$field};
  334. }
  335. else {
  336. $config{$key}=$value[0];
  337. }
  338. }
  339. }
  340. if (%rebuild && ! $form->field("rebuild_asked")) {
  341. my $required=0;
  342. foreach my $field ($form->field) {
  343. $required=1 if $rebuild{$field};
  344. next if exists $rebuild{$field};
  345. $form->field(name => $field, type => "hidden");
  346. }
  347. if ($required) {
  348. $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
  349. $buttons=["Rebuild Wiki", "Cancel"];
  350. }
  351. else {
  352. $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
  353. $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
  354. }
  355. $form->field(name => "rebuild_asked", value => 1, force => 1);
  356. $form->reset(0); # doesn't really make sense here
  357. }
  358. else {
  359. IkiWiki::Setup::dump($config{setupfile});
  360. IkiWiki::saveindex();
  361. IkiWiki::unlockwiki();
  362. # Print the top part of a standard misctemplate,
  363. # then show the rebuild or refresh.
  364. my $divider="xxx";
  365. my $html=IkiWiki::misctemplate("setup", $divider);
  366. IkiWiki::printheader($session);
  367. my ($head, $tail)=split($divider, $html, 2);
  368. print $head."<pre>\n";
  369. my @command;
  370. if ($form->submitted eq 'Rebuild Wiki') {
  371. @command=("ikiwiki", "-setup", $config{setupfile},
  372. "-rebuild", "-v");
  373. }
  374. else {
  375. @command=("ikiwiki", "-setup", $config{setupfile},
  376. "-refresh", "-wrappers", "-v");
  377. }
  378. my $ret=system(@command);
  379. print "\n<pre>";
  380. if ($ret != 0) {
  381. print '<p class="error">'.
  382. sprintf(gettext("<p class=\"error\">Error: %s exited nonzero (%s)"),
  383. join(" ", @command), $ret).
  384. '</p>';
  385. }
  386. print $tail;
  387. exit 0;
  388. }
  389. }
  390. IkiWiki::showform($form, $buttons, $session, $cgi);
  391. }
  392. sub sessioncgi ($$) {
  393. my $cgi=shift;
  394. my $session=shift;
  395. if ($cgi->param("do") eq "setup") {
  396. showform($cgi, $session);
  397. exit;
  398. }
  399. }
  400. sub formbuilder_setup (@) {
  401. my %params=@_;
  402. my $form=$params{form};
  403. if ($form->title eq "preferences") {
  404. push @{$params{buttons}}, "Wiki Setup";
  405. if ($form->submitted && $form->submitted eq "Wiki Setup") {
  406. showform($params{cgi}, $params{session});
  407. exit;
  408. }
  409. }
  410. }
  411. 1