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