summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/remove.pm
blob: 4b527993ba96b2c26095352cc613d935eee8416c (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. }
  66. sub formbuilder_setup (@) {
  67. my %params=@_;
  68. my $form=$params{form};
  69. my $q=$params{cgi};
  70. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  71. $form->field("do") eq "create")) {
  72. # Removal button for the page, and also for attachments.
  73. push @{$params{buttons}}, "Remove" if $form->field("do") eq "edit";
  74. $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
  75. }
  76. }
  77. sub confirmation_form ($$) {
  78. my $q=shift;
  79. my $session=shift;
  80. eval q{use CGI::FormBuilder};
  81. error($@) if $@;
  82. my $f = CGI::FormBuilder->new(
  83. name => "remove",
  84. header => 0,
  85. charset => "utf-8",
  86. method => 'POST',
  87. javascript => 0,
  88. params => $q,
  89. action => $config{cgiurl},
  90. stylesheet => 1,
  91. fields => [qw{do page}],
  92. );
  93. $f->field(name => "sid", type => "hidden", value => $session->id,
  94. force => 1);
  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. IkiWiki::check_canedit($page, $q, $session);
  105. check_canremove($page, $q, $session);
  106. }
  107. # Save current form state to allow returning to it later
  108. # without losing any edits.
  109. # (But don't save what button was submitted, to avoid
  110. # looping back to here.)
  111. # Note: "_submit" is CGI::FormBuilder internals.
  112. $q->param(-name => "_submit", -value => "");
  113. $session->param(postremove => scalar $q->Vars);
  114. IkiWiki::cgi_savesession($session);
  115. my ($f, $buttons)=confirmation_form($q, $session);
  116. $f->title(sprintf(gettext("confirm removal of %s"),
  117. join(", ", map { pagetitle($_) } @pages)));
  118. $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
  119. if (defined $attachment) {
  120. $f->field(name => "attachment", type => "hidden",
  121. value => $attachment, force => 1);
  122. }
  123. IkiWiki::showform($f, $buttons, $session, $q);
  124. exit 0;
  125. }
  126. sub postremove ($) {
  127. my $session=shift;
  128. # Load saved form state and return to edit form.
  129. my $postremove=CGI->new($session->param("postremove"));
  130. $session->clear("postremove");
  131. IkiWiki::cgi_savesession($session);
  132. IkiWiki::cgi($postremove, $session);
  133. }
  134. sub formbuilder (@) {
  135. my %params=@_;
  136. my $form=$params{form};
  137. if (defined $form->field("do") && ($form->field("do") eq "edit" ||
  138. $form->field("do") eq "create")) {
  139. my $q=$params{cgi};
  140. my $session=$params{session};
  141. if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
  142. removal_confirm($q, $session, 0, $form->field("page"));
  143. }
  144. elsif ($form->submitted eq "Remove Attachments") {
  145. my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
  146. if (! @selected) {
  147. error(gettext("Please select the attachments to remove."));
  148. }
  149. removal_confirm($q, $session, 1, @selected);
  150. }
  151. }
  152. }
  153. sub sessioncgi ($$) {
  154. my $q=shift;
  155. if ($q->param("do") eq 'remove') {
  156. my $session=shift;
  157. my ($form, $buttons)=confirmation_form($q, $session);
  158. IkiWiki::decode_form_utf8($form);
  159. if ($form->submitted eq 'Cancel') {
  160. postremove($session);
  161. }
  162. elsif ($form->submitted eq 'Remove' && $form->validate) {
  163. IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
  164. my @pages=$form->field("page");
  165. # Validate removal by checking that the page exists,
  166. # and that the user is allowed to edit(/remove) it.
  167. my @files;
  168. foreach my $page (@pages) {
  169. IkiWiki::check_canedit($page, $q, $session);
  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(
  184. message => gettext("removed"),
  185. session => $session,
  186. );
  187. IkiWiki::enable_commit_hook();
  188. IkiWiki::rcs_update();
  189. }
  190. else {
  191. foreach my $file (@files) {
  192. IkiWiki::prune("$config{srcdir}/$file");
  193. }
  194. }
  195. IkiWiki::refresh();
  196. IkiWiki::saveindex();
  197. if ($q->param("attachment")) {
  198. # Attachments were deleted, so redirect
  199. # back to the edit form.
  200. postremove($session);
  201. }
  202. else {
  203. # The page is gone, so redirect to parent
  204. # of the page.
  205. my $parent=IkiWiki::dirname($pages[0]);
  206. if (! exists $pagesources{$parent}) {
  207. $parent="index";
  208. }
  209. IkiWiki::redirect($q, urlto($parent, '/', 1));
  210. }
  211. }
  212. else {
  213. removal_confirm($q, $session, 0, $form->field("page"));
  214. }
  215. exit 0;
  216. }
  217. }
  218. 1