]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/remove.pm
add rcs_commit_staged and rcs_rename
[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 => "formbuilder_setup", id => "remove", call => \&formbuilder_setup);
10         hook(type => "formbuilder", id => "remove", call => \&formbuilder);
11         hook(type => "sessioncgi", id => "remove", call => \&sessioncgi);
13 } # }}}
15 sub check_canremove ($$$$) {
16         my $page=shift;
17         my $q=shift;
18         my $session=shift;
19         my $attachment=shift;
21         # Must be a known source file.
22         if (! exists $pagesources{$page}) {
23                 error(sprintf(gettext("%s does not exist"),
24                         htmllink("", "", $page, noimageinline => 1)));
25         }
27         # Must exist on disk, and be a regular file.
28         my $file=$pagesources{$page};
29         if (! -e "$config{srcdir}/$file") {
30                 error(sprintf(gettext("%s is not in the srcdir, so it cannot be deleted"), $file));
31         }
32         elsif (-l "$config{srcdir}/$file" && ! -f _) {
33                 error(sprintf(gettext("%s is not a file"), $file));
34         }
35         
36         # Must be editiable.
37         IkiWiki::check_canedit($page, $q, $session);
39         # This is sorta overkill, but better safe
40         # than sorry. If a user can't upload an
41         # attachment, don't let them delete it.
42         if ($attachment) {
43                 IkiWiki::Plugin::attachment::check_canattach($session, $page, $file);
44         }
45 }
47 sub formbuilder_setup (@) { #{{{
48         my %params=@_;
49         my $form=$params{form};
50         my $q=$params{cgi};
52         if (defined $form->field("do") && $form->field("do") eq "edit") {
53                 # Removal button for the page, and also for attachments.
54                 push @{$params{buttons}}, "Remove";
55                 $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
56         }
57 } #}}}
59 sub confirmation_form ($$) { #{{{ 
60         my $q=shift;
61         my $session=shift;
63         eval q{use CGI::FormBuilder};
64         error($@) if $@;
65         my $f = CGI::FormBuilder->new(
66                 name => "remove",
67                 header => 0,
68                 charset => "utf-8",
69                 method => 'POST',
70                 javascript => 0,
71                 params => $q,
72                 action => $config{cgiurl},
73                 stylesheet => IkiWiki::baseurl()."style.css",
74                 fields => [qw{do page}],
75         );
76         
77         $f->field(name => "do", type => "hidden", value => "remove", force => 1);
79         return $f, ["Remove", "Cancel"];
80 } #}}}
82 sub removal_confirm ($$@) {
83         my $q=shift;
84         my $session=shift;
85         my $attachment=shift;
86         my @pages=@_;
88         check_canremove($_, $q, $session, $attachment) foreach @pages;
90         # Save current form state to allow returning to it later
91         # without losing any edits.
92         # (But don't save what button was submitted, to avoid
93         # looping back to here.)
94         # Note: "_submit" is CGI::FormBuilder internals.
95         $q->param(-name => "_submit", -value => "");
96         $session->param(postremove => scalar $q->Vars);
97         IkiWiki::cgi_savesession($session);
98         
99         my ($f, $buttons)=confirmation_form($q, $session);
100         $f->title(sprintf(gettext("confirm removal of %s"),
101                 join(", ", map { IkiWiki::pagetitle($_) } @pages)));
102         $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
103         if (defined $attachment) {
104                 $f->field(name => "attachment", type => "hidden",
105                         value => $attachment, force => 1);
106         }
108         IkiWiki::showform($f, $buttons, $session, $q);
109         exit 0;
112 sub postremove ($) {
113         my $session=shift;
115         # Load saved form state and return to edit form.
116         my $postremove=CGI->new($session->param("postremove"));
117         $session->clear("postremove");
118         IkiWiki::cgi_savesession($session);
119         IkiWiki::cgi($postremove, $session);
122 sub formbuilder (@) { #{{{
123         my %params=@_;
124         my $form=$params{form};
126         if (defined $form->field("do") && $form->field("do") eq "edit") {
127                 my $q=$params{cgi};
128                 my $session=$params{session};
130                 if ($form->submitted eq "Remove") {
131                         removal_confirm($q, $session, 0, $form->field("page"));
132                 }
133                 elsif ($form->submitted eq "Remove Attachments") {
134                         my @selected=$q->param("attachment_select");
135                         if (! @selected) {
136                                 error(gettext("Please select the attachments to remove."));
137                         }
138                         removal_confirm($q, $session, 1, @selected);
139                 }
140         }
141 } #}}}
143 sub sessioncgi ($$) { #{{{
144         my $q=shift;
146         if ($q->param("do") eq 'remove') {
147                 my $session=shift;
148                 my ($form, $buttons)=confirmation_form($q, $session);
149                 IkiWiki::decode_form_utf8($form);
151                 if ($form->submitted eq 'Cancel') {
152                         postremove($session);
153                 }
154                 elsif ($form->submitted eq 'Remove' && $form->validate) {
155                         my @pages=$q->param("page");
156         
157                         # Validate removal by checking that the page exists,
158                         # and that the user is allowed to edit(/remove) it.
159                         my @files;
160                         foreach my $page (@pages) {
161                                 check_canremove($page, $q, $session, $q->param("attachment"));
162                                 
163                                 # This untaint is safe because of the
164                                 # checks performed above, which verify the
165                                 # page is a normal file, etc.
166                                 push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
167                         }
169                         # Do removal, and update the wiki.
170                         require IkiWiki::Render;
171                         if ($config{rcs}) {
172                                 IkiWiki::disable_commit_hook();
173                                 foreach my $file (@files) {
174                                         IkiWiki::rcs_remove($file);
175                                 }
176                                 IkiWiki::rcs_commit_staged(gettext("removed"),
177                                         $session->param("name"), $ENV{REMOTE_ADDR});
178                                 IkiWiki::enable_commit_hook();
179                                 IkiWiki::rcs_update();
180                         }
181                         else {
182                                 foreach my $file (@files) {
183                                         IkiWiki::prune("$config{srcdir}/$file");
184                                 }
185                         }
186                         IkiWiki::refresh();
187                         IkiWiki::saveindex();
189                         if ($q->param("attachment")) {
190                                 # Attachments were deleted, so redirect
191                                 # back to the edit form.
192                                 postremove($session);
193                         }
194                         else {
195                                 # The page is gone, so redirect to parent
196                                 # of the page.
197                                 my $parent=IkiWiki::dirname($pages[0]);
198                                 if (! exists $pagesources{$parent}) {
199                                         $parent="index";
200                                 }
201                                 IkiWiki::redirect($q, $config{url}."/".htmlpage($parent));
202                         }
203                 }
204                 else {
205                         IkiWiki::showform($form, $buttons, $session, $q);
206                 }
208                 exit 0;
209         }