]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/rename.pm
Merge branch 'master' of ssh://git.ikiwiki.info
[git.ikiwiki.info.git] / IkiWiki / Plugin / rename.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::rename;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
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         hook(type => "rename", id => "rename", call => \&rename_subpages);
14 }
16 sub getsetup () {
17         return 
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                         section => "web",
22                 },
23 }
25 sub check_canrename ($$$$$$) {
26         my $src=shift;
27         my $srcfile=shift;
28         my $dest=shift;
29         my $destfile=shift;
30         my $q=shift;
31         my $session=shift;
33         my $attachment=! defined pagetype($pagesources{$src});
35         # Must be a known source file.
36         if (! exists $pagesources{$src}) {
37                 error(sprintf(gettext("%s does not exist"),
38                         htmllink("", "", $src, noimageinline => 1)));
39         }
40         
41         # Must exist on disk, and be a regular file.
42         if (! -e "$config{srcdir}/$srcfile") {
43                 error(sprintf(gettext("%s is not in the srcdir, so it cannot be renamed"), $srcfile));
44         }
45         elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
46                 error(sprintf(gettext("%s is not a file"), $srcfile));
47         }
49         # Must be editable.
50         IkiWiki::check_canedit($src, $q, $session);
51         if ($attachment) {
52                 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
53                         IkiWiki::Plugin::attachment::check_canattach($session, $src, "$config{srcdir}/$srcfile");
54                 }
55                 else {
56                         error("renaming of attachments is not allowed");
57                 }
58         }
59         
60         # Dest checks can be omitted by passing undef.
61         if (defined $dest) {
62                 if ($srcfile eq $destfile) {
63                         error(gettext("no change to the file name was specified"));
64                 }
66                 # Must be a legal filename.
67                 if (IkiWiki::file_pruned($destfile)) {
68                         error(sprintf(gettext("illegal name")));
69                 }
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, "$config{srcdir}/$srcfile");
88                 }
89         }
91         my $canrename;
92         IkiWiki::run_hooks(canrename => sub {
93                 return if defined $canrename;
94                 my $ret=shift->(cgi => $q, session => $session,
95                         src => $src, srcfile => $srcfile,
96                         dest => $dest, destfile => $destfile);
97                 if (defined $ret) {
98                         if ($ret eq "") {
99                                 $canrename=1;
100                         }
101                         elsif (ref $ret eq 'CODE') {
102                                 $ret->();
103                                 $canrename=0;
104                         }
105                         elsif (defined $ret) {
106                                 error($ret);
107                                 $canrename=0;
108                         }
109                 }
110         });
111         return defined $canrename ? $canrename : 1;
114 sub rename_form ($$$) {
115         my $q=shift;
116         my $session=shift;
117         my $page=shift;
119         eval q{use CGI::FormBuilder};
120         error($@) if $@;
121         my $f = CGI::FormBuilder->new(
122                 name => "rename",
123                 title => sprintf(gettext("rename %s"), pagetitle($page)),
124                 header => 0,
125                 charset => "utf-8",
126                 method => 'POST',
127                 javascript => 0,
128                 params => $q,
129                 action => IkiWiki::cgiurl(),
130                 stylesheet => 1,
131                 fields => [qw{do page new_name attachment}],
132         );
133         
134         $f->field(name => "do", type => "hidden", value => "rename", force => 1);
135         $f->field(name => "sid", type => "hidden", value => $session->id,
136                 force => 1);
137         $f->field(name => "page", type => "hidden", value => $page, force => 1);
138         $f->field(name => "new_name", value => pagetitle($page, 1), size => 60);
139         if (!$q->param("attachment")) {
140                 # insert the standard extensions
141                 my @page_types;
142                 if (exists $IkiWiki::hooks{htmlize}) {
143                         foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
144                                 push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
145                         }
146                 }
147                 @page_types=sort @page_types;
148         
149                 # make sure the current extension is in the list
150                 my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
151                 if (! $IkiWiki::hooks{htmlize}{$ext}) {
152                         unshift(@page_types, [$ext, $ext]);
153                 }
154         
155                 $f->field(name => "type", type => 'select',
156                         options => \@page_types,
157                         value => $ext, force => 1);
158                 
159                 foreach my $p (keys %pagesources) {
160                         if ($pagesources{$p}=~m/^\Q$page\E\//) {
161                                 $f->field(name => "subpages",
162                                         label => "",
163                                         type => "checkbox",
164                                         options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
165                                         value => 1,
166                                         force => 1);
167                                 last;
168                         }
169                 }
170         }
171         $f->field(name => "attachment", type => "hidden");
173         return $f, ["Rename", "Cancel"];
176 sub rename_start ($$$$) {
177         my $q=shift;
178         my $session=shift;
179         my $attachment=shift;
180         my $page=shift;
182         # Special case for renaming held attachments; normal checks
183         # don't apply.
184         my $held=$attachment &&
185                 IkiWiki::Plugin::attachment->can("is_held_attachment") &&
186                 IkiWiki::Plugin::attachment::is_held_attachment($page);
187         if (! $held) {
188                 check_canrename($page, $pagesources{$page}, undef, undef,
189                         $q, $session);
190         }
192         # Save current form state to allow returning to it later
193         # without losing any edits.
194         # (But don't save what button was submitted, to avoid
195         # looping back to here.)
196         # Note: "_submit" is CGI::FormBuilder internals.
197         $q->param(-name => "_submit", -value => "");
198         $session->param(postrename => scalar $q->Vars);
199         IkiWiki::cgi_savesession($session);
200         
201         if (defined $attachment) {
202                 $q->param(-name => "attachment", -value => $attachment);
203         }
204         my ($f, $buttons)=rename_form($q, $session, $page);
205         IkiWiki::showform($f, $buttons, $session, $q);
206         exit 0;
209 sub postrename ($$$;$$) {
210         my $cgi=shift;
211         my $session=shift;
212         my $src=shift;
213         my $dest=shift;
214         my $attachment=shift;
216         # Load saved form state and return to edit page, using stored old
217         # cgi state. Or, if the rename was not started on the edit page, 
218         # return to the renamed page.
219         my $postrename=$session->param("postrename");
220         if (! defined $postrename) {
221                 IkiWiki::redirect($cgi, urlto(defined $dest ? $dest : $src));
222                 exit;
223         }
224         my $oldcgi=CGI->new($postrename);
225         $session->clear("postrename");
226         IkiWiki::cgi_savesession($session);
228         if (defined $dest) {
229                 if (! $attachment) {
230                         # They renamed the page they were editing. This requires
231                         # fixups to the edit form state.
232                         # Tweak the edit form to be editing the new page.
233                         $oldcgi->param("page", $dest);
234                 }
236                 # Update edit form content to fix any links present
237                 # on it.
238                 $oldcgi->param("editcontent",
239                         renamepage_hook($dest, $src, $dest,
240                                  $oldcgi->param("editcontent")));
242                 # Get a new edit token; old was likely invalidated.
243                 $oldcgi->param("rcsinfo",
244                         IkiWiki::rcs_prepedit($pagesources{$dest}));
245         }
247         IkiWiki::cgi_editpage($oldcgi, $session);
250 sub formbuilder (@) {
251         my %params=@_;
252         my $form=$params{form};
254         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
255             $form->field("do") eq "create")) {
256                 IkiWiki::decode_form_utf8($form);
257                 my $q=$params{cgi};
258                 my $session=$params{session};
260                 if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
261                         rename_start($q, $session, 0, $form->field("page"));
262                 }
263                 elsif ($form->submitted eq "Rename Attachment") {
264                         my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
265                         if (@selected > 1) {
266                                 error(gettext("Only one attachment can be renamed at a time."));
267                         }
268                         elsif (! @selected) {
269                                 error(gettext("Please select the attachment to rename."))
270                         }
271                         rename_start($q, $session, 1, $selected[0]);
272                 }
273         }
276 my $renamesummary;
278 sub formbuilder_setup (@) {
279         my %params=@_;
280         my $form=$params{form};
281         my $q=$params{cgi};
283         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
284             $form->field("do") eq "create")) {
285                 # Rename button for the page, and also for attachments.
286                 push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
287                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
289                 if (defined $renamesummary) {
290                         $form->tmpl_param(message => $renamesummary);
291                 }
292         }
295 sub sessioncgi ($$) {
296         my $q=shift;
298         if ($q->param("do") eq 'rename') {
299                 my $session=shift;
300                 my ($form, $buttons)=rename_form($q, $session, Encode::decode_utf8($q->param("page")));
301                 IkiWiki::decode_form_utf8($form);
302                 my $src=$form->field("page");
304                 if ($form->submitted eq 'Cancel') {
305                         postrename($q, $session, $src);
306                 }
307                 elsif ($form->submitted eq 'Rename' && $form->validate) {
308                         IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
310                         # These untaints are safe because of the checks
311                         # performed in check_canrename later.
312                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src})
313                                 if exists $pagesources{$src};
314                         my $dest=IkiWiki::possibly_foolish_untaint(titlepage($form->field("new_name")));
315                         my $destfile=$dest;
316                         if (! $q->param("attachment")) {
317                                 my $type=$q->param('type');
318                                 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
319                                         $type=IkiWiki::possibly_foolish_untaint($type);
320                                 }
321                                 else {
322                                         my ($ext)=$srcfile=~/\.([^.]+)$/;
323                                         $type=$ext;
324                                 }
325                                 
326                                 $destfile=newpagefile($dest, $type);
327                         }
328                 
329                         # Special case for renaming held attachments.
330                         my $held=$q->param("attachment") &&
331                                 IkiWiki::Plugin::attachment->can("is_held_attachment") &&
332                                 IkiWiki::Plugin::attachment::is_held_attachment($src);
333                         if ($held) {
334                                 rename($held, IkiWiki::Plugin::attachment::attachment_holding_location($dest));
335                                 postrename($q, $session, $src, $dest, $q->param("attachment"))
336                                         unless defined $srcfile;
337                         }
338                         
339                         # Queue of rename actions to perfom.
340                         my @torename;
341                         push @torename, {
342                                 src => $src,
343                                 srcfile => $srcfile,
344                                 dest => $dest,
345                                 destfile => $destfile,
346                                 required => 1,
347                         };
349                         @torename=rename_hook(
350                                 torename => \@torename,
351                                 done => {},
352                                 cgi => $q,
353                                 session => $session,
354                         );
356                         require IkiWiki::Render;
357                         IkiWiki::disable_commit_hook() if $config{rcs};
358                         my %origpagesources=%pagesources;
360                         # First file renaming.
361                         foreach my $rename (@torename) {
362                                 if ($rename->{required}) {
363                                         do_rename($rename, $q, $session);
364                                 }
365                                 else {
366                                         eval {do_rename($rename, $q, $session)};
367                                         if ($@) {
368                                                 $rename->{error}=$@;
369                                                 next;
370                                         }
371                                 }
373                                 # Temporarily tweak pagesources to point to
374                                 # the renamed file, in case fixlinks needs
375                                 # to edit it.
376                                 $pagesources{$rename->{src}}=$rename->{destfile};
377                         }
378                         IkiWiki::rcs_commit_staged(
379                                 message => sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
380                                 session => $session,
381                         ) if $config{rcs};
383                         # Then link fixups.
384                         foreach my $rename (@torename) {
385                                 next if $rename->{src} eq $rename->{dest};
386                                 next if $rename->{error};
387                                 foreach my $p (fixlinks($rename, $session)) {
388                                         # map old page names to new
389                                         foreach my $r (@torename) {
390                                                 next if $rename->{error};
391                                                 if ($r->{src} eq $p) {
392                                                         $p=$r->{dest};
393                                                         last;
394                                                 }
395                                         }
396                                         push @{$rename->{fixedlinks}}, $p;
397                                 }
398                         }
400                         # Then refresh.
401                         %pagesources=%origpagesources;
402                         if ($config{rcs}) {
403                                 IkiWiki::enable_commit_hook();
404                                 IkiWiki::rcs_update();
405                         }
406                         IkiWiki::refresh();
407                         IkiWiki::saveindex();
409                         # Find pages with remaining, broken links.
410                         foreach my $rename (@torename) {
411                                 next if $rename->{src} eq $rename->{dest};
412                                 
413                                 foreach my $page (keys %links) {
414                                         my $broken=0;
415                                         foreach my $link (@{$links{$page}}) {
416                                                 my $bestlink=bestlink($page, $link);
417                                                 if ($bestlink eq $rename->{src}) {
418                                                         push @{$rename->{brokenlinks}}, $page;
419                                                         last;
420                                                 }
421                                         }
422                                 }
423                         }
425                         # Generate a summary, that will be shown at the top
426                         # of the edit template.
427                         $renamesummary="";
428                         foreach my $rename (@torename) {
429                                 my $template=template("renamesummary.tmpl");
430                                 $template->param(src => $rename->{srcfile});
431                                 $template->param(dest => $rename->{destfile});
432                                 $template->param(error => $rename->{error});
433                                 if ($rename->{src} ne $rename->{dest}) {
434                                         $template->param(brokenlinks_checked => 1);
435                                         $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
436                                         $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
437                                 }
438                                 $renamesummary.=$template->output;
439                         }
441                         postrename($q, $session, $src, $dest, $q->param("attachment"));
442                 }
443                 else {
444                         IkiWiki::showform($form, $buttons, $session, $q);
445                 }
447                 exit 0;
448         }
451 # Add subpages to the list of pages to be renamed, if needed.
452 sub rename_subpages (@) {
453         my %params = @_;
455         my %torename = %{$params{torename}};
456         my $q = $params{cgi};
457         my $src = $torename{src};
458         my $srcfile = $torename{src};
459         my $dest = $torename{dest};
460         my $destfile = $torename{dest};
462         return () unless ($q->param("subpages") && $src ne $dest);
464         my @ret;
465         foreach my $p (keys %pagesources) {
466                 next unless $pagesources{$p}=~m/^\Q$src\E\//;
467                 # If indexpages is enabled, the srcfile should not be confused
468                 # with a subpage.
469                 next if $pagesources{$p} eq $srcfile;
471                 my $d=$pagesources{$p};
472                 $d=~s/^\Q$src\E\//$dest\//;
473                 push @ret, {
474                         src => $p,
475                         srcfile => $pagesources{$p},
476                         dest => pagename($d),
477                         destfile => $d,
478                         required => 0,
479                 };
480         }
481         return @ret;
484 sub linklist {
485         # generates a list of links in a form suitable for FormBuilder
486         my $dest=shift;
487         my $list=shift;
488         # converts a list of pages into a list of links
489         # in a form suitable for FormBuilder.
491         [map {
492                 {
493                         page => htmllink($dest, $dest, $_,
494                                         noimageinline => 1,
495                                         linktext => pagetitle($_),
496                                 )
497                 }
498         } @{$list}]
501 sub renamepage_hook ($$$$) {
502         my ($page, $src, $dest, $content)=@_;
504         IkiWiki::run_hooks(renamepage => sub {
505                 $content=shift->(
506                         page => $page,
507                         oldpage => $src,
508                         newpage => $dest,
509                         content => $content,
510                 );
511         });
513         return $content;
516 sub rename_hook {
517         my %params = @_;
519         my @torename=@{$params{torename}};
520         my %done=%{$params{done}};
521         my $q=$params{cgi};
522         my $session=$params{session};
524         return () unless @torename;
526         my @nextset;
527         foreach my $torename (@torename) {
528                 unless (exists $done{$torename->{src}} && $done{$torename->{src}}) {
529                         IkiWiki::run_hooks(rename => sub {
530                                 push @nextset, shift->(
531                                         torename => $torename,
532                                         cgi => $q,
533                                         session => $session,
534                                 );
535                         });
536                         $done{$torename->{src}}=1;
537                 }
538         }
540         push @torename, rename_hook(
541                 torename => \@nextset,
542                 done => \%done,
543                 cgi => $q,
544                 session => $session,
545         );
547         # dedup
548         my %seen;
549         return grep { ! $seen{$_->{src}}++ } @torename;
552 sub do_rename ($$$) {
553         my $rename=shift;
554         my $q=shift;
555         my $session=shift;
557         # First, check if this rename is allowed.
558         check_canrename($rename->{src},
559                 $rename->{srcfile},
560                 $rename->{dest},
561                 $rename->{destfile},
562                 $q, $session);
564         # Ensure that the dest directory exists and is ok.
565         IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
567         if ($config{rcs}) {
568                 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
569         }
570         else {
571                 if (! rename($config{srcdir}."/".$rename->{srcfile},
572                              $config{srcdir}."/".$rename->{destfile})) {
573                         error("rename: $!");
574                 }
575         }
579 sub fixlinks ($$$) {
580         my $rename=shift;
581         my $session=shift;
583         my @fixedlinks;
585         foreach my $page (keys %links) {
586                 my $needfix=0;
587                 foreach my $link (@{$links{$page}}) {
588                         my $bestlink=bestlink($page, $link);
589                         if ($bestlink eq $rename->{src}) {
590                                 $needfix=1;
591                                 last;
592                         }
593                 }
594                 if ($needfix) {
595                         my $file=$pagesources{$page};
596                         next unless -e $config{srcdir}."/".$file;
597                         my $oldcontent=readfile($config{srcdir}."/".$file);
598                         my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
599                         if ($oldcontent ne $content) {
600                                 my $token=IkiWiki::rcs_prepedit($file);
601                                 eval { writefile($file, $config{srcdir}, $content) };
602                                 next if $@;
603                                 my $conflict=IkiWiki::rcs_commit(
604                                         file => $file,
605                                         message => sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
606                                         token => $token,
607                                         session => $session,
608                                 );
609                                 push @fixedlinks, $page if ! defined $conflict;
610                         }
611                 }
612         }
614         return @fixedlinks;