summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/remove.pm
blob: c8d76368eb31813c0d5b3e40181b68afb1e94f28 (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 => "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 editiable.
  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. } #}}}
  49. sub formbuilder_setup (@) { #{{{
  50. my %params=@_;
  51. my $form=$params{form};
  52. my $q=$params{cgi};
  53. if (defined $form->field("do") && $form->field("do") eq "edit") {
  54. # Removal button for the page, and also for attachments.
  55. push @{$params{buttons}}, "Remove";
  56. $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
  57. }
  58. } #}}}
  59. sub confirmation_form ($$) { #{{{
  60. my $q=shift;
  61. my $session=shift;
  62. eval q{use CGI::FormBuilder};
  63. error($@) if $@;
  64. my $f = CGI::FormBuilder->new(
  65. name => "remove",
  66. header => 0,
  67. charset => "utf-8",
  68. method => 'POST',
  69. javascript => 0,
  70. params => $q,
  71. action => $config{cgiurl},
  72. stylesheet => IkiWiki::baseurl()."style.css",
  73. fields => [qw{do page}],
  74. );
  75. $f->field(name => "do", type => "hidden", value => "remove", force => 1);
  76. return $f, ["Remove", "Cancel"];
  77. } #}}}
  78. sub removal_confirm ($$@) { #{{{
  79. my $q=shift;
  80. my $session=shift;
  81. my $attachment=shift;
  82. my @pages=@_;
  83. check_canremove($_, $q, $session) foreach @pages;
  84. # Save current form state to allow returning to it later
  85. # without losing any edits.
  86. # (But don't save what button was submitted, to avoid
  87. # looping back to here.)
  88. # Note: "_submit" is CGI::FormBuilder internals.
  89. $q->param(-name => "_submit", -value => "");
  90. $session->param(postremove => scalar $q->Vars);
  91. IkiWiki::cgi_savesession($session);
  92. my ($f, $buttons)=confirmation_form($q, $session);
  93. $f->title(sprintf(gettext("confirm removal of %s"),
  94. join(", ", map { pagetitle($_) } @pages)));
  95. $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
  96. if (defined $attachment) {
  97. $f->field(name => "attachment", type => "hidden",
  98. value => $attachment, force => 1);
  99. }
  100. IkiWiki::showform($f, $buttons, $session, $q);
  101. exit 0;
  102. } #}}}
  103. sub postremove ($) { #{{{
  104. my $session=shift;
  105. # Load saved form state and return to edit form.
  106. my $postremove=CGI->new($session->param("postremove"));
  107. $session->clear("postremove");
  108. IkiWiki::cgi_savesession($session);
  109. IkiWiki::cgi($postremove, $session);
  110. } #}}}
  111. sub formbuilder (@) { #{{{
  112. my %params=@_;
  113. my $form=$params{form};
  114. if (defined $form->field("do") && $form->field("do") eq "edit") {
  115. my $q=$params{cgi};
  116. my $session=$params{session};
  117. if ($form->submitted eq "Remove") {
  118. removal_confirm($q, $session, 0, $form->field("page"));
  119. }
  120. elsif ($form->submitted eq "Remove Attachments") {
  121. my @selected=$q->param("attachment_select");
  122. if (! @selected) {
  123. error(gettext("Please select the attachments to remove."));
  124. }
  125. removal_confirm($q, $session, 1, @selected);
  126. }
  127. }
  128. } #}}}
  129. sub sessioncgi ($$) { #{{{
  130. my $q=shift;
  131. if ($q->param("do") eq 'remove') {
  132. my $session=shift;
  133. my ($form, $buttons)=confirmation_form($q, $session);
  134. IkiWiki::decode_form_utf8($form);
  135. if ($form->submitted eq 'Cancel') {
  136. postremove($session);
  137. }
  138. elsif ($form->submitted eq 'Remove' && $form->validate) {
  139. my @pages=$q->param("page");
  140. # Validate removal by checking that the page exists,
  141. # and that the user is allowed to edit(/remove) it.
  142. my @files;
  143. foreach my $page (@pages) {
  144. check_canremove($page, $q, $session);
  145. # This untaint is safe because of the
  146. # checks performed above, which verify the
  147. # page is a normal file, etc.
  148. push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
  149. }
  150. # Do removal, and update the wiki.
  151. require IkiWiki::Render;
  152. if ($config{rcs}) {
  153. IkiWiki::disable_commit_hook();
  154. foreach my $file (@files) {
  155. IkiWiki::rcs_remove($file);
  156. }
  157. IkiWiki::rcs_commit_staged(gettext("removed"),
  158. $session->param("name"), $ENV{REMOTE_ADDR});
  159. IkiWiki::enable_commit_hook();
  160. IkiWiki::rcs_update();
  161. }
  162. else {
  163. foreach my $file (@files) {
  164. IkiWiki::prune("$config{srcdir}/$file");
  165. }
  166. }
  167. IkiWiki::refresh();
  168. IkiWiki::saveindex();
  169. if ($q->param("attachment")) {
  170. # Attachments were deleted, so redirect
  171. # back to the edit form.
  172. postremove($session);
  173. }
  174. else {
  175. # The page is gone, so redirect to parent
  176. # of the page.
  177. my $parent=IkiWiki::dirname($pages[0]);
  178. if (! exists $pagesources{$parent}) {
  179. $parent="index";
  180. }
  181. IkiWiki::redirect($q, urlto($parent, '/', 1));
  182. }
  183. }
  184. else {
  185. IkiWiki::showform($form, $buttons, $session, $q);
  186. }
  187. exit 0;
  188. }
  189. }
  190. 1