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