]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/rename.pm
listdirectives: Avoid listing _comment directives and generally assume any directive...
[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);
14 }
16 sub getsetup () {
17         return 
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                 },
22 }
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;
32         my $attachment=! defined pagetype($pagesources{$src});
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         }
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                 }
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                 }
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 }
92 sub rename_form ($$$) {
93         my $q=shift;
94         my $session=shift;
95         my $page=shift;
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                         foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
120                                 push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
121                         }
122                 }
123                 @page_types=sort @page_types;
124         
125                 # make sure the current extension is in the list
126                 my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
127                 if (! $IkiWiki::hooks{htmlize}{$ext}) {
128                         unshift(@page_types, [$ext, $ext]);
129                 }
130         
131                 $f->field(name => "type", type => 'select',
132                         options => \@page_types,
133                         value => $ext, force => 1);
134                 
135                 foreach my $p (keys %pagesources) {
136                         if ($pagesources{$p}=~m/^\Q$page\E\//) {
137                                 $f->field(name => "subpages",
138                                         label => "",
139                                         type => "checkbox",
140                                         options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
141                                         value => 1,
142                                         force => 1);
143                                 last;
144                         }
145                 }
146         }
147         $f->field(name => "attachment", type => "hidden");
149         return $f, ["Rename", "Cancel"];
152 sub rename_start ($$$$) {
153         my $q=shift;
154         my $session=shift;
155         my $attachment=shift;
156         my $page=shift;
158         check_canrename($page, $pagesources{$page}, undef, undef,
159                 $q, $session);
161         # Save current form state to allow returning to it later
162         # without losing any edits.
163         # (But don't save what button was submitted, to avoid
164         # looping back to here.)
165         # Note: "_submit" is CGI::FormBuilder internals.
166         $q->param(-name => "_submit", -value => "");
167         $session->param(postrename => scalar $q->Vars);
168         IkiWiki::cgi_savesession($session);
169         
170         if (defined $attachment) {
171                 $q->param(-name => "attachment", -value => $attachment);
172         }
173         my ($f, $buttons)=rename_form($q, $session, $page);
174         IkiWiki::showform($f, $buttons, $session, $q);
175         exit 0;
178 sub postrename ($;$$$) {
179         my $session=shift;
180         my $src=shift;
181         my $dest=shift;
182         my $attachment=shift;
184         # Load saved form state and return to edit page.
185         my $postrename=CGI->new($session->param("postrename"));
186         $session->clear("postrename");
187         IkiWiki::cgi_savesession($session);
189         if (defined $dest) {
190                 if (! $attachment) {
191                         # They renamed the page they were editing. This requires
192                         # fixups to the edit form state.
193                         # Tweak the edit form to be editing the new page.
194                         $postrename->param("page", $dest);
195                 }
197                 # Update edit form content to fix any links present
198                 # on it.
199                 $postrename->param("editcontent",
200                         renamepage_hook($dest, $src, $dest,
201                                  $postrename->param("editcontent")));
203                 # Get a new edit token; old was likely invalidated.
204                 $postrename->param("rcsinfo",
205                         IkiWiki::rcs_prepedit($pagesources{$dest}));
206         }
208         IkiWiki::cgi_editpage($postrename, $session);
211 sub formbuilder (@) {
212         my %params=@_;
213         my $form=$params{form};
215         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
216             $form->field("do") eq "create")) {
217                 my $q=$params{cgi};
218                 my $session=$params{session};
220                 if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
221                         rename_start($q, $session, 0, $form->field("page"));
222                 }
223                 elsif ($form->submitted eq "Rename Attachment") {
224                         my @selected=$q->param("attachment_select");
225                         if (@selected > 1) {
226                                 error(gettext("Only one attachment can be renamed at a time."));
227                         }
228                         elsif (! @selected) {
229                                 error(gettext("Please select the attachment to rename."))
230                         }
231                         rename_start($q, $session, 1, $selected[0]);
232                 }
233         }
236 my $renamesummary;
238 sub formbuilder_setup (@) {
239         my %params=@_;
240         my $form=$params{form};
241         my $q=$params{cgi};
243         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
244             $form->field("do") eq "create")) {
245                 # Rename button for the page, and also for attachments.
246                 push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
247                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
249                 if (defined $renamesummary) {
250                         $form->tmpl_param(message => $renamesummary);
251                 }
252         }
255 sub sessioncgi ($$) {
256         my $q=shift;
258         if ($q->param("do") eq 'rename') {
259                 my $session=shift;
260                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
261                 IkiWiki::decode_form_utf8($form);
263                 if ($form->submitted eq 'Cancel') {
264                         postrename($session);
265                 }
266                 elsif ($form->submitted eq 'Rename' && $form->validate) {
267                         # Queue of rename actions to perfom.
268                         my @torename;
270                         # These untaints are safe because of the checks
271                         # performed in check_canrename later.
272                         my $src=$q->param("page");
273                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
274                         my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
275                         my $destfile=$dest;
276                         if (! $q->param("attachment")) {
277                                 my $type=$q->param('type');
278                                 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
279                                         $type=IkiWiki::possibly_foolish_untaint($type);
280                                 }
281                                 else {
282                                         my ($ext)=$srcfile=~/\.([^.]+)$/;
283                                         $type=$ext;
284                                 }
285                                 
286                                 $destfile=newpagefile($dest, $type);
287                         }
288                         push @torename, {
289                                 src => $src,
290                                 srcfile => $srcfile,
291                                 dest => $dest,
292                                 destfile => $destfile,
293                                 required => 1,
294                         };
296                         # See if any subpages need to be renamed.
297                         if ($q->param("subpages") && $src ne $dest) {
298                                 foreach my $p (keys %pagesources) {
299                                         next unless $pagesources{$p}=~m/^\Q$src\E\//;
300                                         # If indexpages is enabled, the
301                                         # srcfile should not be confused
302                                         # with a subpage.
303                                         next if $pagesources{$p} eq $srcfile;
305                                         my $d=$pagesources{$p};
306                                         $d=~s/^\Q$src\E\//$dest\//;
307                                         push @torename, {
308                                                 src => $p,
309                                                 srcfile => $pagesources{$p},
310                                                 dest => pagename($d),
311                                                 destfile => $d,
312                                                 required => 0,
313                                         };
314                                 }
315                         }
316                         
317                         require IkiWiki::Render;
318                         IkiWiki::disable_commit_hook() if $config{rcs};
319                         my %origpagesources=%pagesources;
321                         # First file renaming.
322                         foreach my $rename (@torename) {
323                                 if ($rename->{required}) {
324                                         do_rename($rename, $q, $session);
325                                 }
326                                 else {
327                                         eval {do_rename($rename, $q, $session)};
328                                         if ($@) {
329                                                 $rename->{error}=$@;
330                                                 next;
331                                         }
332                                 }
334                                 # Temporarily tweak pagesources to point to
335                                 # the renamed file, in case fixlinks needs
336                                 # to edit it.
337                                 $pagesources{$rename->{src}}=$rename->{destfile};
338                         }
339                         IkiWiki::rcs_commit_staged(
340                                 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
341                                 $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
343                         # Then link fixups.
344                         foreach my $rename (@torename) {
345                                 next if $rename->{src} eq $rename->{dest};
346                                 next if $rename->{error};
347                                 foreach my $p (fixlinks($rename, $session)) {
348                                         # map old page names to new
349                                         foreach my $r (@torename) {
350                                                 next if $rename->{error};
351                                                 if ($r->{src} eq $p) {
352                                                         $p=$r->{dest};
353                                                         last;
354                                                 }
355                                         }
356                                         push @{$rename->{fixedlinks}}, $p;
357                                 }
358                         }
360                         # Then refresh.
361                         %pagesources=%origpagesources;
362                         if ($config{rcs}) {
363                                 IkiWiki::enable_commit_hook();
364                                 IkiWiki::rcs_update();
365                         }
366                         IkiWiki::refresh();
367                         IkiWiki::saveindex();
369                         # Find pages with remaining, broken links.
370                         foreach my $rename (@torename) {
371                                 next if $rename->{src} eq $rename->{dest};
372                                 
373                                 foreach my $page (keys %links) {
374                                         my $broken=0;
375                                         foreach my $link (@{$links{$page}}) {
376                                                 my $bestlink=bestlink($page, $link);
377                                                 if ($bestlink eq $rename->{src}) {
378                                                         push @{$rename->{brokenlinks}}, $page;
379                                                         last;
380                                                 }
381                                         }
382                                 }
383                         }
385                         # Generate a summary, that will be shown at the top
386                         # of the edit template.
387                         $renamesummary="";
388                         foreach my $rename (@torename) {
389                                 my $template=template("renamesummary.tmpl");
390                                 $template->param(src => $rename->{srcfile});
391                                 $template->param(dest => $rename->{destfile});
392                                 $template->param(error => $rename->{error});
393                                 if ($rename->{src} ne $rename->{dest}) {
394                                         $template->param(brokenlinks_checked => 1);
395                                         $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
396                                         $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
397                                 }
398                                 $renamesummary.=$template->output;
399                         }
401                         postrename($session, $src, $dest, $q->param("attachment"));
402                 }
403                 else {
404                         IkiWiki::showform($form, $buttons, $session, $q);
405                 }
407                 exit 0;
408         }
410                                                 
411 sub linklist {
412         # generates a list of links in a form suitable for FormBuilder
413         my $dest=shift;
414         my $list=shift;
415         # converts a list of pages into a list of links
416         # in a form suitable for FormBuilder.
418         [map {
419                 {
420                         page => htmllink($dest, $dest, $_,
421                                         noimageinline => 1,
422                                         linktext => pagetitle($_),
423                                 )
424                 }
425         } @{$list}]
428 sub renamepage_hook ($$$$) {
429         my ($page, $src, $dest, $content)=@_;
431         IkiWiki::run_hooks(renamepage => sub {
432                 $content=shift->(
433                         page => $page,
434                         oldpage => $src,
435                         newpage => $dest,
436                         content => $content,
437                 );
438         });
440         return $content;
442                         
443 sub do_rename ($$$) {
444         my $rename=shift;
445         my $q=shift;
446         my $session=shift;
448         # First, check if this rename is allowed.
449         check_canrename($rename->{src},
450                 $rename->{srcfile},
451                 $rename->{dest},
452                 $rename->{destfile},
453                 $q, $session);
455         # Ensure that the dest directory exists and is ok.
456         IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
458         if ($config{rcs}) {
459                 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
460         }
461         else {
462                 if (! rename($config{srcdir}."/".$rename->{srcfile},
463                              $config{srcdir}."/".$rename->{destfile})) {
464                         error("rename: $!");
465                 }
466         }
470 sub fixlinks ($$$) {
471         my $rename=shift;
472         my $session=shift;
474         my @fixedlinks;
476         foreach my $page (keys %links) {
477                 my $needfix=0;
478                 foreach my $link (@{$links{$page}}) {
479                         my $bestlink=bestlink($page, $link);
480                         if ($bestlink eq $rename->{src}) {
481                                 $needfix=1;
482                                 last;
483                         }
484                 }
485                 if ($needfix) {
486                         my $file=$pagesources{$page};
487                         my $oldcontent=readfile($config{srcdir}."/".$file);
488                         my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
489                         if ($oldcontent ne $content) {
490                                 my $token=IkiWiki::rcs_prepedit($file);
491                                 eval { writefile($file, $config{srcdir}, $content) };
492                                 next if $@;
493                                 my $conflict=IkiWiki::rcs_commit(
494                                         $file,
495                                         sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
496                                         $token,
497                                         $session->param("name"), 
498                                         $ENV{REMOTE_ADDR}
499                                 );
500                                 push @fixedlinks, $page if ! defined $conflict;
501                         }
502                 }
503         }
505         return @fixedlinks;