summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/remove.pm
blob: d23b2cc1016851ac537303f9a3438df7446f40bd (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 => 1,
  93. fields => [qw{do page}],
  94. );
  95. $f->field(name => "sid", type => "hidden", value => $session->id,
  96. force => 1);
  97. $f->field(name => "do", type => "hidden", value => "remove", force => 1);
  98. return $f, ["Remove", "Cancel"];
  99. }
  100. sub removal_confirm ($$@) {
  101. my $q=shift;
  102. my $session=shift;
  103. my $attachment=shift;
  104. my @pages=@_;
  105. foreach my $page (@pages) {
  106. check_canremove($page, $q, $session);
  107. }
  108. # Save current form state to allow returning to it later
  109. # without losing any edits.
  110. # (But don't save what button was submitted, to avoid
  111. # looping back to here.)
  112. # Note: "_submit" is CGI::FormBuilder internals.
  113. $q->param(-name => "_submit", -value => "");
  114. $session->param(postremove => scalar $q->Vars);
  115. IkiWiki::cgi_savesession($session);
  116. my ($f, $buttons)=confirmation_form($q, $session);
  117. $f->title(sprintf(gettext("confirm removal of %s"),
  118. join(", ", map { pagetitle($_) } @pages)));
  119. $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
  120. if (defined $attachment) {
  121. $f->field(name => "attachment", type => "hidden",
  122. value => $attachment, force => 1);
  123. }
  124. IkiWiki::showform($f, $buttons, $session, $q);
  125. exit 0;
  126. }
  127. sub postremove ($) {
  128. my $session=shift;
  129. # Load saved form state and return to edit form.
  130. my $postremove=CGI->new($session->param("postremove"));
  131. $session->clear("postremove");
  132. IkiWiki::cgi_savesession($session);
  133. IkiWiki::cgi($postremove, $session);
  134. }
  135. sub formbuilder (@) {
  136. my %params=@_;
  137. my $form=$params{form};
  138. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  139. $form->field("do") eq "create")) {
  140. my $q=$params{cgi};
  141. my $session=$params{session};
  142. if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
  143. removal_confirm($q, $session, 0, $form->field("page"));
  144. }
  145. elsif ($form->submitted eq "Remove Attachments") {
  146. my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
  147. if (! @selected) {
  148. error(gettext("Please select the attachments to remove."));
  149. }
  150. removal_confirm($q, $session, 1, @selected);
  151. }
  152. }
  153. }
  154. sub sessioncgi ($$) {
  155. my $q=shift;
  156. if ($q->param("do") eq 'remove') {
  157. my $session=shift;
  158. my ($form, $buttons)=confirmation_form($q, $session);
  159. IkiWiki::decode_form_utf8($form);
  160. if ($form->submitted eq 'Cancel') {
  161. postremove($session);
  162. }
  163. elsif ($form->submitted eq 'Remove' && $form->validate) {
  164. IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
  165. my @pages=$form->field("page");
  166. # Validate removal by checking that the page exists,
  167. # and that the user is allowed to edit(/remove) it.
  168. my @files;
  169. foreach my $page (@pages) {
  170. check_canremove($page, $q, $session);
  171. # This untaint is safe because of the
  172. # checks performed above, which verify the
  173. # page is a normal file, etc.
  174. push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
  175. }
  176. # Do removal, and update the wiki.
  177. require IkiWiki::Render;
  178. if ($config{rcs}) {
  179. IkiWiki::disable_commit_hook();
  180. foreach my $file (@files) {
  181. IkiWiki::rcs_remove($file);
  182. }
  183. IkiWiki::rcs_commit_staged(gettext("removed"),
  184. $session->param("name"), $ENV{REMOTE_ADDR});
  185. IkiWiki::enable_commit_hook();
  186. IkiWiki::rcs_update();
  187. }
  188. else {
  189. foreach my $file (@files) {
  190. IkiWiki::prune("$config{srcdir}/$file");
  191. }
  192. }
  193. IkiWiki::refresh();
  194. IkiWiki::saveindex();
  195. if ($q->param("attachment")) {
  196. # Attachments were deleted, so redirect
  197. # back to the edit form.
  198. postremove($session);
  199. }
  200. else {
  201. # The page is gone, so redirect to parent
  202. # of the page.
  203. my $parent=IkiWiki::dirname($pages[0]);
  204. if (! exists $pagesources{$parent}) {
  205. $parent="index";
  206. }
  207. IkiWiki::redirect($q, urlto($parent, '/', 1));
  208. }
  209. }
  210. else {
  211. removal_confirm($q, $session, 0, $form->field("page"));
  212. }
  213. exit 0;
  214. }
  215. }
  216. 1