link fixup on rename working
[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, and not absolute.
51                 if (IkiWiki::file_pruned($destfile, $config{srcdir}) || 
52                     $destfile=~/^\//) {
53                         error(sprintf(gettext("illegal name")));
54                 }
55
56                 # Must not be a known source file.
57                 if (exists $pagesources{$dest}) {
58                         error(sprintf(gettext("%s already exists"),
59                                 htmllink("", "", $dest, noimageinline => 1)));
60                 }
61         
62                 # Must not exist on disk already.
63                 if (-l "$config{srcdir}/$destfile" || -e _) {
64                         error(sprintf(gettext("%s already exists on disk"), $destfile));
65                 }
66         
67                 # Must be editable.
68                 IkiWiki::check_canedit($dest, $q, $session);
69                 if ($attachment) {
70                         # Note that $srcfile is used here, not $destfile,
71                         # because it wants the current file, to check it.
72                         IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
73                 }
74         }
75 } #}}}
76
77 sub rename_form ($$$) { #{{{ 
78         my $q=shift;
79         my $session=shift;
80         my $page=shift;
81
82         eval q{use CGI::FormBuilder};
83         error($@) if $@;
84         my $f = CGI::FormBuilder->new(
85                 name => "rename",
86                 title => sprintf(gettext("rename %s"), IkiWiki::pagetitle($page)),
87                 header => 0,
88                 charset => "utf-8",
89                 method => 'POST',
90                 javascript => 0,
91                 params => $q,
92                 action => $config{cgiurl},
93                 stylesheet => IkiWiki::baseurl()."style.css",
94                 fields => [qw{do page new_name attachment}],
95         );
96         
97         $f->field(name => "do", type => "hidden", value => "rename", force => 1);
98         $f->field(name => "page", type => "hidden", value => $page, force => 1);
99         $f->field(name => "new_name", value => IkiWiki::pagetitle($page), size => 60);
100         $f->field(name => "attachment", type => "hidden");
101
102         return $f, ["Rename", "Cancel"];
103 } #}}}
104
105 sub rename_start ($$$$) {
106         my $q=shift;
107         my $session=shift;
108         my $attachment=shift;
109         my $page=shift;
110
111         check_canrename($page, $pagesources{$page}, undef, undef,
112                 $q, $session, $attachment);
113
114         # Save current form state to allow returning to it later
115         # without losing any edits.
116         # (But don't save what button was submitted, to avoid
117         # looping back to here.)
118         # Note: "_submit" is CGI::FormBuilder internals.
119         $q->param(-name => "_submit", -value => "");
120         $session->param(postrename => scalar $q->Vars);
121         IkiWiki::cgi_savesession($session);
122         
123         my ($f, $buttons)=rename_form($q, $session, $page);
124         if (defined $attachment) {
125                 $f->field(name => "attachment", value => $attachment, force => 1);
126         }
127         
128         IkiWiki::showform($f, $buttons, $session, $q);
129         exit 0;
130 }
131
132 sub postrename ($;$$) {
133         my $session=shift;
134         my $dest=shift;
135         my $attachment=shift;
136
137         # Load saved form state and return to edit page.
138         my $postrename=CGI->new($session->param("postrename"));
139         $session->clear("postrename");
140         IkiWiki::cgi_savesession($session);
141
142         if (defined $dest && ! $attachment) {
143                 # They renamed the page they were editing. This requires
144                 # fixups to the edit form state.
145                 # Tweak the edit form to be editing the new page.
146                 $postrename->param("page", $dest);
147         }
148
149         # Get a new edit token; old likely not valid.
150         $postrename->param("rcsinfo", IkiWiki::rcs_prepedit($pagesources{$dest}));
151
152         IkiWiki::cgi_editpage($postrename, $session);
153 }
154
155 sub formbuilder (@) { #{{{
156         my %params=@_;
157         my $form=$params{form};
158
159         if (defined $form->field("do") && $form->field("do") eq "edit") {
160                 my $q=$params{cgi};
161                 my $session=$params{session};
162
163                 if ($form->submitted eq "Rename") {
164                         rename_start($q, $session, 0, $form->field("page"));
165                 }
166                 elsif ($form->submitted eq "Rename Attachment") {
167                         my @selected=$q->param("attachment_select");
168                         if (@selected > 1) {
169                                 error(gettext("Only one attachment can be renamed at a time."));
170                         }
171                         elsif (! @selected) {
172                                 error(gettext("Please select the attachment to rename."))
173                         }
174                         rename_start($q, $session, 1, $selected[0]);
175                 }
176         }
177 } #}}}
178
179 my $renamesummary;
180
181 sub formbuilder_setup (@) { #{{{
182         my %params=@_;
183         my $form=$params{form};
184         my $q=$params{cgi};
185
186         if (defined $form->field("do") && $form->field("do") eq "edit") {
187                 # Rename button for the page, and also for attachments.
188                 push @{$params{buttons}}, "Rename";
189                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
190
191                 if (defined $renamesummary) {
192                         $form->tmpl_param(message => $renamesummary);
193                 }
194         }
195 } #}}}
196
197 sub sessioncgi ($$) { #{{{
198         my $q=shift;
199
200         if ($q->param("do") eq 'rename') {
201                 my $session=shift;
202                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
203                 IkiWiki::decode_form_utf8($form);
204
205                 if ($form->submitted eq 'Cancel') {
206                         postrename($session);
207                 }
208                 elsif ($form->submitted eq 'Rename' && $form->validate) {
209                         # These untaints are safe because of the checks
210                         # performed in check_canrename below.
211                         my $src=$q->param("page");
212                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
213                         my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
214
215                         # The extension of dest is the same as src if it's
216                         # a page. If it's an extension, the extension is
217                         # already included.
218                         my $destfile=$dest;
219                         if (! $q->param("attachment")) {
220                                 my ($ext)=$srcfile=~/(\.[^.]+)$/;
221                                 $destfile.=$ext;
222                         }
223
224                         check_canrename($src, $srcfile, $dest, $destfile,
225                                 $q, $session, $q->param("attachment"));
226
227                         # Ensures that the dest directory exists and is ok.
228                         IkiWiki::prep_writefile($destfile, $config{srcdir});
229
230                         # Do rename, update other pages, and refresh site.
231                         IkiWiki::disable_commit_hook() if $config{rcs};
232                         require IkiWiki::Render;
233                         if ($config{rcs}) {
234                                 IkiWiki::rcs_rename($srcfile, $destfile);
235                                 IkiWiki::rcs_commit_staged(
236                                         sprintf(gettext("rename %s to %s"), $src, $dest),
237                                         $session->param("name"), $ENV{REMOTE_ADDR});
238                         }
239                         else {
240                                 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
241                                         error("rename: $!");
242                                 }
243                         }
244                         my @fixedlinks;
245                         foreach my $page (keys %links) {
246                                 my $needfix=0;
247                                 foreach my $link (@{$links{$page}}) {
248                                         my $bestlink=bestlink($page, $link);
249                                         if ($bestlink eq $src) {
250                                                 $needfix=1;
251                                         }
252                                 }
253                                 if ($needfix) {
254                                         my $file=$pagesources{$page};
255                                         my $oldcontent=readfile($config{srcdir}."/".$file);
256                                         my $content=renamepage_hook($page, $src, $dest, $oldcontent);
257                                         if ($oldcontent ne $content) {
258                                                 my $token=IkiWiki::rcs_prepedit($file);
259                                                 eval { writefile($file, $config{srcdir}, $content) };
260                                                 next if $@;
261                                                 my $conflict=IkiWiki::rcs_commit(
262                                                         $file,
263                                                         sprintf(gettext("update for rename of %s to %s"), $src, $dest),
264                                                         $token,
265                                                         $session->param("name"), 
266                                                         $ENV{REMOTE_ADDR}
267                                                 );
268                                                 push @fixedlinks, $page if ! defined $conflict;
269                                         }
270                                 }
271                         }
272                         if ($config{rcs}) {
273                                 IkiWiki::enable_commit_hook();
274                                 IkiWiki::rcs_update();
275                         }
276                         IkiWiki::refresh();
277                         IkiWiki::saveindex();
278
279                         # Scan for any remaining broken links to $src.
280                         my @brokenlinks;
281                         foreach my $page (keys %links) {
282                                 foreach my $link (@{$links{$page}}) {
283                                         my $bestlink=bestlink($page, $link);
284                                         if ($bestlink eq $src) {
285                                                 push @brokenlinks, $page;
286                                         }
287                                 }
288                         }
289
290                         # Generate a rename summary, that will be shown at the top
291                         # of the edit template.
292                         my $template=template("renamesummary.tmpl");
293                         $template->param(src => $src);
294                         $template->param(dest => $dest);
295                         $template->param(brokenlinks => [
296                                 map {
297                                         {
298                                                 page => htmllink($dest, $dest, $_,
299                                                                 noimageinline => 1)
300                                         }
301                                 } @brokenlinks
302                         ]);
303                         $template->param(fixedlinks => [
304                                 map {
305                                         {
306                                                 page => htmllink($dest, $dest, $_,
307                                                                 noimageinline => 1)
308                                         }
309                                 } @fixedlinks
310                         ]);
311                         $renamesummary=$template->output;
312
313                         postrename($session, $dest, $q->param("attachment"));
314                 }
315                 else {
316                         IkiWiki::showform($form, $buttons, $session, $q);
317                 }
318
319                 exit 0;
320         }
321 } #}}}
322
323 sub renamepage_hook ($$$$) { #{{{
324         my ($page, $src, $dest, $content)=@_;
325
326         IkiWiki::run_hooks(renamepage => sub {
327                 $content=shift->(
328                         page => $page,
329                         oldpage => $src,
330                         newpage => $dest,
331                         content => $content,
332                 );
333         });
334
335         return $content;
336 }# }}}
337
338 1