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")) {
238 IkiWiki::decode_form_utf8($form);
240 my $session=$params{session};
242 if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
243 rename_start($q, $session, 0, $form->field("page"));
245 elsif ($form->submitted eq "Rename Attachment") {
246 my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
248 error(gettext("Only one attachment can be renamed at a time."));
250 elsif (! @selected) {
251 error(gettext("Please select the attachment to rename."))
253 rename_start($q, $session, 1, $selected[0]);
260 sub formbuilder_setup (@) {
262 my $form=$params{form};
265 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
266 $form->field("do") eq "create")) {
267 # Rename button for the page, and also for attachments.
268 push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
269 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
271 if (defined $renamesummary) {
272 $form->tmpl_param(message => $renamesummary);
277 sub sessioncgi ($$) {
280 if ($q->param("do") eq 'rename') {
282 my ($form, $buttons)=rename_form($q, $session, Encode::decode_utf8($q->param("page")));
283 IkiWiki::decode_form_utf8($form);
285 if ($form->submitted eq 'Cancel') {
286 postrename($session);
288 elsif ($form->submitted eq 'Rename' && $form->validate) {
289 # Queue of rename actions to perfom.
292 # These untaints are safe because of the checks
293 # performed in check_canrename later.
294 my $src=$form->field("page");
295 my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
296 my $dest=IkiWiki::possibly_foolish_untaint(titlepage($form->field("new_name")));
298 if (! $q->param("attachment")) {
299 my $type=$q->param('type');
300 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
301 $type=IkiWiki::possibly_foolish_untaint($type);
304 my ($ext)=$srcfile=~/\.([^.]+)$/;
308 $destfile=newpagefile($dest, $type);
314 destfile => $destfile,
318 @torename=rename_hook(
319 torename => \@torename,
325 require IkiWiki::Render;
326 IkiWiki::disable_commit_hook() if $config{rcs};
327 my %origpagesources=%pagesources;
329 # First file renaming.
330 foreach my $rename (@torename) {
331 if ($rename->{required}) {
332 do_rename($rename, $q, $session);
335 eval {do_rename($rename, $q, $session)};
342 # Temporarily tweak pagesources to point to
343 # the renamed file, in case fixlinks needs
345 $pagesources{$rename->{src}}=$rename->{destfile};
347 IkiWiki::rcs_commit_staged(
348 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
349 $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
352 foreach my $rename (@torename) {
353 next if $rename->{src} eq $rename->{dest};
354 next if $rename->{error};
355 foreach my $p (fixlinks($rename, $session)) {
356 # map old page names to new
357 foreach my $r (@torename) {
358 next if $rename->{error};
359 if ($r->{src} eq $p) {
364 push @{$rename->{fixedlinks}}, $p;
369 %pagesources=%origpagesources;
371 IkiWiki::enable_commit_hook();
372 IkiWiki::rcs_update();
375 IkiWiki::saveindex();
377 # Find pages with remaining, broken links.
378 foreach my $rename (@torename) {
379 next if $rename->{src} eq $rename->{dest};
381 foreach my $page (keys %links) {
383 foreach my $link (@{$links{$page}}) {
384 my $bestlink=bestlink($page, $link);
385 if ($bestlink eq $rename->{src}) {
386 push @{$rename->{brokenlinks}}, $page;
393 # Generate a summary, that will be shown at the top
394 # of the edit template.
396 foreach my $rename (@torename) {
397 my $template=template("renamesummary.tmpl");
398 $template->param(src => $rename->{srcfile});
399 $template->param(dest => $rename->{destfile});
400 $template->param(error => $rename->{error});
401 if ($rename->{src} ne $rename->{dest}) {
402 $template->param(brokenlinks_checked => 1);
403 $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
404 $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
406 $renamesummary.=$template->output;
409 postrename($session, $src, $dest, $q->param("attachment"));
412 IkiWiki::showform($form, $buttons, $session, $q);
419 # Add subpages to the list of pages to be renamed, if needed.
420 sub rename_subpages (@) {
423 my %torename = %{$params{torename}};
424 my $q = $params{cgi};
425 my $src = $torename{src};
426 my $srcfile = $torename{src};
427 my $dest = $torename{dest};
428 my $destfile = $torename{dest};
430 return () unless ($q->param("subpages") && $src ne $dest);
433 foreach my $p (keys %pagesources) {
434 next unless $pagesources{$p}=~m/^\Q$src\E\//;
435 # If indexpages is enabled, the srcfile should not be confused
437 next if $pagesources{$p} eq $srcfile;
439 my $d=$pagesources{$p};
440 $d=~s/^\Q$src\E\//$dest\//;
443 srcfile => $pagesources{$p},
444 dest => pagename($d),
453 # generates a list of links in a form suitable for FormBuilder
456 # converts a list of pages into a list of links
457 # in a form suitable for FormBuilder.
461 page => htmllink($dest, $dest, $_,
463 linktext => pagetitle($_),
469 sub renamepage_hook ($$$$) {
470 my ($page, $src, $dest, $content)=@_;
472 IkiWiki::run_hooks(renamepage => sub {
487 my @torename=@{$params{torename}};
488 my %done=%{$params{done}};
490 my $session=$params{session};
492 return () unless @torename;
495 foreach my $torename (@torename) {
496 unless (exists $done{$torename->{src}} && $done{$torename->{src}}) {
497 IkiWiki::run_hooks(rename => sub {
498 push @nextset, shift->(
499 torename => $torename,
504 $done{$torename->{src}}=1;
508 push @torename, rename_hook(
509 torename => \@nextset,
517 return grep { ! $seen{$_->{src}}++ } @torename;
520 sub do_rename ($$$) {
525 # First, check if this rename is allowed.
526 check_canrename($rename->{src},
532 # Ensure that the dest directory exists and is ok.
533 IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
536 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
539 if (! rename($config{srcdir}."/".$rename->{srcfile},
540 $config{srcdir}."/".$rename->{destfile})) {
553 foreach my $page (keys %links) {
555 foreach my $link (@{$links{$page}}) {
556 my $bestlink=bestlink($page, $link);
557 if ($bestlink eq $rename->{src}) {
563 my $file=$pagesources{$page};
564 my $oldcontent=readfile($config{srcdir}."/".$file);
565 my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
566 if ($oldcontent ne $content) {
567 my $token=IkiWiki::rcs_prepedit($file);
568 eval { writefile($file, $config{srcdir}, $content) };
570 my $conflict=IkiWiki::rcs_commit(
572 sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
574 $session->param("name"),
577 push @fixedlinks, $page if ! defined $conflict;