]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/remove.pm
tag
[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 2.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                 },
22 } #}}}
24 sub check_canremove ($$$) { #{{{
25         my $page=shift;
26         my $q=shift;
27         my $session=shift;
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)));
33         }
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));
39         }
40         elsif (-l "$config{srcdir}/$file" && ! -f _) {
41                 error(sprintf(gettext("%s is not a file"), $file));
42         }
43         
44         # Must be editiable.
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);
52                 }
53                 else {
54                         error("renaming of attachments is not allowed");
55                 }
56         }
57 } #}}}
59 sub formbuilder_setup (@) { #{{{
60         my %params=@_;
61         my $form=$params{form};
62         my $q=$params{cgi};
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" />');
69         }
70 } #}}}
72 sub confirmation_form ($$) { #{{{ 
73         my $q=shift;
74         my $session=shift;
76         eval q{use CGI::FormBuilder};
77         error($@) if $@;
78         my $f = CGI::FormBuilder->new(
79                 name => "remove",
80                 header => 0,
81                 charset => "utf-8",
82                 method => 'POST',
83                 javascript => 0,
84                 params => $q,
85                 action => $config{cgiurl},
86                 stylesheet => IkiWiki::baseurl()."style.css",
87                 fields => [qw{do page}],
88         );
89         
90         $f->field(name => "do", type => "hidden", value => "remove", force => 1);
92         return $f, ["Remove", "Cancel"];
93 } #}}}
95 sub removal_confirm ($$@) { #{{{
96         my $q=shift;
97         my $session=shift;
98         my $attachment=shift;
99         my @pages=@_;
101         check_canremove($_, $q, $session) foreach @pages;
103         # Save current form state to allow returning to it later
104         # without losing any edits.
105         # (But don't save what button was submitted, to avoid
106         # looping back to here.)
107         # Note: "_submit" is CGI::FormBuilder internals.
108         $q->param(-name => "_submit", -value => "");
109         $session->param(postremove => scalar $q->Vars);
110         IkiWiki::cgi_savesession($session);
111         
112         my ($f, $buttons)=confirmation_form($q, $session);
113         $f->title(sprintf(gettext("confirm removal of %s"),
114                 join(", ", map { pagetitle($_) } @pages)));
115         $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
116         if (defined $attachment) {
117                 $f->field(name => "attachment", type => "hidden",
118                         value => $attachment, force => 1);
119         }
121         IkiWiki::showform($f, $buttons, $session, $q);
122         exit 0;
123 } #}}}
125 sub postremove ($) { #{{{
126         my $session=shift;
128         # Load saved form state and return to edit form.
129         my $postremove=CGI->new($session->param("postremove"));
130         $session->clear("postremove");
131         IkiWiki::cgi_savesession($session);
132         IkiWiki::cgi($postremove, $session);
133 } #}}}
135 sub formbuilder (@) { #{{{
136         my %params=@_;
137         my $form=$params{form};
139         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
140             $form->field("do") eq "create")) {
141                 my $q=$params{cgi};
142                 my $session=$params{session};
144                 if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
145                         removal_confirm($q, $session, 0, $form->field("page"));
146                 }
147                 elsif ($form->submitted eq "Remove Attachments") {
148                         my @selected=$q->param("attachment_select");
149                         if (! @selected) {
150                                 error(gettext("Please select the attachments to remove."));
151                         }
152                         removal_confirm($q, $session, 1, @selected);
153                 }
154         }
155 } #}}}
157 sub sessioncgi ($$) { #{{{
158         my $q=shift;
160         if ($q->param("do") eq 'remove') {
161                 my $session=shift;
162                 my ($form, $buttons)=confirmation_form($q, $session);
163                 IkiWiki::decode_form_utf8($form);
165                 if ($form->submitted eq 'Cancel') {
166                         postremove($session);
167                 }
168                 elsif ($form->submitted eq 'Remove' && $form->validate) {
169                         my @pages=$q->param("page");
170         
171                         # Validate removal by checking that the page exists,
172                         # and that the user is allowed to edit(/remove) it.
173                         my @files;
174                         foreach my $page (@pages) {
175                                 check_canremove($page, $q, $session);
176                                 
177                                 # This untaint is safe because of the
178                                 # checks performed above, which verify the
179                                 # page is a normal file, etc.
180                                 push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
181                         }
183                         # Do removal, and update the wiki.
184                         require IkiWiki::Render;
185                         if ($config{rcs}) {
186                                 IkiWiki::disable_commit_hook();
187                                 foreach my $file (@files) {
188                                         IkiWiki::rcs_remove($file);
189                                 }
190                                 IkiWiki::rcs_commit_staged(gettext("removed"),
191                                         $session->param("name"), $ENV{REMOTE_ADDR});
192                                 IkiWiki::enable_commit_hook();
193                                 IkiWiki::rcs_update();
194                         }
195                         else {
196                                 foreach my $file (@files) {
197                                         IkiWiki::prune("$config{srcdir}/$file");
198                                 }
199                         }
200                         IkiWiki::refresh();
201                         IkiWiki::saveindex();
203                         if ($q->param("attachment")) {
204                                 # Attachments were deleted, so redirect
205                                 # back to the edit form.
206                                 postremove($session);
207                         }
208                         else {
209                                 # The page is gone, so redirect to parent
210                                 # of the page.
211                                 my $parent=IkiWiki::dirname($pages[0]);
212                                 if (! exists $pagesources{$parent}) {
213                                         $parent="index";
214                                 }
215                                 IkiWiki::redirect($q, urlto($parent, '/', 1));
216                         }
217                 }
218                 else {
219                         IkiWiki::showform($form, $buttons, $session, $q);
220                 }
222                 exit 0;
223         }