]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/remove.pm
98f2f9459ffe599b789d28e7ea550fc35aa6317d
[git.ikiwiki.info.git] / IkiWiki / Plugin / remove.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::remove;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
8 sub import { #{{{
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);
13 } # }}}
15 sub formbuilder_setup (@) { #{{{
16         my %params=@_;
17         my $form=$params{form};
18         my $q=$params{cgi};
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" />');
24         }
25 } #}}}
27 sub confirmation_form ($$$) { #{{{ 
28         my $q=shift;
29         my $session=shift;
30         my $page=shift;
32         eval q{use CGI::FormBuilder};
33         error($@) if $@;
34         my @fields=qw(do page);
35         my $f = CGI::FormBuilder->new(
36                 title => sprintf(gettext("confirm removal of %s"),
37                         IkiWiki::pagetitle($page)),
38                 name => "remove",
39                 header => 0,
40                 charset => "utf-8",
41                 method => 'POST',
42                 javascript => 0,
43                 params => $q,
44                 action => $config{cgiurl},
45                 stylesheet => IkiWiki::baseurl()."style.css",
46                 fields => \@fields,
47         );
48         
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"];
53 } #}}}
55 sub formbuilder (@) { #{{{
56         my %params=@_;
57         my $form=$params{form};
59         if (defined $form->field("do") && $form->field("do") eq "edit") {
60                 if ($form->submitted eq "Remove") {
61                         my $q=$params{cgi};
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);
72         
73                         # Display a small confirmation form.
74                         my ($f, $buttons)=confirmation_form($q, $session, $form->field("page"));
75                         IkiWiki::showform($f, $buttons, $session, $q);
76                         exit 0;
77                 }
78                 elsif ($form->submitted eq "Remove Attachments") {
79                         
80                 }
81         }
82 } #}}}
84 sub sessioncgi ($$) { #{{{
85         my $q=shift;
87         if ($q->param("do") eq 'remove') {
88                 my $session=shift;
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);
98                 }
99                 elsif ($form->submitted eq 'Remove' && $form->validate) {
100                         my $page=$form->field("page");
101                         my $file=$pagesources{$page};
102         
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)));
108                         }
109                         IkiWiki::check_canedit($page, $q, $session);
111                         # Do removal, and update the wiki.
112                         require IkiWiki::Render;
113                         if ($config{rcs}) {
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();
121                         }
122                         IkiWiki::prune("$config{srcdir}/$file");
123                         IkiWiki::refresh();
124                         IkiWiki::saveindex();
126                         # Redirect to parent of the page.
127                         my $parent=IkiWiki::dirname($page);
128                         if (! exists $pagesources{$parent}) {
129                                 $parent="index";
130                         }
131                         IkiWiki::redirect($q, $config{url}."/".htmlpage($parent));
132                 }
133                 else {
134                         IkiWiki::showform($form, $buttons, $session, $q);
135                 }
137                 exit 0;
138         }