summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/remove.pm
blob: 1717c8cf3759e07724098390387dedf177ddcbaa (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. # If a user can't upload an attachment, don't let them delete it.
  38. # This is sorta overkill, but better safe than sorry.
  39. if (! defined pagetype($pagesources{$page})) {
  40. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  41. IkiWiki::Plugin::attachment::check_canattach($session, $page, "$config{srcdir}/$file");
  42. }
  43. else {
  44. error("removal of attachments is not allowed");
  45. }
  46. }
  47. my $canremove;
  48. IkiWiki::run_hooks(canremove => sub {
  49. return if defined $canremove;
  50. my $ret=shift->(page => $page, cgi => $q, session => $session);
  51. if (defined $ret) {
  52. if ($ret eq "") {
  53. $canremove=1;
  54. }
  55. elsif (ref $ret eq 'CODE') {
  56. $ret->();
  57. $canremove=0;
  58. }
  59. elsif (defined $ret) {
  60. error($ret);
  61. $canremove=0;
  62. }
  63. }
  64. });
  65. return defined $canremove ? $canremove : 1;
  66. }
  67. sub formbuilder_setup (@) {
  68. my %params=@_;
  69. my $form=$params{form};
  70. my $q=$params{cgi};
  71. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  72. $form->field("do") eq "create")) {
  73. # Removal button for the page, and also for attachments.
  74. push @{$params{buttons}}, "Remove" if $form->field("do") eq "edit";
  75. $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
  76. }
  77. }
  78. sub confirmation_form ($$) {
  79. my $q=shift;
  80. my $session=shift;
  81. eval q{use CGI::FormBuilder};
  82. error($@) if $@;
  83. my $f = CGI::FormBuilder->new(
  84. name => "remove",
  85. header => 0,
  86. charset => "utf-8",
  87. method => 'POST',
  88. javascript => 0,
  89. params => $q,
  90. action => $config{cgiurl},
  91. stylesheet => 1,
  92. fields => [qw{do page}],
  93. );
  94. $f->field(name => "sid", type => "hidden", value => $session->id,
  95. force => 1);
  96. $f->field(name => "do", type => "hidden", value => "remove", force => 1);
  97. return $f, ["Remove", "Cancel"];
  98. }
  99. sub removal_confirm ($$@) {
  100. my $q=shift;
  101. my $session=shift;
  102. my $attachment=shift;
  103. my @pages=@_;
  104. foreach my $page (@pages) {
  105. IkiWiki::check_canedit($page, $q, $session);
  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. IkiWiki::check_canedit($page, $q, $session);
  171. check_canremove($page, $q, $session);
  172. # This untaint is safe because of the
  173. # checks performed above, which verify the
  174. # page is a normal file, etc.
  175. push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
  176. }
  177. # Do removal, and update the wiki.
  178. require IkiWiki::Render;
  179. if ($config{rcs}) {
  180. IkiWiki::disable_commit_hook();
  181. foreach my $file (@files) {
  182. IkiWiki::rcs_remove($file);
  183. }
  184. IkiWiki::rcs_commit_staged(
  185. message => gettext("removed"),
  186. session => $session,
  187. );
  188. IkiWiki::enable_commit_hook();
  189. IkiWiki::rcs_update();
  190. }
  191. else {
  192. foreach my $file (@files) {
  193. IkiWiki::prune("$config{srcdir}/$file");
  194. }
  195. }
  196. IkiWiki::refresh();
  197. IkiWiki::saveindex();
  198. if ($q->param("attachment")) {
  199. # Attachments were deleted, so redirect
  200. # back to the edit form.
  201. postremove($session);
  202. }
  203. else {
  204. # The page is gone, so redirect to parent
  205. # of the page.
  206. my $parent=IkiWiki::dirname($pages[0]);
  207. if (! exists $pagesources{$parent}) {
  208. $parent="index";
  209. }
  210. IkiWiki::redirect($q, urlto($parent, '/', 1));
  211. }
  212. }
  213. else {
  214. removal_confirm($q, $session, 0, $form->field("page"));
  215. }
  216. exit 0;
  217. }
  218. }
  219. 1