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