]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/rename.pm
po: fix canrename hook
[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         }
91         my $canrename;
92         IkiWiki::run_hooks(canrename => sub {
93                 return if defined $canrename;
94                 my $ret=shift->($src, $q, $session);
95                 if (defined $ret) {
96                         if ($ret eq "") {
97                                 $canrename=1;
98                         }
99                         elsif (ref $ret eq 'CODE') {
100                                 $ret->();
101                                 $canrename=0;
102                         }
103                         elsif (defined $ret) {
104                                 error($ret);
105                                 $canrename=0;
106                         }
107                 }
108         });
109         return $canrename;
112 sub rename_form ($$$) {
113         my $q=shift;
114         my $session=shift;
115         my $page=shift;
117         eval q{use CGI::FormBuilder};
118         error($@) if $@;
119         my $f = CGI::FormBuilder->new(
120                 name => "rename",
121                 title => sprintf(gettext("rename %s"), pagetitle($page)),
122                 header => 0,
123                 charset => "utf-8",
124                 method => 'POST',
125                 javascript => 0,
126                 params => $q,
127                 action => $config{cgiurl},
128                 stylesheet => IkiWiki::baseurl()."style.css",
129                 fields => [qw{do page new_name attachment}],
130         );
131         
132         $f->field(name => "do", type => "hidden", value => "rename", force => 1);
133         $f->field(name => "page", type => "hidden", value => $page, force => 1);
134         $f->field(name => "new_name", value => pagetitle($page, 1), size => 60);
135         if (!$q->param("attachment")) {
136                 # insert the standard extensions
137                 my @page_types;
138                 if (exists $IkiWiki::hooks{htmlize}) {
139                         @page_types=grep { !/^_/ }
140                                 keys %{$IkiWiki::hooks{htmlize}};
141                 }
142         
143                 # make sure the current extension is in the list
144                 my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
145                 if (! $IkiWiki::hooks{htmlize}{$ext}) {
146                         unshift(@page_types, $ext);
147                 }
148         
149                 $f->field(name => "type", type => 'select',
150                         options => \@page_types,
151                         value => $ext, force => 1);
152                 
153                 foreach my $p (keys %pagesources) {
154                         if ($pagesources{$p}=~m/^\Q$page\E\//) {
155                                 $f->field(name => "subpages",
156                                         label => "",
157                                         type => "checkbox",
158                                         options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
159                                         value => 1,
160                                         force => 1);
161                                 last;
162                         }
163                 }
164         }
165         $f->field(name => "attachment", type => "hidden");
167         return $f, ["Rename", "Cancel"];
170 sub rename_start ($$$$) {
171         my $q=shift;
172         my $session=shift;
173         my $attachment=shift;
174         my $page=shift;
176         check_canrename($page, $pagesources{$page}, undef, undef,
177                 $q, $session);
179         # Save current form state to allow returning to it later
180         # without losing any edits.
181         # (But don't save what button was submitted, to avoid
182         # looping back to here.)
183         # Note: "_submit" is CGI::FormBuilder internals.
184         $q->param(-name => "_submit", -value => "");
185         $session->param(postrename => scalar $q->Vars);
186         IkiWiki::cgi_savesession($session);
187         
188         if (defined $attachment) {
189                 $q->param(-name => "attachment", -value => $attachment);
190         }
191         my ($f, $buttons)=rename_form($q, $session, $page);
192         IkiWiki::showform($f, $buttons, $session, $q);
193         exit 0;
196 sub postrename ($;$$$) {
197         my $session=shift;
198         my $src=shift;
199         my $dest=shift;
200         my $attachment=shift;
202         # Load saved form state and return to edit page.
203         my $postrename=CGI->new($session->param("postrename"));
204         $session->clear("postrename");
205         IkiWiki::cgi_savesession($session);
207         if (defined $dest) {
208                 if (! $attachment) {
209                         # They renamed the page they were editing. This requires
210                         # fixups to the edit form state.
211                         # Tweak the edit form to be editing the new page.
212                         $postrename->param("page", $dest);
213                 }
215                 # Update edit form content to fix any links present
216                 # on it.
217                 $postrename->param("editcontent",
218                         renamelink_hook($dest, $src, $dest,
219                                  $postrename->param("editcontent")));
221                 # Get a new edit token; old was likely invalidated.
222                 $postrename->param("rcsinfo",
223                         IkiWiki::rcs_prepedit($pagesources{$dest}));
224         }
226         IkiWiki::cgi_editpage($postrename, $session);
229 sub formbuilder (@) {
230         my %params=@_;
231         my $form=$params{form};
233         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
234             $form->field("do") eq "create")) {
235                 my $q=$params{cgi};
236                 my $session=$params{session};
238                 if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
239                         rename_start($q, $session, 0, $form->field("page"));
240                 }
241                 elsif ($form->submitted eq "Rename Attachment") {
242                         my @selected=$q->param("attachment_select");
243                         if (@selected > 1) {
244                                 error(gettext("Only one attachment can be renamed at a time."));
245                         }
246                         elsif (! @selected) {
247                                 error(gettext("Please select the attachment to rename."))
248                         }
249                         rename_start($q, $session, 1, $selected[0]);
250                 }
251         }
254 my $renamesummary;
256 sub formbuilder_setup (@) {
257         my %params=@_;
258         my $form=$params{form};
259         my $q=$params{cgi};
261         if (defined $form->field("do") && ($form->field("do") eq "edit" ||
262             $form->field("do") eq "create")) {
263                 # Rename button for the page, and also for attachments.
264                 push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
265                 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
267                 if (defined $renamesummary) {
268                         $form->tmpl_param(message => $renamesummary);
269                 }
270         }
273 sub sessioncgi ($$) {
274         my $q=shift;
276         if ($q->param("do") eq 'rename') {
277                 my $session=shift;
278                 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
279                 IkiWiki::decode_form_utf8($form);
281                 if ($form->submitted eq 'Cancel') {
282                         postrename($session);
283                 }
284                 elsif ($form->submitted eq 'Rename' && $form->validate) {
285                         # Queue of rename actions to perfom.
286                         my @torename;
288                         # These untaints are safe because of the checks
289                         # performed in check_canrename later.
290                         my $src=$q->param("page");
291                         my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
292                         my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
293                         my $destfile=$dest;
294                         if (! $q->param("attachment")) {
295                                 my $type=$q->param('type');
296                                 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
297                                         $type=IkiWiki::possibly_foolish_untaint($type);
298                                 }
299                                 else {
300                                         my ($ext)=$srcfile=~/\.([^.]+)$/;
301                                         $type=$ext;
302                                 }
303                                 
304                                 $destfile=newpagefile($dest, $type);
305                         }
306                         push @torename, {
307                                 src => $src,
308                                 srcfile => $srcfile,
309                                 dest => $dest,
310                                 destfile => $destfile,
311                                 required => 1,
312                         };
314                         IkiWiki::run_hooks(rename => sub { shift->(\@torename, $q, $session); });
316                         # See if any subpages need to be renamed.
317                         if ($q->param("subpages") && $src ne $dest) {
318                                 foreach my $p (keys %pagesources) {
319                                         next unless $pagesources{$p}=~m/^\Q$src\E\//;
320                                         # If indexpages is enabled, the
321                                         # srcfile should not be confused
322                                         # with a subpage.
323                                         next if $pagesources{$p} eq $srcfile;
325                                         my $d=$pagesources{$p};
326                                         $d=~s/^\Q$src\E\//$dest\//;
327                                         push @torename, {
328                                                 src => $p,
329                                                 srcfile => $pagesources{$p},
330                                                 dest => pagename($d),
331                                                 destfile => $d,
332                                                 required => 0,
333                                         };
334                                 }
335                         }
336                         
337                         require IkiWiki::Render;
338                         IkiWiki::disable_commit_hook() if $config{rcs};
339                         my %origpagesources=%pagesources;
341                         # First file renaming.
342                         foreach my $rename (@torename) {
343                                 if ($rename->{required}) {
344                                         do_rename($rename, $q, $session);
345                                 }
346                                 else {
347                                         eval {do_rename($rename, $q, $session)};
348                                         if ($@) {
349                                                 $rename->{error}=$@;
350                                                 next;
351                                         }
352                                 }
354                                 # Temporarily tweak pagesources to point to
355                                 # the renamed file, in case fixlinks needs
356                                 # to edit it.
357                                 $pagesources{$rename->{src}}=$rename->{destfile};
358                         }
359                         IkiWiki::rcs_commit_staged(
360                                 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
361                                 $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
363                         # Then link fixups.
364                         foreach my $rename (@torename) {
365                                 next if $rename->{src} eq $rename->{dest};
366                                 next if $rename->{error};
367                                 foreach my $p (fixlinks($rename, $session)) {
368                                         # map old page names to new
369                                         foreach my $r (@torename) {
370                                                 next if $rename->{error};
371                                                 if ($r->{src} eq $p) {
372                                                         $p=$r->{dest};
373                                                         last;
374                                                 }
375                                         }
376                                         push @{$rename->{fixedlinks}}, $p;
377                                 }
378                         }
380                         # Then refresh.
381                         %pagesources=%origpagesources;
382                         if ($config{rcs}) {
383                                 IkiWiki::enable_commit_hook();
384                                 IkiWiki::rcs_update();
385                         }
386                         IkiWiki::refresh();
387                         IkiWiki::saveindex();
389                         # Find pages with remaining, broken links.
390                         foreach my $rename (@torename) {
391                                 next if $rename->{src} eq $rename->{dest};
392                                 
393                                 foreach my $page (keys %links) {
394                                         my $broken=0;
395                                         foreach my $link (@{$links{$page}}) {
396                                                 my $bestlink=bestlink($page, $link);
397                                                 if ($bestlink eq $rename->{src}) {
398                                                         push @{$rename->{brokenlinks}}, $page;
399                                                         last;
400                                                 }
401                                         }
402                                 }
403                         }
405                         # Generate a summary, that will be shown at the top
406                         # of the edit template.
407                         $renamesummary="";
408                         foreach my $rename (@torename) {
409                                 my $template=template("renamesummary.tmpl");
410                                 $template->param(src => $rename->{srcfile});
411                                 $template->param(dest => $rename->{destfile});
412                                 $template->param(error => $rename->{error});
413                                 if ($rename->{src} ne $rename->{dest}) {
414                                         $template->param(brokenlinks_checked => 1);
415                                         $template->param(brokenlinks => [
416                                                 map {
417                                                         {
418                                                                 page => htmllink($rename->{dest}, $rename->{dest}, $_,
419                                                                                 noimageinline => 1)
420                                                         }
421                                                 } @{$rename->{brokenlinks}}
422                                         ]);
423                                         $template->param(fixedlinks => [
424                                                 map {
425                                                         {
426                                                                 page => htmllink($rename->{dest}, $rename->{dest}, $_,
427                                                                                 noimageinline => 1)
428                                                         }
429                                                 } @{$rename->{fixedlinks}}
430                                         ]);
431                                 }
432                                 $renamesummary.=$template->output;
433                         }
435                         postrename($session, $src, $dest, $q->param("attachment"));
436                 }
437                 else {
438                         IkiWiki::showform($form, $buttons, $session, $q);
439                 }
441                 exit 0;
442         }
445 sub renamelink_hook ($$$$) {
446         my ($page, $src, $dest, $content)=@_;
448         IkiWiki::run_hooks(renamelink => sub {
449                 $content=shift->(
450                         page => $page,
451                         oldpage => $src,
452                         newpage => $dest,
453                         content => $content,
454                 );
455         });
457         return $content;
459                         
460 sub do_rename ($$$) {
461         my $rename=shift;
462         my $q=shift;
463         my $session=shift;
465         # First, check if this rename is allowed.
466         check_canrename($rename->{src},
467                 $rename->{srcfile},
468                 $rename->{dest},
469                 $rename->{destfile},
470                 $q, $session);
472         # Ensure that the dest directory exists and is ok.
473         IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
475         if ($config{rcs}) {
476                 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
477         }
478         else {
479                 if (! rename($config{srcdir}."/".$rename->{srcfile},
480                              $config{srcdir}."/".$rename->{destfile})) {
481                         error("rename: $!");
482                 }
483         }
487 sub fixlinks ($$$) {
488         my $rename=shift;
489         my $session=shift;
491         my @fixedlinks;
493         foreach my $page (keys %links) {
494                 my $needfix=0;
495                 foreach my $link (@{$links{$page}}) {
496                         my $bestlink=bestlink($page, $link);
497                         if ($bestlink eq $rename->{src}) {
498                                 $needfix=1;
499                                 last;
500                         }
501                 }
502                 if ($needfix) {
503                         my $file=$pagesources{$page};
504                         my $oldcontent=readfile($config{srcdir}."/".$file);
505                         my $content=renamelink_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
506                         if ($oldcontent ne $content) {
507                                 my $token=IkiWiki::rcs_prepedit($file);
508                                 eval { writefile($file, $config{srcdir}, $content) };
509                                 next if $@;
510                                 my $conflict=IkiWiki::rcs_commit(
511                                         $file,
512                                         sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
513                                         $token,
514                                         $session->param("name"), 
515                                         $ENV{REMOTE_ADDR}
516                                 );
517                                 push @fixedlinks, $page if ! defined $conflict;
518                         }
519                 }
520         }
522         return @fixedlinks;