b94447020de8f5f00cdb81776546a52a6ca00499
[ikiwiki.git] / IkiWiki / Plugin / remove.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::remove;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 sub import {
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);
13
14 }
15
16 sub getsetup () {
17         return 
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                         section => "web",
22                 },
23 }
24
25 sub check_canremove ($$$) {
26         my $page=shift;
27         my $q=shift;
28         my $session=shift;
29
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)));
34         }
35
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));
40         }
41         elsif (-l "$config{srcdir}/$file" && ! -f _) {
42                 error(sprintf(gettext("%s is not a file"), $file));
43         }
44         
45         # If a user can't upload an attachment, don't let them delete it.
46         # This is sorta overkill, but better safe than sorry.
47         if (! defined pagetype($pagesources{$page})) {
48                 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
49                         IkiWiki::Plugin::attachment::check_canattach($session, $page, "$config{srcdir}/$file");
50                 }
51                 else {
52                         error("removal of attachments is not allowed");
53                 }
54         }
55
56         my $canremove;
57         IkiWiki::run_hooks(canremove => sub {
58                 return if defined $canremove;
59                 my $ret=shift->(page => $page, cgi => $q, session => $session);
60                 if (defined $ret) {
61                         if ($ret eq "") {
62                                 $canremove=1;
63                         }
64                         elsif (ref $ret eq 'CODE') {
65                                 $ret->();
66                                 $canremove=0;
67                         }
68                         elsif (defined $ret) {
69                                 error($ret);
70                                 $canremove=0;
71                         }
72                 }
73         });
74         return defined $canremove ? $canremove : 1;
75 }
76
77 sub formbuilder_setup (@) {
78         my %params=@_;
79         my $form=$params{form};
80         my $q=$params{cgi};
81
82         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
83             $form->field("do") eq "create")) {
84                 # Removal button for the page, and also for attachments.
85                 push @{$params{buttons}}, "Remove" if $form->field("do") eq "edit";
86                 $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
87         }
88 }
89
90 sub confirmation_form ($$) {
91         my $q=shift;
92         my $session=shift;
93
94         eval q{use CGI::FormBuilder};
95         error($@) if $@;
96         my $f = CGI::FormBuilder->new(
97                 name => "remove",
98                 header => 0,
99                 charset => "utf-8",
100                 method => 'POST',
101                 javascript => 0,
102                 params => $q,
103                 action => IkiWiki::cgiurl(),
104                 stylesheet => 1,
105                 fields => [qw{do page}],
106         );
107         
108         $f->field(name => "sid", type => "hidden", value => $session->id,
109                 force => 1);
110         $f->field(name => "do", type => "hidden", value => "remove", force => 1);
111
112         return $f, ["Remove", "Cancel"];
113 }
114
115 sub removal_confirm ($$@) {
116         my $q=shift;
117         my $session=shift;
118         my $attachment=shift;
119         my @pages=@_;
120                 
121         # Special case for unsaved attachments.
122         foreach my $page (@pages) {
123                 if (IkiWiki::Plugin::attachment->can("is_held_attachment")) {
124                         my $f=IkiWiki::Plugin::attachment::is_held_attachment($page);
125                         if (defined $f) {
126                                 print STDERR "!! remove $f\n";
127                                 require IkiWiki::Render;
128                                 IkiWiki::prune($f);
129                         }
130                 }
131         }
132         @pages=grep { exists $pagesources{$_} } @pages;
133         return unless @pages;
134
135         foreach my $page (@pages) {
136                 IkiWiki::check_canedit($page, $q, $session);
137                 check_canremove($page, $q, $session);
138         }
139
140         # Save current form state to allow returning to it later
141         # without losing any edits.
142         # (But don't save what button was submitted, to avoid
143         # looping back to here.)
144         # Note: "_submit" is CGI::FormBuilder internals.
145         $q->param(-name => "_submit", -value => "");
146         $session->param(postremove => scalar $q->Vars);
147         IkiWiki::cgi_savesession($session);
148         
149         my ($f, $buttons)=confirmation_form($q, $session);
150         $f->title(sprintf(gettext("confirm removal of %s"),
151                 join(", ", map { pagetitle($_) } @pages)));
152         $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
153         if (defined $attachment) {
154                 $f->field(name => "attachment", type => "hidden",
155                         value => $attachment, force => 1);
156         }
157
158         IkiWiki::showform($f, $buttons, $session, $q);
159         exit 0;
160 }
161
162 sub postremove ($) {
163         my $session=shift;
164
165         # Load saved form state and return to edit form.
166         my $postremove=CGI->new($session->param("postremove"));
167         $session->clear("postremove");
168         IkiWiki::cgi_savesession($session);
169         IkiWiki::cgi($postremove, $session);
170 }
171
172 sub formbuilder (@) {
173         my %params=@_;
174         my $form=$params{form};
175
176         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
177             $form->field("do") eq "create")) {
178                 my $q=$params{cgi};
179                 my $session=$params{session};
180
181                 if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
182                         removal_confirm($q, $session, 0, $form->field("page"));
183                 }
184                 elsif ($form->submitted eq "Remove Attachments") {
185                         my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
186                         if (! @selected) {
187                                 error(gettext("Please select the attachments to remove."));
188                         }
189                         removal_confirm($q, $session, 1, @selected);
190                 }
191         }
192 }
193
194 sub sessioncgi ($$) {
195         my $q=shift;
196
197         if ($q->param("do") eq 'remove') {
198                 my $session=shift;
199                 my ($form, $buttons)=confirmation_form($q, $session);
200                 IkiWiki::decode_form_utf8($form);
201
202                 if ($form->submitted eq 'Cancel') {
203                         postremove($session);
204                 }
205                 elsif ($form->submitted eq 'Remove' && $form->validate) {
206                         IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
207
208                         my @pages=$form->field("page");
209                         
210                         # Validate removal by checking that the page exists,
211                         # and that the user is allowed to edit(/remove) it.
212                         my @files;
213                         foreach my $page (@pages) {
214                                 IkiWiki::check_canedit($page, $q, $session);
215                                 check_canremove($page, $q, $session);
216                                 
217                                 # This untaint is safe because of the
218                                 # checks performed above, which verify the
219                                 # page is a normal file, etc.
220                                 push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
221                         }
222
223                         # Do removal, and update the wiki.
224                         require IkiWiki::Render;
225                         if ($config{rcs}) {
226                                 IkiWiki::disable_commit_hook();
227                                 foreach my $file (@files) {
228                                         IkiWiki::rcs_remove($file);
229                                 }
230                                 IkiWiki::rcs_commit_staged(
231                                         message => gettext("removed"),
232                                         session => $session,
233                                 );
234                                 IkiWiki::enable_commit_hook();
235                                 IkiWiki::rcs_update();
236                         }
237                         else {
238                                 foreach my $file (@files) {
239                                         IkiWiki::prune("$config{srcdir}/$file");
240                                 }
241                         }
242                         IkiWiki::refresh();
243                         IkiWiki::saveindex();
244
245                         if ($q->param("attachment")) {
246                                 # Attachments were deleted, so redirect
247                                 # back to the edit form.
248                                 postremove($session);
249                         }
250                         else {
251                                 # The page is gone, so redirect to parent
252                                 # of the page.
253                                 my $parent=IkiWiki::dirname($pages[0]);
254                                 if (! exists $pagesources{$parent}) {
255                                         $parent="index";
256                                 }
257                                 IkiWiki::redirect($q, urlto($parent));
258                         }
259                 }
260                 else {
261                         removal_confirm($q, $session, 0, $form->field("page"));
262                 }
263
264                 exit 0;
265         }
266 }
267
268 1