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