From: intrigeri Date: Sun, 8 Mar 2009 10:44:00 +0000 (+0100) Subject: rename hook: run once per file to be renamed X-Git-Tag: 3.15~212 X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/commitdiff_plain/8cfe428a28b2714c57282566466f4f7bdbdfdffb rename hook: run once per file to be renamed ... as Joey suggested on todo/need_global_renamepage_hook This hook is applied recursively to returned additional rename hashes, so that it handles the case where two plugins use the hook: plugin A would see when plugin B adds a new file to be renamed. The full set of rename hashes can no longer be changed by hook functions, that are only allowed to return any additional rename hashes it wants to add. Rationale: the correct behavior of the recursion would be hard, if not impossible, to define, if already considered pages were changing on the run. Signed-off-by: intrigeri --- diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 9a3df79ac..f25beba72 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -295,7 +295,7 @@ sub pagetemplate (@) { sub renamepages(@) { my %params = @_; - my @torename = @{$params{torename}}; + my %torename = %{$params{torename}}; my $session = $params{session}; # Save the page(s) the user asked to rename, so that our @@ -305,24 +305,25 @@ sub renamepages(@) { # - a user trying to directly rename a translation # This is why this hook has to be run first, before the list of pages # to rename is modified by other plugins. - $session->param(po_orig_torename => \@torename); + my @orig_torename; + @orig_torename=@{$session->param("po_orig_torename")} + if defined $session->param("po_orig_torename"); + push @orig_torename, $torename{src}; + $session->param(po_orig_torename => \@orig_torename); IkiWiki::cgi_savesession($session); - my @ret=@torename; - # iterate on @torename and push onto @ret, so that we don't iterate - # on the items we added ourselves - foreach my $rename (@torename) { - next unless istranslatable($rename->{src}); - my %otherpages=%{otherlanguages($rename->{src})}; - while (my ($lang, $otherpage) = each %otherpages) { - push @ret, { - src => $otherpage, - srcfile => $pagesources{$otherpage}, - dest => otherlanguage($rename->{dest}, $lang), - destfile => $rename->{dest}.".".$lang.".po", - required => 0, - }; - } + return () unless istranslatable($torename{src}); + + my @ret; + my %otherpages=%{otherlanguages($torename{src})}; + while (my ($lang, $otherpage) = each %otherpages) { + push @ret, { + src => $otherpage, + srcfile => $pagesources{$otherpage}, + dest => otherlanguage($torename{dest}, $lang), + destfile => $torename{dest}.".".$lang.".po", + required => 0, + }; } return @ret; } @@ -443,7 +444,7 @@ sub canrename (@) { # by looking for the master page in the list of to-be-renamed pages we # saved early in the renaming process. my $orig_torename = $session->param("po_orig_torename"); - unless (grep { $_->{src} eq $masterpage } @{$orig_torename}) { + unless (grep { $_ eq $masterpage } @{$orig_torename}) { return gettext("Can not rename a translation. Renaming the master page, ". "though, renames its translations as well."); } diff --git a/IkiWiki/Plugin/rename.pm b/IkiWiki/Plugin/rename.pm index f39c93332..ccbf09c3f 100644 --- a/IkiWiki/Plugin/rename.pm +++ b/IkiWiki/Plugin/rename.pm @@ -312,14 +312,6 @@ sub sessioncgi ($$) { required => 1, }; - IkiWiki::run_hooks(rename => sub { - @torename=shift->( - torename => \@torename, - cgi => $q, - session => $session - ); - }); - # See if any subpages need to be renamed. if ($q->param("subpages") && $src ne $dest) { foreach my $p (keys %pagesources) { @@ -341,6 +333,13 @@ sub sessioncgi ($$) { } } + @torename=rename_hook( + torename => \@torename, + done => {}, + cgi => $q, + session => $session, + ); + require IkiWiki::Render; IkiWiki::disable_commit_hook() if $config{rcs}; my %origpagesources=%pagesources; @@ -466,6 +465,42 @@ sub renamepage_hook ($$$$) { return $content; } + +sub rename_hook (@) { + my %params = @_; + + my @torename=@{$params{torename}}; + my %done=%{$params{done}}; + my $q=$params{cgi}; + my $session=$params{session}; + + debug("rename_hook called with ".scalar(@torename)." args."); + my @nextset; + if (@torename) { + 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, + ); + return @torename; + } + else { + return (); + } +} sub do_rename ($$$) { my $rename=shift; diff --git a/doc/plugins/write.mdwn b/doc/plugins/write.mdwn index be01605e8..cccfb9bba 100644 --- a/doc/plugins/write.mdwn +++ b/doc/plugins/write.mdwn @@ -422,14 +422,18 @@ new page. hook(type => "rename", id => "foo", call => \&rename); When a page or set of pages is renamed, the referenced function is -called, and is passed named parameters: +called for every page, and is passed named parameters: -* `torename`: a reference to an array of hashes with keys: `src`, `srcfile`, - `dest`, `destfile`, `required`. Such a hook function can either return the - array content unchanged, or modify it and return the modified version. +* `torename`: a reference to a hash with keys: `src`, `srcfile`, + `dest`, `destfile`, `required`. * `cgi`: a CGI object * `session`: a session object. +Such a hook function returns any additional rename hashes it wants to +add. This hook is applied recursively to returned additional rename +hashes, so that it handles the case where two plugins use the hook: +plugin A would see when plugin B adds a new file to be renamed. + ### getsetup hook(type => "getsetup", id => "foo", call => \&getsetup);