]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/rename.pm
po: prevent a slave page to depend on itself
[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 2.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), 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");
147         return $f, ["Rename", "Cancel"];
148 } #}}}
150 sub rename_start ($$$$) { #{{{
151         my $q=shift;
152         my $session=shift;
153         my $attachment=shift;
154         my $page=shift;
156         check_canrename($page, $pagesources{$page}, undef, undef,
157                 $q, $session);
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 } #}}}
176 sub postrename ($;$$$) { #{{{
177         my $session=shift;
178         my $src=shift;
179         my $dest=shift;
180         my $attachment=shift;
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);
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                 }
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")));
201                 # Get a new edit token; old was likely invalidated.
202                 $postrename->param("rcsinfo",
203                         IkiWiki::rcs_prepedit($pagesources{$dest}));
204         }
206         IkiWiki::cgi_editpage($postrename, $session);
207 } #}}}
209 sub formbuilder (@) { #{{{
210         my %params=@_;
211         my $form=$params{form};
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};
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 } #}}}
234 my $renamesummary;
236 sub formbuilder_setup (@) { #{{{
237         my %params=@_;
238         my $form=$params{form};
239         my $q=$params{cgi};
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" />');
247                 if (defined $renamesummary) {
248                         $form->tmpl_param(message => $renamesummary);
249                 }
250         }
251 } #}}}
253 sub sessioncgi ($$) { #{{{
254         my $q=shift;
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);
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;
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                         };
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;
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;
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                                 }
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};
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                         }
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();
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                         }
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 => [
394                                                 map {
395                                                         {
396                                                                 page => htmllink($rename->{dest}, $rename->{dest}, $_,
397                                                                                 noimageinline => 1)
398                                                         }
399                                                 } @{$rename->{brokenlinks}}
400                                         ]);
401                                         $template->param(fixedlinks => [
402                                                 map {
403                                                         {
404                                                                 page => htmllink($rename->{dest}, $rename->{dest}, $_,
405                                                                                 noimageinline => 1)
406                                                         }
407                                                 } @{$rename->{fixedlinks}}
408                                         ]);
409                                 }
410                                 $renamesummary.=$template->output;
411                         }
413                         postrename($session, $src, $dest, $q->param("attachment"));
414                 }
415                 else {
416                         IkiWiki::showform($form, $buttons, $session, $q);
417                 }
419                 exit 0;
420         }
421 } #}}}
423 sub renamepage_hook ($$$$) { #{{{
424         my ($page, $src, $dest, $content)=@_;
426         IkiWiki::run_hooks(renamepage => sub {
427                 $content=shift->(
428                         page => $page,
429                         oldpage => $src,
430                         newpage => $dest,
431                         content => $content,
432                 );
433         });
435         return $content;
436 }# }}}
437                         
438 sub do_rename ($$$) { #{{{
439         my $rename=shift;
440         my $q=shift;
441         my $session=shift;
443         # First, check if this rename is allowed.
444         check_canrename($rename->{src},
445                 $rename->{srcfile},
446                 $rename->{dest},
447                 $rename->{destfile},
448                 $q, $session);
450         # Ensure that the dest directory exists and is ok.
451         IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
453         if ($config{rcs}) {
454                 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
455         }
456         else {
457                 if (! rename($config{srcdir}."/".$rename->{srcfile},
458                              $config{srcdir}."/".$rename->{destfile})) {
459                         error("rename: $!");
460                 }
461         }
463 } # }}}
465 sub fixlinks ($$$) { #{{{
466         my $rename=shift;
467         my $session=shift;
469         my @fixedlinks;
471         foreach my $page (keys %links) {
472                 my $needfix=0;
473                 foreach my $link (@{$links{$page}}) {
474                         my $bestlink=bestlink($page, $link);
475                         if ($bestlink eq $rename->{src}) {
476                                 $needfix=1;
477                                 last;
478                         }
479                 }
480                 if ($needfix) {
481                         my $file=$pagesources{$page};
482                         my $oldcontent=readfile($config{srcdir}."/".$file);
483                         my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
484                         if ($oldcontent ne $content) {
485                                 my $token=IkiWiki::rcs_prepedit($file);
486                                 eval { writefile($file, $config{srcdir}, $content) };
487                                 next if $@;
488                                 my $conflict=IkiWiki::rcs_commit(
489                                         $file,
490                                         sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
491                                         $token,
492                                         $session->param("name"), 
493                                         $ENV{REMOTE_ADDR}
494                                 );
495                                 push @fixedlinks, $page if ! defined $conflict;
496                         }
497                 }
498         }
500         return @fixedlinks;
501 } #}}}