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 <intrigeri@boum.org>master
parent
ae474d8e14
commit
8cfe428a28
|
@ -295,7 +295,7 @@ sub pagetemplate (@) {
|
||||||
sub renamepages(@) {
|
sub renamepages(@) {
|
||||||
my %params = @_;
|
my %params = @_;
|
||||||
|
|
||||||
my @torename = @{$params{torename}};
|
my %torename = %{$params{torename}};
|
||||||
my $session = $params{session};
|
my $session = $params{session};
|
||||||
|
|
||||||
# Save the page(s) the user asked to rename, so that our
|
# Save the page(s) the user asked to rename, so that our
|
||||||
|
@ -305,25 +305,26 @@ sub renamepages(@) {
|
||||||
# - a user trying to directly rename a translation
|
# - a user trying to directly rename a translation
|
||||||
# This is why this hook has to be run first, before the list of pages
|
# This is why this hook has to be run first, before the list of pages
|
||||||
# to rename is modified by other plugins.
|
# 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;
|
return () unless istranslatable($torename{src});
|
||||||
# iterate on @torename and push onto @ret, so that we don't iterate
|
|
||||||
# on the items we added ourselves
|
my @ret;
|
||||||
foreach my $rename (@torename) {
|
my %otherpages=%{otherlanguages($torename{src})};
|
||||||
next unless istranslatable($rename->{src});
|
|
||||||
my %otherpages=%{otherlanguages($rename->{src})};
|
|
||||||
while (my ($lang, $otherpage) = each %otherpages) {
|
while (my ($lang, $otherpage) = each %otherpages) {
|
||||||
push @ret, {
|
push @ret, {
|
||||||
src => $otherpage,
|
src => $otherpage,
|
||||||
srcfile => $pagesources{$otherpage},
|
srcfile => $pagesources{$otherpage},
|
||||||
dest => otherlanguage($rename->{dest}, $lang),
|
dest => otherlanguage($torename{dest}, $lang),
|
||||||
destfile => $rename->{dest}.".".$lang.".po",
|
destfile => $torename{dest}.".".$lang.".po",
|
||||||
required => 0,
|
required => 0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return @ret;
|
return @ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -443,7 +444,7 @@ sub canrename (@) {
|
||||||
# by looking for the master page in the list of to-be-renamed pages we
|
# by looking for the master page in the list of to-be-renamed pages we
|
||||||
# saved early in the renaming process.
|
# saved early in the renaming process.
|
||||||
my $orig_torename = $session->param("po_orig_torename");
|
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, ".
|
return gettext("Can not rename a translation. Renaming the master page, ".
|
||||||
"though, renames its translations as well.");
|
"though, renames its translations as well.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -312,14 +312,6 @@ sub sessioncgi ($$) {
|
||||||
required => 1,
|
required => 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
IkiWiki::run_hooks(rename => sub {
|
|
||||||
@torename=shift->(
|
|
||||||
torename => \@torename,
|
|
||||||
cgi => $q,
|
|
||||||
session => $session
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
# See if any subpages need to be renamed.
|
# See if any subpages need to be renamed.
|
||||||
if ($q->param("subpages") && $src ne $dest) {
|
if ($q->param("subpages") && $src ne $dest) {
|
||||||
foreach my $p (keys %pagesources) {
|
foreach my $p (keys %pagesources) {
|
||||||
|
@ -341,6 +333,13 @@ sub sessioncgi ($$) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@torename=rename_hook(
|
||||||
|
torename => \@torename,
|
||||||
|
done => {},
|
||||||
|
cgi => $q,
|
||||||
|
session => $session,
|
||||||
|
);
|
||||||
|
|
||||||
require IkiWiki::Render;
|
require IkiWiki::Render;
|
||||||
IkiWiki::disable_commit_hook() if $config{rcs};
|
IkiWiki::disable_commit_hook() if $config{rcs};
|
||||||
my %origpagesources=%pagesources;
|
my %origpagesources=%pagesources;
|
||||||
|
@ -467,6 +466,42 @@ sub renamepage_hook ($$$$) {
|
||||||
return $content;
|
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 ($$$) {
|
sub do_rename ($$$) {
|
||||||
my $rename=shift;
|
my $rename=shift;
|
||||||
my $q=shift;
|
my $q=shift;
|
||||||
|
|
|
@ -422,14 +422,18 @@ new page.
|
||||||
hook(type => "rename", id => "foo", call => \&rename);
|
hook(type => "rename", id => "foo", call => \&rename);
|
||||||
|
|
||||||
When a page or set of pages is renamed, the referenced function is
|
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`,
|
* `torename`: a reference to a hash with keys: `src`, `srcfile`,
|
||||||
`dest`, `destfile`, `required`. Such a hook function can either return the
|
`dest`, `destfile`, `required`.
|
||||||
array content unchanged, or modify it and return the modified version.
|
|
||||||
* `cgi`: a CGI object
|
* `cgi`: a CGI object
|
||||||
* `session`: a session 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
|
### getsetup
|
||||||
|
|
||||||
hook(type => "getsetup", id => "foo", call => \&getsetup);
|
hook(type => "getsetup", id => "foo", call => \&getsetup);
|
||||||
|
|
Loading…
Reference in New Issue