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