summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/remove.pm
blob: 0fc180f699ce0b2071067404bf3845b2a60b3a9e (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::remove;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. sub import {
  7. hook(type => "getsetup", id => "remove", call => \&getsetup);
  8. hook(type => "formbuilder_setup", id => "remove", call => \&formbuilder_setup);
  9. hook(type => "formbuilder", id => "remove", call => \&formbuilder);
  10. hook(type => "sessioncgi", id => "remove", call => \&sessioncgi);
  11. }
  12. sub getsetup () {
  13. return
  14. plugin => {
  15. safe => 1,
  16. rebuild => 0,
  17. section => "web",
  18. },
  19. }
  20. sub check_canremove ($$$) {
  21. my $page=shift;
  22. my $q=shift;
  23. my $session=shift;
  24. # Must be a known source file.
  25. if (! exists $pagesources{$page}) {
  26. error(sprintf(gettext("%s does not exist"),
  27. htmllink("", "", $page, noimageinline => 1)));
  28. }
  29. # Must exist on disk, and be a regular file.
  30. my $file=$pagesources{$page};
  31. if (! -e "$config{srcdir}/$file") {
  32. error(sprintf(gettext("%s is not in the srcdir, so it cannot be deleted"), $file));
  33. }
  34. elsif (-l "$config{srcdir}/$file" && ! -f _) {
  35. error(sprintf(gettext("%s is not a file"), $file));
  36. }
  37. # Must be editable.
  38. IkiWiki::check_canedit($page, $q, $session);
  39. # If a user can't upload an attachment, don't let them delete it.
  40. # This is sorta overkill, but better safe than sorry.
  41. if (! defined pagetype($pagesources{$page})) {
  42. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  43. IkiWiki::Plugin::attachment::check_canattach($session, $page, "$config{srcdir}/$file");
  44. }
  45. else {
  46. error("removal of attachments is not allowed");
  47. }
  48. }
  49. my $canremove;
  50. IkiWiki::run_hooks(canremove => sub {
  51. return if defined $canremove;
  52. my $ret=shift->(page => $page, cgi => $q, session => $session);
  53. if (defined $ret) {
  54. if ($ret eq "") {
  55. $canremove=1;
  56. }
  57. elsif (ref $ret eq 'CODE') {
  58. $ret->();
  59. $canremove=0;
  60. }
  61. elsif (defined $ret) {
  62. error($ret);
  63. $canremove=0;
  64. }
  65. }
  66. });
  67. }
  68. sub formbuilder_setup (@) {
  69. my %params=@_;
  70. my $form=$params{form};
  71. my $q=$params{cgi};
  72. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  73. $form->field("do") eq "create")) {
  74. # Removal button for the page, and also for attachments.
  75. push @{$params{buttons}}, "Remove" if $form->field("do") eq "edit";
  76. $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
  77. }
  78. }
  79. sub confirmation_form ($$) {
  80. my $q=shift;
  81. my $session=shift;
  82. eval q{use CGI::FormBuilder};
  83. error($@) if $@;
  84. my $f = CGI::FormBuilder->new(
  85. name => "remove",
  86. header => 0,
  87. charset => "utf-8",
  88. method => 'POST',
  89. javascript => 0,
  90. params => $q,
  91. action => $config{cgiurl},
  92. stylesheet => IkiWiki::baseurl()."style.css",
  93. fields => [qw{do page}],
  94. );
  95. $f->field(name => "do", type => "hidden", value => "remove", force => 1);
  96. return $f, ["Remove", "Cancel"];
  97. }
  98. sub removal_confirm ($$@) {
  99. my $q=shift;
  100. my $session=shift;
  101. my $attachment=shift;
  102. my @pages=@_;
  103. foreach my $page (@pages) {
  104. check_canremove($page, $q, $session);
  105. }
  106. # Save current form state to allow returning to it later
  107. # without losing any edits.
  108. # (But don't save what button was submitted, to avoid
  109. # looping back to here.)
  110. # Note: "_submit" is CGI::FormBuilder internals.
  111. $q->param(-name => "_submit", -value => "");
  112. $session->param(postremove => scalar $q->Vars);
  113. IkiWiki::cgi_savesession($session);
  114. my ($f, $buttons)=confirmation_form($q, $session);
  115. $f->title(sprintf(gettext("confirm removal of %s"),
  116. join(", ", map { pagetitle($_) } @pages)));
  117. $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
  118. if (defined $attachment) {
  119. $f->field(name => "attachment", type => "hidden",
  120. value => $attachment, force => 1);
  121. }
  122. IkiWiki::showform($f, $buttons, $session, $q);
  123. exit 0;
  124. }
  125. sub postremove ($) {
  126. my $session=shift;
  127. # Load saved form state and return to edit form.
  128. my $postremove=CGI->new($session->param("postremove"));
  129. $session->clear("postremove");
  130. IkiWiki::cgi_savesession($session);
  131. IkiWiki::cgi($postremove, $session);
  132. }
  133. sub formbuilder (@) {
  134. my %params=@_;
  135. my $form=$params{form};
  136. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  137. $form->field("do") eq "create")) {
  138. my $q=$params{cgi};
  139. my $session=$params{session};
  140. if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
  141. removal_confirm($q, $session, 0, $form->field("page"));
  142. }
  143. elsif ($form->submitted eq "Remove Attachments") {
  144. my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
  145. if (! @selected) {
  146. error(gettext("Please select the attachments to remove."));
  147. }
  148. removal_confirm($q, $session, 1, @selected);
  149. }
  150. }
  151. }
  152. sub sessioncgi ($$) {
  153. my $q=shift;
  154. if ($q->param("do") eq 'remove') {
  155. my $session=shift;
  156. my ($form, $buttons)=confirmation_form($q, $session);
  157. IkiWiki::decode_form_utf8($form);
  158. if ($form->submitted eq 'Cancel') {
  159. postremove($session);
  160. }
  161. elsif ($form->submitted eq 'Remove' && $form->validate) {
  162. my @pages=$form->field("page");
  163. # Validate removal by checking that the page exists,
  164. # and that the user is allowed to edit(/remove) it.
  165. my @files;
  166. foreach my $page (@pages) {
  167. check_canremove($page, $q, $session);
  168. # This untaint is safe because of the
  169. # checks performed above, which verify the
  170. # page is a normal file, etc.
  171. push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
  172. }
  173. # Do removal, and update the wiki.
  174. require IkiWiki::Render;
  175. if ($config{rcs}) {
  176. IkiWiki::disable_commit_hook();
  177. foreach my $file (@files) {
  178. IkiWiki::rcs_remove($file);
  179. }
  180. IkiWiki::rcs_commit_staged(gettext("removed"),
  181. $session->param("name"), $ENV{REMOTE_ADDR});
  182. IkiWiki::enable_commit_hook();
  183. IkiWiki::rcs_update();
  184. }
  185. else {
  186. foreach my $file (@files) {
  187. IkiWiki::prune("$config{srcdir}/$file");
  188. }
  189. }
  190. IkiWiki::refresh();
  191. IkiWiki::saveindex();
  192. if ($q->param("attachment")) {
  193. # Attachments were deleted, so redirect
  194. # back to the edit form.
  195. postremove($session);
  196. }
  197. else {
  198. # The page is gone, so redirect to parent
  199. # of the page.
  200. my $parent=IkiWiki::dirname($pages[0]);
  201. if (! exists $pagesources{$parent}) {
  202. $parent="index";
  203. }
  204. IkiWiki::redirect($q, urlto($parent, '/', 1));
  205. }
  206. }
  207. else {
  208. removal_confirm($q, $session, 0, $form->field("page"));
  209. }
  210. exit 0;
  211. }
  212. }
  213. 1