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