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);
16 sub getsetup () { #{{{
24 sub check_canrename ($$$$$$) { #{{{
32 my $attachment=! defined IkiWiki::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"), IkiWiki::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 => IkiWiki::pagetitle($page), 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 $f->field(name => "subpages",
136 options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
140 $f->field(name => "attachment", type => "hidden");
142 return $f, ["Rename", "Cancel"];
145 sub rename_start ($$$$) { #{{{
148 my $attachment=shift;
151 check_canrename($page, $pagesources{$page}, undef, undef,
154 # Save current form state to allow returning to it later
155 # without losing any edits.
156 # (But don't save what button was submitted, to avoid
157 # looping back to here.)
158 # Note: "_submit" is CGI::FormBuilder internals.
159 $q->param(-name => "_submit", -value => "");
160 $session->param(postrename => scalar $q->Vars);
161 IkiWiki::cgi_savesession($session);
163 my ($f, $buttons)=rename_form($q, $session, $page);
164 if (defined $attachment) {
165 $f->field(name => "attachment", value => $attachment, force => 1);
168 IkiWiki::showform($f, $buttons, $session, $q);
172 sub postrename ($;$$$) { #{{{
176 my $attachment=shift;
178 # Load saved form state and return to edit page.
179 my $postrename=CGI->new($session->param("postrename"));
180 $session->clear("postrename");
181 IkiWiki::cgi_savesession($session);
185 # They renamed the page they were editing. This requires
186 # fixups to the edit form state.
187 # Tweak the edit form to be editing the new page.
188 $postrename->param("page", $dest);
191 # Update edit form content to fix any links present
193 $postrename->param("editcontent",
194 renamepage_hook($dest, $src, $dest,
195 $postrename->param("editcontent")));
197 # Get a new edit token; old was likely invalidated.
198 $postrename->param("rcsinfo",
199 IkiWiki::rcs_prepedit($pagesources{$dest}));
202 IkiWiki::cgi_editpage($postrename, $session);
205 sub formbuilder (@) { #{{{
207 my $form=$params{form};
209 if (defined $form->field("do") && $form->field("do") eq "edit") {
211 my $session=$params{session};
213 if ($form->submitted eq "Rename") {
214 rename_start($q, $session, 0, $form->field("page"));
216 elsif ($form->submitted eq "Rename Attachment") {
217 my @selected=$q->param("attachment_select");
219 error(gettext("Only one attachment can be renamed at a time."));
221 elsif (! @selected) {
222 error(gettext("Please select the attachment to rename."))
224 rename_start($q, $session, 1, $selected[0]);
231 sub formbuilder_setup (@) { #{{{
233 my $form=$params{form};
236 if (defined $form->field("do") && $form->field("do") eq "edit") {
237 # Rename button for the page, and also for attachments.
238 push @{$params{buttons}}, "Rename";
239 $form->tmpl_param("field-rename" => '<input name="_submit" type="submit" value="Rename Attachment" />');
241 if (defined $renamesummary) {
242 $form->tmpl_param(message => $renamesummary);
247 sub sessioncgi ($$) { #{{{
250 if ($q->param("do") eq 'rename') {
252 my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
253 IkiWiki::decode_form_utf8($form);
255 if ($form->submitted eq 'Cancel') {
256 postrename($session);
258 elsif ($form->submitted eq 'Rename' && $form->validate) {
259 # These untaints are safe because of the checks
260 # performed in check_canrename below.
261 my $src=$q->param("page");
262 my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
263 my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
266 if (! $q->param("attachment")) {
267 my $type=$q->param('type');
268 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
269 $type=IkiWiki::possibly_foolish_untaint($type);
272 my ($ext)=$srcfile=~/\.([^.]+)$/;
276 $destfile.=".".$type;
279 check_canrename($src, $srcfile, $dest, $destfile,
282 # See if subpages need to be renamed.
284 if ($q->param("subpages") && $src ne $dest) {
285 foreach my $p (keys %pagesources) {
286 if ($pagesources{$p}=~m/^\Q$src\E\/$/) {
292 # Begin renaming process, which will end with a
294 require IkiWiki::Render;
295 IkiWiki::disable_commit_hook() if $config{rcs};
297 do_rename($srcfile, $destfile, $session);
299 foreach my $subpage (@subpages) {
300 my $subsrc=$pagesources{$subpage};
302 $subdest=~s/^\Q$src\E\//$dest/;
304 do_rename($subsrc, $subdest, $session)
310 push @fixedlinks, fixlinks($src, $dest, $session);
313 # End renaming process and refresh wiki.
315 IkiWiki::enable_commit_hook();
316 IkiWiki::rcs_update();
319 IkiWiki::saveindex();
321 # Scan for any remaining broken links to $src.
324 foreach my $page (keys %links) {
326 foreach my $link (@{$links{$page}}) {
327 my $bestlink=bestlink($page, $link);
328 if ($bestlink eq $src) {
333 push @brokenlinks, $page if $broken;
337 # Generate a rename summary, that will be shown at the top
338 # of the edit template.
339 my $template=template("renamesummary.tmpl");
340 $template->param(src => $srcfile);
341 $template->param(dest => $destfile);
343 $template->param(brokenlinks_checked => 1);
344 $template->param(brokenlinks => [
347 page => htmllink($dest, $dest, $_,
352 $template->param(fixedlinks => [
355 page => htmllink($dest, $dest, $_,
361 $renamesummary=$template->output;
363 postrename($session, $src, $dest, $q->param("attachment"));
366 IkiWiki::showform($form, $buttons, $session, $q);
373 sub renamepage_hook ($$$$) { #{{{
374 my ($page, $src, $dest, $content)=@_;
376 IkiWiki::run_hooks(renamepage => sub {
388 sub do_rename ($$$) { #{{{
393 # Actual file rename happens here.
394 # First, ensure that the dest directory exists and is ok.
395 IkiWiki::prep_writefile($destfile, $config{srcdir});
397 IkiWiki::rcs_rename($srcfile, $destfile);
398 IkiWiki::rcs_commit_staged(
399 sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
400 $session->param("name"), $ENV{REMOTE_ADDR});
403 if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
409 sub fixlinks ($$$) { #{{{
416 foreach my $page (keys %links) {
418 foreach my $link (@{$links{$page}}) {
419 my $bestlink=bestlink($page, $link);
420 if ($bestlink eq $src) {
426 my $file=$pagesources{$page};
427 my $oldcontent=readfile($config{srcdir}."/".$file);
428 my $content=renamepage_hook($page, $src, $dest, $oldcontent);
429 if ($oldcontent ne $content) {
430 my $token=IkiWiki::rcs_prepedit($file);
431 eval { writefile($file, $config{srcdir}, $content) };
433 my $conflict=IkiWiki::rcs_commit(
435 sprintf(gettext("update for rename of %s to %s"), $src, $dest),
437 $session->param("name"),
440 push @fixedlinks, $page if ! defined $conflict;