]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/remove.pm
4b527993ba96b2c26095352cc613d935eee8416c
[git.ikiwiki.info.git] / IkiWiki / Plugin / remove.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::remove;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
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);
14 }
16 sub getsetup () {
17         return 
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                         section => "web",
22                 },
23 }
25 sub check_canremove ($$$) {
26         my $page=shift;
27         my $q=shift;
28         my $session=shift;
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         }
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         }
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 }
76 sub formbuilder_setup (@) {
77         my %params=@_;
78         my $form=$params{form};
79         my $q=$params{cgi};
81         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
82             $form->field("do") eq "create")) {
83                 # Removal button for the page, and also for attachments.
84                 push @{$params{buttons}}, "Remove" if $form->field("do") eq "edit";
85                 $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
86         }
87 }
89 sub confirmation_form ($$) {
90         my $q=shift;
91         my $session=shift;
93         eval q{use CGI::FormBuilder};
94         error($@) if $@;
95         my $f = CGI::FormBuilder->new(
96                 name => "remove",
97                 header => 0,
98                 charset => "utf-8",
99                 method => 'POST',
100                 javascript => 0,
101                 params => $q,
102                 action => $config{cgiurl},
103                 stylesheet => 1,
104                 fields => [qw{do page}],
105         );
106         
107         $f->field(name => "sid", type => "hidden", value => $session->id,
108                 force => 1);
109         $f->field(name => "do", type => "hidden", value => "remove", force => 1);
111         return $f, ["Remove", "Cancel"];
114 sub removal_confirm ($$@) {
115         my $q=shift;
116         my $session=shift;
117         my $attachment=shift;
118         my @pages=@_;
120         foreach my $page (@pages) {
121                 IkiWiki::check_canedit($page, $q, $session);
122                 check_canremove($page, $q, $session);
123         }
125         # Save current form state to allow returning to it later
126         # without losing any edits.
127         # (But don't save what button was submitted, to avoid
128         # looping back to here.)
129         # Note: "_submit" is CGI::FormBuilder internals.
130         $q->param(-name => "_submit", -value => "");
131         $session->param(postremove => scalar $q->Vars);
132         IkiWiki::cgi_savesession($session);
133         
134         my ($f, $buttons)=confirmation_form($q, $session);
135         $f->title(sprintf(gettext("confirm removal of %s"),
136                 join(", ", map { pagetitle($_) } @pages)));
137         $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
138         if (defined $attachment) {
139                 $f->field(name => "attachment", type => "hidden",
140                         value => $attachment, force => 1);
141         }
143         IkiWiki::showform($f, $buttons, $session, $q);
144         exit 0;
147 sub postremove ($) {
148         my $session=shift;
150         # Load saved form state and return to edit form.
151         my $postremove=CGI->new($session->param("postremove"));
152         $session->clear("postremove");
153         IkiWiki::cgi_savesession($session);
154         IkiWiki::cgi($postremove, $session);
157 sub formbuilder (@) {
158         my %params=@_;
159         my $form=$params{form};
161         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
162             $form->field("do") eq "create")) {
163                 my $q=$params{cgi};
164                 my $session=$params{session};
166                 if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
167                         removal_confirm($q, $session, 0, $form->field("page"));
168                 }
169                 elsif ($form->submitted eq "Remove Attachments") {
170                         my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
171                         if (! @selected) {
172                                 error(gettext("Please select the attachments to remove."));
173                         }
174                         removal_confirm($q, $session, 1, @selected);
175                 }
176         }
179 sub sessioncgi ($$) {
180         my $q=shift;
182         if ($q->param("do") eq 'remove') {
183                 my $session=shift;
184                 my ($form, $buttons)=confirmation_form($q, $session);
185                 IkiWiki::decode_form_utf8($form);
187                 if ($form->submitted eq 'Cancel') {
188                         postremove($session);
189                 }
190                 elsif ($form->submitted eq 'Remove' && $form->validate) {
191                         IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
193                         my @pages=$form->field("page");
194         
195                         # Validate removal by checking that the page exists,
196                         # and that the user is allowed to edit(/remove) it.
197                         my @files;
198                         foreach my $page (@pages) {
199                                 IkiWiki::check_canedit($page, $q, $session);
200                                 check_canremove($page, $q, $session);
201                                 
202                                 # This untaint is safe because of the
203                                 # checks performed above, which verify the
204                                 # page is a normal file, etc.
205                                 push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
206                         }
208                         # Do removal, and update the wiki.
209                         require IkiWiki::Render;
210                         if ($config{rcs}) {
211                                 IkiWiki::disable_commit_hook();
212                                 foreach my $file (@files) {
213                                         IkiWiki::rcs_remove($file);
214                                 }
215                                 IkiWiki::rcs_commit_staged(
216                                         message => gettext("removed"),
217                                         session => $session,
218                                 );
219                                 IkiWiki::enable_commit_hook();
220                                 IkiWiki::rcs_update();
221                         }
222                         else {
223                                 foreach my $file (@files) {
224                                         IkiWiki::prune("$config{srcdir}/$file");
225                                 }
226                         }
227                         IkiWiki::refresh();
228                         IkiWiki::saveindex();
230                         if ($q->param("attachment")) {
231                                 # Attachments were deleted, so redirect
232                                 # back to the edit form.
233                                 postremove($session);
234                         }
235                         else {
236                                 # The page is gone, so redirect to parent
237                                 # of the page.
238                                 my $parent=IkiWiki::dirname($pages[0]);
239                                 if (! exists $pagesources{$parent}) {
240                                         $parent="index";
241                                 }
242                                 IkiWiki::redirect($q, urlto($parent, '/', 1));
243                         }
244                 }
245                 else {
246                         removal_confirm($q, $session, 0, $form->field("page"));
247                 }
249                 exit 0;
250         }