]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/commitdiff
Merge branch 'master' into tova
authorJoey Hess <joey@kodama.kitenet.net>
Tue, 23 Sep 2008 21:42:38 +0000 (17:42 -0400)
committerJoey Hess <joey@kodama.kitenet.net>
Tue, 23 Sep 2008 21:42:38 +0000 (17:42 -0400)
1  2 
IkiWiki/Plugin/rename.pm

diff --combined IkiWiki/Plugin/rename.pm
index 3936a4a60c0471e24d5a994b6a4a43e10abd6e6a,f441ac025f12e5b1c06282295c821366674cdfc0..f0dfb2413856c5dcc2bbd1eae2930cc86ec5bac3
@@@ -21,14 -21,15 +21,15 @@@ sub getsetup () { #{{
                },
  } #}}}
  
- sub check_canrename ($$$$$$$) { #{{{
+ sub check_canrename ($$$$$$) { #{{{
        my $src=shift;
        my $srcfile=shift;
        my $dest=shift;
        my $destfile=shift;
        my $q=shift;
        my $session=shift;
-       my $attachment=shift;
+       my $attachment=! defined IkiWiki::pagetype($pagesources{$src});
  
        # Must be a known source file.
        if (! exists $pagesources{$src}) {
        # Must be editable.
        IkiWiki::check_canedit($src, $q, $session);
        if ($attachment) {
-               IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
+               if (IkiWiki::Plugin::attachment->can("check_canattach")) {
+                       IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
+               }
+               else {
+                       error("renaming of attachments is not allowed");
+               }
        }
        
        # Dest checks can be omitted by passing undef.
@@@ -123,13 -129,6 +129,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");
  
@@@ -143,7 -142,7 +149,7 @@@ sub rename_start ($$$$) { #{{
        my $page=shift;
  
        check_canrename($page, $pagesources{$page}, undef, undef,
-               $q, $session, $attachment);
+               $q, $session);
  
        # Save current form state to allow returning to it later
        # without losing any edits.
@@@ -271,40 -270,56 +277,40 @@@ sub sessioncgi ($$) { #{{
                        }
  
                        check_canrename($src, $srcfile, $dest, $destfile,
-                               $q, $session, $q->param("attachment"));
+                               $q, $session);
  
 -                      # Ensures that the dest directory exists and is ok.
 -                      IkiWiki::prep_writefile($destfile, $config{srcdir});
 +                      # See if subpages need to be renamed.
 +                      my @subpages;
 +                      if ($q->param("subpages") && $src ne $dest) {
 +                              foreach my $p (keys %pagesources) {
 +                                      if ($pagesources{$p}=~m/^\Q$src\E\/$/) {
 +                                              push @subpages, $p;
 +                                      }
 +                              }
 +                      }
  
 -                      # 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"), $srcfile, $destfile),
 -                                      $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);
 +
 +                      foreach my $subpage (@subpages) {
 +                              my $subsrc=$pagesources{$subpage};
 +                              my $subdest=$subsrc;
 +                              $subdest=~s/^\Q$src\E\//$dest/;
 +                              eval {
 +                                      do_rename($subsrc, $subdest, $session)
 +                              };
                        }
 +
                        my @fixedlinks;
                        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"), $srcfile, $destfile),
 -                                                              $token,
 -                                                              $session->param("name"), 
 -                                                              $ENV{REMOTE_ADDR}
 -                                                      );
 -                                                      push @fixedlinks, $page if ! defined $conflict;
 -                                              }
 -                                      }
 -                              }
 +                              push @fixedlinks, fixlinks($src, $dest, $session);
                        }
 +
 +                      # End renaming process and refresh wiki.
                        if ($config{rcs}) {
                                IkiWiki::enable_commit_hook();
                                IkiWiki::rcs_update();
@@@ -378,65 -393,5 +384,65 @@@ 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: $!");
 +              }
 +      }
 +} # }}}
 +
 +sub fixlinks ($$$) { #{{{
 +      my $src=shift;
 +      my $dest=shift;
 +      my $session=shift;
 +
 +      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 ($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;
 +                      }
 +              }
 +      }
 +
 +      return @fixedlinks;
 +} #}}}
  
  1