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);
24 sub check_canrename ($$$$$$) {
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)));
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));
44 elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
45 error(sprintf(gettext("%s is not a file"), $srcfile));
49 IkiWiki::check_canedit($src, $q, $session);
51 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
52 IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
55 error("renaming of attachments is not allowed");
59 # Dest checks can be omitted by passing undef.
61 if ($srcfile eq $destfile) {
62 error(gettext("no change to the file name was specified"));
65 # Must be a legal filename, and not absolute.
66 if (IkiWiki::file_pruned($destfile, $config{srcdir}) ||
68 error(sprintf(gettext("illegal name")));
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)));
77 # Must not exist on disk already.
78 if (-l "$config{srcdir}/$destfile" || -e _) {
79 error(sprintf(gettext("%s already exists on disk"), $destfile));
83 IkiWiki::check_canedit($dest, $q, $session);
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);
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);
101 elsif (ref $ret eq 'CODE') {
105 elsif (defined $ret) {
113 sub rename_form ($$$) {
118 eval q{use CGI::FormBuilder};
120 my $f = CGI::FormBuilder->new(
122 title => sprintf(gettext("rename %s"), pagetitle($page)),
128 action => $config{cgiurl},
129 stylesheet => IkiWiki::baseurl()."style.css",
130 fields => [qw{do page new_name attachment}],
133 $f->field(name => "do", type => "hidden", value => "rename", force => 1);
134 $f->field(name => "page", type => "hidden", value => $page, force => 1);
135 $f->field(name => "new_name", value => pagetitle($page, 1), size => 60);
136 if (!$q->param("attachment")) {
137 # insert the standard extensions
139 if (exists $IkiWiki::hooks{htmlize}) {
140 foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
141 push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
144 @page_types=sort @page_types;
146 # make sure the current extension is in the list
147 my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
148 if (! $IkiWiki::hooks{htmlize}{$ext}) {
149 unshift(@page_types, [$ext, $ext]);
152 $f->field(name => "type", type => 'select',
153 options => \@page_types,
154 value => $ext, force => 1);
156 foreach my $p (keys %pagesources) {
157 if ($pagesources{$p}=~m/^\Q$page\E\//) {
158 $f->field(name => "subpages",
161 options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
168 $f->field(name => "attachment", type => "hidden");
170 return $f, ["Rename", "Cancel"];
173 sub rename_start ($$$$) {
176 my $attachment=shift;
179 check_canrename($page, $pagesources{$page}, undef, undef,
182 # Save current form state to allow returning to it later
183 # without losing any edits.
184 # (But don't save what button was submitted, to avoid
185 # looping back to here.)
186 # Note: "_submit" is CGI::FormBuilder internals.
187 $q->param(-name => "_submit", -value => "");
188 $session->param(postrename => scalar $q->Vars);
189 IkiWiki::cgi_savesession($session);
191 if (defined $attachment) {
192 $q->param(-name => "attachment", -value => $attachment);
194 my ($f, $buttons)=rename_form($q, $session, $page);
195 IkiWiki::showform($f, $buttons, $session, $q);
199 sub postrename ($;$$$) {
203 my $attachment=shift;
205 # Load saved form state and return to edit page.
206 my $postrename=CGI->new($session->param("postrename"));
207 $session->clear("postrename");
208 IkiWiki::cgi_savesession($session);
212 # They renamed the page they were editing. This requires
213 # fixups to the edit form state.
214 # Tweak the edit form to be editing the new page.
215 $postrename->param("page", $dest);
218 # Update edit form content to fix any links present
220 $postrename->param("editcontent",
221 renamepage_hook($dest, $src, $dest,
222 $postrename->param("editcontent")));
224 # Get a new edit token; old was likely invalidated.
225 $postrename->param("rcsinfo",
226 IkiWiki::rcs_prepedit($pagesources{$dest}));
229 IkiWiki::cgi_editpage($postrename, $session);
232 sub formbuilder (@) {
234 my $form=$params{form};
236 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
237 $form->field("do") eq "create")) {
239 my $session=$params{session};
241 if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
242 rename_start($q, $session, 0, $form->field("page"));
244 elsif ($form->submitted eq "Rename Attachment") {
245 my @selected=$q->param("attachment_select");
247 error(gettext("Only one attachment can be renamed at a time."));
249 elsif (! @selected) {
250 error(gettext("Please select the attachment to rename."))
252 rename_start($q, $session, 1, $selected[0]);
259 sub formbuilder_setup (@) {
261 my $form=$params{form};
264 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
265 $form->field("do") eq "create")) {
266 # Rename button for the page, and also for attachments.
267 push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
268 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
270 if (defined $renamesummary) {
271 $form->tmpl_param(message => $renamesummary);
276 sub sessioncgi ($$) {
279 if ($q->param("do") eq 'rename') {
281 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
282 IkiWiki::decode_form_utf8($form);
284 if ($form->submitted eq 'Cancel') {
285 postrename($session);
287 elsif ($form->submitted eq 'Rename' && $form->validate) {
288 # Queue of rename actions to perfom.
291 # These untaints are safe because of the checks
292 # performed in check_canrename later.
293 my $src=$q->param("page");
294 my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
295 my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
297 if (! $q->param("attachment")) {
298 my $type=$q->param('type');
299 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
300 $type=IkiWiki::possibly_foolish_untaint($type);
303 my ($ext)=$srcfile=~/\.([^.]+)$/;
307 $destfile=newpagefile($dest, $type);
313 destfile => $destfile,
317 @torename=rename_hook(
318 torename => \@torename,
324 require IkiWiki::Render;
325 IkiWiki::disable_commit_hook() if $config{rcs};
326 my %origpagesources=%pagesources;
328 # First file renaming.
329 foreach my $rename (@torename) {
330 if ($rename->{required}) {
331 do_rename($rename, $q, $session);
334 eval {do_rename($rename, $q, $session)};
341 # Temporarily tweak pagesources to point to
342 # the renamed file, in case fixlinks needs
344 $pagesources{$rename->{src}}=$rename->{destfile};
346 IkiWiki::rcs_commit_staged(
347 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
348 $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
351 foreach my $rename (@torename) {
352 next if $rename->{src} eq $rename->{dest};
353 next if $rename->{error};
354 foreach my $p (fixlinks($rename, $session)) {
355 # map old page names to new
356 foreach my $r (@torename) {
357 next if $rename->{error};
358 if ($r->{src} eq $p) {
363 push @{$rename->{fixedlinks}}, $p;
368 %pagesources=%origpagesources;
370 IkiWiki::enable_commit_hook();
371 IkiWiki::rcs_update();
374 IkiWiki::saveindex();
376 # Find pages with remaining, broken links.
377 foreach my $rename (@torename) {
378 next if $rename->{src} eq $rename->{dest};
380 foreach my $page (keys %links) {
382 foreach my $link (@{$links{$page}}) {
383 my $bestlink=bestlink($page, $link);
384 if ($bestlink eq $rename->{src}) {
385 push @{$rename->{brokenlinks}}, $page;
392 # Generate a summary, that will be shown at the top
393 # of the edit template.
395 foreach my $rename (@torename) {
396 my $template=template("renamesummary.tmpl");
397 $template->param(src => $rename->{srcfile});
398 $template->param(dest => $rename->{destfile});
399 $template->param(error => $rename->{error});
400 if ($rename->{src} ne $rename->{dest}) {
401 $template->param(brokenlinks_checked => 1);
402 $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
403 $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
405 $renamesummary.=$template->output;
408 postrename($session, $src, $dest, $q->param("attachment"));
411 IkiWiki::showform($form, $buttons, $session, $q);
418 # Add subpages to the list of pages to be renamed, if needed.
419 sub rename_subpages (@) {
422 my %torename = %{$params{torename}};
423 my $q = $params{cgi};
424 my $src = $torename{src};
425 my $srcfile = $torename{src};
426 my $dest = $torename{dest};
427 my $destfile = $torename{dest};
429 return () unless ($q->param("subpages") && $src ne $dest);
432 foreach my $p (keys %pagesources) {
433 next unless $pagesources{$p}=~m/^\Q$src\E\//;
434 # If indexpages is enabled, the srcfile should not be confused
436 next if $pagesources{$p} eq $srcfile;
438 my $d=$pagesources{$p};
439 $d=~s/^\Q$src\E\//$dest\//;
442 srcfile => $pagesources{$p},
443 dest => pagename($d),
452 # generates a list of links in a form suitable for FormBuilder
455 # converts a list of pages into a list of links
456 # in a form suitable for FormBuilder.
460 page => htmllink($dest, $dest, $_,
462 linktext => pagetitle($_),
468 sub renamepage_hook ($$$$) {
469 my ($page, $src, $dest, $content)=@_;
471 IkiWiki::run_hooks(renamepage => sub {
483 sub rename_hook (@) {
486 my @torename=@{$params{torename}};
487 my %done=%{$params{done}};
489 my $session=$params{session};
491 return () unless @torename;
494 foreach my $torename (@torename) {
495 unless (exists $done{$torename->{src}} && $done{$torename->{src}}) {
496 IkiWiki::run_hooks(rename => sub {
497 push @nextset, shift->(
498 torename => $torename,
503 $done{$torename->{src}}=1;
507 push @torename, rename_hook(
508 torename => \@nextset,
516 return grep { ! $seen{$_->{src}}++ } @torename;
519 sub do_rename ($$$) {
524 # First, check if this rename is allowed.
525 check_canrename($rename->{src},
531 # Ensure that the dest directory exists and is ok.
532 IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
535 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
538 if (! rename($config{srcdir}."/".$rename->{srcfile},
539 $config{srcdir}."/".$rename->{destfile})) {
552 foreach my $page (keys %links) {
554 foreach my $link (@{$links{$page}}) {
555 my $bestlink=bestlink($page, $link);
556 if ($bestlink eq $rename->{src}) {
562 my $file=$pagesources{$page};
563 my $oldcontent=readfile($config{srcdir}."/".$file);
564 my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
565 if ($oldcontent ne $content) {
566 my $token=IkiWiki::rcs_prepedit($file);
567 eval { writefile($file, $config{srcdir}, $content) };
569 my $conflict=IkiWiki::rcs_commit(
571 sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
573 $session->param("name"),
576 push @fixedlinks, $page if ! defined $conflict;