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->(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 @page_types=grep { !/^_/ }
141 keys %{$IkiWiki::hooks{htmlize}};
144 # make sure the current extension is in the list
145 my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
146 if (! $IkiWiki::hooks{htmlize}{$ext}) {
147 unshift(@page_types, $ext);
150 $f->field(name => "type", type => 'select',
151 options => \@page_types,
152 value => $ext, force => 1);
154 foreach my $p (keys %pagesources) {
155 if ($pagesources{$p}=~m/^\Q$page\E\//) {
156 $f->field(name => "subpages",
159 options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
166 $f->field(name => "attachment", type => "hidden");
168 return $f, ["Rename", "Cancel"];
171 sub rename_start ($$$$) {
174 my $attachment=shift;
177 check_canrename($page, $pagesources{$page}, undef, undef,
180 # Save current form state to allow returning to it later
181 # without losing any edits.
182 # (But don't save what button was submitted, to avoid
183 # looping back to here.)
184 # Note: "_submit" is CGI::FormBuilder internals.
185 $q->param(-name => "_submit", -value => "");
186 $session->param(postrename => scalar $q->Vars);
187 IkiWiki::cgi_savesession($session);
189 if (defined $attachment) {
190 $q->param(-name => "attachment", -value => $attachment);
192 my ($f, $buttons)=rename_form($q, $session, $page);
193 IkiWiki::showform($f, $buttons, $session, $q);
197 sub postrename ($;$$$) {
201 my $attachment=shift;
203 # Load saved form state and return to edit page.
204 my $postrename=CGI->new($session->param("postrename"));
205 $session->clear("postrename");
206 IkiWiki::cgi_savesession($session);
210 # They renamed the page they were editing. This requires
211 # fixups to the edit form state.
212 # Tweak the edit form to be editing the new page.
213 $postrename->param("page", $dest);
216 # Update edit form content to fix any links present
218 $postrename->param("editcontent",
219 renamepage_hook($dest, $src, $dest,
220 $postrename->param("editcontent")));
222 # Get a new edit token; old was likely invalidated.
223 $postrename->param("rcsinfo",
224 IkiWiki::rcs_prepedit($pagesources{$dest}));
227 IkiWiki::cgi_editpage($postrename, $session);
230 sub formbuilder (@) {
232 my $form=$params{form};
234 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
235 $form->field("do") eq "create")) {
237 my $session=$params{session};
239 if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
240 rename_start($q, $session, 0, $form->field("page"));
242 elsif ($form->submitted eq "Rename Attachment") {
243 my @selected=$q->param("attachment_select");
245 error(gettext("Only one attachment can be renamed at a time."));
247 elsif (! @selected) {
248 error(gettext("Please select the attachment to rename."))
250 rename_start($q, $session, 1, $selected[0]);
257 sub formbuilder_setup (@) {
259 my $form=$params{form};
262 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
263 $form->field("do") eq "create")) {
264 # Rename button for the page, and also for attachments.
265 push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
266 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
268 if (defined $renamesummary) {
269 $form->tmpl_param(message => $renamesummary);
274 sub sessioncgi ($$) {
277 if ($q->param("do") eq 'rename') {
279 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
280 IkiWiki::decode_form_utf8($form);
282 if ($form->submitted eq 'Cancel') {
283 postrename($session);
285 elsif ($form->submitted eq 'Rename' && $form->validate) {
286 # Queue of rename actions to perfom.
289 # These untaints are safe because of the checks
290 # performed in check_canrename later.
291 my $src=$q->param("page");
292 my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
293 my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
295 if (! $q->param("attachment")) {
296 my $type=$q->param('type');
297 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
298 $type=IkiWiki::possibly_foolish_untaint($type);
301 my ($ext)=$srcfile=~/\.([^.]+)$/;
305 $destfile=newpagefile($dest, $type);
311 destfile => $destfile,
315 IkiWiki::run_hooks(rename => sub { shift->(\@torename, $q, $session); });
317 # See if any subpages need to be renamed.
318 if ($q->param("subpages") && $src ne $dest) {
319 foreach my $p (keys %pagesources) {
320 next unless $pagesources{$p}=~m/^\Q$src\E\//;
321 # If indexpages is enabled, the
322 # srcfile should not be confused
324 next if $pagesources{$p} eq $srcfile;
326 my $d=$pagesources{$p};
327 $d=~s/^\Q$src\E\//$dest\//;
330 srcfile => $pagesources{$p},
331 dest => pagename($d),
338 require IkiWiki::Render;
339 IkiWiki::disable_commit_hook() if $config{rcs};
340 my %origpagesources=%pagesources;
342 # First file renaming.
343 foreach my $rename (@torename) {
344 if ($rename->{required}) {
345 do_rename($rename, $q, $session);
348 eval {do_rename($rename, $q, $session)};
355 # Temporarily tweak pagesources to point to
356 # the renamed file, in case fixlinks needs
358 $pagesources{$rename->{src}}=$rename->{destfile};
360 IkiWiki::rcs_commit_staged(
361 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
362 $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
365 foreach my $rename (@torename) {
366 next if $rename->{src} eq $rename->{dest};
367 next if $rename->{error};
368 foreach my $p (fixlinks($rename, $session)) {
369 # map old page names to new
370 foreach my $r (@torename) {
371 next if $rename->{error};
372 if ($r->{src} eq $p) {
377 push @{$rename->{fixedlinks}}, $p;
382 %pagesources=%origpagesources;
384 IkiWiki::enable_commit_hook();
385 IkiWiki::rcs_update();
388 IkiWiki::saveindex();
390 # Find pages with remaining, broken links.
391 foreach my $rename (@torename) {
392 next if $rename->{src} eq $rename->{dest};
394 foreach my $page (keys %links) {
396 foreach my $link (@{$links{$page}}) {
397 my $bestlink=bestlink($page, $link);
398 if ($bestlink eq $rename->{src}) {
399 push @{$rename->{brokenlinks}}, $page;
406 # Generate a summary, that will be shown at the top
407 # of the edit template.
409 foreach my $rename (@torename) {
410 my $template=template("renamesummary.tmpl");
411 $template->param(src => $rename->{srcfile});
412 $template->param(dest => $rename->{destfile});
413 $template->param(error => $rename->{error});
414 if ($rename->{src} ne $rename->{dest}) {
415 $template->param(brokenlinks_checked => 1);
416 $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
417 $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
419 $renamesummary.=$template->output;
422 postrename($session, $src, $dest, $q->param("attachment"));
425 IkiWiki::showform($form, $buttons, $session, $q);
433 # generates a list of links in a form suitable for FormBuilder
436 # converts a list of pages into a list of links
437 # in a form suitable for FormBuilder.
441 page => htmllink($dest, $dest, $_,
443 linktext => pagetitle($_),
449 sub renamepage_hook ($$$$) {
450 my ($page, $src, $dest, $content)=@_;
452 IkiWiki::run_hooks(renamepage => sub {
464 sub do_rename ($$$) {
469 # First, check if this rename is allowed.
470 check_canrename($rename->{src},
476 # Ensure that the dest directory exists and is ok.
477 IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
480 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
483 if (! rename($config{srcdir}."/".$rename->{srcfile},
484 $config{srcdir}."/".$rename->{destfile})) {
497 foreach my $page (keys %links) {
499 foreach my $link (@{$links{$page}}) {
500 my $bestlink=bestlink($page, $link);
501 if ($bestlink eq $rename->{src}) {
507 my $file=$pagesources{$page};
508 my $oldcontent=readfile($config{srcdir}."/".$file);
509 my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
510 if ($oldcontent ne $content) {
511 my $token=IkiWiki::rcs_prepedit($file);
512 eval { writefile($file, $config{srcdir}, $content) };
514 my $conflict=IkiWiki::rcs_commit(
516 sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
518 $session->param("name"),
521 push @fixedlinks, $page if ! defined $conflict;