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