2 package IkiWiki::Plugin::remove;
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);
15 sub check_canremove ($$$$) { #{{{
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)));
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));
32 elsif (-l "$config{srcdir}/$file" && ! -f _) {
33 error(sprintf(gettext("%s is not a file"), $file));
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.
43 IkiWiki::Plugin::attachment::check_canattach($session, $page, $file);
47 sub formbuilder_setup (@) { #{{{
49 my $form=$params{form};
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" />');
59 sub confirmation_form ($$) { #{{{
63 eval q{use CGI::FormBuilder};
65 my $f = CGI::FormBuilder->new(
72 action => $config{cgiurl},
73 stylesheet => IkiWiki::baseurl()."style.css",
74 fields => [qw{do page}],
77 $f->field(name => "do", type => "hidden", value => "remove", force => 1);
79 return $f, ["Remove", "Cancel"];
82 sub removal_confirm ($$@) { #{{{
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);
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);
108 IkiWiki::showform($f, $buttons, $session, $q);
112 sub postremove ($) { #{{{
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 (@) { #{{{
124 my $form=$params{form};
126 if (defined $form->field("do") && $form->field("do") eq "edit") {
128 my $session=$params{session};
130 if ($form->submitted eq "Remove") {
131 removal_confirm($q, $session, 0, $form->field("page"));
133 elsif ($form->submitted eq "Remove Attachments") {
134 my @selected=$q->param("attachment_select");
136 error(gettext("Please select the attachments to remove."));
138 removal_confirm($q, $session, 1, @selected);
143 sub sessioncgi ($$) { #{{{
146 if ($q->param("do") eq 'remove') {
148 my ($form, $buttons)=confirmation_form($q, $session);
149 IkiWiki::decode_form_utf8($form);
151 if ($form->submitted eq 'Cancel') {
152 postremove($session);
154 elsif ($form->submitted eq 'Remove' && $form->validate) {
155 my @pages=$q->param("page");
157 # Validate removal by checking that the page exists,
158 # and that the user is allowed to edit(/remove) it.
160 foreach my $page (@pages) {
161 check_canremove($page, $q, $session, $q->param("attachment"));
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});
169 # Do removal, and update the wiki.
170 require IkiWiki::Render;
172 IkiWiki::disable_commit_hook();
173 foreach my $file (@files) {
174 IkiWiki::rcs_remove($file);
176 IkiWiki::rcs_commit_staged(gettext("removed"),
177 $session->param("name"), $ENV{REMOTE_ADDR});
178 IkiWiki::enable_commit_hook();
179 IkiWiki::rcs_update();
182 foreach my $file (@files) {
183 IkiWiki::prune("$config{srcdir}/$file");
187 IkiWiki::saveindex();
189 if ($q->param("attachment")) {
190 # Attachments were deleted, so redirect
191 # back to the edit form.
192 postremove($session);
195 # The page is gone, so redirect to parent
197 my $parent=IkiWiki::dirname($pages[0]);
198 if (! exists $pagesources{$parent}) {
201 IkiWiki::redirect($q, $config{url}."/".htmlpage($parent));
205 IkiWiki::showform($form, $buttons, $session, $q);