From 54477ecc51e4504fa66d0f0cc6da50029225131a Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 7 Jun 2023 14:28:34 -0300 Subject: [PATCH] Bug 33749: Add TrimFields field class This patch adds a RecordProcessor filter class that takes care of performing the field cleanup that was originally done in the static method `Koha::MetadataRecord::stripWhitespaceChars`. In order to test the results, I adapted the tests to use the filter instead of the original method. To test: 1. Run: $ ktd --shell k$ prove t/Koha_MetadataRecord.t => SUCCESS: Tests pass 2. Apply this patch 3. Repeat 1 => SUCCESS: Tests still pass 4. Sign off :-D Signed-off-by: David Nind Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/Filter/MARC/TrimFields.pm | 71 ++++++++++++++++++++++++++++++++++ t/Koha_MetadataRecord.t | 4 +- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 Koha/Filter/MARC/TrimFields.pm diff --git a/Koha/Filter/MARC/TrimFields.pm b/Koha/Filter/MARC/TrimFields.pm new file mode 100644 index 0000000000..f5a9d9fcef --- /dev/null +++ b/Koha/Filter/MARC/TrimFields.pm @@ -0,0 +1,71 @@ +package Koha::Filter::MARC::TrimFields; + +# Copyright 2023 Koha Development team + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +=head1 NAME + +Koha::Filter::MARC::TrimFields - Trims MARC::Record object data fields. + +=head1 SYNOPSIS + + my $p = Koha::RecordProcessor->new({ filters => ['TrimFields'] }); + + my $metadata = Koha::Biblio::Metadatas->find($biblio_id); + my $record = $metadata->record; + + $p->process($record); + +=head1 DESCRIPTION + +Filter to trim MARC::Record object data fields. + +=cut + +use Modern::Perl; + +use base qw(Koha::RecordProcessor::Base); +our $NAME = 'TrimFields'; + +=head2 filter + +Trim MARC::Record object data fields. + +=cut + +sub filter { + my ( $self, $record ) = @_; + + return + unless $record and ref($record) eq 'MARC::Record'; + + foreach my $field ( $record->fields ) { + unless ( $field->is_control_field ) { + foreach my $subfield ( $field->subfields ) { + my $key = $subfield->[0]; + my $value = $subfield->[1]; + $value =~ s/[\n\r]+/ /g; + $value =~ s/^\s+|\s+$//g; + $field->add_subfields( $key => $value ); # add subfield to the end of the subfield list + $field->delete_subfield( pos => 0 ); # delete the subfield at the top of the subfield list + } + } + } + return $record; +} + +1; diff --git a/t/Koha_MetadataRecord.t b/t/Koha_MetadataRecord.t index 7202f4a86f..f44700970c 100755 --- a/t/Koha_MetadataRecord.t +++ b/t/Koha_MetadataRecord.t @@ -22,6 +22,7 @@ use Modern::Perl; use Test::More tests => 6; use Test::Warn; +use Koha::RecordProcessor; use MARC::Record; BEGIN { @@ -156,7 +157,8 @@ subtest "stripWhitespaceChars() tests" => sub { [ '521', ' ', ' ', a => "This is a\t test!\t" ], ); - $record = Koha::MetadataRecord::stripWhitespaceChars( $record ); + my $p = Koha::RecordProcessor->new({ filters => ['TrimFields'] }); + $p->process( $record ); my $get520a = $record->subfield('520','a'); is( $get520a, "This is a test!", "Whitespace characters are appropriately stripped or replaced with spaces" ); -- 2.39.5