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