From 15ec842de0f1950bd945558d8566b681fd0bc587 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 13 Dec 2021 09:59:15 +0000 Subject: [PATCH] Bug 23873: Enable capture groups in MarcModificationTemplates This patch adds support for regular expression capture groups in MarcModificationTemplates. We escape double quotes in the replacement string, then quote the whole string before applying the `ee` modifier to the final regex application. See https://blog.james.rcpt.to/2010/10/25/perl-search-and-replace-using-variables/ for further details. Test plan 1) Attempt to use capture groups in your regular expression based MarcModificationTemplate and note it does not work. 2) Apply the patch and try again, this time the capture group should yield the expected results. --- Koha/SimpleMARC.pm | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Koha/SimpleMARC.pm b/Koha/SimpleMARC.pm index a501a9441f..b4c68208e1 100644 --- a/Koha/SimpleMARC.pm +++ b/Koha/SimpleMARC.pm @@ -636,6 +636,8 @@ sub _modify_values { my $regex = $params->{regex}; if ( $regex and $regex->{search} ) { + $regex->{replace} =~ s/"/\\"/g; # Protection from embedded code + $regex->{replace} = '"' . $regex->{replace} . '"'; # Put in a string for /ee $regex->{modifiers} //= q||; my @available_modifiers = qw( i g ); my $modifiers = q||; @@ -645,16 +647,16 @@ sub _modify_values { } foreach my $value ( @$values ) { if ( $modifiers =~ m/^(ig|gi)$/ ) { - $value =~ s/$regex->{search}/$regex->{replace}/ig; + $value =~ s/$regex->{search}/$regex->{replace}/igee; } elsif ( $modifiers eq 'i' ) { - $value =~ s/$regex->{search}/$regex->{replace}/i; + $value =~ s/$regex->{search}/$regex->{replace}/iee; } elsif ( $modifiers eq 'g' ) { - $value =~ s/$regex->{search}/$regex->{replace}/g; + $value =~ s/$regex->{search}/$regex->{replace}/gee; } else { - $value =~ s/$regex->{search}/$regex->{replace}/; + $value =~ s/$regex->{search}/$regex->{replace}/ee; } } } -- 2.20.1