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
);
25 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));
45 # If a user can't upload an attachment, don't let them delete it.
46 # This is sorta overkill, but better safe than sorry.
47 if (! defined pagetype
($pagesources{$page})) {
48 if (IkiWiki
::Plugin
::attachment
->can("check_canattach")) {
49 IkiWiki
::Plugin
::attachment
::check_canattach
($session, $page, "$config{srcdir}/$file");
52 error
("removal of attachments is not allowed");
57 IkiWiki
::run_hooks
(canremove
=> sub {
58 return if defined $canremove;
59 my $ret=shift->(page
=> $page, cgi
=> $q, session
=> $session);
64 elsif (ref $ret eq 'CODE') {
68 elsif (defined $ret) {
74 return defined $canremove ?
$canremove : 1;
77 sub formbuilder_setup
(@
) {
79 my $form=$params{form
};
82 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
83 $form->field("do") eq "create")) {
84 # Removal button for the page, and also for attachments.
85 push @
{$params{buttons
}}, "Remove" if $form->field("do") eq "edit";
86 $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
90 sub confirmation_form
($$) {
94 eval q{use CGI::FormBuilder};
96 my $f = CGI
::FormBuilder
->new(
103 action
=> $config{cgiurl
},
105 fields
=> [qw{do page
}],
108 $f->field(name
=> "sid", type
=> "hidden", value
=> $session->id,
110 $f->field(name
=> "do", type
=> "hidden", value
=> "remove", force
=> 1);
112 return $f, ["Remove", "Cancel"];
115 sub removal_confirm
($$@
) {
118 my $attachment=shift;
121 foreach my $page (@pages) {
122 IkiWiki
::check_canedit
($page, $q, $session);
123 check_canremove
($page, $q, $session);
126 # Save current form state to allow returning to it later
127 # without losing any edits.
128 # (But don't save what button was submitted, to avoid
129 # looping back to here.)
130 # Note: "_submit" is CGI::FormBuilder internals.
131 $q->param(-name
=> "_submit", -value
=> "");
132 $session->param(postremove
=> scalar $q->Vars);
133 IkiWiki
::cgi_savesession
($session);
135 my ($f, $buttons)=confirmation_form
($q, $session);
136 $f->title(sprintf(gettext
("confirm removal of %s"),
137 join(", ", map { pagetitle
($_) } @pages)));
138 $f->field(name
=> "page", type
=> "hidden", value
=> \
@pages, force
=> 1);
139 if (defined $attachment) {
140 $f->field(name
=> "attachment", type
=> "hidden",
141 value
=> $attachment, force
=> 1);
144 IkiWiki
::showform
($f, $buttons, $session, $q);
151 # Load saved form state and return to edit form.
152 my $postremove=CGI
->new($session->param("postremove"));
153 $session->clear("postremove");
154 IkiWiki
::cgi_savesession
($session);
155 IkiWiki
::cgi
($postremove, $session);
158 sub formbuilder
(@
) {
160 my $form=$params{form
};
162 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
163 $form->field("do") eq "create")) {
165 my $session=$params{session
};
167 if ($form->submitted eq "Remove" && $form->field("do") eq "edit") {
168 removal_confirm
($q, $session, 0, $form->field("page"));
170 elsif ($form->submitted eq "Remove Attachments") {
171 my @selected=map { Encode
::decode_utf8
($_) } $q->param("attachment_select");
173 error
(gettext
("Please select the attachments to remove."));
175 removal_confirm
($q, $session, 1, @selected);
180 sub sessioncgi
($$) {
183 if ($q->param("do") eq 'remove') {
185 my ($form, $buttons)=confirmation_form
($q, $session);
186 IkiWiki
::decode_form_utf8
($form);
188 if ($form->submitted eq 'Cancel') {
189 postremove
($session);
191 elsif ($form->submitted eq 'Remove' && $form->validate) {
192 IkiWiki
::checksessionexpiry
($q, $session, $q->param('sid'));
194 my @pages=$form->field("page");
196 # Validate removal by checking that the page exists,
197 # and that the user is allowed to edit(/remove) it.
199 foreach my $page (@pages) {
200 IkiWiki
::check_canedit
($page, $q, $session);
201 check_canremove
($page, $q, $session);
203 # This untaint is safe because of the
204 # checks performed above, which verify the
205 # page is a normal file, etc.
206 push @files, IkiWiki
::possibly_foolish_untaint
($pagesources{$page});
209 # Do removal, and update the wiki.
210 require IkiWiki
::Render
;
212 IkiWiki
::disable_commit_hook
();
213 foreach my $file (@files) {
214 IkiWiki
::rcs_remove
($file);
216 IkiWiki
::rcs_commit_staged
(
217 message
=> gettext
("removed"),
220 IkiWiki
::enable_commit_hook
();
221 IkiWiki
::rcs_update
();
224 foreach my $file (@files) {
225 IkiWiki
::prune
("$config{srcdir}/$file");
229 IkiWiki
::saveindex
();
231 if ($q->param("attachment")) {
232 # Attachments were deleted, so redirect
233 # back to the edit form.
234 postremove
($session);
237 # The page is gone, so redirect to parent
239 my $parent=IkiWiki
::dirname
($pages[0]);
240 if (! exists $pagesources{$parent}) {
243 IkiWiki
::redirect
($q, urlto
($parent, '/', 1));
247 removal_confirm
($q, $session, 0, $form->field("page"));