summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/remove.pm
blob: f62bfb1d75d55d0f78096567085e9f993a194307 (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. },
  18. }
  19. sub check_canremove ($$$) {
  20. my $page=shift;
  21. my $q=shift;
  22. my $session=shift;
  23. # Must be a known source file.
  24. if (! exists $pagesources{$page}) {
  25. error(sprintf(gettext("%s does not exist"),
  26. htmllink("", "", $page, noimageinline => 1)));
  27. }
  28. # Must exist on disk, and be a regular file.
  29. my $file=$pagesources{$page};
  30. if (! -e "$config{srcdir}/$file") {
  31. error(sprintf(gettext("%s is not in the srcdir, so it cannot be deleted"), $file));
  32. }
  33. elsif (-l "$config{srcdir}/$file" && ! -f _) {
  34. error(sprintf(gettext("%s is not a file"), $file));
  35. }
  36. # Must be editable.
  37. IkiWiki::check_canedit($page, $q, $session);
  38. # If a user can't upload an attachment, don't let them delete it.
  39. # This is sorta overkill, but better safe than sorry.
  40. if (! defined pagetype($pagesources{$page})) {
  41. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  42. IkiWiki::Plugin::attachment::check_canattach($session, $page, $file);
  43. }
  44. else {
  45. error("renaming of attachments is not allowed");
  46. }
  47. }
  48. my $canremove;
  49. IkiWiki::run_hooks(canremove => sub {
  50. return if defined $canremove;
  51. my $ret=shift->($page, $q, $session);
  52. if (defined $ret) {
  53. if ($ret eq "") {
  54. $canremove=1;
  55. }
  56. elsif (ref $ret eq 'CODE') {
  57. $ret->();
  58. $canremove=0;
  59. }
  60. elsif (defined $ret) {
  61. error($ret);
  62. $canremove=0;
  63. }
  64. }
  65. });
  66. return $canremove;
  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=$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=$q->param("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, $q->param("page"));
  209. }
  210. exit 0;
  211. }
  212. }
  213. 1