summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/websetup.pm
blob: 9cb5eb13c13740ed6e609679c49b0468458ec115 (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. section => "web",
  19. },
  20. websetup_force_plugins => {
  21. type => "string",
  22. example => [],
  23. description => "list of plugins that cannot be enabled/disabled via the web interface",
  24. safe => 0,
  25. rebuild => 0,
  26. },
  27. websetup_unsafe => {
  28. type => "string",
  29. example => [],
  30. description => "list of additional setup field keys to treat as unsafe",
  31. safe => 0,
  32. rebuild => 0,
  33. },
  34. websetup_show_unsafe => {
  35. type => "boolean",
  36. example => 1,
  37. description => "show unsafe settings, read-only, in web interface?",
  38. safe => 0,
  39. rebuild => 0,
  40. },
  41. }
  42. sub checkconfig () {
  43. if (! exists $config{websetup_show_unsafe}) {
  44. $config{websetup_show_unsafe}=1;
  45. }
  46. }
  47. sub formatexample ($$) {
  48. my $example=shift;
  49. my $value=shift;
  50. if (defined $value && length $value) {
  51. return "";
  52. }
  53. elsif (defined $example && ! ref $example && length $example) {
  54. return "<br/ ><small>Example: <tt>$example</tt></small>";
  55. }
  56. else {
  57. return "";
  58. }
  59. }
  60. sub issafe ($) {
  61. my $key=shift;
  62. return ! grep { $_ eq $key } @{$config{websetup_unsafe}};
  63. }
  64. sub showfields ($$$@) {
  65. my $form=shift;
  66. my $plugin=shift;
  67. my $enabled=shift;
  68. my @show;
  69. my %plugininfo;
  70. while (@_) {
  71. my $key=shift;
  72. my %info=%{shift()};
  73. if ($key eq 'plugin') {
  74. %plugininfo=%info;
  75. next;
  76. }
  77. # skip internal settings
  78. next if defined $info{type} && $info{type} eq "internal";
  79. # XXX hashes not handled yet
  80. next if ref $config{$key} && ref $config{$key} eq 'HASH' || ref $info{example} eq 'HASH';
  81. # maybe skip unsafe settings
  82. next if ! ($config{websetup_show_unsafe} && $config{websetup_advanced}) &&
  83. (! $info{safe} || ! issafe($key));
  84. # maybe skip advanced settings
  85. next if $info{advanced} && ! $config{websetup_advanced};
  86. # these are handled specially, so don't show
  87. next if $key eq 'add_plugins' || $key eq 'disable_plugins';
  88. push @show, $key, \%info;
  89. }
  90. my $section=defined $plugin
  91. ? sprintf(gettext("%s plugin:"), $plugininfo{section})." ".$plugin
  92. : "main";
  93. my %enabledfields;
  94. my $shownfields=0;
  95. my $plugin_forced=defined $plugin && (! $plugininfo{safe} ||
  96. (exists $config{websetup_force_plugins} && grep { $_ eq $plugin } @{$config{websetup_force_plugins}}));
  97. if ($plugin_forced && ! $enabled) {
  98. # plugin is forced disabled, so skip its settings
  99. @show=();
  100. }
  101. my $section_fieldset;
  102. if (defined $plugin) {
  103. # Define the combined fieldset for the plugin's section.
  104. # This ensures that this fieldset comes first.
  105. $section_fieldset=sprintf(gettext("%s plugins"), $plugininfo{section});
  106. $form->field(name => "placeholder.$plugininfo{section}",
  107. type => "hidden",
  108. fieldset => $section_fieldset);
  109. }
  110. # show plugin toggle
  111. if (defined $plugin && (! $plugin_forced || $config{websetup_advanced})) {
  112. my $name="enable.$plugin";
  113. $form->field(
  114. name => $name,
  115. label => "",
  116. type => "checkbox",
  117. fieldset => $section,
  118. options => [ [ 1 => sprintf(gettext("enable %s?"), $plugin) ]]
  119. );
  120. if (! $form->submitted) {
  121. $form->field(name => $name, value => $enabled);
  122. }
  123. if ($plugin_forced) {
  124. $form->field(name => $name, disabled => 1);
  125. }
  126. else {
  127. $enabledfields{$name}=[$name, \%plugininfo];
  128. }
  129. }
  130. # show plugin settings
  131. while (@show) {
  132. my $key=shift @show;
  133. my %info=%{shift @show};
  134. my $description=$info{description};
  135. if (exists $info{link} && length $info{link}) {
  136. if ($info{link} =~ /^\w+:\/\//) {
  137. $description="<a href=\"$info{link}\">$description</a>";
  138. }
  139. else {
  140. $description=htmllink("", "", $info{link}, noimageinline => 1, linktext => $description);
  141. }
  142. }
  143. # multiple plugins can have the same field
  144. my $name=defined $plugin ? $plugin.".".$key : $section.".".$key;
  145. my $value=$config{$key};
  146. if (! defined $value) {
  147. $value="";
  148. }
  149. if (ref $value eq 'ARRAY' || ref $info{example} eq 'ARRAY') {
  150. $value=[(ref $value eq 'ARRAY' ? map { Encode::encode_utf8($_) } @{$value} : "")];
  151. push @$value, "", "" if $info{safe} && issafe($key); # blank items for expansion
  152. }
  153. else {
  154. $value=Encode::encode_utf8($value);
  155. }
  156. if ($info{type} eq "string") {
  157. $form->field(
  158. name => $name,
  159. label => $description,
  160. comment => formatexample($info{example}, $value),
  161. type => "text",
  162. value => $value,
  163. size => 60,
  164. fieldset => $section,
  165. );
  166. }
  167. elsif ($info{type} eq "pagespec") {
  168. $form->field(
  169. name => $name,
  170. label => $description,
  171. comment => formatexample($info{example}, $value),
  172. type => "text",
  173. value => $value,
  174. size => 60,
  175. validate => \&IkiWiki::pagespec_valid,
  176. fieldset => $section,
  177. );
  178. }
  179. elsif ($info{type} eq "integer") {
  180. $form->field(
  181. name => $name,
  182. label => $description,
  183. comment => formatexample($info{example}, $value),
  184. type => "text",
  185. value => $value,
  186. size => 5,
  187. validate => '/^[0-9]+$/',
  188. fieldset => $section,
  189. );
  190. }
  191. elsif ($info{type} eq "boolean") {
  192. $form->field(
  193. name => $name,
  194. label => "",
  195. type => "checkbox",
  196. options => [ [ 1 => $description ] ],
  197. fieldset => $section,
  198. );
  199. if (! $form->submitted) {
  200. $form->field(name => $name, value => $value);
  201. }
  202. }
  203. if (! $info{safe} || ! issafe($key)) {
  204. $form->field(name => $name, disabled => 1);
  205. }
  206. else {
  207. $enabledfields{$name}=[$key, \%info];
  208. }
  209. $shownfields++;
  210. }
  211. # if no fields were shown for the plugin, drop it into a combined
  212. # fieldset for its section
  213. if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
  214. ! $shownfields) {
  215. $form->field(name => "enable.$plugin", fieldset => $section_fieldset);
  216. }
  217. return %enabledfields;
  218. }
  219. sub enable_plugin ($) {
  220. my $plugin=shift;
  221. $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
  222. push @{$config{add_plugins}}, $plugin;
  223. }
  224. sub disable_plugin ($) {
  225. my $plugin=shift;
  226. if (grep { $_ eq $plugin } @{$config{add_plugins}}) {
  227. $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
  228. }
  229. else {
  230. push @{$config{disable_plugins}}, $plugin;
  231. }
  232. }
  233. sub showform ($$) {
  234. my $cgi=shift;
  235. my $session=shift;
  236. IkiWiki::needsignin($cgi, $session);
  237. if (! defined $session->param("name") ||
  238. ! IkiWiki::is_admin($session->param("name"))) {
  239. error(gettext("you are not logged in as an admin"));
  240. }
  241. if (! exists $config{setupfile}) {
  242. error(gettext("setup file for this wiki is not known"));
  243. }
  244. eval q{use CGI::FormBuilder};
  245. error($@) if $@;
  246. my $form = CGI::FormBuilder->new(
  247. title => "setup",
  248. name => "setup",
  249. header => 0,
  250. charset => "utf-8",
  251. method => 'POST',
  252. javascript => 0,
  253. reset => 1,
  254. params => $cgi,
  255. fieldsets => [
  256. [main => gettext("main")],
  257. ],
  258. action => $config{cgiurl},
  259. template => {type => 'div'},
  260. stylesheet => 1,
  261. );
  262. $form->field(name => "do", type => "hidden", value => "setup",
  263. force => 1);
  264. $form->field(name => "rebuild_asked", type => "hidden");
  265. if ($form->submitted eq 'Basic Mode') {
  266. $form->field(name => "showadvanced", type => "hidden",
  267. value => 0, force => 1);
  268. }
  269. elsif ($form->submitted eq 'Advanced Mode') {
  270. $form->field(name => "showadvanced", type => "hidden",
  271. value => 1, force => 1);
  272. }
  273. my $advancedtoggle;
  274. if ($form->field("showadvanced")) {
  275. $config{websetup_advanced}=1;
  276. $advancedtoggle="Basic Mode";
  277. }
  278. else {
  279. $config{websetup_advanced}=0;
  280. $advancedtoggle="Advanced Mode";
  281. }
  282. my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
  283. IkiWiki::decode_form_utf8($form);
  284. IkiWiki::run_hooks(formbuilder_setup => sub {
  285. shift->(form => $form, cgi => $cgi, session => $session,
  286. buttons => $buttons);
  287. });
  288. my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
  289. # record all currently enabled plugins before all are loaded
  290. my %enabled_plugins=%IkiWiki::loaded_plugins;
  291. # per-plugin setup
  292. require IkiWiki::Setup;
  293. foreach my $pair (IkiWiki::Setup::getsetup()) {
  294. my $plugin=$pair->[0];
  295. my $setup=$pair->[1];
  296. my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
  297. if (%shown) {
  298. $fields{$_}=$shown{$_} foreach keys %shown;
  299. }
  300. }
  301. IkiWiki::decode_form_utf8($form);
  302. if ($form->submitted eq "Cancel") {
  303. IkiWiki::redirect($cgi, $config{url});
  304. return;
  305. }
  306. elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
  307. # Push values from form into %config, avoiding unnecessary
  308. # changes, and keeping track of which changes need a
  309. # rebuild.
  310. my %rebuild;
  311. foreach my $field (keys %fields) {
  312. my %info=%{$fields{$field}->[1]};
  313. my $key=$fields{$field}->[0];
  314. my @value=$form->field($field);
  315. if (! @value) {
  316. @value=0;
  317. }
  318. if (! $info{safe} || ! issafe($key)) {
  319. error("unsafe field $key"); # should never happen
  320. }
  321. if (exists $info{rebuild} &&
  322. ($info{rebuild} || ! defined $info{rebuild})) {
  323. $rebuild{$field}=$info{rebuild};
  324. }
  325. if ($field=~/^enable\.(.*)/) {
  326. my $plugin=$1;
  327. $value[0]=0 if ! length $value[0];
  328. if ($value[0] != exists $enabled_plugins{$plugin}) {
  329. if ($value[0]) {
  330. enable_plugin($plugin);
  331. }
  332. else {
  333. disable_plugin($plugin);
  334. }
  335. }
  336. else {
  337. delete $rebuild{$field};
  338. }
  339. next;
  340. }
  341. if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
  342. @value=sort grep { length $_ } @value;
  343. my @oldvalue=sort grep { length $_ }
  344. (defined $config{$key} ? @{$config{$key}} : ());
  345. my $same=(@oldvalue) == (@value);
  346. for (my $x=0; $same && $x < @value; $x++) {
  347. $same=0 if $value[$x] ne $oldvalue[$x];
  348. }
  349. if ($same) {
  350. delete $rebuild{$field};
  351. }
  352. else {
  353. $config{$key}=\@value;
  354. }
  355. }
  356. elsif (ref $config{$key} || ref $info{example}) {
  357. error("complex field $key"); # should never happen
  358. }
  359. else {
  360. if (defined $config{$key} && $config{$key} eq $value[0]) {
  361. delete $rebuild{$field};
  362. }
  363. elsif (! defined $config{$key} && ! length $value[0]) {
  364. delete $rebuild{$field};
  365. }
  366. elsif ((! defined $config{$key} || ! $config{$key}) &&
  367. ! $value[0] && $info{type} eq "boolean") {
  368. delete $rebuild{$field};
  369. }
  370. else {
  371. $config{$key}=$value[0];
  372. }
  373. }
  374. }
  375. if (%rebuild && ! $form->field("rebuild_asked")) {
  376. my $required=0;
  377. foreach my $field ($form->field) {
  378. $required=1 if $rebuild{$field};
  379. next if exists $rebuild{$field};
  380. $form->field(name => $field, type => "hidden");
  381. }
  382. if ($required) {
  383. $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
  384. $buttons=["Rebuild Wiki", "Cancel"];
  385. }
  386. else {
  387. $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
  388. $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
  389. }
  390. $form->field(name => "rebuild_asked", value => 1, force => 1);
  391. $form->reset(0); # doesn't really make sense here
  392. }
  393. else {
  394. my $oldsetup=readfile($config{setupfile});
  395. IkiWiki::Setup::dump($config{setupfile});
  396. IkiWiki::saveindex();
  397. IkiWiki::unlockwiki();
  398. # Print the top part of a standard misctemplate,
  399. # then show the rebuild or refresh.
  400. my $divider="xxx";
  401. my $html=IkiWiki::misctemplate("setup", $divider);
  402. IkiWiki::printheader($session);
  403. my ($head, $tail)=split($divider, $html, 2);
  404. print $head."<pre>\n";
  405. my @command;
  406. if ($form->submitted eq 'Rebuild Wiki') {
  407. @command=("ikiwiki", "-setup", $config{setupfile},
  408. "-rebuild", "-v");
  409. }
  410. else {
  411. @command=("ikiwiki", "-setup", $config{setupfile},
  412. "-refresh", "-wrappers", "-v");
  413. }
  414. close STDERR;
  415. open(STDERR, ">&STDOUT");
  416. my $ret=system(@command);
  417. print "\n<\/pre>";
  418. if ($ret != 0) {
  419. print '<p class="error">'.
  420. sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
  421. join(" ", @command), $ret).
  422. '</p>';
  423. open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
  424. print OUT $oldsetup;
  425. close OUT;
  426. }
  427. print $tail;
  428. exit 0;
  429. }
  430. }
  431. IkiWiki::showform($form, $buttons, $session, $cgi);
  432. }
  433. sub sessioncgi ($$) {
  434. my $cgi=shift;
  435. my $session=shift;
  436. if ($cgi->param("do") eq "setup") {
  437. showform($cgi, $session);
  438. exit;
  439. }
  440. }
  441. sub formbuilder_setup (@) {
  442. my %params=@_;
  443. my $form=$params{form};
  444. if ($form->title eq "preferences" &&
  445. IkiWiki::is_admin($params{session}->param("name"))) {
  446. push @{$params{buttons}}, "Setup";
  447. if ($form->submitted && $form->submitted eq "Setup") {
  448. showform($params{cgi}, $params{session});
  449. exit;
  450. }
  451. }
  452. }
  453. 1