]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/remove.pm
Options set in the setup file are now immediatly loaded by ikiwiki -setup. This allow...
[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;
28         my $attachment=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         # Must be editiable.
46         IkiWiki::check_canedit($page, $q, $session);
48         # This is sorta overkill, but better safe
49         # than sorry. If a user can't upload an
50         # attachment, don't let them delete it.
51         if ($attachment) {
52                 IkiWiki::Plugin::attachment::check_canattach($session, $page, $file);
53         }
54 } #}}}
56 sub formbuilder_setup (@) { #{{{
57         my %params=@_;
58         my $form=$params{form};
59         my $q=$params{cgi};
61         if (defined $form->field("do") && $form->field("do") eq "edit") {
62                 # Removal button for the page, and also for attachments.
63                 push @{$params{buttons}}, "Remove";
64                 $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
65         }
66 } #}}}
68 sub confirmation_form ($$) { #{{{ 
69         my $q=shift;
70         my $session=shift;
72         eval q{use CGI::FormBuilder};
73         error($@) if $@;
74         my $f = CGI::FormBuilder->new(
75                 name => "remove",
76                 header => 0,
77                 charset => "utf-8",
78                 method => 'POST',
79                 javascript => 0,
80                 params => $q,
81                 action => $config{cgiurl},
82                 stylesheet => IkiWiki::baseurl()."style.css",
83                 fields => [qw{do page}],
84         );
85         
86         $f->field(name => "do", type => "hidden", value => "remove", force => 1);
88         return $f, ["Remove", "Cancel"];
89 } #}}}
91 sub removal_confirm ($$@) { #{{{
92         my $q=shift;
93         my $session=shift;
94         my $attachment=shift;
95         my @pages=@_;
97         check_canremove($_, $q, $session, $attachment) foreach @pages;
99         # Save current form state to allow returning to it later
100         # without losing any edits.
101         # (But don't save what button was submitted, to avoid
102         # looping back to here.)
103         # Note: "_submit" is CGI::FormBuilder internals.
104         $q->param(-name => "_submit", -value => "");
105         $session->param(postremove => scalar $q->Vars);
106         IkiWiki::cgi_savesession($session);
107         
108         my ($f, $buttons)=confirmation_form($q, $session);
109         $f->title(sprintf(gettext("confirm removal of %s"),
110                 join(", ", map { IkiWiki::pagetitle($_) } @pages)));
111         $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
112         if (defined $attachment) {
113                 $f->field(name => "attachment", type => "hidden",
114                         value => $attachment, force => 1);
115         }
117         IkiWiki::showform($f, $buttons, $session, $q);
118         exit 0;
119 } #}}}
121 sub postremove ($) { #{{{
122         my $session=shift;
124         # Load saved form state and return to edit form.
125         my $postremove=CGI->new($session->param("postremove"));
126         $session->clear("postremove");
127         IkiWiki::cgi_savesession($session);
128         IkiWiki::cgi($postremove, $session);
129 } #}}}
131 sub formbuilder (@) { #{{{
132         my %params=@_;
133         my $form=$params{form};
135         if (defined $form->field("do") && $form->field("do") eq "edit") {
136                 my $q=$params{cgi};
137                 my $session=$params{session};
139                 if ($form->submitted eq "Remove") {
140                         removal_confirm($q, $session, 0, $form->field("page"));
141                 }
142                 elsif ($form->submitted eq "Remove Attachments") {
143                         my @selected=$q->param("attachment_select");
144                         if (! @selected) {
145                                 error(gettext("Please select the attachments to remove."));
146                         }
147                         removal_confirm($q, $session, 1, @selected);
148                 }
149         }
150 } #}}}
152 sub sessioncgi ($$) { #{{{
153         my $q=shift;
155         if ($q->param("do") eq 'remove') {
156                 my $session=shift;
157                 my ($form, $buttons)=confirmation_form($q, $session);
158                 IkiWiki::decode_form_utf8($form);
160                 if ($form->submitted eq 'Cancel') {
161                         postremove($session);
162                 }
163                 elsif ($form->submitted eq 'Remove' && $form->validate) {
164                         my @pages=$q->param("page");
165         
166                         # Validate removal by checking that the page exists,
167                         # and that the user is allowed to edit(/remove) it.
168                         my @files;
169                         foreach my $page (@pages) {
170                                 check_canremove($page, $q, $session, $q->param("attachment"));
171                                 
172                                 # This untaint is safe because of the
173                                 # checks performed above, which verify the
174                                 # page is a normal file, etc.
175                                 push @files, IkiWiki::possibly_foolish_untaint($pagesources{$page});
176                         }
178                         # Do removal, and update the wiki.
179                         require IkiWiki::Render;
180                         if ($config{rcs}) {
181                                 IkiWiki::disable_commit_hook();
182                                 foreach my $file (@files) {
183                                         IkiWiki::rcs_remove($file);
184                                 }
185                                 IkiWiki::rcs_commit_staged(gettext("removed"),
186                                         $session->param("name"), $ENV{REMOTE_ADDR});
187                                 IkiWiki::enable_commit_hook();
188                                 IkiWiki::rcs_update();
189                         }
190                         else {
191                                 foreach my $file (@files) {
192                                         IkiWiki::prune("$config{srcdir}/$file");
193                                 }
194                         }
195                         IkiWiki::refresh();
196                         IkiWiki::saveindex();
198                         if ($q->param("attachment")) {
199                                 # Attachments were deleted, so redirect
200                                 # back to the edit form.
201                                 postremove($session);
202                         }
203                         else {
204                                 # The page is gone, so redirect to parent
205                                 # of the page.
206                                 my $parent=IkiWiki::dirname($pages[0]);
207                                 if (! exists $pagesources{$parent}) {
208                                         $parent="index";
209                                 }
210                                 IkiWiki::redirect($q, $config{url}."/".htmlpage($parent));
211                         }
212                 }
213                 else {
214                         IkiWiki::showform($form, $buttons, $session, $q);
215                 }
217                 exit 0;
218         }