2 package IkiWiki::Plugin::remove;
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);
16 sub getsetup () { #{{{
24 sub check_canremove ($$$$) { #{{{
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)));
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));
41 elsif (-l "$config{srcdir}/$file" && ! -f _) {
42 error(sprintf(gettext("%s is not a file"), $file));
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.
52 IkiWiki::Plugin::attachment::check_canattach($session, $page, $file);
56 sub formbuilder_setup (@) { #{{{
58 my $form=$params{form};
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" />');
68 sub confirmation_form ($$) { #{{{
72 eval q{use CGI::FormBuilder};
74 my $f = CGI::FormBuilder->new(
81 action => $config{cgiurl},
82 stylesheet => IkiWiki::baseurl()."style.css",
83 fields => [qw{do page}],
86 $f->field(name => "do", type => "hidden", value => "remove", force => 1);
88 return $f, ["Remove", "Cancel"];
91 sub removal_confirm ($$@) { #{{{
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);
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);
117 IkiWiki::showform($f, $buttons, $session, $q);
121 sub postremove ($) { #{{{
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);
131 sub formbuilder (@) { #{{{
133 my $form=$params{form};
135 if (defined $form->field("do") && $form->field("do") eq "edit") {
137 my $session=$params{session};
139 if ($form->submitted eq "Remove") {
140 removal_confirm($q, $session, 0, $form->field("page"));
142 elsif ($form->submitted eq "Remove Attachments") {
143 my @selected=$q->param("attachment_select");
145 error(gettext("Please select the attachments to remove."));
147 removal_confirm($q, $session, 1, @selected);
152 sub sessioncgi ($$) { #{{{
155 if ($q->param("do") eq 'remove') {
157 my ($form, $buttons)=confirmation_form($q, $session);
158 IkiWiki::decode_form_utf8($form);
160 if ($form->submitted eq 'Cancel') {
161 postremove($session);
163 elsif ($form->submitted eq 'Remove' && $form->validate) {
164 my @pages=$q->param("page");
166 # Validate removal by checking that the page exists,
167 # and that the user is allowed to edit(/remove) it.
169 foreach my $page (@pages) {
170 check_canremove($page, $q, $session, $q->param("attachment"));
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});
178 # Do removal, and update the wiki.
179 require IkiWiki::Render;
181 IkiWiki::disable_commit_hook();
182 foreach my $file (@files) {
183 IkiWiki::rcs_remove($file);
185 IkiWiki::rcs_commit_staged(gettext("removed"),
186 $session->param("name"), $ENV{REMOTE_ADDR});
187 IkiWiki::enable_commit_hook();
188 IkiWiki::rcs_update();
191 foreach my $file (@files) {
192 IkiWiki::prune("$config{srcdir}/$file");
196 IkiWiki::saveindex();
198 if ($q->param("attachment")) {
199 # Attachments were deleted, so redirect
200 # back to the edit form.
201 postremove($session);
204 # The page is gone, so redirect to parent
206 my $parent=IkiWiki::dirname($pages[0]);
207 if (! exists $pagesources{$parent}) {
210 IkiWiki::redirect($q, $config{url}."/".htmlpage($parent));
214 IkiWiki::showform($form, $buttons, $session, $q);