]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/rename.pm
factor out do_rename
[ikiwiki.git] / IkiWiki / Plugin / rename.pm
index 2db8be0f3f5a7493b4437e571f6614bd85491360..fcfdee424948f15479ad4a770d217e46c3191776 100644 (file)
@@ -123,6 +123,13 @@ sub rename_form ($$$) { #{{{
                $f->field(name => "type", type => 'select',
                        options => \@page_types,
                        value => $ext, force => 1);
+               
+               $f->field(name => "subpages",
+                       label => "",
+                       type => "checkbox",
+                       options => [ [ 1 => gettext("Also rename SubPages and attachments") ] ],
+                       value => 1,
+                       force => 1);
        }
        $f->field(name => "attachment", type => "hidden");
 
@@ -249,15 +256,13 @@ sub sessioncgi ($$) { #{{{
                        my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
                        my $dest=IkiWiki::possibly_foolish_untaint(IkiWiki::titlepage($q->param("new_name")));
 
-                       # The extension of dest is the same as src if it's
-                       # a page. If it's an attachment, the extension is
-                       # already included.
                        my $destfile=$dest;
                        if (! $q->param("attachment")) {
                                my $type=$q->param('type');
                                if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
                                        $type=IkiWiki::possibly_foolish_untaint($type);
-                               } else {
+                               }
+                               else {
                                        my ($ext)=$srcfile=~/\.([^.]+)$/;
                                        $type=$ext;
                                }
@@ -268,52 +273,46 @@ sub sessioncgi ($$) { #{{{
                        check_canrename($src, $srcfile, $dest, $destfile,
                                $q, $session, $q->param("attachment"));
 
-                       # Ensures that the dest directory exists and is ok.
-                       IkiWiki::prep_writefile($destfile, $config{srcdir});
-
-                       # Do rename, update other pages, and refresh site.
-                       IkiWiki::disable_commit_hook() if $config{rcs};
+                       # Begin renaming process, which will end with a
+                       # wiki refresh.
                        require IkiWiki::Render;
-                       if ($config{rcs}) {
-                               IkiWiki::rcs_rename($srcfile, $destfile);
-                               IkiWiki::rcs_commit_staged(
-                                       sprintf(gettext("rename %s to %s"), $src, $dest),
-                                       $session->param("name"), $ENV{REMOTE_ADDR});
-                       }
-                       else {
-                               if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
-                                       error("rename: $!");
-                               }
-                       }
+                       IkiWiki::disable_commit_hook() if $config{rcs};
+
+                       do_rename($srcfile, $destfile, $session);
+
                        my @fixedlinks;
-                       foreach my $page (keys %links) {
-                               my $needfix=0;
-                               foreach my $link (@{$links{$page}}) {
-                                       my $bestlink=bestlink($page, $link);
-                                       if ($bestlink eq $src) {
-                                               $needfix=1;
-                                               last;
+                       if ($src ne $dest) {
+                               foreach my $page (keys %links) {
+                                       my $needfix=0;
+                                       foreach my $link (@{$links{$page}}) {
+                                               my $bestlink=bestlink($page, $link);
+                                               if ($bestlink eq $src) {
+                                                       $needfix=1;
+                                                       last;
+                                               }
                                        }
-                               }
-                               if ($needfix) {
-                                       my $file=$pagesources{$page};
-                                       my $oldcontent=readfile($config{srcdir}."/".$file);
-                                       my $content=renamepage_hook($page, $src, $dest, $oldcontent);
-                                       if ($oldcontent ne $content) {
-                                               my $token=IkiWiki::rcs_prepedit($file);
-                                               eval { writefile($file, $config{srcdir}, $content) };
-                                               next if $@;
-                                               my $conflict=IkiWiki::rcs_commit(
-                                                       $file,
-                                                       sprintf(gettext("update for rename of %s to %s"), $src, $dest),
-                                                       $token,
-                                                       $session->param("name"), 
-                                                       $ENV{REMOTE_ADDR}
-                                               );
-                                               push @fixedlinks, $page if ! defined $conflict;
+                                       if ($needfix) {
+                                               my $file=$pagesources{$page};
+                                               my $oldcontent=readfile($config{srcdir}."/".$file);
+                                               my $content=renamepage_hook($page, $src, $dest, $oldcontent);
+                                               if ($oldcontent ne $content) {
+                                                       my $token=IkiWiki::rcs_prepedit($file);
+                                                       eval { writefile($file, $config{srcdir}, $content) };
+                                                       next if $@;
+                                                       my $conflict=IkiWiki::rcs_commit(
+                                                               $file,
+                                                               sprintf(gettext("update for rename of %s to %s"), $srcfile, $destfile),
+                                                               $token,
+                                                               $session->param("name"), 
+                                                               $ENV{REMOTE_ADDR}
+                                                       );
+                                                       push @fixedlinks, $page if ! defined $conflict;
+                                               }
                                        }
                                }
                        }
+
+                       # End renaming process and refresh wiki.
                        if ($config{rcs}) {
                                IkiWiki::enable_commit_hook();
                                IkiWiki::rcs_update();
@@ -323,39 +322,44 @@ sub sessioncgi ($$) { #{{{
 
                        # Scan for any remaining broken links to $src.
                        my @brokenlinks;
-                       foreach my $page (keys %links) {
-                               my $broken=0;
-                               foreach my $link (@{$links{$page}}) {
-                                       my $bestlink=bestlink($page, $link);
-                                       if ($bestlink eq $src) {
-                                               $broken=1;
-                                               last;
+                       if ($src ne $dest) {
+                               foreach my $page (keys %links) {
+                                       my $broken=0;
+                                       foreach my $link (@{$links{$page}}) {
+                                               my $bestlink=bestlink($page, $link);
+                                               if ($bestlink eq $src) {
+                                                       $broken=1;
+                                                       last;
+                                               }
                                        }
+                                       push @brokenlinks, $page if $broken;
                                }
-                               push @brokenlinks, $page if $broken;
                        }
 
                        # Generate a rename summary, that will be shown at the top
                        # of the edit template.
                        my $template=template("renamesummary.tmpl");
-                       $template->param(src => $src);
-                       $template->param(dest => $dest);
-                       $template->param(brokenlinks => [
-                               map {
-                                       {
-                                               page => htmllink($dest, $dest, $_,
-                                                               noimageinline => 1)
-                                       }
-                               } @brokenlinks
-                       ]);
-                       $template->param(fixedlinks => [
-                               map {
-                                       {
-                                               page => htmllink($dest, $dest, $_,
-                                                               noimageinline => 1)
-                                       }
-                               } @fixedlinks
-                       ]);
+                       $template->param(src => $srcfile);
+                       $template->param(dest => $destfile);
+                       if ($src ne $dest) {
+                               $template->param(brokenlinks_checked => 1);
+                               $template->param(brokenlinks => [
+                                       map {
+                                               {
+                                                       page => htmllink($dest, $dest, $_,
+                                                                       noimageinline => 1)
+                                               }
+                                       } @brokenlinks
+                               ]);
+                               $template->param(fixedlinks => [
+                                       map {
+                                               {
+                                                       page => htmllink($dest, $dest, $_,
+                                                                       noimageinline => 1)
+                                               }
+                                       } @fixedlinks
+                               ]);
+                       }
                        $renamesummary=$template->output;
 
                        postrename($session, $src, $dest, $q->param("attachment"));
@@ -382,5 +386,26 @@ sub renamepage_hook ($$$$) { #{{{
 
        return $content;
 }# }}}
+                       
+sub do_rename ($$$) { #{{{
+       my $srcfile=shift;
+       my $destfile=shift;
+       my $session=shift;
+                       
+       # Actual file rename happens here.
+       # First, ensure that the dest directory exists and is ok.
+       IkiWiki::prep_writefile($destfile, $config{srcdir});
+       if ($config{rcs}) {
+               IkiWiki::rcs_rename($srcfile, $destfile);
+               IkiWiki::rcs_commit_staged(
+                       sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
+                       $session->param("name"), $ENV{REMOTE_ADDR});
+       }
+       else {
+               if (! rename("$config{srcdir}/$srcfile", "$config{srcdir}/$destfile")) {
+                       error("rename: $!");
+               }
+       }
+} # }}}
 
 1