summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/websetup.pm
blob: 2a5e27bf921ee98edc0ee7f12f1ca15497595368 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::websetup;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.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=[@{$value}, "", ""]; # blank items for expansion
  124. }
  125. if ($info{type} eq "string") {
  126. $form->field(
  127. name => $name,
  128. label => $description,
  129. comment => formatexample($info{example}, $value),
  130. type => "text",
  131. value => $value,
  132. size => 60,
  133. fieldset => $section,
  134. );
  135. }
  136. elsif ($info{type} eq "pagespec") {
  137. $form->field(
  138. name => $name,
  139. label => $description,
  140. comment => formatexample($info{example}, $value),
  141. type => "text",
  142. value => $value,
  143. size => 60,
  144. validate => \&IkiWiki::pagespec_valid,
  145. fieldset => $section,
  146. );
  147. }
  148. elsif ($info{type} eq "integer") {
  149. $form->field(
  150. name => $name,
  151. label => $description,
  152. comment => formatexample($info{example}, $value),
  153. type => "text",
  154. value => $value,
  155. size => 5,
  156. validate => '/^[0-9]+$/',
  157. fieldset => $section,
  158. );
  159. }
  160. elsif ($info{type} eq "boolean") {
  161. $form->field(
  162. name => $name,
  163. label => "",
  164. type => "checkbox",
  165. options => [ [ 1 => $description ] ],
  166. fieldset => $section,
  167. );
  168. if (! $form->submitted) {
  169. $form->field(name => $name, value => $value);
  170. }
  171. }
  172. if (! $info{safe}) {
  173. $form->field(name => $name, disabled => 1);
  174. }
  175. else {
  176. $enabledfields{$name}=[$key, \%info];
  177. }
  178. $shownfields++;
  179. }
  180. # if no fields were shown for the plugin, drop it into the
  181. # plugins fieldset
  182. if (defined $plugin && (! $plugin_forced || $config{websetup_advanced}) &&
  183. ! $shownfields) {
  184. $form->field(name => "enable.$plugin", fieldset => "plugins");
  185. }
  186. return %enabledfields;
  187. } #}}}
  188. sub enable_plugin ($) { #{{{
  189. my $plugin=shift;
  190. $config{disable_plugins}=[grep { $_ ne $plugin } @{$config{disable_plugins}}];
  191. push @{$config{add_plugins}}, $plugin;
  192. }
  193. sub disable_plugin ($) { #{{{
  194. my $plugin=shift;
  195. if (grep { $_ eq $plugin } @{$config{add_plugins}}) {
  196. $config{add_plugins}=[grep { $_ ne $plugin } @{$config{add_plugins}}];
  197. }
  198. else {
  199. push @{$config{disable_plugins}}, $plugin;
  200. }
  201. }
  202. sub showform ($$) { #{{{
  203. my $cgi=shift;
  204. my $session=shift;
  205. if (! defined $session->param("name") ||
  206. ! IkiWiki::is_admin($session->param("name"))) {
  207. error(gettext("you are not logged in as an admin"));
  208. }
  209. eval q{use CGI::FormBuilder};
  210. error($@) if $@;
  211. my $form = CGI::FormBuilder->new(
  212. title => "setup",
  213. name => "setup",
  214. header => 0,
  215. charset => "utf-8",
  216. method => 'POST',
  217. javascript => 0,
  218. reset => 1,
  219. params => $cgi,
  220. fieldsets => [
  221. [main => gettext("main")],
  222. [plugins => gettext("plugins")]
  223. ],
  224. action => $config{cgiurl},
  225. template => {type => 'div'},
  226. stylesheet => IkiWiki::baseurl()."style.css",
  227. );
  228. $form->field(name => "do", type => "hidden", value => "setup",
  229. force => 1);
  230. $form->field(name => "rebuild_asked", type => "hidden");
  231. if ($form->submitted eq 'Basic Mode') {
  232. $form->field(name => "showadvanced", type => "hidden",
  233. value => 0, force => 1);
  234. }
  235. elsif ($form->submitted eq 'Advanced Mode') {
  236. $form->field(name => "showadvanced", type => "hidden",
  237. value => 1, force => 1);
  238. }
  239. my $advancedtoggle;
  240. if ($form->field("showadvanced")) {
  241. $config{websetup_advanced}=1;
  242. $advancedtoggle="Basic Mode";
  243. }
  244. else {
  245. $config{websetup_advanced}=0;
  246. $advancedtoggle="Advanced Mode";
  247. }
  248. my $buttons=["Save Setup", $advancedtoggle, "Cancel"];
  249. IkiWiki::decode_form_utf8($form);
  250. IkiWiki::run_hooks(formbuilder_setup => sub {
  251. shift->(form => $form, cgi => $cgi, session => $session,
  252. buttons => $buttons);
  253. });
  254. IkiWiki::decode_form_utf8($form);
  255. my %fields=showfields($form, undef, undef, IkiWiki::getsetup());
  256. # record all currently enabled plugins before all are loaded
  257. my %enabled_plugins=%IkiWiki::loaded_plugins;
  258. # per-plugin setup
  259. require IkiWiki::Setup;
  260. foreach my $pair (IkiWiki::Setup::getsetup()) {
  261. my $plugin=$pair->[0];
  262. my $setup=$pair->[1];
  263. my %shown=showfields($form, $plugin, $enabled_plugins{$plugin}, @{$setup});
  264. if (%shown) {
  265. $fields{$_}=$shown{$_} foreach keys %shown;
  266. }
  267. }
  268. if ($form->submitted eq "Cancel") {
  269. IkiWiki::redirect($cgi, $config{url});
  270. return;
  271. }
  272. elsif (($form->submitted eq 'Save Setup' || $form->submitted eq 'Rebuild Wiki') && $form->validate) {
  273. # Push values from form into %config, avoiding unnecessary
  274. # changes, and keeping track of which changes need a
  275. # rebuild.
  276. my %rebuild;
  277. foreach my $field (keys %fields) {
  278. my %info=%{$fields{$field}->[1]};
  279. my $key=$fields{$field}->[0];
  280. my @value=$form->field($field);
  281. if (! @value) {
  282. @value=0;
  283. }
  284. if (! $info{safe}) {
  285. error("unsafe field $key"); # should never happen
  286. }
  287. if (exists $info{rebuild} &&
  288. ($info{rebuild} || ! defined $info{rebuild})) {
  289. $rebuild{$field}=$info{rebuild};
  290. }
  291. if ($field=~/^enable\.(.*)/) {
  292. my $plugin=$1;
  293. $value[0]=0 if ! length $value[0];
  294. if ($value[0] != exists $enabled_plugins{$plugin}) {
  295. if ($value[0]) {
  296. enable_plugin($plugin);
  297. }
  298. else {
  299. disable_plugin($plugin);
  300. }
  301. }
  302. else {
  303. delete $rebuild{$field};
  304. }
  305. next;
  306. }
  307. if (ref $config{$key} eq "ARRAY" || ref $info{example} eq "ARRAY") {
  308. @value=sort grep { length $_ } @value;
  309. my @oldvalue=sort grep { length $_ }
  310. (defined $config{$key} ? @{$config{$key}} : ());
  311. if ((@oldvalue) == (@value)) {
  312. delete $rebuild{$field};
  313. }
  314. else {
  315. $config{$key}=\@value;
  316. }
  317. }
  318. elsif (ref $config{$key} || ref $info{example}) {
  319. error("complex field $key"); # should never happen
  320. }
  321. else {
  322. if (defined $config{$key} && $config{$key} eq $value[0]) {
  323. delete $rebuild{$field};
  324. }
  325. elsif (! defined $config{$key} && ! length $value[0]) {
  326. delete $rebuild{$field};
  327. }
  328. elsif ((! defined $config{$key} || ! $config{$key}) &&
  329. ! $value[0] && $info{type} eq "boolean") {
  330. delete $rebuild{$field};
  331. }
  332. else {
  333. $config{$key}=$value[0];
  334. }
  335. }
  336. }
  337. if (%rebuild && ! $form->field("rebuild_asked")) {
  338. my $required=0;
  339. foreach my $field ($form->field) {
  340. $required=1 if $rebuild{$field};
  341. next if exists $rebuild{$field};
  342. $form->field(name => $field, type => "hidden");
  343. }
  344. if ($required) {
  345. $form->text(gettext("The configuration changes shown below require a wiki rebuild to take effect."));
  346. $buttons=["Rebuild Wiki", "Cancel"];
  347. }
  348. else {
  349. $form->text(gettext("For the configuration changes shown below to fully take effect, you may need to rebuild the wiki."));
  350. $buttons=["Rebuild Wiki", "Save Setup", "Cancel"];
  351. }
  352. $form->field(name => "rebuild_asked", value => 1, force => 1);
  353. $form->reset(0); # doesn't really make sense here
  354. }
  355. else {
  356. $form->field(name => "rebuild_asked", type => "hidden",
  357. value => 0, force => 1);
  358. # TODO save to real path
  359. IkiWiki::Setup::dump("/tmp/s");
  360. $form->text(gettext("Setup saved."));
  361. if ($form->submitted eq 'Rebuild Wiki') {
  362. # TODO rebuild
  363. }
  364. else {
  365. # TODO refresh wiki and wrappers
  366. }
  367. }
  368. }
  369. IkiWiki::showform($form, $buttons, $session, $cgi);
  370. } #}}}
  371. sub sessioncgi ($$) { #{{{
  372. my $cgi=shift;
  373. my $session=shift;
  374. if ($cgi->param("do") eq "setup") {
  375. showform($cgi, $session);
  376. exit;
  377. }
  378. } #}}}
  379. sub formbuilder_setup (@) { #{{{
  380. my %params=@_;
  381. my $form=$params{form};
  382. if ($form->title eq "preferences") {
  383. push @{$params{buttons}}, "Wiki Setup";
  384. if ($form->submitted && $form->submitted eq "Wiki Setup") {
  385. showform($params{cgi}, $params{session});
  386. exit;
  387. }
  388. }
  389. } #}}}
  390. 1