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);
26 return grep { defined $_ } (
28 $IkiWiki::Plugin::transient::transientdir,
32 sub check_canremove ($$$) {
37 # Must be a known source file.
38 if (! exists $pagesources{$page}) {
39 error(sprintf(gettext("%s does not exist"),
40 htmllink("", "", $page, noimageinline => 1)));
43 # Must exist in either the srcdir or a suitable underlay (e.g.
44 # transient underlay), and be a regular file.
45 my $file=$pagesources{$page};
48 foreach my $srcdir (allowed_dirs()) {
49 if (-e "$srcdir/$file") {
56 error(sprintf(gettext("%s is not in the srcdir, so it cannot be deleted"), $file));
58 elsif (-l "$dir/$file" && ! -f _) {
59 error(sprintf(gettext("%s is not a file"), $file));
62 # If a user can't upload an attachment, don't let them delete it.
63 # This is sorta overkill, but better safe than sorry.
64 if (! defined pagetype($pagesources{$page})) {
65 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
66 IkiWiki::Plugin::attachment::check_canattach($session, $page, "$dir/$file");
69 error("removal of attachments is not allowed");
74 IkiWiki::run_hooks(canremove => sub {
75 return if defined $canremove;
76 my $ret=shift->(page => $page, cgi => $q, session => $session);
81 elsif (ref $ret eq 'CODE') {
85 elsif (defined $ret) {
91 return defined $canremove ? $canremove : 1;
94 sub formbuilder_setup (@) {
96 my $form=$params{form};
99 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
100 $form->field("do") eq "create")) {
101 # Removal button for the page, and also for attachments.
102 push @{$params{buttons}}, "Remove" if $form->field("do") eq "edit";
103 $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
107 sub confirmation_form ($$) {
111 eval q{use CGI::FormBuilder};
113 my $f = CGI::FormBuilder->new(
120 action => IkiWiki::cgiurl(),
122 fields => [qw{do page}],
125 $f->field(name => "sid", type => "hidden", value => $session->id,
127 $f->field(name => "do", type => "hidden", value => "remove", force => 1);
129 return $f, ["Remove", "Cancel"];
132 sub removal_confirm ($$@) {
135 my $attachment=shift;
138 # Special case for unsaved attachments.
139 foreach my $page (@pages) {
140 if ($attachment && IkiWiki::Plugin::attachment->can("is_held_attachment")) {
141 my $f=IkiWiki::Plugin::attachment::is_held_attachment($page);
143 require IkiWiki::Render;
144 IkiWiki::prune($f, "$config{wikistatedir}/attachments");
148 @pages=grep { exists $pagesources{$_} } @pages;
149 return unless @pages;
151 foreach my $page (@pages) {
152 IkiWiki::check_canedit($page, $q, $session);
153 check_canremove($page, $q, $session);
156 # Save current form state to allow returning to it later
157 # without losing any edits.
158 # (But don't save what button was submitted, to avoid
159 # looping back to here.)
160 # Note: "_submit" is CGI::FormBuilder internals.
161 $q->param(-name => "_submit", -value => "");
162 $session->param(postremove => scalar $q->Vars);
163 IkiWiki::cgi_savesession($session);
165 my ($f, $buttons)=confirmation_form($q, $session);
166 $f->title(sprintf(gettext("confirm removal of %s"),
167 join(", ", map { pagetitle($_) } @pages)));
168 $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
169 if (defined $attachment) {
170 $f->field(name => "attachment", type => "hidden",
171 value => $attachment, force => 1);
174 IkiWiki::showform($f, $buttons, $session, $q);
181 # Load saved form state and return to edit form.
182 my $postremove=CGI->new($session->param("postremove"));
183 $session->clear("postremove");
184 IkiWiki::cgi_savesession($session);
185 IkiWiki::cgi($postremove, $session);
188 sub formbuilder (@) {
190 my $form=$params{form};
192 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
193 $form->field("do") eq "create")) {
195 my $session=$params{session};
197 if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
198 removal_confirm($q, $session, 0, $form->field("page"));
200 elsif ($form->submitted eq "Remove Attachments") {
201 my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
203 error(gettext("Please select the attachments to remove."));
205 removal_confirm($q, $session, 1, @selected);
210 sub sessioncgi ($$) {
213 if ($q->param("do") eq 'remove') {
215 my ($form, $buttons)=confirmation_form($q, $session);
216 IkiWiki::decode_form_utf8($form);
218 if ($form->submitted eq 'Cancel') {
219 postremove($session);
221 elsif ($form->submitted eq 'Remove' && $form->validate) {
222 IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
224 my @pages=$form->field("page");
226 # Validate removal by checking that the page exists,
227 # and that the user is allowed to edit(/remove) it.
229 foreach my $page (@pages) {
230 IkiWiki::check_canedit($page, $q, $session);
231 check_canremove($page, $q, $session);
233 # This untaint is safe because of the
234 # checks performed above, which verify the
235 # page is a normal file, etc.
236 push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
239 # Do removal, and update the wiki.
240 require IkiWiki::Render;
242 IkiWiki::disable_commit_hook();
246 foreach my $file (@files) {
247 foreach my $srcdir (allowed_dirs()) {
248 if (-e "$srcdir/$file") {
249 if ($srcdir eq $config{srcdir} && $config{rcs}) {
250 IkiWiki::rcs_remove($file);
254 IkiWiki::prune("$srcdir/$file", $srcdir);
262 IkiWiki::rcs_commit_staged(
263 message => gettext("removed"),
267 IkiWiki::enable_commit_hook();
268 IkiWiki::rcs_update();
272 IkiWiki::saveindex();
274 if ($q->param("attachment")) {
275 # Attachments were deleted, so redirect
276 # back to the edit form.
277 postremove($session);
280 # The page is gone, so redirect to parent
282 my $parent=IkiWiki::dirname($pages[0]);
283 if (! exists $pagesources{$parent}) {
286 IkiWiki::redirect($q, urlto($parent));
290 removal_confirm($q, $session, 0, $form->field("page"));