]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/rename.pm
really add
[ikiwiki.git] / IkiWiki / Plugin / rename.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::rename;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         hook(type => "formbuilder_setup", id => "rename", call => \&formbuilder_setup);
10         hook(type => "formbuilder", id => "rename", call => \&formbuilder);
11         hook(type => "sessioncgi", id => "rename", 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                 # Rename button for the page, and also for attachments.
22                 push @{$params{buttons}}, "Rename";
23                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
24         }
25 } #}}}
26
27 sub rename_form ($$$) { #{{{ 
28         my $q=shift;
29         my $session=shift;
30         my $page=shift;
31
32         eval q{use CGI::FormBuilder};
33         error($@) if $@;
34         my $f = CGI::FormBuilder->new(
35                 name => "rename",
36                 title => sprintf(gettext("rename %s"), IkiWiki::pagetitle($page)),
37                 header => 0,
38                 charset => "utf-8",
39                 method => 'POST',
40                 javascript => 0,
41                 params => $q,
42                 action => $config{cgiurl},
43                 stylesheet => IkiWiki::baseurl()."style.css",
44                 fields => [qw{do page new_name attachment}],
45         );
46         
47         $f->field(name => "do", type => "hidden", value => "rename", force => 1);
48         $f->field(name => "page", type => "hidden", value => $page, force => 1);
49         $f->field(name => "new_name", value => IkiWiki::pagetitle($page), size => 60);
50         $f->field(name => "attachment", type => "hidden");
51
52         return $f, ["Rename", "Cancel"];
53 } #}}}
54
55 sub rename_start ($$$$) {
56         my $q=shift;
57         my $session=shift;
58         my $attachment=shift;
59         my $page=shift;
60
61         # Save current form state to allow returning to it later
62         # without losing any edits.
63         # (But don't save what button was submitted, to avoid
64         # looping back to here.)
65         # Note: "_submit" is CGI::FormBuilder internals.
66         $q->param(-name => "_submit", -value => "");
67         $session->param(postrename => scalar $q->Vars);
68         IkiWiki::cgi_savesession($session);
69         
70         my ($f, $buttons)=rename_form($q, $session, $page);
71         if (defined $attachment) {
72                 $f->field(name => "attachment", value => $attachment, force => 1);
73         }
74         
75         IkiWiki::showform($f, $buttons, $session, $q);
76         exit 0;
77 }
78
79 sub postrename ($;$) {
80         my $session=shift;
81         my $newname=shift;
82
83         # Load saved form state and return to edit form.
84         my $postrename=CGI->new($session->param("postrename"));
85         if (defined $newname) {
86                 # They renamed the page they were editing.
87                 # Tweak the edit form to be editing the new
88                 # page name, and redirect back to it.
89                 # (Deep evil here.)
90                 error("don't know how to redir back!"); ## FIXME
91         }
92         $session->clear("postrename");
93         IkiWiki::cgi_savesession($session);
94         IkiWiki::cgi($postrename, $session);
95 }
96
97 sub formbuilder (@) { #{{{
98         my %params=@_;
99         my $form=$params{form};
100
101         if (defined $form->field("do") && $form->field("do") eq "edit") {
102                 my $q=$params{cgi};
103                 my $session=$params{session};
104
105                 if ($form->submitted eq "Rename") {
106                         rename_start($q, $session, 0, $form->field("page"));
107                 }
108                 elsif ($form->submitted eq "Rename Attachment") {
109                         my @selected=$q->param("attachment_select");
110                         if (@selected > 1) {
111                                 error(gettext("Only one attachment can be renamed at a time."));
112                         }
113                         elsif (! @selected) {
114                                 error(gettext("Please select the attachment to rename."))
115                         }
116                         rename_start($q, $session, 1, $selected[0]);
117                 }
118         }
119 } #}}}
120
121 sub sessioncgi ($$) { #{{{
122         my $q=shift;
123
124         if ($q->param("do") eq 'rename') {
125                 my $session=shift;
126                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
127                 IkiWiki::decode_form_utf8($form);
128
129                 if ($form->submitted eq 'Cancel') {
130                         postrename($session);
131                 }
132                 elsif ($form->submitted eq 'Rename' && $form->validate) {
133                         my $page=$q->param("page");
134
135                         # This untaint is safe because of the checks below.
136                         my $file=IkiWiki::possibly_foolish_untaint($pagesources{$page});
137
138                         # Must be a known source file.
139                         if (! defined $file) {
140                                 error(sprintf(gettext("%s does not exist"),
141                                 htmllink("", "", $page, noimageinline => 1)));
142                         }
143                                 
144                         # Must be editiable.
145                         IkiWiki::check_canedit($page, $q, $session);
146
147                         # Must exist on disk, and be a regular file.
148                         if (! -e "$config{srcdir}/$file") {
149                                 error(sprintf(gettext("%s is not in the srcdir, so it cannot be deleted"), $file));
150                         }
151                         elsif (-l "$config{srcdir}/$file" && ! -f _) {
152                                 error(sprintf(gettext("%s is not a file"), $file));
153                         }
154
155                         # TODO: check attachment limits
156
157                         my $dest=IkiWiki::titlepage($q->param("new_name"));
158                         # XXX TODO check $dest!
159
160                         # Do rename, and update the wiki.
161                         require IkiWiki::Render;
162                         if ($config{rcs}) {
163                                 IkiWiki::disable_commit_hook();
164                                 my $token=IkiWiki::rcs_prepedit($file);
165                                 IkiWiki::rcs_rename($file, $dest);
166                                 IkiWiki::rcs_commit($file, gettext("rename $file to $dest"),
167                                         $token, $session->param("name"), $ENV{REMOTE_ADDR});
168                                 IkiWiki::enable_commit_hook();
169                                 IkiWiki::rcs_update();
170                         }
171                         else {
172                                 if (! rename("$config{srcdir}/$file", "$config{srcdir}/$dest")) {
173                                         error("rename: $!");
174                                 }
175                         }
176                         IkiWiki::refresh();
177                         IkiWiki::saveindex();
178
179                         if ($q->param("attachment")) {
180                                 postrename($session);
181                         }
182                         else {
183                                 postrename($session, $dest);
184                         }
185                 }
186                 else {
187                         IkiWiki::showform($form, $buttons, $session, $q);
188                 }
189
190                 exit 0;
191         }
192 }
193
194 1