View | Details | Raw Unified | Return to bug 33749
Collapse All | Expand All

(-)a/C4/AuthoritiesMarc.pm (-1 / +3 lines)
Lines 35-40 use Koha::Authority::MergeRequests; Link Here
35
use Koha::Authority::Types;
35
use Koha::Authority::Types;
36
use Koha::Authority;
36
use Koha::Authority;
37
use Koha::Libraries;
37
use Koha::Libraries;
38
use Koha::RecordProcessor;
38
use Koha::SearchEngine;
39
use Koha::SearchEngine;
39
use Koha::SearchEngine::Indexer;
40
use Koha::SearchEngine::Indexer;
40
use Koha::SearchEngine::Search;
41
use Koha::SearchEngine::Search;
Lines 659-665 sub AddAuthority { Link Here
659
  }
660
  }
660
661
661
    if ( C4::Context->preference('StripWhitespaceChars') ) {
662
    if ( C4::Context->preference('StripWhitespaceChars') ) {
662
        $record = Koha::MetadataRecord::stripWhitespaceChars( $record );
663
        my $p = Koha::RecordProcessor->new({ filters => qw(TrimFields) });
664
        $p->process( $record );
663
    }
665
    }
664
666
665
    # Save record into auth_header, update 001
667
    # Save record into auth_header, update 001
(-)a/C4/Biblio.pm (-2 / +3 lines)
Lines 110-120 use Koha::Holds; Link Here
110
use Koha::ItemTypes;
110
use Koha::ItemTypes;
111
use Koha::MarcOverlayRules;
111
use Koha::MarcOverlayRules;
112
use Koha::Plugins;
112
use Koha::Plugins;
113
use Koha::RecordProcessor;
113
use Koha::SearchEngine;
114
use Koha::SearchEngine;
114
use Koha::SearchEngine::Indexer;
115
use Koha::SearchEngine::Indexer;
115
use Koha::Libraries;
116
use Koha::Libraries;
116
use Koha::Util::MARC;
117
use Koha::Util::MARC;
117
use Koha::MetadataRecord;
118
118
119
=head1 NAME
119
=head1 NAME
120
120
Lines 2829-2835 sub ModBiblioMarc { Link Here
2829
    }
2829
    }
2830
2830
2831
    if ( C4::Context->preference('StripWhitespaceChars') ) {
2831
    if ( C4::Context->preference('StripWhitespaceChars') ) {
2832
        $record = Koha::MetadataRecord::stripWhitespaceChars( $record );
2832
        my $p = Koha::RecordProcessor->new({ filters => qw(TrimFields) });
2833
        $p->process( $record );
2833
    }
2834
    }
2834
2835
2835
    my $metadata = {
2836
    my $metadata = {
(-)a/Koha/MetadataRecord.pm (-27 lines)
Lines 108-138 sub createMergeHash { Link Here
108
    }
108
    }
109
}
109
}
110
110
111
=head2 stripWhitespaceChars
112
113
    $record = Koha::MetadataRecord::stripWhitespaceChars( $record );
114
115
Strip leading and trailing whitespace characters from input fields.
116
117
=cut
118
119
sub stripWhitespaceChars {
120
    my ( $record ) = @_;
121
122
    foreach my $field ( $record->fields ) {
123
        unless ( $field->is_control_field ) {
124
            foreach my $subfield ( $field->subfields ) {
125
                my $key = $subfield->[0];
126
                my $value = $subfield->[1];
127
                $value =~ s/[\n\r]+/ /g;
128
                $value =~ s/^\s+|\s+$//g;
129
                $field->add_subfields( $key => $value ); # add subfield to the end of the subfield list
130
                $field->delete_subfield( pos => 0 ); # delete the subfield at the top of the subfield list
131
            }
132
        }
133
    }
134
135
    return $record;
136
}
137
138
1;
111
1;
(-)a/t/Koha_MetadataRecord.t (-24 / +1 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 6;
22
use Test::More tests => 5;
23
use Test::Warn;
23
use Test::Warn;
24
24
25
use Koha::RecordProcessor;
25
use Koha::RecordProcessor;
Lines 143-168 subtest "new() tests" => sub { Link Here
143
143
144
    is( $metadata_record, undef, 'record object mandatory')
144
    is( $metadata_record, undef, 'record object mandatory')
145
};
145
};
146
147
subtest "stripWhitespaceChars() tests" => sub {
148
    plan tests => 2;
149
150
    # Test default values with a MARC::Record record
151
    my $record = MARC::Record->new();
152
153
    $record->add_fields(
154
        [ '001', '1234' ],
155
        [ '150', ' ', ' ', a => 'Test' ],
156
        [ '520', ' ', ' ', a => "This is\na test!\t" ],
157
        [ '521', ' ', ' ', a => "This is a\t test!\t" ],
158
    );
159
160
    my $p = Koha::RecordProcessor->new({ filters => ['TrimFields'] });
161
    $p->process( $record );
162
163
    my $get520a = $record->subfield('520','a');
164
    is( $get520a, "This is a test!", "Whitespace characters are appropriately stripped or replaced with spaces" );
165
166
    my $get521a = $record->subfield('521','a');
167
    is( $get521a, "This is a\t test!", "Trailing tabs are stripped while inner tabs are kept" );
168
};
(-)a/t/RecordProcessor.t (-1 / +24 lines)
Lines 203-206 subtest 'options() tests' => sub { Link Here
203
    );
203
    );
204
};
204
};
205
205
206
subtest "'TrimFields' filter tests" => sub {
207
208
    plan tests => 2;
209
210
    # Test default values with a MARC::Record record
211
    my $record = MARC::Record->new();
212
213
    $record->add_fields(
214
        [ '001', '1234' ],
215
        [ '150', ' ', ' ', a => 'Test' ],
216
        [ '520', ' ', ' ', a => "This is\na test!\t" ],
217
        [ '521', ' ', ' ', a => "This is a\t test!\t" ],
218
    );
219
220
    my $p = Koha::RecordProcessor->new({ filters => ['TrimFields'] });
221
    $p->process( $record );
222
223
    my $get520a = $record->subfield('520','a');
224
    is( $get520a, "This is a test!", "Whitespace characters are appropriately stripped or replaced with spaces" );
225
226
    my $get521a = $record->subfield('521','a');
227
    is( $get521a, "This is a\t test!", "Trailing tabs are stripped while inner tabs are kept" );
228
};
229
206
done_testing();
230
done_testing();
207
- 

Return to bug 33749