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 sub formbuilder_setup (@) {
61 my $form=$params{form};
64 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
65 $form->field("do") eq "create")) {
66 # Removal button for the page, and also for attachments.
67 push @{$params{buttons}}, "Remove" if $form->field("do") eq "edit";
68 $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
72 sub confirmation_form ($$) {
76 eval q{use CGI::FormBuilder};
78 my $f = CGI::FormBuilder->new(
85 action => $config{cgiurl},
86 stylesheet => IkiWiki::baseurl()."style.css",
87 fields => [qw{do page}],
90 $f->field(name => "do", type => "hidden", value => "remove", force => 1);
92 return $f, ["Remove", "Cancel"];
95 sub removal_confirm ($$@) {
101 foreach my $page (@pages) {
102 check_canremove($page, $q, $session);
105 # Save current form state to allow returning to it later
106 # without losing any edits.
107 # (But don't save what button was submitted, to avoid
108 # looping back to here.)
109 # Note: "_submit" is CGI::FormBuilder internals.
110 $q->param(-name => "_submit", -value => "");
111 $session->param(postremove => scalar $q->Vars);
112 IkiWiki::cgi_savesession($session);
114 my ($f, $buttons)=confirmation_form($q, $session);
115 $f->title(sprintf(gettext("confirm removal of %s"),
116 join(", ", map { pagetitle($_) } @pages)));
117 $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
118 if (defined $attachment) {
119 $f->field(name => "attachment", type => "hidden",
120 value => $attachment, force => 1);
123 IkiWiki::showform($f, $buttons, $session, $q);
130 # Load saved form state and return to edit form.
131 my $postremove=CGI->new($session->param("postremove"));
132 $session->clear("postremove");
133 IkiWiki::cgi_savesession($session);
134 IkiWiki::cgi($postremove, $session);
137 sub formbuilder (@) {
139 my $form=$params{form};
141 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
142 $form->field("do") eq "create")) {
144 my $session=$params{session};
146 if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
147 removal_confirm($q, $session, 0, $form->field("page"));
149 elsif ($form->submitted eq "Remove Attachments") {
150 my @selected=$q->param("attachment_select");
152 error(gettext("Please select the attachments to remove."));
154 removal_confirm($q, $session, 1, @selected);
159 sub sessioncgi ($$) {
162 if ($q->param("do") eq 'remove') {
164 my ($form, $buttons)=confirmation_form($q, $session);
165 IkiWiki::decode_form_utf8($form);
167 if ($form->submitted eq 'Cancel') {
168 postremove($session);
170 elsif ($form->submitted eq 'Remove' && $form->validate) {
171 my @pages=$q->param("page");
173 # Validate removal by checking that the page exists,
174 # and that the user is allowed to edit(/remove) it.
176 foreach my $page (@pages) {
177 check_canremove($page, $q, $session);
179 # This untaint is safe because of the
180 # checks performed above, which verify the
181 # page is a normal file, etc.
182 push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
185 # Do removal, and update the wiki.
186 require IkiWiki::Render;
188 IkiWiki::disable_commit_hook();
189 foreach my $file (@files) {
190 IkiWiki::rcs_remove($file);
192 IkiWiki::rcs_commit_staged(gettext("removed"),
193 $session->param("name"), $ENV{REMOTE_ADDR});
194 IkiWiki::enable_commit_hook();
195 IkiWiki::rcs_update();
198 foreach my $file (@files) {
199 IkiWiki::prune("$config{srcdir}/$file");
203 IkiWiki::saveindex();
205 if ($q->param("attachment")) {
206 # Attachments were deleted, so redirect
207 # back to the edit form.
208 postremove($session);
211 # The page is gone, so redirect to parent
213 my $parent=IkiWiki::dirname($pages[0]);
214 if (! exists $pagesources{$parent}) {
217 IkiWiki::redirect($q, urlto($parent, '/', 1));
221 removal_confirm($q, $session, 0, $q->param("page"));