]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/rename.pm
make message a bit more generic
[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 check_canrename ($$$$$$$) { #{{{
16         my $src=shift;
17         my $srcfile=shift;
18         my $dest=shift;
19         my $destfile=shift;
20         my $q=shift;
21         my $session=shift;
22         my $attachment=shift;
23
24         # Must be a known source file.
25         if (! exists $pagesources{$src}) {
26                 error(sprintf(gettext("%s does not exist"),
27                         htmllink("", "", $src, noimageinline => 1)));
28         }
29         
30         # Must exist on disk, and be a regular file.
31         if (! -e "$config{srcdir}/$srcfile") {
32                 error(sprintf(gettext("%s is not in the srcdir, so it cannot be renamed"), $srcfile));
33         }
34         elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
35                 error(sprintf(gettext("%s is not a file"), $srcfile));
36         }
37
38         # Must be editable.
39         IkiWiki::check_canedit($src, $q, $session);
40         if ($attachment) {
41                 IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
42         }
43         
44         # Dest checks can be omitted by passing undef.
45         if (defined $dest) {
46                 if ($src eq $dest || $srcfile eq $destfile) {
47                         error(gettext("no change to the file name was specified"));
48                 }
49
50                 # Must be a legal filename.     
51                 if (file_pruned($destfile, $config{srcdir})) {
52                         error(sprintf(gettext("illegal name")));
53                 }
54
55                 # Must not be a known source file.
56                 if (exists $pagesources{$dest}) {
57                         error(sprintf(gettext("%s already exists"),
58                                 htmllink("", "", $dest, noimageinline => 1)));
59                 }
60         
61                 # Must not exist on disk already.
62                 if (-l "$config{srcdir}/$destfile" || -e _) {
63                         error(sprintf(gettext("%s already exists on disk"), $destfile));
64                 }
65         
66                 # Must be editable.
67                 IkiWiki::check_canedit($dest, $q, $session);
68                 if ($attachment) {
69                         IkiWiki::Plugin::attachment::check_canattach($session, $dest, $destfile);
70                 }
71         }
72 } #}}}
73
74 sub formbuilder_setup (@) { #{{{
75         my %params=@_;
76         my $form=$params{form};
77         my $q=$params{cgi};
78
79         if (defined $form->field("do") && $form->field("do") eq "edit") {
80                 # Rename button for the page, and also for attachments.
81                 push @{$params{buttons}}, "Rename";
82                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
83         }
84 } #}}}
85
86 sub rename_form ($$$) { #{{{ 
87         my $q=shift;
88         my $session=shift;
89         my $page=shift;
90
91         eval q{use CGI::FormBuilder};
92         error($@) if $@;
93         my $f = CGI::FormBuilder->new(
94                 name => "rename",
95                 title => sprintf(gettext("rename %s"), IkiWiki::pagetitle($page)),
96                 header => 0,
97                 charset => "utf-8",
98                 method => 'POST',
99                 javascript => 0,
100                 params => $q,
101                 action => $config{cgiurl},
102                 stylesheet => IkiWiki::baseurl()."style.css",
103                 fields => [qw{do page new_name attachment}],
104         );
105         
106         $f->field(name => "do", type => "hidden", value => "rename", force => 1);
107         $f->field(name => "page", type => "hidden", value => $page, force => 1);
108         $f->field(name => "new_name", value => IkiWiki::pagetitle($page), size => 60);
109         $f->field(name => "attachment", type => "hidden");
110
111         return $f, ["Rename", "Cancel"];
112 } #}}}
113
114 sub rename_start ($$$$) {
115         my $q=shift;
116         my $session=shift;
117         my $attachment=shift;
118         my $page=shift;
119
120         check_canrename($page, $pagesources{$page}, undef, undef,
121                 $q, $session, $attachment);
122
123         # Save current form state to allow returning to it later
124         # without losing any edits.
125         # (But don't save what button was submitted, to avoid
126         # looping back to here.)
127         # Note: "_submit" is CGI::FormBuilder internals.
128         $q->param(-name => "_submit", -value => "");
129         $session->param(postrename => scalar $q->Vars);
130         IkiWiki::cgi_savesession($session);
131         
132         my ($f, $buttons)=rename_form($q, $session, $page);
133         if (defined $attachment) {
134                 $f->field(name => "attachment", value => $attachment, force => 1);
135         }
136         
137         IkiWiki::showform($f, $buttons, $session, $q);
138         exit 0;
139 }
140
141 sub postrename ($;$) {
142         my $session=shift;
143         my $newname=shift;
144
145         # Load saved form state and return to edit form.
146         my $postrename=CGI->new($session->param("postrename"));
147         if (defined $newname) {
148                 # They renamed the page they were editing.
149                 # Tweak the edit form to be editing the new
150                 # page name, and redirect back to it.
151                 # (Deep evil here.)
152                 error("don't know how to redir back!"); ## FIXME
153         }
154         $session->clear("postrename");
155         IkiWiki::cgi_savesession($session);
156         IkiWiki::cgi($postrename, $session);
157 }
158
159 sub formbuilder (@) { #{{{
160         my %params=@_;
161         my $form=$params{form};
162
163         if (defined $form->field("do") && $form->field("do") eq "edit") {
164                 my $q=$params{cgi};
165                 my $session=$params{session};
166
167                 if ($form->submitted eq "Rename") {
168                         rename_start($q, $session, 0, $form->field("page"));
169                 }
170                 elsif ($form->submitted eq "Rename Attachment") {
171                         my @selected=$q->param("attachment_select");
172                         if (@selected > 1) {
173                                 error(gettext("Only one attachment can be renamed at a time."));
174                         }
175                         elsif (! @selected) {
176                                 error(gettext("Please select the attachment to rename."))
177                         }
178                         rename_start($q, $session, 1, $selected[0]);
179                 }
180         }
181 } #}}}
182
183 sub sessioncgi ($$) { #{{{
184         my $q=shift;
185
186         if ($q->param("do") eq 'rename') {
187                 my $session=shift;
188                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
189                 IkiWiki::decode_form_utf8($form);
190
191                 if ($form->submitted eq 'Cancel') {
192                         postrename($session);
193                 }
194                 elsif ($form->submitted eq 'Rename' && $form->validate) {
195                         # These untaints are safe because of the checks
196                         # performed in check_canrename below.
197                         my $src=$q->param("page");
198                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
199                         my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
200
201                         # The extension of dest is the same as src if it's
202                         # a page. If it's an extension, the extension is
203                         # already included.
204                         my $destfile=$dest;
205                         if (! $q->param("attachment")) {
206                                 my ($ext)=$srcfile=~/(\.[^.]+)$/;
207                                 $destfile.=$ext;
208                         }
209
210                         check_canrename($src, $srcfile, $dest, $destfile,
211                                 $q, $session, $q->param("attachment"));
212
213                         # Do rename, and update the wiki.
214                         require IkiWiki::Render;
215                         if ($config{rcs}) {
216                                 IkiWiki::disable_commit_hook();
217                                 my $token=IkiWiki::rcs_prepedit($srcfile);
218                                 IkiWiki::rcs_rename($srcfile, $destfile);
219                                 # TODO commit destfile too
220                                 IkiWiki::rcs_commit($srcfile, gettext("rename $srcfile to $destfile"),
221                                         $token, $session->param("name"), $ENV{REMOTE_ADDR});
222                                 IkiWiki::enable_commit_hook();
223                                 IkiWiki::rcs_update();
224                         }
225                         else {
226                                 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
227                                         error("rename: $!");
228                                 }
229                         }
230                         IkiWiki::refresh();
231                         IkiWiki::saveindex();
232
233                         if ($q->param("attachment")) {
234                                 postrename($session);
235                         }
236                         else {
237                                 postrename($session, $dest);
238                         }
239                 }
240                 else {
241                         IkiWiki::showform($form, $buttons, $session, $q);
242                 }
243
244                 exit 0;
245         }
246 }
247
248 1