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);
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->($src, $q, $session);
99 elsif (ref $ret eq 'CODE') {
103 elsif (defined $ret) {
112 sub rename_form ($$$) {
117 eval q{use CGI::FormBuilder};
119 my $f = CGI::FormBuilder->new(
121 title => sprintf(gettext("rename %s"), pagetitle($page)),
127 action => $config{cgiurl},
128 stylesheet => IkiWiki::baseurl()."style.css",
129 fields => [qw{do page new_name attachment}],
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
138 if (exists $IkiWiki::hooks{htmlize}) {
139 @page_types=grep { !/^_/ }
140 keys %{$IkiWiki::hooks{htmlize}};
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);
149 $f->field(name => "type", type => 'select',
150 options => \@page_types,
151 value => $ext, force => 1);
153 foreach my $p (keys %pagesources) {
154 if ($pagesources{$p}=~m/^\Q$page\E\//) {
155 $f->field(name => "subpages",
158 options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
165 $f->field(name => "attachment", type => "hidden");
167 return $f, ["Rename", "Cancel"];
170 sub rename_start ($$$$) {
173 my $attachment=shift;
176 check_canrename($page, $pagesources{$page}, undef, undef,
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);
188 if (defined $attachment) {
189 $q->param(-name => "attachment", -value => $attachment);
191 my ($f, $buttons)=rename_form($q, $session, $page);
192 IkiWiki::showform($f, $buttons, $session, $q);
196 sub postrename ($;$$$) {
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);
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);
215 # Update edit form content to fix any links present
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}));
226 IkiWiki::cgi_editpage($postrename, $session);
229 sub formbuilder (@) {
231 my $form=$params{form};
233 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
234 $form->field("do") eq "create")) {
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"));
241 elsif ($form->submitted eq "Rename Attachment") {
242 my @selected=$q->param("attachment_select");
244 error(gettext("Only one attachment can be renamed at a time."));
246 elsif (! @selected) {
247 error(gettext("Please select the attachment to rename."))
249 rename_start($q, $session, 1, $selected[0]);
256 sub formbuilder_setup (@) {
258 my $form=$params{form};
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);
273 sub sessioncgi ($$) {
276 if ($q->param("do") eq 'rename') {
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);
284 elsif ($form->submitted eq 'Rename' && $form->validate) {
285 # Queue of rename actions to perfom.
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")));
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);
300 my ($ext)=$srcfile=~/\.([^.]+)$/;
304 $destfile=newpagefile($dest, $type);
310 destfile => $destfile,
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
323 next if $pagesources{$p} eq $srcfile;
325 my $d=$pagesources{$p};
326 $d=~s/^\Q$src\E\//$dest\//;
329 srcfile => $pagesources{$p},
330 dest => pagename($d),
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);
347 eval {do_rename($rename, $q, $session)};
354 # Temporarily tweak pagesources to point to
355 # the renamed file, in case fixlinks needs
357 $pagesources{$rename->{src}}=$rename->{destfile};
359 IkiWiki::rcs_commit_staged(
360 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
361 $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
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) {
376 push @{$rename->{fixedlinks}}, $p;
381 %pagesources=%origpagesources;
383 IkiWiki::enable_commit_hook();
384 IkiWiki::rcs_update();
387 IkiWiki::saveindex();
389 # Find pages with remaining, broken links.
390 foreach my $rename (@torename) {
391 next if $rename->{src} eq $rename->{dest};
393 foreach my $page (keys %links) {
395 foreach my $link (@{$links{$page}}) {
396 my $bestlink=bestlink($page, $link);
397 if ($bestlink eq $rename->{src}) {
398 push @{$rename->{brokenlinks}}, $page;
405 # Generate a summary, that will be shown at the top
406 # of the edit template.
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 => [
418 page => htmllink($rename->{dest}, $rename->{dest}, $_,
421 } @{$rename->{brokenlinks}}
423 $template->param(fixedlinks => [
426 page => htmllink($rename->{dest}, $rename->{dest}, $_,
429 } @{$rename->{fixedlinks}}
432 $renamesummary.=$template->output;
435 postrename($session, $src, $dest, $q->param("attachment"));
438 IkiWiki::showform($form, $buttons, $session, $q);
445 sub renamelink_hook ($$$$) {
446 my ($page, $src, $dest, $content)=@_;
448 IkiWiki::run_hooks(renamelink => sub {
460 sub do_rename ($$$) {
465 # First, check if this rename is allowed.
466 check_canrename($rename->{src},
472 # Ensure that the dest directory exists and is ok.
473 IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
476 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
479 if (! rename($config{srcdir}."/".$rename->{srcfile},
480 $config{srcdir}."/".$rename->{destfile})) {
493 foreach my $page (keys %links) {
495 foreach my $link (@{$links{$page}}) {
496 my $bestlink=bestlink($page, $link);
497 if ($bestlink eq $rename->{src}) {
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) };
510 my $conflict=IkiWiki::rcs_commit(
512 sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
514 $session->param("name"),
517 push @fixedlinks, $page if ! defined $conflict;