]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/remove.pm
redirect after removal
[ikiwiki.git] / IkiWiki / Plugin / remove.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::remove;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
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);
12
13 } # }}}
14
15 sub formbuilder_setup (@) { #{{{
16         my %params=@_;
17         my $form=$params{form};
18         my $q=$params{cgi};
19
20         if (defined $form->field("do") && $form->field("do") eq "edit") {
21                 push @{$params{buttons}}, "Remove";
22                 # TODO button for attachments
23         }
24 } #}}}
25
26 sub confirmation_form ($$) { #{{{ 
27         my $q=shift;
28         my $session=shift;
29
30         eval q{use CGI::FormBuilder};
31         error($@) if $@;
32         my @fields=qw(do page);
33         my $f = CGI::FormBuilder->new(
34                 title => "confirm removal",
35                 name => "remove",
36                 header => 0,
37                 charset => "utf-8",
38                 method => 'POST',
39                 javascript => 0,
40                 params => $q,
41                 action => $config{cgiurl},
42                 stylesheet => IkiWiki::baseurl()."style.css",
43                 fields => \@fields,
44         );
45         
46         $f->field(name => "do", type => "hidden", value => "remove", force => 1);
47         $f->field(name => "page", label => "Will remove:", size => 60,
48                 validate => sub {
49                 # Validate page by checking that the page exists, and that
50                 # the user is allowed to edit(/delete) it.
51                 my $page=IkiWiki::titlepage(shift);
52                 if (! exists $pagesources{$page}) {
53                         $f->field(name => "page", message => gettext("page does not exist"));
54                         return 0;
55                 }
56                 else {
57                         IkiWiki::check_canedit($page, $q, $session);
58                         return 1;
59                 }
60         });
61
62         return $f, ["Remove", "Cancel"];
63 } #}}}
64
65 sub formbuilder (@) { #{{{
66         my %params=@_;
67         my $form=$params{form};
68
69         if (defined $form->field("do") && $form->field("do") eq "edit" &&
70             $form->submitted eq "Remove") {
71                 my $q=$params{cgi};
72                 my $session=$params{session};
73
74                 # Save current form state to allow returning to it later
75                 # without losing any edits.
76                 # (But don't save what button was submitted, to avoid
77                 # looping back to here.)
78                 # Note: "_submit" is CGI::FormBuilder internals.
79                 $q->param(-name => "_submit", -value => "");
80                 $session->param(postremove => scalar $q->Vars);
81                 IkiWiki::cgi_savesession($session);
82
83                 # Display a small confirmation form.
84                 my ($f, $buttons)=confirmation_form($q, $session);
85                 $f->field(name => "page", 
86                         value => IkiWiki::pagetitle($form->field("page")),
87                         force => 1);
88                 IkiWiki::showform($f, $buttons, $session, $q);
89                 exit 0;
90         }
91 } #}}}
92
93 sub sessioncgi ($$) { #{{{
94         my $q=shift;
95
96         if ($q->param("do") eq 'remove') {
97                 my $session=shift;
98                 my ($form, $buttons)=confirmation_form($q, $session);
99                 IkiWiki::decode_form_utf8($form);
100                 if ($form->submitted eq 'Cancel') {
101                         # Load saved form state and return to edit form.
102                         my $postremove=CGI->new($session->param("postremove"));
103                         $session->clear("postremove");
104                         IkiWiki::cgi_savesession($session);
105                         IkiWiki::cgi($postremove, $session);
106                         exit 0;
107                 }
108                 elsif ($form->submitted eq 'Remove' && $form->validate) {
109                         my $page=IkiWiki::titlepage($form->field("page"));
110                         my $file=$pagesources{$page};
111
112                         # Do removal, and update the wiki.
113                         require IkiWiki::Render;
114                         if ($config{rcs}) {
115                                 IkiWiki::rcs_remove($file);
116                                 IkiWiki::disable_commit_hook();
117                                 IkiWiki::rcs_commit($file, gettext("removed"),
118                                         IkiWiki::rcs_prepedit($file),
119                                         $session->param("name"), $ENV{REMOTE_ADDR});
120                                 IkiWiki::enable_commit_hook();
121                                 IkiWiki::rcs_update();
122                         }
123                         IkiWiki::prune("$config{srcdir}/$file");
124                         IkiWiki::refresh();
125                         IkiWiki::saveindex();
126
127                         # Redirect to parent of the page.
128                         my $parent=IkiWiki::dirname($page);
129                         if (! exists $pagesources{$parent}) {
130                                 $parent="index";
131                         }
132                         IkiWiki::redirect($q, $config{url}."/".htmlpage($parent));
133                         exit 0;
134                 }
135                 else {
136                         IkiWiki::showform($form, $buttons, $session, $q);
137                         exit 0;
138                 }
139         }
140 }
141
142 1