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);
27 return grep { defined $_ } (
29 $IkiWiki::Plugin::transient::transientdir,
33 sub check_canremove ($$$) {
38 # Must be a known source file.
39 if (! exists $pagesources{$page}) {
40 error(sprintf(gettext("%s does not exist"),
41 htmllink("", "", $page, noimageinline => 1)));
44 # Must exist in either the srcdir or a suitable underlay (e.g.
45 # transient underlay), and be a regular file.
46 my $file=$pagesources{$page};
49 foreach my $srcdir (allowed_dirs()) {
50 if (-e "$srcdir/$file") {
57 error(sprintf(gettext("%s is not in the srcdir, so it cannot be deleted"), $file));
59 elsif (-l "$dir/$file" && ! -f _) {
60 error(sprintf(gettext("%s is not a file"), $file));
63 # If a user can't upload an attachment, don't let them delete it.
64 # This is sorta overkill, but better safe than sorry.
65 if (! defined pagetype($pagesources{$page})) {
66 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
67 IkiWiki::Plugin::attachment::check_canattach($session, $page, "$dir/$file");
70 error("removal of attachments is not allowed");
75 IkiWiki::run_hooks(canremove => sub {
76 return if defined $canremove;
77 my $ret=shift->(page => $page, cgi => $q, session => $session);
82 elsif (ref $ret eq 'CODE') {
86 elsif (defined $ret) {
92 return defined $canremove ? $canremove : 1;
95 sub formbuilder_setup (@) {
97 my $form=$params{form};
100 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
101 $form->field("do") eq "create")) {
102 # Removal button for the page, and also for attachments.
103 push @{$params{buttons}}, "Remove" if $form->field("do") eq "edit";
104 $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
108 sub confirmation_form ($$) {
112 eval q{use CGI::FormBuilder};
114 my $f = CGI::FormBuilder->new(
121 action => IkiWiki::cgiurl(),
123 fields => [qw{do page}],
126 $f->field(name => "sid", type => "hidden", value => $session->id,
128 $f->field(name => "do", type => "hidden", value => "remove", force => 1);
130 return $f, ["Remove", "Cancel"];
133 sub removal_confirm ($$@) {
136 my $attachment=shift;
139 # Special case for unsaved attachments.
140 foreach my $page (@pages) {
141 if ($attachment && IkiWiki::Plugin::attachment->can("is_held_attachment")) {
142 my $f=IkiWiki::Plugin::attachment::is_held_attachment($page);
144 require IkiWiki::Render;
145 IkiWiki::prune($f, "$config{wikistatedir}/attachments");
149 @pages=grep { exists $pagesources{$_} } @pages;
150 return unless @pages;
152 foreach my $page (@pages) {
153 IkiWiki::check_canedit($page, $q, $session);
154 check_canremove($page, $q, $session);
157 # Save current form state to allow returning to it later
158 # without losing any edits.
159 # (But don't save what button was submitted, to avoid
160 # looping back to here.)
161 # Note: "_submit" is CGI::FormBuilder internals.
162 $q->param(-name => "_submit", -value => "");
163 $session->param(postremove => scalar $q->Vars);
164 IkiWiki::cgi_savesession($session);
166 my ($f, $buttons)=confirmation_form($q, $session);
167 $f->title(sprintf(gettext("confirm removal of %s"),
168 join(", ", map { pagetitle($_) } @pages)));
169 $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
170 if (defined $attachment) {
171 $f->field(name => "attachment", type => "hidden",
172 value => $attachment, force => 1);
175 IkiWiki::showform($f, $buttons, $session, $q);
182 # Load saved form state and return to edit form.
183 my $postremove=CGI->new($session->param("postremove"));
184 $session->clear("postremove");
185 IkiWiki::cgi_savesession($session);
186 IkiWiki::cgi($postremove, $session);
189 sub formbuilder (@) {
191 my $form=$params{form};
193 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
194 $form->field("do") eq "create")) {
196 my $session=$params{session};
198 if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
199 # deliberately taking multiple values of page
200 my @pages = $form->field("page");
201 removal_confirm($q, $session, 0, @pages);
203 elsif ($form->submitted eq "Remove Attachments") {
204 my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
206 error(gettext("Please select the attachments to remove."));
208 removal_confirm($q, $session, 1, @selected);
213 sub sessioncgi ($$) {
216 if ($q->param("do") eq 'remove') {
218 my ($form, $buttons)=confirmation_form($q, $session);
219 IkiWiki::decode_form_utf8($form);
221 # deliberately taking multiple values of page
222 my @pages = $form->field("page");
224 if ($form->submitted eq 'Cancel') {
225 postremove($session);
227 elsif ($form->submitted eq 'Remove' && $form->validate) {
228 IkiWiki::checksessionexpiry($q, $session);
230 # Validate removal by checking that the page exists,
231 # and that the user is allowed to edit(/remove) it.
233 foreach my $page (@pages) {
234 IkiWiki::check_canedit($page, $q, $session);
235 check_canremove($page, $q, $session);
237 # This untaint is safe because of the
238 # checks performed above, which verify the
239 # page is a normal file, etc.
240 push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
243 # Do removal, and update the wiki.
244 require IkiWiki::Render;
246 IkiWiki::disable_commit_hook();
250 foreach my $file (@files) {
251 foreach my $srcdir (allowed_dirs()) {
252 if (-e "$srcdir/$file") {
253 if ($srcdir eq $config{srcdir} && $config{rcs}) {
254 IkiWiki::rcs_remove($file);
258 IkiWiki::prune("$srcdir/$file", $srcdir);
266 IkiWiki::rcs_commit_staged(
267 message => gettext("removed"),
271 IkiWiki::enable_commit_hook();
272 IkiWiki::rcs_update();
276 IkiWiki::saveindex();
278 if ($q->param("attachment")) {
279 # Attachments were deleted, so redirect
280 # back to the edit form.
281 postremove($session);
284 # The page is gone, so redirect to parent
286 my $parent=IkiWiki::dirname($pages[0]);
287 if (! exists $pagesources{$parent}) {
290 IkiWiki::redirect($q, urlto($parent));
294 removal_confirm($q, $session, 0, @pages);