]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/rename.pm
typo
[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 (IkiWiki::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                         # Note that $srcfile is used here, not $destfile,
70                         # because it wants the current file, to check it.
71                         IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
72                 }
73         }
74 } #}}}
75
76 sub formbuilder_setup (@) { #{{{
77         my %params=@_;
78         my $form=$params{form};
79         my $q=$params{cgi};
80
81         if (defined $form->field("do") && $form->field("do") eq "edit") {
82                 # Rename button for the page, and also for attachments.
83                 push @{$params{buttons}}, "Rename";
84                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
85         }
86 } #}}}
87
88 sub rename_form ($$$) { #{{{ 
89         my $q=shift;
90         my $session=shift;
91         my $page=shift;
92
93         eval q{use CGI::FormBuilder};
94         error($@) if $@;
95         my $f = CGI::FormBuilder->new(
96                 name => "rename",
97                 title => sprintf(gettext("rename %s"), IkiWiki::pagetitle($page)),
98                 header => 0,
99                 charset => "utf-8",
100                 method => 'POST',
101                 javascript => 0,
102                 params => $q,
103                 action => $config{cgiurl},
104                 stylesheet => IkiWiki::baseurl()."style.css",
105                 fields => [qw{do page new_name attachment}],
106         );
107         
108         $f->field(name => "do", type => "hidden", value => "rename", force => 1);
109         $f->field(name => "page", type => "hidden", value => $page, force => 1);
110         $f->field(name => "new_name", value => IkiWiki::pagetitle($page), size => 60);
111         $f->field(name => "attachment", type => "hidden");
112
113         return $f, ["Rename", "Cancel"];
114 } #}}}
115
116 sub rename_start ($$$$) {
117         my $q=shift;
118         my $session=shift;
119         my $attachment=shift;
120         my $page=shift;
121
122         check_canrename($page, $pagesources{$page}, undef, undef,
123                 $q, $session, $attachment);
124
125         # Save current form state to allow returning to it later
126         # without losing any edits.
127         # (But don't save what button was submitted, to avoid
128         # looping back to here.)
129         # Note: "_submit" is CGI::FormBuilder internals.
130         $q->param(-name => "_submit", -value => "");
131         $session->param(postrename => scalar $q->Vars);
132         IkiWiki::cgi_savesession($session);
133         
134         my ($f, $buttons)=rename_form($q, $session, $page);
135         if (defined $attachment) {
136                 $f->field(name => "attachment", value => $attachment, force => 1);
137         }
138         
139         IkiWiki::showform($f, $buttons, $session, $q);
140         exit 0;
141 }
142
143 sub postrename ($;$) {
144         my $session=shift;
145         my $newname=shift;
146
147         # Load saved form state and return to edit form.
148         my $postrename=CGI->new($session->param("postrename"));
149         if (defined $newname) {
150                 # They renamed the page they were editing.
151                 # Tweak the edit form to be editing the new
152                 # page name, and redirect back to it.
153                 # (Deep evil here.)
154                 error("don't know how to redir back!"); ## FIXME
155         }
156         $session->clear("postrename");
157         IkiWiki::cgi_savesession($session);
158         IkiWiki::cgi($postrename, $session);
159 }
160
161 sub formbuilder (@) { #{{{
162         my %params=@_;
163         my $form=$params{form};
164
165         if (defined $form->field("do") && $form->field("do") eq "edit") {
166                 my $q=$params{cgi};
167                 my $session=$params{session};
168
169                 if ($form->submitted eq "Rename") {
170                         rename_start($q, $session, 0, $form->field("page"));
171                 }
172                 elsif ($form->submitted eq "Rename Attachment") {
173                         my @selected=$q->param("attachment_select");
174                         if (@selected > 1) {
175                                 error(gettext("Only one attachment can be renamed at a time."));
176                         }
177                         elsif (! @selected) {
178                                 error(gettext("Please select the attachment to rename."))
179                         }
180                         rename_start($q, $session, 1, $selected[0]);
181                 }
182         }
183 } #}}}
184
185 sub sessioncgi ($$) { #{{{
186         my $q=shift;
187
188         if ($q->param("do") eq 'rename') {
189                 my $session=shift;
190                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
191                 IkiWiki::decode_form_utf8($form);
192
193                 if ($form->submitted eq 'Cancel') {
194                         postrename($session);
195                 }
196                 elsif ($form->submitted eq 'Rename' && $form->validate) {
197                         # These untaints are safe because of the checks
198                         # performed in check_canrename below.
199                         my $src=$q->param("page");
200                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
201                         my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
202
203                         # The extension of dest is the same as src if it's
204                         # a page. If it's an extension, the extension is
205                         # already included.
206                         my $destfile=$dest;
207                         if (! $q->param("attachment")) {
208                                 my ($ext)=$srcfile=~/(\.[^.]+)$/;
209                                 $destfile.=$ext;
210                         }
211
212                         check_canrename($src, $srcfile, $dest, $destfile,
213                                 $q, $session, $q->param("attachment"));
214
215                         # Ensures that the dest directory exists and is ok.
216                         IkiWiki::prep_writefile($destfile, $config{srcdir});
217
218                         # Do rename, and update the wiki.
219                         require IkiWiki::Render;
220                         if ($config{rcs}) {
221                                 IkiWiki::disable_commit_hook();
222                                 IkiWiki::rcs_rename($srcfile, $destfile);
223                                 IkiWiki::rcs_commit_staged(gettext("rename $srcfile to $destfile"),
224                                         $session->param("name"), $ENV{REMOTE_ADDR});
225                                 IkiWiki::enable_commit_hook();
226                                 IkiWiki::rcs_update();
227                         }
228                         else {
229                                 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
230                                         error("rename: $!");
231                                 }
232                         }
233                         IkiWiki::refresh();
234                         IkiWiki::saveindex();
235
236                         if ($q->param("attachment")) {
237                                 postrename($session);
238                         }
239                         else {
240                                 postrename($session, $dest);
241                         }
242                 }
243                 else {
244                         IkiWiki::showform($form, $buttons, $session, $q);
245                 }
246
247                 exit 0;
248         }
249 }
250
251 1