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);
25 sub check_canremove ($$$) {
30 # Must be a known source file.
31 if (! exists $pagesources{$page}) {
32 error(sprintf(gettext("%s does not exist"),
33 htmllink("", "", $page, noimageinline => 1)));
36 # Must exist on disk, and be a regular file.
37 my $file=$pagesources{$page};
38 if (! -e "$config{srcdir}/$file") {
39 error(sprintf(gettext("%s is not in the srcdir, so it cannot be deleted"), $file));
41 elsif (-l "$config{srcdir}/$file" && ! -f _) {
42 error(sprintf(gettext("%s is not a file"), $file));
46 IkiWiki::check_canedit($page, $q, $session);
48 # If a user can't upload an attachment, don't let them delete it.
49 # This is sorta overkill, but better safe than sorry.
50 if (! defined pagetype($pagesources{$page})) {
51 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
52 IkiWiki::Plugin::attachment::check_canattach($session, $page, "$config{srcdir}/$file");
55 error("removal of attachments is not allowed");
60 IkiWiki::run_hooks(canremove => sub {
61 return if defined $canremove;
62 my $ret=shift->(page => $page, cgi => $q, session => $session);
67 elsif (ref $ret eq 'CODE') {
71 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},
107 fields => [qw{do page}],
110 $f->field(name => "sid", type => "hidden", value => $session->id,
112 $f->field(name => "do", type => "hidden", value => "remove", force => 1);
114 return $f, ["Remove", "Cancel"];
117 sub removal_confirm ($$@) {
120 my $attachment=shift;
123 foreach my $page (@pages) {
124 check_canremove($page, $q, $session);
127 # Save current form state to allow returning to it later
128 # without losing any edits.
129 # (But don't save what button was submitted, to avoid
130 # looping back to here.)
131 # Note: "_submit" is CGI::FormBuilder internals.
132 $q->param(-name => "_submit", -value => "");
133 $session->param(postremove => scalar $q->Vars);
134 IkiWiki::cgi_savesession($session);
136 my ($f, $buttons)=confirmation_form($q, $session);
137 $f->title(sprintf(gettext("confirm removal of %s"),
138 join(", ", map { pagetitle($_) } @pages)));
139 $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
140 if (defined $attachment) {
141 $f->field(name => "attachment", type => "hidden",
142 value => $attachment, force => 1);
145 IkiWiki::showform($f, $buttons, $session, $q);
152 # Load saved form state and return to edit form.
153 my $postremove=CGI->new($session->param("postremove"));
154 $session->clear("postremove");
155 IkiWiki::cgi_savesession($session);
156 IkiWiki::cgi($postremove, $session);
159 sub formbuilder (@) {
161 my $form=$params{form};
163 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
164 $form->field("do") eq "create")) {
166 my $session=$params{session};
168 if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
169 removal_confirm($q, $session, 0, $form->field("page"));
171 elsif ($form->submitted eq "Remove Attachments") {
172 my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
174 error(gettext("Please select the attachments to remove."));
176 removal_confirm($q, $session, 1, @selected);
181 sub sessioncgi ($$) {
184 if ($q->param("do") eq 'remove') {
186 my ($form, $buttons)=confirmation_form($q, $session);
187 IkiWiki::decode_form_utf8($form);
189 if ($form->submitted eq 'Cancel') {
190 postremove($session);
192 elsif ($form->submitted eq 'Remove' && $form->validate) {
193 IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
195 my @pages=$form->field("page");
197 # Validate removal by checking that the page exists,
198 # and that the user is allowed to edit(/remove) it.
200 foreach my $page (@pages) {
201 check_canremove($page, $q, $session);
203 # This untaint is safe because of the
204 # checks performed above, which verify the
205 # page is a normal file, etc.
206 push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
209 # Do removal, and update the wiki.
210 require IkiWiki::Render;
212 IkiWiki::disable_commit_hook();
213 foreach my $file (@files) {
214 IkiWiki::rcs_remove($file);
216 IkiWiki::rcs_commit_staged(gettext("removed"),
217 $session->param("name"), $ENV{REMOTE_ADDR});
218 IkiWiki::enable_commit_hook();
219 IkiWiki::rcs_update();
222 foreach my $file (@files) {
223 IkiWiki::prune("$config{srcdir}/$file");
227 IkiWiki::saveindex();
229 if ($q->param("attachment")) {
230 # Attachments were deleted, so redirect
231 # back to the edit form.
232 postremove($session);
235 # The page is gone, so redirect to parent
237 my $parent=IkiWiki::dirname($pages[0]);
238 if (! exists $pagesources{$parent}) {
241 IkiWiki::redirect($q, urlto($parent, '/', 1));
245 removal_confirm($q, $session, 0, $form->field("page"));