]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/rename.pm
Merge branch 'file_pruned_revamp'
[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         });
113 sub rename_form ($$$) {
114         my $q=shift;
115         my $session=shift;
116         my $page=shift;
118         eval q{use CGI::FormBuilder};
119         error($@) if $@;
120         my $f = CGI::FormBuilder->new(
121                 name => "rename",
122                 title => sprintf(gettext("rename %s"), pagetitle($page)),
123                 header => 0,
124                 charset => "utf-8",
125                 method => 'POST',
126                 javascript => 0,
127                 params => $q,
128                 action => $config{cgiurl},
129                 stylesheet => IkiWiki::baseurl()."style.css",
130                 fields => [qw{do page new_name attachment}],
131         );
132         
133         $f->field(name => "do", type => "hidden", value => "rename", force => 1);
134         $f->field(name => "page", type => "hidden", value => $page, force => 1);
135         $f->field(name => "new_name", value => pagetitle($page, 1), size => 60);
136         if (!$q->param("attachment")) {
137                 # insert the standard extensions
138                 my @page_types;
139                 if (exists $IkiWiki::hooks{htmlize}) {
140                         foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
141                                 push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
142                         }
143                 }
144                 @page_types=sort @page_types;
145         
146                 # make sure the current extension is in the list
147                 my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
148                 if (! $IkiWiki::hooks{htmlize}{$ext}) {
149                         unshift(@page_types, [$ext, $ext]);
150                 }
151         
152                 $f->field(name => "type", type => 'select',
153                         options => \@page_types,
154                         value => $ext, force => 1);
155                 
156                 foreach my $p (keys %pagesources) {
157                         if ($pagesources{$p}=~m/^\Q$page\E\//) {
158                                 $f->field(name => "subpages",
159                                         label => "",
160                                         type => "checkbox",
161                                         options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
162                                         value => 1,
163                                         force => 1);
164                                 last;
165                         }
166                 }
167         }
168         $f->field(name => "attachment", type => "hidden");
170         return $f, ["Rename", "Cancel"];
173 sub rename_start ($$$$) {
174         my $q=shift;
175         my $session=shift;
176         my $attachment=shift;
177         my $page=shift;
179         check_canrename($page, $pagesources{$page}, undef, undef,
180                 $q, $session);
182         # Save current form state to allow returning to it later
183         # without losing any edits.
184         # (But don't save what button was submitted, to avoid
185         # looping back to here.)
186         # Note: "_submit" is CGI::FormBuilder internals.
187         $q->param(-name => "_submit", -value => "");
188         $session->param(postrename => scalar $q->Vars);
189         IkiWiki::cgi_savesession($session);
190         
191         if (defined $attachment) {
192                 $q->param(-name => "attachment", -value => $attachment);
193         }
194         my ($f, $buttons)=rename_form($q, $session, $page);
195         IkiWiki::showform($f, $buttons, $session, $q);
196         exit 0;
199 sub postrename ($;$$$) {
200         my $session=shift;
201         my $src=shift;
202         my $dest=shift;
203         my $attachment=shift;
205         # Load saved form state and return to edit page.
206         my $postrename=CGI->new($session->param("postrename"));
207         $session->clear("postrename");
208         IkiWiki::cgi_savesession($session);
210         if (defined $dest) {
211                 if (! $attachment) {
212                         # They renamed the page they were editing. This requires
213                         # fixups to the edit form state.
214                         # Tweak the edit form to be editing the new page.
215                         $postrename->param("page", $dest);
216                 }
218                 # Update edit form content to fix any links present
219                 # on it.
220                 $postrename->param("editcontent",
221                         renamepage_hook($dest, $src, $dest,
222                                  $postrename->param("editcontent")));
224                 # Get a new edit token; old was likely invalidated.
225                 $postrename->param("rcsinfo",
226                         IkiWiki::rcs_prepedit($pagesources{$dest}));
227         }
229         IkiWiki::cgi_editpage($postrename, $session);
232 sub formbuilder (@) {
233         my %params=@_;
234         my $form=$params{form};
236         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
237             $form->field("do") eq "create")) {
238                 IkiWiki::decode_form_utf8($form);
239                 my $q=$params{cgi};
240                 my $session=$params{session};
242                 if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
243                         rename_start($q, $session, 0, $form->field("page"));
244                 }
245                 elsif ($form->submitted eq "Rename Attachment") {
246                         my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
247                         if (@selected > 1) {
248                                 error(gettext("Only one attachment can be renamed at a time."));
249                         }
250                         elsif (! @selected) {
251                                 error(gettext("Please select the attachment to rename."))
252                         }
253                         rename_start($q, $session, 1, $selected[0]);
254                 }
255         }
258 my $renamesummary;
260 sub formbuilder_setup (@) {
261         my %params=@_;
262         my $form=$params{form};
263         my $q=$params{cgi};
265         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
266             $form->field("do") eq "create")) {
267                 # Rename button for the page, and also for attachments.
268                 push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
269                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
271                 if (defined $renamesummary) {
272                         $form->tmpl_param(message => $renamesummary);
273                 }
274         }
277 sub sessioncgi ($$) {
278         my $q=shift;
280         if ($q->param("do") eq 'rename') {
281                 my $session=shift;
282                 my ($form, $buttons)=rename_form($q, $session, Encode::decode_utf8($q->param("page")));
283                 IkiWiki::decode_form_utf8($form);
285                 if ($form->submitted eq 'Cancel') {
286                         postrename($session);
287                 }
288                 elsif ($form->submitted eq 'Rename' && $form->validate) {
289                         # Queue of rename actions to perfom.
290                         my @torename;
292                         # These untaints are safe because of the checks
293                         # performed in check_canrename later.
294                         my $src=$form->field("page");
295                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
296                         my $dest=IkiWiki::possibly_foolish_untaint(titlepage($form->field("new_name")));
297                         my $destfile=$dest;
298                         if (! $q->param("attachment")) {
299                                 my $type=$q->param('type');
300                                 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
301                                         $type=IkiWiki::possibly_foolish_untaint($type);
302                                 }
303                                 else {
304                                         my ($ext)=$srcfile=~/\.([^.]+)$/;
305                                         $type=$ext;
306                                 }
307                                 
308                                 $destfile=newpagefile($dest, $type);
309                         }
310                         push @torename, {
311                                 src => $src,
312                                 srcfile => $srcfile,
313                                 dest => $dest,
314                                 destfile => $destfile,
315                                 required => 1,
316                         };
318                         @torename=rename_hook(
319                                 torename => \@torename,
320                                 done => {},
321                                 cgi => $q,
322                                 session => $session,
323                         );
325                         require IkiWiki::Render;
326                         IkiWiki::disable_commit_hook() if $config{rcs};
327                         my %origpagesources=%pagesources;
329                         # First file renaming.
330                         foreach my $rename (@torename) {
331                                 if ($rename->{required}) {
332                                         do_rename($rename, $q, $session);
333                                 }
334                                 else {
335                                         eval {do_rename($rename, $q, $session)};
336                                         if ($@) {
337                                                 $rename->{error}=$@;
338                                                 next;
339                                         }
340                                 }
342                                 # Temporarily tweak pagesources to point to
343                                 # the renamed file, in case fixlinks needs
344                                 # to edit it.
345                                 $pagesources{$rename->{src}}=$rename->{destfile};
346                         }
347                         IkiWiki::rcs_commit_staged(
348                                 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
349                                 $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
351                         # Then link fixups.
352                         foreach my $rename (@torename) {
353                                 next if $rename->{src} eq $rename->{dest};
354                                 next if $rename->{error};
355                                 foreach my $p (fixlinks($rename, $session)) {
356                                         # map old page names to new
357                                         foreach my $r (@torename) {
358                                                 next if $rename->{error};
359                                                 if ($r->{src} eq $p) {
360                                                         $p=$r->{dest};
361                                                         last;
362                                                 }
363                                         }
364                                         push @{$rename->{fixedlinks}}, $p;
365                                 }
366                         }
368                         # Then refresh.
369                         %pagesources=%origpagesources;
370                         if ($config{rcs}) {
371                                 IkiWiki::enable_commit_hook();
372                                 IkiWiki::rcs_update();
373                         }
374                         IkiWiki::refresh();
375                         IkiWiki::saveindex();
377                         # Find pages with remaining, broken links.
378                         foreach my $rename (@torename) {
379                                 next if $rename->{src} eq $rename->{dest};
380                                 
381                                 foreach my $page (keys %links) {
382                                         my $broken=0;
383                                         foreach my $link (@{$links{$page}}) {
384                                                 my $bestlink=bestlink($page, $link);
385                                                 if ($bestlink eq $rename->{src}) {
386                                                         push @{$rename->{brokenlinks}}, $page;
387                                                         last;
388                                                 }
389                                         }
390                                 }
391                         }
393                         # Generate a summary, that will be shown at the top
394                         # of the edit template.
395                         $renamesummary="";
396                         foreach my $rename (@torename) {
397                                 my $template=template("renamesummary.tmpl");
398                                 $template->param(src => $rename->{srcfile});
399                                 $template->param(dest => $rename->{destfile});
400                                 $template->param(error => $rename->{error});
401                                 if ($rename->{src} ne $rename->{dest}) {
402                                         $template->param(brokenlinks_checked => 1);
403                                         $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
404                                         $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
405                                 }
406                                 $renamesummary.=$template->output;
407                         }
409                         postrename($session, $src, $dest, $q->param("attachment"));
410                 }
411                 else {
412                         IkiWiki::showform($form, $buttons, $session, $q);
413                 }
415                 exit 0;
416         }
419 # Add subpages to the list of pages to be renamed, if needed.
420 sub rename_subpages (@) {
421         my %params = @_;
423         my %torename = %{$params{torename}};
424         my $q = $params{cgi};
425         my $src = $torename{src};
426         my $srcfile = $torename{src};
427         my $dest = $torename{dest};
428         my $destfile = $torename{dest};
430         return () unless ($q->param("subpages") && $src ne $dest);
432         my @ret;
433         foreach my $p (keys %pagesources) {
434                 next unless $pagesources{$p}=~m/^\Q$src\E\//;
435                 # If indexpages is enabled, the srcfile should not be confused
436                 # with a subpage.
437                 next if $pagesources{$p} eq $srcfile;
439                 my $d=$pagesources{$p};
440                 $d=~s/^\Q$src\E\//$dest\//;
441                 push @ret, {
442                         src => $p,
443                         srcfile => $pagesources{$p},
444                         dest => pagename($d),
445                         destfile => $d,
446                         required => 0,
447                 };
448         }
449         return @ret;
452 sub linklist {
453         # generates a list of links in a form suitable for FormBuilder
454         my $dest=shift;
455         my $list=shift;
456         # converts a list of pages into a list of links
457         # in a form suitable for FormBuilder.
459         [map {
460                 {
461                         page => htmllink($dest, $dest, $_,
462                                         noimageinline => 1,
463                                         linktext => pagetitle($_),
464                                 )
465                 }
466         } @{$list}]
469 sub renamepage_hook ($$$$) {
470         my ($page, $src, $dest, $content)=@_;
472         IkiWiki::run_hooks(renamepage => sub {
473                 $content=shift->(
474                         page => $page,
475                         oldpage => $src,
476                         newpage => $dest,
477                         content => $content,
478                 );
479         });
481         return $content;
484 sub rename_hook {
485         my %params = @_;
487         my @torename=@{$params{torename}};
488         my %done=%{$params{done}};
489         my $q=$params{cgi};
490         my $session=$params{session};
492         return () unless @torename;
494         my @nextset;
495         foreach my $torename (@torename) {
496                 unless (exists $done{$torename->{src}} && $done{$torename->{src}}) {
497                         IkiWiki::run_hooks(rename => sub {
498                                 push @nextset, shift->(
499                                         torename => $torename,
500                                         cgi => $q,
501                                         session => $session,
502                                 );
503                         });
504                         $done{$torename->{src}}=1;
505                 }
506         }
508         push @torename, rename_hook(
509                 torename => \@nextset,
510                 done => \%done,
511                 cgi => $q,
512                 session => $session,
513         );
515         # dedup
516         my %seen;
517         return grep { ! $seen{$_->{src}}++ } @torename;
520 sub do_rename ($$$) {
521         my $rename=shift;
522         my $q=shift;
523         my $session=shift;
525         # First, check if this rename is allowed.
526         check_canrename($rename->{src},
527                 $rename->{srcfile},
528                 $rename->{dest},
529                 $rename->{destfile},
530                 $q, $session);
532         # Ensure that the dest directory exists and is ok.
533         IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
535         if ($config{rcs}) {
536                 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
537         }
538         else {
539                 if (! rename($config{srcdir}."/".$rename->{srcfile},
540                              $config{srcdir}."/".$rename->{destfile})) {
541                         error("rename: $!");
542                 }
543         }
547 sub fixlinks ($$$) {
548         my $rename=shift;
549         my $session=shift;
551         my @fixedlinks;
553         foreach my $page (keys %links) {
554                 my $needfix=0;
555                 foreach my $link (@{$links{$page}}) {
556                         my $bestlink=bestlink($page, $link);
557                         if ($bestlink eq $rename->{src}) {
558                                 $needfix=1;
559                                 last;
560                         }
561                 }
562                 if ($needfix) {
563                         my $file=$pagesources{$page};
564                         my $oldcontent=readfile($config{srcdir}."/".$file);
565                         my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
566                         if ($oldcontent ne $content) {
567                                 my $token=IkiWiki::rcs_prepedit($file);
568                                 eval { writefile($file, $config{srcdir}, $content) };
569                                 next if $@;
570                                 my $conflict=IkiWiki::rcs_commit(
571                                         $file,
572                                         sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
573                                         $token,
574                                         $session->param("name"), 
575                                         $ENV{REMOTE_ADDR}
576                                 );
577                                 push @fixedlinks, $page if ! defined $conflict;
578                         }
579                 }
580         }
582         return @fixedlinks;