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 sub rename_form ($$$) {
97 eval q{use CGI::FormBuilder};
99 my $f = CGI::FormBuilder->new(
101 title => sprintf(gettext("rename %s"), pagetitle($page)),
107 action => $config{cgiurl},
108 stylesheet => IkiWiki::baseurl()."style.css",
109 fields => [qw{do page new_name attachment}],
112 $f->field(name => "do", type => "hidden", value => "rename", force => 1);
113 $f->field(name => "page", type => "hidden", value => $page, force => 1);
114 $f->field(name => "new_name", value => pagetitle($page, 1), size => 60);
115 if (!$q->param("attachment")) {
116 # insert the standard extensions
118 if (exists $IkiWiki::hooks{htmlize}) {
119 @page_types=grep { !/^_/ }
120 keys %{$IkiWiki::hooks{htmlize}};
123 # make sure the current extension is in the list
124 my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
125 if (! $IkiWiki::hooks{htmlize}{$ext}) {
126 unshift(@page_types, $ext);
129 $f->field(name => "type", type => 'select',
130 options => \@page_types,
131 value => $ext, force => 1);
133 foreach my $p (keys %pagesources) {
134 if ($pagesources{$p}=~m/^\Q$page\E\//) {
135 $f->field(name => "subpages",
138 options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
145 $f->field(name => "attachment", type => "hidden");
147 return $f, ["Rename", "Cancel"];
150 sub rename_start ($$$$) {
153 my $attachment=shift;
156 check_canrename($page, $pagesources{$page}, undef, undef,
159 # Save current form state to allow returning to it later
160 # without losing any edits.
161 # (But don't save what button was submitted, to avoid
162 # looping back to here.)
163 # Note: "_submit" is CGI::FormBuilder internals.
164 $q->param(-name => "_submit", -value => "");
165 $session->param(postrename => scalar $q->Vars);
166 IkiWiki::cgi_savesession($session);
168 if (defined $attachment) {
169 $q->param(-name => "attachment", -value => $attachment);
171 my ($f, $buttons)=rename_form($q, $session, $page);
172 IkiWiki::showform($f, $buttons, $session, $q);
176 sub postrename ($;$$$) {
180 my $attachment=shift;
182 # Load saved form state and return to edit page.
183 my $postrename=CGI->new($session->param("postrename"));
184 $session->clear("postrename");
185 IkiWiki::cgi_savesession($session);
189 # They renamed the page they were editing. This requires
190 # fixups to the edit form state.
191 # Tweak the edit form to be editing the new page.
192 $postrename->param("page", $dest);
195 # Update edit form content to fix any links present
197 $postrename->param("editcontent",
198 renamepage_hook($dest, $src, $dest,
199 $postrename->param("editcontent")));
201 # Get a new edit token; old was likely invalidated.
202 $postrename->param("rcsinfo",
203 IkiWiki::rcs_prepedit($pagesources{$dest}));
206 IkiWiki::cgi_editpage($postrename, $session);
209 sub formbuilder (@) {
211 my $form=$params{form};
213 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
214 $form->field("do") eq "create")) {
216 my $session=$params{session};
218 if ($form->submitted eq "Rename" && $form->field("do") eq "edit") {
219 rename_start($q, $session, 0, $form->field("page"));
221 elsif ($form->submitted eq "Rename Attachment") {
222 my @selected=$q->param("attachment_select");
224 error(gettext("Only one attachment can be renamed at a time."));
226 elsif (! @selected) {
227 error(gettext("Please select the attachment to rename."))
229 rename_start($q, $session, 1, $selected[0]);
236 sub formbuilder_setup (@) {
238 my $form=$params{form};
241 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
242 $form->field("do") eq "create")) {
243 # Rename button for the page, and also for attachments.
244 push @{$params{buttons}}, "Rename" if $form->field("do") eq "edit";
245 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
247 if (defined $renamesummary) {
248 $form->tmpl_param(message => $renamesummary);
253 sub sessioncgi ($$) {
256 if ($q->param("do") eq 'rename') {
258 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
259 IkiWiki::decode_form_utf8($form);
261 if ($form->submitted eq 'Cancel') {
262 postrename($session);
264 elsif ($form->submitted eq 'Rename' && $form->validate) {
265 # Queue of rename actions to perfom.
268 # These untaints are safe because of the checks
269 # performed in check_canrename later.
270 my $src=$q->param("page");
271 my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
272 my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
274 if (! $q->param("attachment")) {
275 my $type=$q->param('type');
276 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
277 $type=IkiWiki::possibly_foolish_untaint($type);
280 my ($ext)=$srcfile=~/\.([^.]+)$/;
284 $destfile=newpagefile($dest, $type);
290 destfile => $destfile,
294 # See if any subpages need to be renamed.
295 if ($q->param("subpages") && $src ne $dest) {
296 foreach my $p (keys %pagesources) {
297 next unless $pagesources{$p}=~m/^\Q$src\E\//;
298 # If indexpages is enabled, the
299 # srcfile should not be confused
301 next if $pagesources{$p} eq $srcfile;
303 my $d=$pagesources{$p};
304 $d=~s/^\Q$src\E\//$dest\//;
307 srcfile => $pagesources{$p},
308 dest => pagename($d),
315 require IkiWiki::Render;
316 IkiWiki::disable_commit_hook() if $config{rcs};
317 my %origpagesources=%pagesources;
319 # First file renaming.
320 foreach my $rename (@torename) {
321 if ($rename->{required}) {
322 do_rename($rename, $q, $session);
325 eval {do_rename($rename, $q, $session)};
332 # Temporarily tweak pagesources to point to
333 # the renamed file, in case fixlinks needs
335 $pagesources{$rename->{src}}=$rename->{destfile};
337 IkiWiki::rcs_commit_staged(
338 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
339 $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
342 foreach my $rename (@torename) {
343 next if $rename->{src} eq $rename->{dest};
344 next if $rename->{error};
345 foreach my $p (fixlinks($rename, $session)) {
346 # map old page names to new
347 foreach my $r (@torename) {
348 next if $rename->{error};
349 if ($r->{src} eq $p) {
354 push @{$rename->{fixedlinks}}, $p;
359 %pagesources=%origpagesources;
361 IkiWiki::enable_commit_hook();
362 IkiWiki::rcs_update();
365 IkiWiki::saveindex();
367 # Find pages with remaining, broken links.
368 foreach my $rename (@torename) {
369 next if $rename->{src} eq $rename->{dest};
371 foreach my $page (keys %links) {
373 foreach my $link (@{$links{$page}}) {
374 my $bestlink=bestlink($page, $link);
375 if ($bestlink eq $rename->{src}) {
376 push @{$rename->{brokenlinks}}, $page;
383 # Generate a summary, that will be shown at the top
384 # of the edit template.
386 foreach my $rename (@torename) {
387 my $template=template("renamesummary.tmpl");
388 $template->param(src => $rename->{srcfile});
389 $template->param(dest => $rename->{destfile});
390 $template->param(error => $rename->{error});
391 if ($rename->{src} ne $rename->{dest}) {
392 $template->param(brokenlinks_checked => 1);
393 $template->param(brokenlinks => linklist($rename->{dest}, $rename->{brokenlinks}));
394 $template->param(fixedlinks => linklist($rename->{dest}, $rename->{fixedlinks}));
396 $renamesummary.=$template->output;
399 postrename($session, $src, $dest, $q->param("attachment"));
402 IkiWiki::showform($form, $buttons, $session, $q);
410 # generates a list of links in a form suitable for FormBuilder
413 # converts a list of pages into a list of links
414 # in a form suitable for FormBuilder.
418 page => htmllink($dest, $dest, $_,
420 linktext => pagetitle($_),
426 sub renamepage_hook ($$$$) {
427 my ($page, $src, $dest, $content)=@_;
429 IkiWiki::run_hooks(renamepage => sub {
441 sub do_rename ($$$) {
446 # First, check if this rename is allowed.
447 check_canrename($rename->{src},
453 # Ensure that the dest directory exists and is ok.
454 IkiWiki::prep_writefile($rename->{destfile}, $config{srcdir});
457 IkiWiki::rcs_rename($rename->{srcfile}, $rename->{destfile});
460 if (! rename($config{srcdir}."/".$rename->{srcfile},
461 $config{srcdir}."/".$rename->{destfile})) {
474 foreach my $page (keys %links) {
476 foreach my $link (@{$links{$page}}) {
477 my $bestlink=bestlink($page, $link);
478 if ($bestlink eq $rename->{src}) {
484 my $file=$pagesources{$page};
485 my $oldcontent=readfile($config{srcdir}."/".$file);
486 my $content=renamepage_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
487 if ($oldcontent ne $content) {
488 my $token=IkiWiki::rcs_prepedit($file);
489 eval { writefile($file, $config{srcdir}, $content) };
491 my $conflict=IkiWiki::rcs_commit(
493 sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
495 $session->param("name"),
498 push @fixedlinks, $page if ! defined $conflict;