... 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 <intrigeri@boum.org>
sub renamepages(@) {
my %params = @_;
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
my $session = $params{session};
# Save the page(s) the user asked to rename, so that our
# - 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.
# - 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);
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,
+ };
# 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");
# 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.");
}
return gettext("Can not rename a translation. Renaming the master page, ".
"though, renames its translations as well.");
}
- 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) {
# See if any subpages need to be renamed.
if ($q->param("subpages") && $src ne $dest) {
foreach my $p (keys %pagesources) {
+ @torename=rename_hook(
+ torename => \@torename,
+ done => {},
+ cgi => $q,
+ session => $session,
+ );
+
require IkiWiki::Render;
IkiWiki::disable_commit_hook() if $config{rcs};
my %origpagesources=%pagesources;
require IkiWiki::Render;
IkiWiki::disable_commit_hook() if $config{rcs};
my %origpagesources=%pagesources;
+
+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;
sub do_rename ($$$) {
my $rename=shift;
hook(type => "rename", id => "foo", call => \&rename);
When a page or set of pages is renamed, the referenced function is
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.
* `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);
### getsetup
hook(type => "getsetup", id => "foo", call => \&getsetup);