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 ($$$) { #{{{
32 eval q{use CGI::FormBuilder};
34 my @fields=qw(do page);
35 my $f = CGI::FormBuilder->new(
36 title => sprintf(gettext("confirm removal of %s"),
37 IkiWiki::pagetitle($page)),
44 action => $config{cgiurl},
45 stylesheet => IkiWiki::baseurl()."style.css",
49 $f->field(name => "do", type => "hidden", value => "remove", force => 1);
50 $f->field(name => "page", type => "hidden", value => $page, force => 1);
52 return $f, ["Remove", "Cancel"];
55 sub formbuilder (@) { #{{{
57 my $form=$params{form};
59 if (defined $form->field("do") && $form->field("do") eq "edit") {
60 if ($form->submitted eq "Remove") {
62 my $session=$params{session};
64 # Save current form state to allow returning to it later
65 # without losing any edits.
66 # (But don't save what button was submitted, to avoid
67 # looping back to here.)
68 # Note: "_submit" is CGI::FormBuilder internals.
69 $q->param(-name => "_submit", -value => "");
70 $session->param(postremove => scalar $q->Vars);
71 IkiWiki::cgi_savesession($session);
73 # Display a small confirmation form.
74 my ($f, $buttons)=confirmation_form($q, $session, $form->field("page"));
75 IkiWiki::showform($f, $buttons, $session, $q);
78 elsif ($form->submitted eq "Remove Attachments") {
84 sub sessioncgi ($$) { #{{{
87 if ($q->param("do") eq 'remove') {
89 my ($form, $buttons)=confirmation_form($q, $session, $session->param("page"));
90 IkiWiki::decode_form_utf8($form);
92 if ($form->submitted eq 'Cancel') {
93 # Load saved form state and return to edit form.
94 my $postremove=CGI->new($session->param("postremove"));
95 $session->clear("postremove");
96 IkiWiki::cgi_savesession($session);
97 IkiWiki::cgi($postremove, $session);
99 elsif ($form->submitted eq 'Remove' && $form->validate) {
100 my $page=$form->field("page");
101 my $file=$pagesources{$page};
103 # Validate removal by checking that the page exists,
104 # and that the user is allowed to edit(/remove) it.
105 if (! exists $pagesources{$page}) {
106 error(sprintf(gettext("%s does not exist"),
107 htmllink("", "", $page, noimageinline => 1)));
109 IkiWiki::check_canedit($page, $q, $session);
111 # Do removal, and update the wiki.
112 require IkiWiki::Render;
114 IkiWiki::rcs_remove($file);
115 IkiWiki::disable_commit_hook();
116 IkiWiki::rcs_commit($file, gettext("removed"),
117 IkiWiki::rcs_prepedit($file),
118 $session->param("name"), $ENV{REMOTE_ADDR});
119 IkiWiki::enable_commit_hook();
120 IkiWiki::rcs_update();
122 IkiWiki::prune("$config{srcdir}/$file");
124 IkiWiki::saveindex();
126 # Redirect to parent of the page.
127 my $parent=IkiWiki::dirname($page);
128 if (! exists $pagesources{$parent}) {
131 IkiWiki::redirect($q, $config{url}."/".htmlpage($parent));
134 IkiWiki::showform($form, $buttons, $session, $q);