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 formbuilder_setup (@) { #{{{
17 my $form=$params{form};
20 if (defined $form->field("do") && $form->field("do") eq "edit") {
21 # Removal button for the page, and also for attachments.
22 push @{$params{buttons}}, "Remove";
23 $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
27 sub confirmation_form ($$) { #{{{
31 eval q{use CGI::FormBuilder};
33 my $f = CGI::FormBuilder->new(
40 action => $config{cgiurl},
41 stylesheet => IkiWiki::baseurl()."style.css",
42 fields => [qw{do page}],
45 $f->field(name => "do", type => "hidden", value => "remove", force => 1);
47 return $f, ["Remove", "Cancel"];
50 sub removal_confirm ($$@) {
56 # Save current form state to allow returning to it later
57 # without losing any edits.
58 # (But don't save what button was submitted, to avoid
59 # looping back to here.)
60 # Note: "_submit" is CGI::FormBuilder internals.
61 $q->param(-name => "_submit", -value => "");
62 $session->param(postremove => scalar $q->Vars);
63 IkiWiki::cgi_savesession($session);
65 my ($f, $buttons)=confirmation_form($q, $session);
66 $f->title(sprintf(gettext("confirm removal of %s"),
67 join(", ", map { IkiWiki::pagetitle($_) } @pages)));
68 $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
69 if (defined $attachment) {
70 $f->field(name => "attachment", type => "hidden",
71 value => $attachment, force => 1);
74 IkiWiki::showform($f, $buttons, $session, $q);
81 # Load saved form state and return to edit form.
82 my $postremove=CGI->new($session->param("postremove"));
83 $session->clear("postremove");
84 IkiWiki::cgi_savesession($session);
85 IkiWiki::cgi($postremove, $session);
88 sub formbuilder (@) { #{{{
90 my $form=$params{form};
92 if (defined $form->field("do") && $form->field("do") eq "edit") {
94 my $session=$params{session};
96 if ($form->submitted eq "Remove") {
97 removal_confirm($q, $session, 0, $form->field("page"));
99 elsif ($form->submitted eq "Remove Attachments") {
100 my @selected=$q->param("attachment_select");
102 error(gettext("Please select the attachments to remove."));
104 removal_confirm($q, $session, 1, @selected);
109 sub sessioncgi ($$) { #{{{
112 if ($q->param("do") eq 'remove') {
114 my ($form, $buttons)=confirmation_form($q, $session);
115 IkiWiki::decode_form_utf8($form);
117 if ($form->submitted eq 'Cancel') {
118 postremove($session);
120 elsif ($form->submitted eq 'Remove' && $form->validate) {
121 my @pages=$q->param("page");
123 # Validate removal by checking that the page exists,
124 # and that the user is allowed to edit(/remove) it.
126 foreach my $page (@pages) {
127 # Must be a known source file.
128 if (! exists $pagesources{$page}) {
129 error(sprintf(gettext("%s does not exist"),
130 htmllink("", "", $page, noimageinline => 1)));
133 # Must exist on disk, and be a regular file.
134 my $file=$pagesources{$page};
135 if (! -e "$config{srcdir}/$file") {
136 error(sprintf(gettext("%s is not in the srcdir, so it cannot be deleted"), $file));
138 elsif (-l "$config{srcdir}/$file" && ! -f _) {
139 error(sprintf(gettext("%s is not a file"), $file));
143 IkiWiki::check_canedit($page, $q, $session);
145 # This is sorta overkill, but better safe
146 # than sorry. If a user can't upload an
147 # attachment, don't let them delete it.
148 if ($q->param("attachment")) {
149 IkiWiki::Plugin::attachment::check_canattach($session, $page, $file);
152 push @files, IkiWiki::possibly_foolish_untaint($file);
155 # Do removal, and update the wiki.
156 require IkiWiki::Render;
158 IkiWiki::disable_commit_hook();
159 foreach my $file (@files) {
160 my $token=IkiWiki::rcs_prepedit($file);
161 IkiWiki::rcs_remove($file);
162 IkiWiki::rcs_commit($file, gettext("removed"),
163 $token, $session->param("name"), $ENV{REMOTE_ADDR});
165 IkiWiki::enable_commit_hook();
166 IkiWiki::rcs_update();
169 foreach my $file (@files) {
170 IkiWiki::prune("$config{srcdir}/$file");
174 IkiWiki::saveindex();
176 if ($q->param("attachment")) {
177 # Attachments were deleted, so redirect
178 # back to the edit form.
179 postremove($session);
182 # The page is gone, so redirect to parent
184 my $parent=IkiWiki::dirname($pages[0]);
185 if (! exists $pagesources{$parent}) {
188 IkiWiki::redirect($q, $config{url}."/".htmlpage($parent));
192 IkiWiki::showform($form, $buttons, $session, $q);