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);
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 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 IkiWiki::run_hooks(canremove => sub {
60 return if defined $canremove;
61 my $ret=shift->($page, $q, $session);
66 elsif (ref $ret eq 'CODE') {
70 elsif (defined $ret) {
79 sub formbuilder_setup (@) {
81 my $form=$params{form};
84 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
85 $form->field("do") eq "create")) {
86 # Removal button for the page, and also for attachments.
87 push @{$params{buttons}}, "Remove" if $form->field("do") eq "edit";
88 $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
92 sub confirmation_form ($$) {
96 eval q{use CGI::FormBuilder};
98 my $f = CGI::FormBuilder->new(
105 action => $config{cgiurl},
106 stylesheet => IkiWiki::baseurl()."style.css",
107 fields => [qw{do page}],
110 $f->field(name => "do", type => "hidden", value => "remove", force => 1);
112 return $f, ["Remove", "Cancel"];
115 sub removal_confirm ($$@) {
118 my $attachment=shift;
121 foreach my $page (@pages) {
122 check_canremove($page, $q, $session);
125 # Save current form state to allow returning to it later
126 # without losing any edits.
127 # (But don't save what button was submitted, to avoid
128 # looping back to here.)
129 # Note: "_submit" is CGI::FormBuilder internals.
130 $q->param(-name => "_submit", -value => "");
131 $session->param(postremove => scalar $q->Vars);
132 IkiWiki::cgi_savesession($session);
134 my ($f, $buttons)=confirmation_form($q, $session);
135 $f->title(sprintf(gettext("confirm removal of %s"),
136 join(", ", map { pagetitle($_) } @pages)));
137 $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
138 if (defined $attachment) {
139 $f->field(name => "attachment", type => "hidden",
140 value => $attachment, force => 1);
143 IkiWiki::showform($f, $buttons, $session, $q);
150 # Load saved form state and return to edit form.
151 my $postremove=CGI->new($session->param("postremove"));
152 $session->clear("postremove");
153 IkiWiki::cgi_savesession($session);
154 IkiWiki::cgi($postremove, $session);
157 sub formbuilder (@) {
159 my $form=$params{form};
161 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
162 $form->field("do") eq "create")) {
164 my $session=$params{session};
166 if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
167 removal_confirm($q, $session, 0, $form->field("page"));
169 elsif ($form->submitted eq "Remove Attachments") {
170 my @selected=$q->param("attachment_select");
172 error(gettext("Please select the attachments to remove."));
174 removal_confirm($q, $session, 1, @selected);
179 sub sessioncgi ($$) {
182 if ($q->param("do") eq 'remove') {
184 my ($form, $buttons)=confirmation_form($q, $session);
185 IkiWiki::decode_form_utf8($form);
187 if ($form->submitted eq 'Cancel') {
188 postremove($session);
190 elsif ($form->submitted eq 'Remove' && $form->validate) {
191 my @pages=$q->param("page");
193 # Validate removal by checking that the page exists,
194 # and that the user is allowed to edit(/remove) it.
196 foreach my $page (@pages) {
197 check_canremove($page, $q, $session);
199 # This untaint is safe because of the
200 # checks performed above, which verify the
201 # page is a normal file, etc.
202 push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
205 # Do removal, and update the wiki.
206 require IkiWiki::Render;
208 IkiWiki::disable_commit_hook();
209 foreach my $file (@files) {
210 IkiWiki::rcs_remove($file);
212 IkiWiki::rcs_commit_staged(gettext("removed"),
213 $session->param("name"), $ENV{REMOTE_ADDR});
214 IkiWiki::enable_commit_hook();
215 IkiWiki::rcs_update();
218 foreach my $file (@files) {
219 IkiWiki::prune("$config{srcdir}/$file");
223 IkiWiki::saveindex();
225 if ($q->param("attachment")) {
226 # Attachments were deleted, so redirect
227 # back to the edit form.
228 postremove($session);
231 # The page is gone, so redirect to parent
233 my $parent=IkiWiki::dirname($pages[0]);
234 if (! exists $pagesources{$parent}) {
237 IkiWiki::redirect($q, urlto($parent, '/', 1));
241 removal_confirm($q, $session, 0, $q->param("page"));