summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/websetup.pm
blob: 11b4428e3fc0a55f6637516c56b2dca8a7460163 (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. $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
  227. push @{$config{disable_plugins}}, $plugin;
  228. }
  229. sub showform ($$) {
  230. my $cgi=shift;
  231. my $session=shift;
  232. IkiWiki::needsignin($cgi, $session);
  233. if (! defined $session->param("name") ||
  234. ! IkiWiki::is_admin($session->param("name"))) {
  235. error(gettext("you are not logged in as an admin"));
  236. }
  237. if (! exists $config{setupfile}) {
  238. error(gettext("setup file for this wiki is not known"));
  239. }
  240. eval q{use CGI::FormBuilder};
  241. error($@) if $@;
  242. my $form = CGI::FormBuilder->new(
  243. title => "setup",
  244. name => "setup",
  245. header => 0,
  246. charset => "utf-8",
  247. method => 'POST',
  248. javascript => 0,
  249. reset => 1,
  250. params => $cgi,
  251. fieldsets => [
  252. [main => gettext("main")],
  253. ],
  254. action => $config{cgiurl},
  255. template => {type => 'div'},
  256. stylesheet => 1,
  257. );
  258. $form->field(name => "do", type => "hidden", value => "setup",
  259. force => 1);
  260. $form->field(name => "rebuild_asked", type => "hidden");
  261. if ($form->submitted eq 'Basic Mode') {
  262. $form->field(name => "showadvanced", type => "hidden",
  263. value => 0, force => 1);
  264. }
  265. elsif ($form->submitted eq 'Advanced Mode') {
  266. $form->field(name => "showadvanced", type => "hidden",
  267. value => 1, force => 1);
  268. }
  269. my $advancedtoggle;
  270. if ($form->field("showadvanced")) {
  271. $config{websetup_advanced}=1;
  272. $advancedtoggle="Basic Mode";
  273. }
  274. else {
  275. $config{websetup_advanced}=0;
  276. $advancedtoggle="Advanced Mode";
  277. }
  278. my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
  279. IkiWiki::decode_form_utf8($form);
  280. IkiWiki::run_hooks(formbuilder_setup => sub {
  281. shift->(form => $form, cgi => $cgi, session => $session,
  282. buttons => $buttons);
  283. });
  284. my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
  285. # record all currently enabled plugins before all are loaded
  286. my %enabled_plugins=%IkiWiki::loaded_plugins;
  287. # per-plugin setup
  288. require IkiWiki::Setup;
  289. foreach my $pair (IkiWiki::Setup::getsetup()) {
  290. my $plugin=$pair->[0];
  291. my $setup=$pair->[1];
  292. my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
  293. if (%shown) {
  294. $fields{$_}=$shown{$_} foreach keys %shown;
  295. }
  296. }
  297. IkiWiki::decode_form_utf8($form);
  298. if ($form->submitted eq "Cancel") {
  299. IkiWiki::redirect($cgi, $config{url});
  300. return;
  301. }
  302. elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
  303. # Push values from form into %config, avoiding unnecessary
  304. # changes, and keeping track of which changes need a
  305. # rebuild.
  306. my %rebuild;
  307. foreach my $field (keys %fields) {
  308. my %info=%{$fields{$field}->[1]};
  309. my $key=$fields{$field}->[0];
  310. my @value=$form->field($field);
  311. if (! @value) {
  312. @value=0;
  313. }
  314. if (! $info{safe} || ! issafe($key)) {
  315. error("unsafe field $key"); # should never happen
  316. }
  317. if (exists $info{rebuild} &&
  318. ($info{rebuild} || ! defined $info{rebuild})) {
  319. $rebuild{$field}=$info{rebuild};
  320. }
  321. if ($field=~/^enable\.(.*)/) {
  322. my $plugin=$1;
  323. $value[0]=0 if ! length $value[0];
  324. if ($value[0] != exists $enabled_plugins{$plugin}) {
  325. if ($value[0]) {
  326. enable_plugin($plugin);
  327. }
  328. else {
  329. disable_plugin($plugin);
  330. }
  331. }
  332. else {
  333. delete $rebuild{$field};
  334. }
  335. next;
  336. }
  337. if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
  338. @value=sort grep { length $_ } @value;
  339. my @oldvalue=sort grep { length $_ }
  340. (defined $config{$key} ? @{$config{$key}} : ());
  341. my $same=(@oldvalue) == (@value);
  342. for (my $x=0; $same && $x < @value; $x++) {
  343. $same=0 if $value[$x] ne $oldvalue[$x];
  344. }
  345. if ($same) {
  346. delete $rebuild{$field};
  347. }
  348. else {
  349. $config{$key}=\@value;
  350. }
  351. }
  352. elsif (ref $config{$key} || ref $info{example}) {
  353. error("complex field $key"); # should never happen
  354. }
  355. else {
  356. if (defined $config{$key} && $config{$key} eq $value[0]) {
  357. delete $rebuild{$field};
  358. }
  359. elsif (! defined $config{$key} && ! length $value[0]) {
  360. delete $rebuild{$field};
  361. }
  362. elsif ((! defined $config{$key} || ! $config{$key}) &&
  363. ! $value[0] && $info{type} eq "boolean") {
  364. delete $rebuild{$field};
  365. }
  366. else {
  367. $config{$key}=$value[0];
  368. }
  369. }
  370. }
  371. if (%rebuild && ! $form->field("rebuild_asked")) {
  372. my $required=0;
  373. foreach my $field ($form->field) {
  374. $required=1 if $rebuild{$field};
  375. next if exists $rebuild{$field};
  376. $form->field(name => $field, type => "hidden");
  377. }
  378. if ($required) {
  379. $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
  380. $buttons=["Rebuild Wiki", "Cancel"];
  381. }
  382. else {
  383. $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
  384. $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
  385. }
  386. $form->field(name => "rebuild_asked", value => 1, force => 1);
  387. $form->reset(0); # doesn't really make sense here
  388. }
  389. else {
  390. my $oldsetup=readfile($config{setupfile});
  391. IkiWiki::Setup::dump($config{setupfile});
  392. IkiWiki::saveindex();
  393. IkiWiki::unlockwiki();
  394. # Print the top part of a standard misctemplate,
  395. # then show the rebuild or refresh, live.
  396. my $divider="\0";
  397. my $html=IkiWiki::misctemplate("setup", $divider);
  398. IkiWiki::printheader($session);
  399. my ($head, $tail)=split($divider, $html, 2);
  400. print $head."<pre>\n";
  401. my @command;
  402. if ($form->submitted eq 'Rebuild Wiki') {
  403. @command=("ikiwiki", "-setup", $config{setupfile},
  404. "-rebuild", "-v");
  405. }
  406. else {
  407. @command=("ikiwiki", "-setup", $config{setupfile},
  408. "-refresh", "-wrappers", "-v");
  409. }
  410. close STDERR;
  411. open(STDERR, ">&STDOUT");
  412. my $ret=system(@command);
  413. print "\n<\/pre>";
  414. if ($ret != 0) {
  415. print '<p class="error">'.
  416. sprintf(gettext("Error: %s exited nonzero (%s). Discarding setup changes."),
  417. join(" ", @command), $ret).
  418. '</p>';
  419. open(OUT, ">", $config{setupfile}) || error("$config{setupfile}: $!");
  420. print OUT $oldsetup;
  421. close OUT;
  422. }
  423. print $tail;
  424. exit 0;
  425. }
  426. }
  427. IkiWiki::showform($form, $buttons, $session, $cgi);
  428. }
  429. sub sessioncgi ($$) {
  430. my $cgi=shift;
  431. my $session=shift;
  432. if ($cgi->param("do") eq "setup") {
  433. showform($cgi, $session);
  434. exit;
  435. }
  436. }
  437. sub formbuilder_setup (@) {
  438. my %params=@_;
  439. my $form=$params{form};
  440. if ($form->title eq "preferences" &&
  441. IkiWiki::is_admin($params{session}->param("name"))) {
  442. push @{$params{buttons}}, "Setup";
  443. if ($form->submitted && $form->submitted eq "Setup") {
  444. showform($params{cgi}, $params{session});
  445. exit;
  446. }
  447. }
  448. }
  449. 1