Summary: | Allow item batch modification to use capturing groups in regex option | ||
---|---|---|---|
Product: | Koha | Reporter: | Andrew Fuerste-Henry <andrew> |
Component: | Cataloging | Assignee: | Martin Renvoize (ashimema) <martin.renvoize> |
Status: | CLOSED FIXED | QA Contact: | Nick Clemens (kidclamp) <nick> |
Severity: | enhancement | ||
Priority: | P5 - low | CC: | aspencatteam, fridolin.somers, kyle, m.de.rooy, martin.renvoize, nick |
Version: | Main | Keywords: | Manual |
Hardware: | All | ||
OS: | All | ||
See Also: | https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=23873 | ||
Change sponsored?: | --- | Patch complexity: | --- |
Documentation contact: | Documentation submission: | ||
Text to go in the release notes: |
This enhancement adds support for capture groups in the regular expression option of batch item modification.
One may now use `(\d+)` for example to capture digits in the match and then the `$1` placeholder in the replace.
|
Version(s) released in: |
22.05.00
|
Circulation function: | |||
Attachments: |
Bug 29781: This patch adds a unit test for capture groups support in Koha::Items
Bug 29781: Enable capture groups in batch item modification Bug 29781: Unit test Bug 29781: Enable capture groups in batch item modification Bug 29781: Unit test Bug 29781: Enable capture groups in batch item modification Bug 29781: Unit test Bug 29781: Enable capture groups in batch item modification Bug 29781: Unit test Bug 29781: Enable capture groups in batch item modification |
Description
Andrew Fuerste-Henry
2022-01-03 19:13:38 UTC
Created attachment 129526 [details] [review] Bug 29781: This patch adds a unit test for capture groups support in Koha::Items Test plan 1) Run the test prior to applying the capture groups fix, note it fails. 2) Apply the capture groups fix patch 3) Run the test again, it should now pass Created attachment 129527 [details] [review] Bug 29781: Enable capture groups in batch item modification 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 batch item modification and note it does not work. 2) Apply the patch and try again, this time the capture group should yield the expected results. 3) Run t/SimpleMARC.t and confirm the tests still pass. Created attachment 129528 [details] [review] Bug 29781: Unit test This patch adds a unit test for capture groups support in Koha::Items Test plan 1) Run the test prior to applying the capture groups fix, note it fails. 2) Apply the capture groups fix patch 3) Run the test again, it should now pass Created attachment 129529 [details] [review] Bug 29781: Enable capture groups in batch item modification 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 batch item modification and note it does not work. 2) Apply the patch and try again, this time the capture group should yield the expected results. 3) Run t/SimpleMARC.t and confirm the tests still pass. A quick hypothetical example using this to find call numbers that don't have volume and copy numbers in the correct order. To test: 1: have 2 items with these call numbers: - KF 123 .A987 c. 11 v. 22 - KF 123 .A987 v. 3 c. 1 2: put those items in item batch mod 3: do a regex substitution on call number: - s/(^[A-Z]+ \d+\b)\s+(\.[A-Z]+\d+\b)\s+(c\.\s\d+\b)\s+(v\.\s\d+\b)/$1 $2 $4 $3/ 4: Observe your call numbers are now: - KF 123 .A987 v. 22 c.11 - KF 123 .A987 v. 3 c. 1 Created attachment 129573 [details] [review] Bug 29781: Unit test This patch adds a unit test for capture groups support in Koha::Items Test plan 1) Run the test prior to applying the capture groups fix, note it fails. 2) Apply the capture groups fix patch 3) Run the test again, it should now pass Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com> Created attachment 129574 [details] [review] Bug 29781: Enable capture groups in batch item modification 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 batch item modification and note it does not work. 2) Apply the patch and try again, this time the capture group should yield the expected results. 3) Run t/SimpleMARC.t and confirm the tests still pass. Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com> Signing off. Created attachment 130498 [details] [review] Bug 29781: Unit test This patch adds a unit test for capture groups support in Koha::Items Test plan 1) Run the test prior to applying the capture groups fix, note it fails. 2) Apply the capture groups fix patch 3) Run the test again, it should now pass Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com> Signed-off-by: Jo Hunter <jhunter@clicweb.org> Created attachment 130499 [details] [review] Bug 29781: Enable capture groups in batch item modification 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 batch item modification and note it does not work. 2) Apply the patch and try again, this time the capture group should yield the expected results. 3) Run t/SimpleMARC.t and confirm the tests still pass. Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com> Signed-off-by: Jo Hunter <jhunter@clicweb.org> Created attachment 130508 [details] [review] Bug 29781: Unit test This patch adds a unit test for capture groups support in Koha::Items Test plan 1) Run the test prior to applying the capture groups fix, note it fails. 2) Apply the capture groups fix patch 3) Run the test again, it should now pass Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com> Signed-off-by: Jo Hunter <jhunter@clicweb.org> Signed-off-by: Nick Clemens <nick@bywatersolutions.com> Created attachment 130509 [details] [review] Bug 29781: Enable capture groups in batch item modification 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 batch item modification and note it does not work. 2) Apply the patch and try again, this time the capture group should yield the expected results. 3) Run t/SimpleMARC.t and confirm the tests still pass. Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com> Signed-off-by: Jo Hunter <jhunter@clicweb.org> Signed-off-by: Nick Clemens <nick@bywatersolutions.com> I tried executing code and couldn't exploit it perl -e 'use Koha::Items; warn Koha::Items::apply_regex({value=>q{KF 123 .A987 c. 11 v. 22},search=>q{(^[A-Z]+ \d+\b)\s+(\.[A-Z]+\d+\b)\s+(c\.\s\d+\b)\s+(v\.\s\d+\b)}, replace=>q{ $1 $2 $4 $3 object_class() }});' If I comment out the lines that quote the replace I get 'Koha::Items' - so that protection seems to work Great :) Looking forward to see Bug 23873 in passed QA It will be important to document this. Pushed to master for 22.05, thanks to everybody involved [U+1F984] |