]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/rename.pm
Added a comment
[git.ikiwiki.info.git] / IkiWiki / Plugin / rename.pm
index 37f643eefbdc7ce02122fcec10e1a14c814497b7..8e32d41aecfd58934fc7353612c680162abd2914 100644 (file)
@@ -10,7 +10,7 @@ sub import {
        hook(type => "formbuilder_setup", id => "rename", call => \&formbuilder_setup);
        hook(type => "formbuilder", id => "rename", call => \&formbuilder);
        hook(type => "sessioncgi", id => "rename", call => \&sessioncgi);
-
+       hook(type => "rename", id => "rename", call => \&rename_subpages);
 }
 
 sub getsetup () {
@@ -18,6 +18,7 @@ sub getsetup () {
                plugin => {
                        safe => 1,
                        rebuild => 0,
+                       section => "web",
                },
 }
 
@@ -49,7 +50,7 @@ sub check_canrename ($$$$$$) {
        IkiWiki::check_canedit($src, $q, $session);
        if ($attachment) {
                if (IkiWiki::Plugin::attachment->can("check_canattach")) {
-                       IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
+                       IkiWiki::Plugin::attachment::check_canattach($session, $src, "$config{srcdir}/$srcfile");
                }
                else {
                        error("renaming of attachments is not allowed");
@@ -62,9 +63,8 @@ sub check_canrename ($$$$$$) {
                        error(gettext("no change to the file name was specified"));
                }
 
-               # Must be a legal filename, and not absolute.
-               if (IkiWiki::file_pruned($destfile, $config{srcdir}) || 
-                   $destfile=~/^\//) {
+               # Must be a legal filename.
+               if (IkiWiki::file_pruned($destfile)) {
                        error(sprintf(gettext("illegal name")));
                }
 
@@ -84,15 +84,16 @@ sub check_canrename ($$$$$$) {
                if ($attachment) {
                        # Note that $srcfile is used here, not $destfile,
                        # because it wants the current file, to check it.
-                       IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
+                       IkiWiki::Plugin::attachment::check_canattach($session, $dest, "$config{srcdir}/$srcfile");
                }
        }
 
        my $canrename;
        IkiWiki::run_hooks(canrename => sub {
                return if defined $canrename;
-               my $ret=shift->($q, $session, src => $src, srcfile => $srcfile,
-                               dest => $dest, destfile => $destfile);
+               my $ret=shift->(cgi => $q, session => $session,
+                       src => $src, srcfile => $srcfile,
+                       dest => $dest, destfile => $destfile);
                if (defined $ret) {
                        if ($ret eq "") {
                                $canrename=1;
@@ -107,6 +108,7 @@ sub check_canrename ($$$$$$) {
                        }
                }
        });
+       return defined $canrename ? $canrename : 1;
 }
 
 sub rename_form ($$$) {
@@ -124,26 +126,30 @@ sub rename_form ($$$) {
                method => 'POST',
                javascript => 0,
                params => $q,
-               action => $config{cgiurl},
-               stylesheet => IkiWiki::baseurl()."style.css",
+               action => IkiWiki::cgiurl(),
+               stylesheet => 1,
                fields => [qw{do page new_name attachment}],
        );
        
        $f->field(name => "do", type => "hidden", value => "rename", force => 1);
+       $f->field(name => "sid", type => "hidden", value => $session->id,
+               force => 1);
        $f->field(name => "page", type => "hidden", value => $page, force => 1);
        $f->field(name => "new_name", value => pagetitle($page, 1), size => 60);
        if (!$q->param("attachment")) {
                # insert the standard extensions
                my @page_types;
                if (exists $IkiWiki::hooks{htmlize}) {
-                       @page_types=grep { !/^_/ }
-                               keys %{$IkiWiki::hooks{htmlize}};
+                       foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
+                               push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
+                       }
                }
+               @page_types=sort @page_types;
        
                # make sure the current extension is in the list
                my ($ext) = $pagesources{$page}=~/\.([^.]+)$/;
                if (! $IkiWiki::hooks{htmlize}{$ext}) {
-                       unshift(@page_types, $ext);
+                       unshift(@page_types, [$ext, $ext]);
                }
        
                $f->field(name => "type", type => 'select',
@@ -173,8 +179,15 @@ sub rename_start ($$$$) {
        my $attachment=shift;
        my $page=shift;
 
-       check_canrename($page, $pagesources{$page}, undef, undef,
-               $q, $session);
+       # Special case for renaming held attachments; normal checks
+       # don't apply.
+       my $held=$attachment &&
+               IkiWiki::Plugin::attachment->can("is_held_attachment") &&
+               IkiWiki::Plugin::attachment::is_held_attachment($page);
+       if (! $held) {
+               check_canrename($page, $pagesources{$page}, undef, undef,
+                       $q, $session);
+       }
 
        # Save current form state to allow returning to it later
        # without losing any edits.
@@ -215,7 +228,7 @@ sub postrename ($;$$$) {
                # Update edit form content to fix any links present
                # on it.
                $postrename->param("editcontent",
-                       renamelink_hook($dest, $src, $dest,
+                       renamepage_hook($dest, $src, $dest,
                                 $postrename->param("editcontent")));
 
                # Get a new edit token; old was likely invalidated.
@@ -232,6 +245,7 @@ sub formbuilder (@) {
 
        if (defined $form->field("do") && ($form->field("do") eq "edit" ||
            $form->field("do") eq "create")) {
+               IkiWiki::decode_form_utf8($form);
                my $q=$params{cgi};
                my $session=$params{session};
 
@@ -239,7 +253,7 @@ sub formbuilder (@) {
                        rename_start($q, $session, 0, $form->field("page"));
                }
                elsif ($form->submitted eq "Rename Attachment") {
-                       my @selected=$q->param("attachment_select");
+                       my @selected=map { Encode::decode_utf8($_) } $q->param("attachment_select");
                        if (@selected > 1) {
                                error(gettext("Only one attachment can be renamed at a time."));
                        }
@@ -275,21 +289,21 @@ sub sessioncgi ($$) {
 
        if ($q->param("do") eq 'rename') {
                my $session=shift;
-               my ($form, $buttons)=rename_form($q, $session, $q->param("page"));
+               my ($form, $buttons)=rename_form($q, $session, Encode::decode_utf8($q->param("page")));
                IkiWiki::decode_form_utf8($form);
 
                if ($form->submitted eq 'Cancel') {
                        postrename($session);
                }
                elsif ($form->submitted eq 'Rename' && $form->validate) {
-                       # Queue of rename actions to perfom.
-                       my @torename;
+                       IkiWiki::checksessionexpiry($q, $session, $q->param('sid'));
 
                        # These untaints are safe because of the checks
                        # performed in check_canrename later.
-                       my $src=$q->param("page");
-                       my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src});
-                       my $dest=IkiWiki::possibly_foolish_untaint(titlepage($q->param("new_name")));
+                       my $src=$form->field("page");
+                       my $srcfile=IkiWiki::possibly_foolish_untaint($pagesources{$src})
+                               if exists $pagesources{$src};
+                       my $dest=IkiWiki::possibly_foolish_untaint(titlepage($form->field("new_name")));
                        my $destfile=$dest;
                        if (! $q->param("attachment")) {
                                my $type=$q->param('type');
@@ -303,6 +317,19 @@ sub sessioncgi ($$) {
                                
                                $destfile=newpagefile($dest, $type);
                        }
+               
+                       # Special case for renaming held attachments.
+                       my $held=$q->param("attachment") &&
+                               IkiWiki::Plugin::attachment->can("is_held_attachment") &&
+                               IkiWiki::Plugin::attachment::is_held_attachment($src);
+                       if ($held) {
+                               rename($held, IkiWiki::Plugin::attachment::attachment_holding_location($dest));
+                               postrename($session, $src, $dest, $q->param("attachment"))
+                                       unless defined $srcfile;
+                       }
+                       
+                       # Queue of rename actions to perfom.
+                       my @torename;
                        push @torename, {
                                src => $src,
                                srcfile => $srcfile,
@@ -311,29 +338,13 @@ sub sessioncgi ($$) {
                                required => 1,
                        };
 
-                       IkiWiki::run_hooks(rename => sub { shift->(\@torename, $q, $session); });
-
-                       # See if any subpages need to be renamed.
-                       if ($q->param("subpages") && $src ne $dest) {
-                               foreach my $p (keys %pagesources) {
-                                       next unless $pagesources{$p}=~m/^\Q$src\E\//;
-                                       # If indexpages is enabled, the
-                                       # srcfile should not be confused
-                                       # with a subpage.
-                                       next if $pagesources{$p} eq $srcfile;
-
-                                       my $d=$pagesources{$p};
-                                       $d=~s/^\Q$src\E\//$dest\//;
-                                       push @torename, {
-                                               src => $p,
-                                               srcfile => $pagesources{$p},
-                                               dest => pagename($d),
-                                               destfile => $d,
-                                               required => 0,
-                                       };
-                               }
-                       }
-                       
+                       @torename=rename_hook(
+                               torename => \@torename,
+                               done => {},
+                               cgi => $q,
+                               session => $session,
+                       );
+
                        require IkiWiki::Render;
                        IkiWiki::disable_commit_hook() if $config{rcs};
                        my %origpagesources=%pagesources;
@@ -357,8 +368,9 @@ sub sessioncgi ($$) {
                                $pagesources{$rename->{src}}=$rename->{destfile};
                        }
                        IkiWiki::rcs_commit_staged(
-                               sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
-                               $session->param("name"), $ENV{REMOTE_ADDR}) if $config{rcs};
+                               message => sprintf(gettext("rename %s to %s"), $srcfile, $destfile),
+                               session => $session,
+                       ) if $config{rcs};
 
                        # Then link fixups.
                        foreach my $rename (@torename) {
@@ -427,7 +439,40 @@ sub sessioncgi ($$) {
                exit 0;
        }
 }
-                                               
+
+# Add subpages to the list of pages to be renamed, if needed.
+sub rename_subpages (@) {
+       my %params = @_;
+
+       my %torename = %{$params{torename}};
+       my $q = $params{cgi};
+       my $src = $torename{src};
+       my $srcfile = $torename{src};
+       my $dest = $torename{dest};
+       my $destfile = $torename{dest};
+
+       return () unless ($q->param("subpages") && $src ne $dest);
+
+       my @ret;
+       foreach my $p (keys %pagesources) {
+               next unless $pagesources{$p}=~m/^\Q$src\E\//;
+               # If indexpages is enabled, the srcfile should not be confused
+               # with a subpage.
+               next if $pagesources{$p} eq $srcfile;
+
+               my $d=$pagesources{$p};
+               $d=~s/^\Q$src\E\//$dest\//;
+               push @ret, {
+                       src => $p,
+                       srcfile => $pagesources{$p},
+                       dest => pagename($d),
+                       destfile => $d,
+                       required => 0,
+               };
+       }
+       return @ret;
+}
+
 sub linklist {
        # generates a list of links in a form suitable for FormBuilder
        my $dest=shift;
@@ -445,10 +490,10 @@ sub linklist {
        } @{$list}]
 }
 
-sub renamelink_hook ($$$$) {
+sub renamepage_hook ($$$$) {
        my ($page, $src, $dest, $content)=@_;
 
-       IkiWiki::run_hooks(renamelink => sub {
+       IkiWiki::run_hooks(renamepage => sub {
                $content=shift->(
                        page => $page,
                        oldpage => $src,
@@ -459,7 +504,43 @@ sub renamelink_hook ($$$$) {
 
        return $content;
 }
-                       
+
+sub rename_hook {
+       my %params = @_;
+
+       my @torename=@{$params{torename}};
+       my %done=%{$params{done}};
+       my $q=$params{cgi};
+       my $session=$params{session};
+
+       return () unless @torename;
+
+       my @nextset;
+       foreach my $torename (@torename) {
+               unless (exists $done{$torename->{src}} && $done{$torename->{src}}) {
+                       IkiWiki::run_hooks(rename => sub {
+                               push @nextset, shift->(
+                                       torename => $torename,
+                                       cgi => $q,
+                                       session => $session,
+                               );
+                       });
+                       $done{$torename->{src}}=1;
+               }
+       }
+
+       push @torename, rename_hook(
+               torename => \@nextset,
+               done => \%done,
+               cgi => $q,
+               session => $session,
+       );
+
+       # dedup
+       my %seen;
+       return grep { ! $seen{$_->{src}}++ } @torename;
+}
+
 sub do_rename ($$$) {
        my $rename=shift;
        my $q=shift;
@@ -504,18 +585,18 @@ sub fixlinks ($$$) {
                }
                if ($needfix) {
                        my $file=$pagesources{$page};
+                       next unless -e $config{srcdir}."/".$file;
                        my $oldcontent=readfile($config{srcdir}."/".$file);
-                       my $content=renamelink_hook($page, $rename->{src}, $rename->{dest}, $oldcontent);
+                       my $content=renamepage_hook($page, $rename->{src}, $rename->{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"), $rename->{srcfile}, $rename->{destfile}),
-                                       $token,
-                                       $session->param("name"), 
-                                       $ENV{REMOTE_ADDR}
+                                       file => $file,
+                                       message => sprintf(gettext("update for rename of %s to %s"), $rename->{srcfile}, $rename->{destfile}),
+                                       token => $token,
+                                       session => $session,
                                );
                                push @fixedlinks, $page if ! defined $conflict;
                        }