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

(-)a/C4/AuthoritiesMarc.pm (-10 / +1 lines)
Lines 642-657 sub AddAuthority { Link Here
642
  }
642
  }
643
643
644
    if ( C4::Context->preference('StripWhitespaceChars') ) {
644
    if ( C4::Context->preference('StripWhitespaceChars') ) {
645
        foreach my $field ( $record->fields ) {
645
        $record = Koha::MetadataRecord::stripWhitespaceChars( $record );
646
            foreach my $subfield ( $field->subfields ) {
647
                my $key = $subfield->[0];
648
                my $value = $subfield->[1];
649
                $value =~ s/[\n\r]+/ /g;
650
                $value =~ s/^\s+|\s+$|^\t+|\t+$//g;
651
                $field->add_subfields( $key => $value ); # add subfield to the end of the subfield list
652
                $field->delete_subfield( pos => 0 ); # delete the subfield at the top of the subfield list
653
            }
654
        }
655
    }
646
    }
656
647
657
    # Save record into auth_header, update 001
648
    # Save record into auth_header, update 001
(-)a/C4/Biblio.pm (-10 / +1 lines)
Lines 2746-2761 sub ModBiblioMarc { Link Here
2746
    }
2746
    }
2747
2747
2748
    if ( C4::Context->preference('StripWhitespaceChars') ) {
2748
    if ( C4::Context->preference('StripWhitespaceChars') ) {
2749
        foreach my $field ( $record->fields ) {
2749
        $record = Koha::MetadataRecord::stripWhitespaceChars( $record );
2750
            foreach my $subfield ( $field->subfields ) {
2751
                my $key = $subfield->[0];
2752
                my $value = $subfield->[1];
2753
                $value =~ s/[\n\r]+/ /g;
2754
                $value =~ s/^\s+|\s+$|^\t+|\t+$//g;
2755
                $field->add_subfields( $key => $value ); # add subfield to the end of the subfield list
2756
                $field->delete_subfield( pos => 0 ); # delete the subfield at the top of the subfield list
2757
            }
2758
        }
2759
    }
2750
    }
2760
2751
2761
    my $metadata = {
2752
    my $metadata = {
(-)a/Koha/MetadataRecord.pm (+25 lines)
Lines 108-111 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
        foreach my $subfield ( $field->subfields ) {
124
            my $key = $subfield->[0];
125
            my $value = $subfield->[1];
126
            $value =~ s/[\n\r]+/ /g;
127
            $value =~ s/^\s+|\s+$|^\t+|\t+$//g;
128
            $field->add_subfields( $key => $value ); # add subfield to the end of the subfield list
129
            $field->delete_subfield( pos => 0 ); # delete the subfield at the top of the subfield list
130
        }
131
    }
132
133
    return $record;
134
}
135
111
1;
136
1;
(-)a/t/Koha_MetadataRecord.t (-2 / +22 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 5;
22
use Test::More tests => 6;
23
use Test::Warn;
23
use Test::Warn;
24
24
25
use MARC::Record;
25
use MARC::Record;
Lines 143-145 subtest "new() tests" => sub { Link Here
143
    is( $metadata_record, undef, 'record object mandatory')
143
    is( $metadata_record, undef, 'record object mandatory')
144
};
144
};
145
145
146
- 
146
subtest "stripWhitespaceChars() tests" => sub {
147
    plan tests => 2;
148
149
    # Test default values with a MARC::Record record
150
    my $record = MARC::Record->new();
151
152
    $record->add_fields(
153
        [ '001', '1234' ],
154
        [ '150', ' ', ' ', a => 'Test' ],
155
        [ '520', ' ', ' ', a => "This is\na test!\t" ],
156
        [ '521', ' ', ' ', a => "This is a\t test!\t" ],
157
    );
158
159
    $record = Koha::MetadataRecord::stripWhitespaceChars( $record );
160
161
    my $get520a = $record->subfield('520','a');
162
    is( $get520a, "This is a test!", "Whitespace characters are appropriately stripped or replaced with spaces" );
163
164
    my $get521a = $record->subfield('521','a');
165
    is( $get521a, "This is a\t test!", "Trailing tabs are stripped while inner tabs are kept" );
166
};

Return to bug 30358