Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki] / IkiWiki / Plugin / rename.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::rename;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 sub import {
9         hook(type => "getsetup", id => "rename", call => \&getsetup);
10         hook(type => "formbuilder_setup", id => "rename", call => \&formbuilder_setup);
11         hook(type => "formbuilder", id => "rename", call => \&formbuilder);
12         hook(type => "sessioncgi", id => "rename", call => \&sessioncgi);
13
14 }
15
16 sub getsetup () {
17         return 
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                 },
22 }
23
24 sub check_canrename ($$$$$$) {
25         my $src=shift;
26         my $srcfile=shift;
27         my $dest=shift;
28         my $destfile=shift;
29         my $q=shift;
30         my $session=shift;
31
32         my $attachment=! defined pagetype($pagesources{$src});
33
34         # Must be a known source file.
35         if (! exists $pagesources{$src}) {
36                 error(sprintf(gettext("%s does not exist"),
37                         htmllink("", "", $src, noimageinline => 1)));
38         }
39         
40         # Must exist on disk, and be a regular file.
41         if (! -e "$config{srcdir}/$srcfile") {
42                 error(sprintf(gettext("%s is not in the srcdir, so it cannot be renamed"), $srcfile));
43         }
44         elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
45                 error(sprintf(gettext("%s is not a file"), $srcfile));
46         }
47
48         # Must be editable.
49         IkiWiki::check_canedit($src, $q, $session);
50         if ($attachment) {
51                 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
52                         IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
53                 }
54                 else {
55                         error("renaming of attachments is not allowed");
56                 }
57         }
58         
59         # Dest checks can be omitted by passing undef.
60         if (defined $dest) {
61                 if ($srcfile eq $destfile) {
62                         error(gettext("no change to the file name was specified"));
63                 }
64
65                 # Must be a legal filename, and not absolute.
66                 if (IkiWiki::file_pruned($destfile, $config{srcdir}) || 
67                     $destfile=~/^\//) {
68                         error(sprintf(gettext("illegal name")));
69                 }
70
71                 # Must not be a known source file.
72                 if ($src ne $dest && exists $pagesources{$dest}) {
73                         error(sprintf(gettext("%s already exists"),
74                                 htmllink("", "", $dest, noimageinline => 1)));
75                 }
76         
77                 # Must not exist on disk already.
78                 if (-l "$config{srcdir}/$destfile" || -e _) {
79                         error(sprintf(gettext("%s already exists on disk"), $destfile));
80                 }
81         
82                 # Must be editable.
83                 IkiWiki::check_canedit($dest, $q, $session);
84                 if ($attachment) {
85                         # Note that $srcfile is used here, not $destfile,
86                         # because it wants the current file, to check it.
87                         IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
88                 }
89         }
90 }
91
92 sub rename_form ($$$) {
93         my $q=shift;
94         my $session=shift;
95         my $page=shift;
96
97         eval q{use CGI::FormBuilder};
98         error($@) if $@;
99         my $f = CGI::FormBuilder->new(
100                 name => "rename",
101                 title => sprintf(gettext("rename %s"), pagetitle($page)),
102                 header => 0,
103                 charset => "utf-8",
104                 method => 'POST',
105                 javascript => 0,
106                 params => $q,
107                 action => $config{cgiurl},
108                 stylesheet => IkiWiki::baseurl()."style.css",
109                 fields => [qw{do page new_name attachment}],
110         );
111         
112         $f->field(name => "do", type => "hidden", value => "rename", force => 1);
113         $f->field(name => "page", type => "hidden", value => $page, force => 1);
114         $f->field(name => "new_name", value => pagetitle($page, 1), size => 60);
115         if (!$q->param("attachment")) {
116                 # insert the standard extensions
117                 my @page_types;
118                 if (exists $IkiWiki::hooks{htmlize}) {
119                         @page_types=grep { !/^_/ }
120                                 keys %{$IkiWiki::hooks{htmlize}};
121                 }
122         
123                 # make sure the current extension is in the list
124                 my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
125                 if (! $IkiWiki::hooks{htmlize}{$ext}) {
126                         unshift(@page_types, $ext);
127                 }
128         
129                 $f->field(name => "type", type => 'select',
130                         options => \@page_types,
131                         value => $ext, force => 1);
132                 
133                 foreach my $p (keys %pagesources) {
134                         if ($pagesources{$p}=~m/^\Q$page\E\//) {
135                                 $f->field(name => "subpages",
136                                         label => "",
137                                         type => "checkbox",
138                                         options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
139                                         value => 1,
140                                         force => 1);
141                                 last;
142                         }
143                 }
144         }
145         $f->field(name => "attachment", type => "hidden");
146
147         return $f, ["Rename", "Cancel"];
148 }
149
150 sub rename_start ($$$$) {
151         my $q=shift;
152         my $session=shift;
153         my $attachment=shift;
154         my $page=shift;
155
156         check_canrename($page, $pagesources{$page}, undef, undef,
157                 $q, $session);
158
159         # Save current form state to allow returning to it later
160         # without losing any edits.
161         # (But don't save what button was submitted, to avoid
162         # looping back to here.)
163         # Note: "_submit" is CGI::FormBuilder internals.
164         $q->param(-name => "_submit", -value => "");
165         $session->param(postrename => scalar $q->Vars);
166         IkiWiki::cgi_savesession($session);
167         
168         if (defined $attachment) {
169                 $q->param(-name => "attachment", -value => $attachment);
170         }
171         my ($f, $buttons)=rename_form($q, $session, $page);
172         IkiWiki::showform($f, $buttons, $session, $q);
173         exit 0;
174 }
175
176 sub postrename ($;$$$) {
177         my $session=shift;
178         my $src=shift;
179         my $dest=shift;
180         my $attachment=shift;
181
182         # Load saved form state and return to edit page.
183         my $postrename=CGI->new($session->param("postrename"));
184         $session->clear("postrename");
185         IkiWiki::cgi_savesession($session);
186
187         if (defined $dest) {
188                 if (! $attachment) {
189                         # They renamed the page they were editing. This requires
190                         # fixups to the edit form state.
191                         # Tweak the edit form to be editing the new page.
192                         $postrename->param("page", $dest);
193                 }
194
195                 # Update edit form content to fix any links present
196                 # on it.
197                 $postrename->param("editcontent",
198                         renamepage_hook($dest, $src, $dest,
199                                  $postrename->param("editcontent")));
200
201                 # Get a new edit token; old was likely invalidated.
202                 $postrename->param("rcsinfo",
203                         IkiWiki::rcs_prepedit($pagesources{$dest}));
204         }
205
206         IkiWiki::cgi_editpage($postrename, $session);
207 }
208
209 sub formbuilder (@) {
210         my %params=@_;
211         my $form=$params{form};
212
213         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
214             $form->field("do") eq "create")) {
215                 my $q=$params{cgi};
216                 my $session=$params{session};
217
218                 if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
219                         rename_start($q, $session, 0, $form->field("page"));
220                 }
221                 elsif ($form->submitted eq "Rename Attachment") {
222                         my @selected=$q->param("attachment_select");
223                         if (@selected > 1) {
224                                 error(gettext("Only one attachment can be renamed at a time."));
225                         }
226                         elsif (! @selected) {
227                                 error(gettext("Please select the attachment to rename."))
228                         }
229                         rename_start($q, $session, 1, $selected[0]);
230                 }
231         }
232 }
233
234 my $renamesummary;
235
236 sub formbuilder_setup (@) {
237         my %params=@_;
238         my $form=$params{form};
239         my $q=$params{cgi};
240
241         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
242             $form->field("do") eq "create")) {
243                 # Rename button for the page, and also for attachments.
244                 push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
245                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
246
247                 if (defined $renamesummary) {
248                         $form->tmpl_param(message => $renamesummary);
249                 }
250         }
251 }
252
253 sub sessioncgi ($$) {
254         my $q=shift;
255
256         if ($q->param("do") eq 'rename') {
257                 my $session=shift;
258                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
259                 IkiWiki::decode_form_utf8($form);
260
261                 if ($form->submitted eq 'Cancel') {
262                         postrename($session);
263                 }
264                 elsif ($form->submitted eq 'Rename' && $form->validate) {
265                         # Queue of rename actions to perfom.
266                         my @torename;
267
268                         # These untaints are safe because of the checks
269                         # performed in check_canrename later.
270                         my $src=$q->param("page");
271                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
272                         my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
273                         my $destfile=$dest;
274                         if (! $q->param("attachment")) {
275                                 my $type=$q->param('type');
276                                 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
277                                         $type=IkiWiki::possibly_foolish_untaint($type);
278                                 }
279                                 else {
280                                         my ($ext)=$srcfile=~/\.([^.]+)$/;
281                                         $type=$ext;
282                                 }
283                                 
284                                 $destfile=newpagefile($dest, $type);
285                         }
286                         push @torename, {
287                                 src => $src,
288                                 srcfile => $srcfile,
289                                 dest => $dest,
290                                 destfile => $destfile,
291                                 required => 1,
292                         };
293
294                         # See if any subpages need to be renamed.
295                         if ($q->param("subpages") && $src ne $dest) {
296                                 foreach my $p (keys %pagesources) {
297                                         next unless $pagesources{$p}=~m/^\Q$src\E\//;
298                                         # If indexpages is enabled, the
299                                         # srcfile should not be confused
300                                         # with a subpage.
301                                         next if $pagesources{$p} eq $srcfile;
302
303                                         my $d=$pagesources{$p};
304                                         $d=~s/^\Q$src\E\//$dest\//;
305                                         push @torename, {
306                                                 src => $p,
307                                                 srcfile => $pagesources{$p},
308                                                 dest => pagename($d),
309                                                 destfile => $d,
310                                                 required => 0,
311                                         };
312                                 }
313                         }
314                         
315                         require IkiWiki::Render;
316                         IkiWiki::disable_commit_hook() if $config{rcs};
317                         my %origpagesources=%pagesources;
318
319                         # First file renaming.
320                         foreach my $rename (@torename) {
321                                 if ($rename->{required}) {
322                                         do_rename($rename, $q, $session);
323                                 }
324                                 else {
325                                         eval {do_rename($rename, $q, $session)};
326                                         if ($@) {
327                                                 $rename->{error}=$@;
328                                                 next;
329                                         }
330                                 }
331
332                                 # Temporarily tweak pagesources to point to
333                                 # the renamed file, in case fixlinks needs
334                                 # to edit it.
335                                 $pagesources{$rename->{src}}=$rename->{destfile};
336                         }
337                         IkiWiki::rcs_commit_staged(
338                                 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
339                                 $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
340
341                         # Then link fixups.
342                         foreach my $rename (@torename) {
343                                 next if $rename->{src} eq $rename->{dest};
344                                 next if $rename->{error};
345                                 foreach my $p (fixlinks($rename, $session)) {
346                                         # map old page names to new
347                                         foreach my $r (@torename) {
348                                                 next if $rename->{error};
349                                                 if ($r->{src} eq $p) {
350                                                         $p=$r->{dest};
351                                                         last;
352                                                 }
353                                         }
354                                         push @{$rename->{fixedlinks}}, $p;
355                                 }
356                         }
357
358                         # Then refresh.
359                         %pagesources=%origpagesources;
360                         if ($config{rcs}) {
361                                 IkiWiki::enable_commit_hook();
362                                 IkiWiki::rcs_update();
363                         }
364                         IkiWiki::refresh();
365                         IkiWiki::saveindex();
366
367                         # Find pages with remaining, broken links.
368                         foreach my $rename (@torename) {
369                                 next if $rename->{src} eq $rename->{dest};
370                                 
371                                 foreach my $page (keys %links) {
372                                         my $broken=0;
373                                         foreach my $link (@{$links{$page}}) {
374                                                 my $bestlink=bestlink($page, $link);
375                                                 if ($bestlink eq $rename->{src}) {
376                                                         push @{$rename->{brokenlinks}}, $page;
377                                                         last;
378                                                 }
379                                         }
380                                 }
381                         }
382
383                         # Generate a summary, that will be shown at the top
384                         # of the edit template.
385                         $renamesummary="";
386                         foreach my $rename (@torename) {
387                                 my $template=template("renamesummary.tmpl");
388                                 $template->param(src => $rename->{srcfile});
389                                 $template->param(dest => $rename->{destfile});
390                                 $template->param(error => $rename->{error});
391                                 if ($rename->{src} ne $rename->{dest}) {
392                                         $template->param(brokenlinks_checked => 1);
393                                         $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
394                                         $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
395                                 }
396                                 $renamesummary.=$template->output;
397                         }
398
399                         postrename($session, $src, $dest, $q->param("attachment"));
400                 }
401                 else {
402                         IkiWiki::showform($form, $buttons, $session, $q);
403                 }
404
405                 exit 0;
406         }
407 }
408                                                 
409 sub linklist {
410         # generates a list of links in a form suitable for FormBuilder
411         my $dest=shift;
412         my $list=shift;
413         # converts a list of pages into a list of links
414         # in a form suitable for FormBuilder.
415
416         [map {
417                 {
418                         page => htmllink($dest, $dest, $_,
419                                         noimageinline => 1,
420                                         linktext => pagetitle($_),
421                                 )
422                 }
423         } @{$list}]
424 }
425
426 sub renamepage_hook ($$$$) {
427         my ($page, $src, $dest, $content)=@_;
428
429         IkiWiki::run_hooks(renamepage => sub {
430                 $content=shift->(
431                         page => $page,
432                         oldpage => $src,
433                         newpage => $dest,
434                         content => $content,
435                 );
436         });
437
438         return $content;
439 }
440                         
441 sub do_rename ($$$) {
442         my $rename=shift;
443         my $q=shift;
444         my $session=shift;
445
446         # First, check if this rename is allowed.
447         check_canrename($rename->{src},
448                 $rename->{srcfile},
449                 $rename->{dest},
450                 $rename->{destfile},
451                 $q, $session);
452
453         # Ensure that the dest directory exists and is ok.
454         IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
455
456         if ($config{rcs}) {
457                 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
458         }
459         else {
460                 if (! rename($config{srcdir}."/".$rename->{srcfile},
461                              $config{srcdir}."/".$rename->{destfile})) {
462                         error("rename: $!");
463                 }
464         }
465
466 }
467
468 sub fixlinks ($$$) {
469         my $rename=shift;
470         my $session=shift;
471
472         my @fixedlinks;
473
474         foreach my $page (keys %links) {
475                 my $needfix=0;
476                 foreach my $link (@{$links{$page}}) {
477                         my $bestlink=bestlink($page, $link);
478                         if ($bestlink eq $rename->{src}) {
479                                 $needfix=1;
480                                 last;
481                         }
482                 }
483                 if ($needfix) {
484                         my $file=$pagesources{$page};
485                         my $oldcontent=readfile($config{srcdir}."/".$file);
486                         my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
487                         if ($oldcontent ne $content) {
488                                 my $token=IkiWiki::rcs_prepedit($file);
489                                 eval { writefile($file, $config{srcdir}, $content) };
490                                 next if $@;
491                                 my $conflict=IkiWiki::rcs_commit(
492                                         $file,
493                                         sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
494                                         $token,
495                                         $session->param("name"), 
496                                         $ENV{REMOTE_ADDR}
497                                 );
498                                 push @fixedlinks, $page if ! defined $conflict;
499                         }
500                 }
501         }
502
503         return @fixedlinks;
504 }
505
506 1