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