2 package IkiWiki::Plugin::rename;
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);
25 sub check_canrename ($$$$$$) {
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)));
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));
45 elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
46 error(sprintf(gettext("%s is not a file"), $srcfile));
50 IkiWiki::check_canedit($src, $q, $session);
52 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
53 IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
56 error("renaming of attachments is not allowed");
60 # Dest checks can be omitted by passing undef.
62 if ($srcfile eq $destfile) {
63 error(gettext("no change to the file name was specified"));
66 # Must be a legal filename, and not absolute.
67 if (IkiWiki::file_pruned($destfile, $config{srcdir}) ||
69 error(sprintf(gettext("illegal name")));
72 # Must not be a known source file.
73 if ($src ne $dest && exists $pagesources{$dest}) {
74 error(sprintf(gettext("%s already exists"),
75 htmllink("", "", $dest, noimageinline => 1)));
78 # Must not exist on disk already.
79 if (-l "$config{srcdir}/$destfile" || -e _) {
80 error(sprintf(gettext("%s already exists on disk"), $destfile));
84 IkiWiki::check_canedit($dest, $q, $session);
86 # Note that $srcfile is used here, not $destfile,
87 # because it wants the current file, to check it.
88 IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
93 IkiWiki::run_hooks(canrename => sub {
94 return if defined $canrename;
95 my $ret=shift->(cgi => $q, session => $session,
96 src => $src, srcfile => $srcfile,
97 dest => $dest, destfile => $destfile);
102 elsif (ref $ret eq 'CODE') {
106 elsif (defined $ret) {
114 sub rename_form ($$$) {
119 eval q{use CGI::FormBuilder};
121 my $f = CGI::FormBuilder->new(
123 title => sprintf(gettext("rename %s"), pagetitle($page)),
129 action => $config{cgiurl},
130 stylesheet => IkiWiki::baseurl()."style.css",
131 fields => [qw{do page new_name attachment}],
134 $f->field(name => "do", type => "hidden", value => "rename", force => 1);
135 $f->field(name => "page", type => "hidden", value => $page, force => 1);
136 $f->field(name => "new_name", value => pagetitle($page, 1), size => 60);
137 if (!$q->param("attachment")) {
138 # insert the standard extensions
140 if (exists $IkiWiki::hooks{htmlize}) {
141 foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
142 push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
145 @page_types=sort @page_types;
147 # make sure the current extension is in the list
148 my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
149 if (! $IkiWiki::hooks{htmlize}{$ext}) {
150 unshift(@page_types, [$ext, $ext]);
153 $f->field(name => "type", type => 'select',
154 options => \@page_types,
155 value => $ext, force => 1);
157 foreach my $p (keys %pagesources) {
158 if ($pagesources{$p}=~m/^\Q$page\E\//) {
159 $f->field(name => "subpages",
162 options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
169 $f->field(name => "attachment", type => "hidden");
171 return $f, ["Rename", "Cancel"];
174 sub rename_start ($$$$) {
177 my $attachment=shift;
180 check_canrename($page, $pagesources{$page}, undef, undef,
183 # Save current form state to allow returning to it later
184 # without losing any edits.
185 # (But don't save what button was submitted, to avoid
186 # looping back to here.)
187 # Note: "_submit" is CGI::FormBuilder internals.
188 $q->param(-name => "_submit", -value => "");
189 $session->param(postrename => scalar $q->Vars);
190 IkiWiki::cgi_savesession($session);
192 if (defined $attachment) {
193 $q->param(-name => "attachment", -value => $attachment);
195 my ($f, $buttons)=rename_form($q, $session, $page);
196 IkiWiki::showform($f, $buttons, $session, $q);
200 sub postrename ($;$$$) {
204 my $attachment=shift;
206 # Load saved form state and return to edit page.
207 my $postrename=CGI->new($session->param("postrename"));
208 $session->clear("postrename");
209 IkiWiki::cgi_savesession($session);
213 # They renamed the page they were editing. This requires
214 # fixups to the edit form state.
215 # Tweak the edit form to be editing the new page.
216 $postrename->param("page", $dest);
219 # Update edit form content to fix any links present
221 $postrename->param("editcontent",
222 renamepage_hook($dest, $src, $dest,
223 $postrename->param("editcontent")));
225 # Get a new edit token; old was likely invalidated.
226 $postrename->param("rcsinfo",
227 IkiWiki::rcs_prepedit($pagesources{$dest}));
230 IkiWiki::cgi_editpage($postrename, $session);
233 sub formbuilder (@) {
235 my $form=$params{form};
237 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
238 $form->field("do") eq "create")) {
239 IkiWiki::decode_form_utf8($form);
241 my $session=$params{session};
243 if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
244 rename_start($q, $session, 0, $form->field("page"));
246 elsif ($form->submitted eq "Rename Attachment") {
247 my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
249 error(gettext("Only one attachment can be renamed at a time."));
251 elsif (! @selected) {
252 error(gettext("Please select the attachment to rename."))
254 rename_start($q, $session, 1, $selected[0]);
261 sub formbuilder_setup (@) {
263 my $form=$params{form};
266 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
267 $form->field("do") eq "create")) {
268 # Rename button for the page, and also for attachments.
269 push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
270 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
272 if (defined $renamesummary) {
273 $form->tmpl_param(message => $renamesummary);
278 sub sessioncgi ($$) {
281 if ($q->param("do") eq 'rename') {
283 my ($form, $buttons)=rename_form($q, $session, Encode::decode_utf8($q->param("page")));
284 IkiWiki::decode_form_utf8($form);
286 if ($form->submitted eq 'Cancel') {
287 postrename($session);
289 elsif ($form->submitted eq 'Rename' && $form->validate) {
290 # Queue of rename actions to perfom.
293 # These untaints are safe because of the checks
294 # performed in check_canrename later.
295 my $src=$form->field("page");
296 my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
297 my $dest=IkiWiki::possibly_foolish_untaint(titlepage($form->field("new_name")));
299 if (! $q->param("attachment")) {
300 my $type=$q->param('type');
301 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
302 $type=IkiWiki::possibly_foolish_untaint($type);
305 my ($ext)=$srcfile=~/\.([^.]+)$/;
309 $destfile=newpagefile($dest, $type);
315 destfile => $destfile,
319 @torename=rename_hook(
320 torename => \@torename,
326 require IkiWiki::Render;
327 IkiWiki::disable_commit_hook() if $config{rcs};
328 my %origpagesources=%pagesources;
330 # First file renaming.
331 foreach my $rename (@torename) {
332 if ($rename->{required}) {
333 do_rename($rename, $q, $session);
336 eval {do_rename($rename, $q, $session)};
343 # Temporarily tweak pagesources to point to
344 # the renamed file, in case fixlinks needs
346 $pagesources{$rename->{src}}=$rename->{destfile};
348 IkiWiki::rcs_commit_staged(
349 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
350 $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
353 foreach my $rename (@torename) {
354 next if $rename->{src} eq $rename->{dest};
355 next if $rename->{error};
356 foreach my $p (fixlinks($rename, $session)) {
357 # map old page names to new
358 foreach my $r (@torename) {
359 next if $rename->{error};
360 if ($r->{src} eq $p) {
365 push @{$rename->{fixedlinks}}, $p;
370 %pagesources=%origpagesources;
372 IkiWiki::enable_commit_hook();
373 IkiWiki::rcs_update();
376 IkiWiki::saveindex();
378 # Find pages with remaining, broken links.
379 foreach my $rename (@torename) {
380 next if $rename->{src} eq $rename->{dest};
382 foreach my $page (keys %links) {
384 foreach my $link (@{$links{$page}}) {
385 my $bestlink=bestlink($page, $link);
386 if ($bestlink eq $rename->{src}) {
387 push @{$rename->{brokenlinks}}, $page;
394 # Generate a summary, that will be shown at the top
395 # of the edit template.
397 foreach my $rename (@torename) {
398 my $template=template("renamesummary.tmpl");
399 $template->param(src => $rename->{srcfile});
400 $template->param(dest => $rename->{destfile});
401 $template->param(error => $rename->{error});
402 if ($rename->{src} ne $rename->{dest}) {
403 $template->param(brokenlinks_checked => 1);
404 $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
405 $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
407 $renamesummary.=$template->output;
410 postrename($session, $src, $dest, $q->param("attachment"));
413 IkiWiki::showform($form, $buttons, $session, $q);
420 # Add subpages to the list of pages to be renamed, if needed.
421 sub rename_subpages (@) {
424 my %torename = %{$params{torename}};
425 my $q = $params{cgi};
426 my $src = $torename{src};
427 my $srcfile = $torename{src};
428 my $dest = $torename{dest};
429 my $destfile = $torename{dest};
431 return () unless ($q->param("subpages") && $src ne $dest);
434 foreach my $p (keys %pagesources) {
435 next unless $pagesources{$p}=~m/^\Q$src\E\//;
436 # If indexpages is enabled, the srcfile should not be confused
438 next if $pagesources{$p} eq $srcfile;
440 my $d=$pagesources{$p};
441 $d=~s/^\Q$src\E\//$dest\//;
444 srcfile => $pagesources{$p},
445 dest => pagename($d),
454 # generates a list of links in a form suitable for FormBuilder
457 # converts a list of pages into a list of links
458 # in a form suitable for FormBuilder.
462 page => htmllink($dest, $dest, $_,
464 linktext => pagetitle($_),
470 sub renamepage_hook ($$$$) {
471 my ($page, $src, $dest, $content)=@_;
473 IkiWiki::run_hooks(renamepage => sub {
488 my @torename=@{$params{torename}};
489 my %done=%{$params{done}};
491 my $session=$params{session};
493 return () unless @torename;
496 foreach my $torename (@torename) {
497 unless (exists $done{$torename->{src}} && $done{$torename->{src}}) {
498 IkiWiki::run_hooks(rename => sub {
499 push @nextset, shift->(
500 torename => $torename,
505 $done{$torename->{src}}=1;
509 push @torename, rename_hook(
510 torename => \@nextset,
518 return grep { ! $seen{$_->{src}}++ } @torename;
521 sub do_rename ($$$) {
526 # First, check if this rename is allowed.
527 check_canrename($rename->{src},
533 # Ensure that the dest directory exists and is ok.
534 IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
537 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
540 if (! rename($config{srcdir}."/".$rename->{srcfile},
541 $config{srcdir}."/".$rename->{destfile})) {
554 foreach my $page (keys %links) {
556 foreach my $link (@{$links{$page}}) {
557 my $bestlink=bestlink($page, $link);
558 if ($bestlink eq $rename->{src}) {
564 my $file=$pagesources{$page};
565 my $oldcontent=readfile($config{srcdir}."/".$file);
566 my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
567 if ($oldcontent ne $content) {
568 my $token=IkiWiki::rcs_prepedit($file);
569 eval { writefile($file, $config{srcdir}, $content) };
571 my $conflict=IkiWiki::rcs_commit(
573 sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
575 $session->param("name"),
578 push @fixedlinks, $page if ! defined $conflict;