2 package IkiWiki::Plugin::remove;
9 hook(type => "getsetup", id => "remove", call => \&getsetup);
10 hook(type => "formbuilder_setup", id => "remove", call => \&formbuilder_setup);
11 hook(type => "formbuilder", id => "remove", call => \&formbuilder);
12 hook(type => "sessioncgi", id => "remove", call => \&sessioncgi);
16 sub getsetup () { #{{{
24 sub check_canremove ($$$) { #{{{
29 # Must be a known source file.
30 if (! exists $pagesources{$page}) {
31 error(sprintf(gettext("%s does not exist"),
32 htmllink("", "", $page, noimageinline => 1)));
35 # Must exist on disk, and be a regular file.
36 my $file=$pagesources{$page};
37 if (! -e "$config{srcdir}/$file") {
38 error(sprintf(gettext("%s is not in the srcdir, so it cannot be deleted"), $file));
40 elsif (-l "$config{srcdir}/$file" && ! -f _) {
41 error(sprintf(gettext("%s is not a file"), $file));
45 IkiWiki::check_canedit($page, $q, $session);
47 # If a user can't upload an attachment, don't let them delete it.
48 # This is sorta overkill, but better safe than sorry.
49 if (! defined IkiWiki::pagetype($pagesources{$page})) {
50 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
51 IkiWiki::Plugin::attachment::check_canattach($session, $page, $file);
54 error("renaming of attachments is not allowed");
59 sub formbuilder_setup (@) { #{{{
61 my $form=$params{form};
64 if (defined $form->field("do") && $form->field("do") eq "edit") {
65 # Removal button for the page, and also for attachments.
66 push @{$params{buttons}}, "Remove";
67 $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
71 sub confirmation_form ($$) { #{{{
75 eval q{use CGI::FormBuilder};
77 my $f = CGI::FormBuilder->new(
84 action => $config{cgiurl},
85 stylesheet => IkiWiki::baseurl()."style.css",
86 fields => [qw{do page}],
89 $f->field(name => "do", type => "hidden", value => "remove", force => 1);
91 return $f, ["Remove", "Cancel"];
94 sub removal_confirm ($$@) { #{{{
100 check_canremove($_, $q, $session) foreach @pages;
102 # Save current form state to allow returning to it later
103 # without losing any edits.
104 # (But don't save what button was submitted, to avoid
105 # looping back to here.)
106 # Note: "_submit" is CGI::FormBuilder internals.
107 $q->param(-name => "_submit", -value => "");
108 $session->param(postremove => scalar $q->Vars);
109 IkiWiki::cgi_savesession($session);
111 my ($f, $buttons)=confirmation_form($q, $session);
112 $f->title(sprintf(gettext("confirm removal of %s"),
113 join(", ", map { IkiWiki::pagetitle($_) } @pages)));
114 $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
115 if (defined $attachment) {
116 $f->field(name => "attachment", type => "hidden",
117 value => $attachment, force => 1);
120 IkiWiki::showform($f, $buttons, $session, $q);
124 sub postremove ($) { #{{{
127 # Load saved form state and return to edit form.
128 my $postremove=CGI->new($session->param("postremove"));
129 $session->clear("postremove");
130 IkiWiki::cgi_savesession($session);
131 IkiWiki::cgi($postremove, $session);
134 sub formbuilder (@) { #{{{
136 my $form=$params{form};
138 if (defined $form->field("do") && $form->field("do") eq "edit") {
140 my $session=$params{session};
142 if ($form->submitted eq "Remove") {
143 removal_confirm($q, $session, 0, $form->field("page"));
145 elsif ($form->submitted eq "Remove Attachments") {
146 my @selected=$q->param("attachment_select");
148 error(gettext("Please select the attachments to remove."));
150 removal_confirm($q, $session, 1, @selected);
155 sub sessioncgi ($$) { #{{{
158 if ($q->param("do") eq 'remove') {
160 my ($form, $buttons)=confirmation_form($q, $session);
161 IkiWiki::decode_form_utf8($form);
163 if ($form->submitted eq 'Cancel') {
164 postremove($session);
166 elsif ($form->submitted eq 'Remove' && $form->validate) {
167 my @pages=$q->param("page");
169 # Validate removal by checking that the page exists,
170 # and that the user is allowed to edit(/remove) it.
172 foreach my $page (@pages) {
173 check_canremove($page, $q, $session);
175 # This untaint is safe because of the
176 # checks performed above, which verify the
177 # page is a normal file, etc.
178 push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
181 # Do removal, and update the wiki.
182 require IkiWiki::Render;
184 IkiWiki::disable_commit_hook();
185 foreach my $file (@files) {
186 IkiWiki::rcs_remove($file);
188 IkiWiki::rcs_commit_staged(gettext("removed"),
189 $session->param("name"), $ENV{REMOTE_ADDR});
190 IkiWiki::enable_commit_hook();
191 IkiWiki::rcs_update();
194 foreach my $file (@files) {
195 IkiWiki::prune("$config{srcdir}/$file");
199 IkiWiki::saveindex();
201 if ($q->param("attachment")) {
202 # Attachments were deleted, so redirect
203 # back to the edit form.
204 postremove($session);
207 # The page is gone, so redirect to parent
209 my $parent=IkiWiki::dirname($pages[0]);
210 if (! exists $pagesources{$parent}) {
213 IkiWiki::redirect($q, $config{url}."/".htmlpage($parent));
217 IkiWiki::showform($form, $buttons, $session, $q);