Bug 30358: Strip leading/trailing whitespace characters from input fields when cataloguing
This enhancement adds a system preference StripWhitespaceChars which, when enabled, will strip leading and trailing whitespace characters from all fields when cataloguing both bibliographic records and authority records. Whitespace characters that will be stripped are: - spaces - newlines - carriage returns - tabs To test: 1. Apply patch and install database updates 2. Go to Administration, system preferences, find the new StripWhitespaceChars preference. It should be "Don't strip" by default. Change it to "Strip". 3. Search for a biblio record and edit it. Put some leading or trailing whitespace characters in input fields and textarea fields and save. 4. Confirm these characters are removed when you save the record. 5. Repeat steps 3 and 4 for authority records. 6. Confirm tests pass t/db_dependent/Biblio/ModBiblioMarc.t Sponsored-by: Educational Services Australia SCIS Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Bug 30358: (follow-up) Also strip inner newlines This patch amends the StripWhitespaceChars system preference to also strip inner newlines (line breaks and carriage returns) when enabled. Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Bug 30358: (follow-up) Inner newlines should be replaced with a space Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Bug 30358: (follow-up) Fixing tests and including for inner newlines Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Bug 30358: (follow-up) Clarify syspref wording about fields affected Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Bug 30358: (follow-up) Consider field has multiple subfields of same key To test: 1) Click the clone subfield button to make multiple subfields with the same key, i.e. 500$a$a$a 2) Save the record and confirm that the fields contain the correct data after whitespaces are stripped. Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Bug 30358: (follow-up) Put multiple subfields fix on auth side Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Bug 30358: (follow-up) stripWhitespaceChars subroutine and tests To test: Confirm test plan above still works as expected and tests pass in t/Koha_MetadataRecord.t Sponsored-by: Catalyst IT Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Bug 30358: (follow-up) Fixing ModBiblioMarc.t tests Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Bug 30358: (follow-up) Do not strip whitespace from control fields Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Bug 30358: (follow-up) Simplify regex The regex does the following: 1. Replace newlines and carriage returns with a space 2. Replace leading and trailing whitespace with nothing (strip) Signed-off-by: Hammat Wele <hammat.wele@inlibro.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
parent
9633c0a7de
commit
39b17d0526
|
|
@ -655,9 +655,13 @@ sub AddAuthority {
|
|||
$field->update($auth_type_subfield=>$authtypecode);
|
||||
}
|
||||
else {
|
||||
$record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode);
|
||||
$record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode);
|
||||
}
|
||||
|
||||
if ( C4::Context->preference('StripWhitespaceChars') ) {
|
||||
$record = Koha::MetadataRecord::stripWhitespaceChars( $record );
|
||||
}
|
||||
|
||||
# Save record into auth_header, update 001
|
||||
my $action;
|
||||
my $authority;
|
||||
|
|
|
|||
|
|
@ -114,6 +114,7 @@ use Koha::SearchEngine;
|
|||
use Koha::SearchEngine::Indexer;
|
||||
use Koha::Libraries;
|
||||
use Koha::Util::MARC;
|
||||
use Koha::MetadataRecord;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
|
|
@ -2811,6 +2812,10 @@ sub ModBiblioMarc {
|
|||
$f005->update(sprintf("%4d%02d%02d%02d%02d%04.1f",@a)) if $f005;
|
||||
}
|
||||
|
||||
if ( C4::Context->preference('StripWhitespaceChars') ) {
|
||||
$record = Koha::MetadataRecord::stripWhitespaceChars( $record );
|
||||
}
|
||||
|
||||
my $metadata = {
|
||||
biblionumber => $biblionumber,
|
||||
format => 'marcxml',
|
||||
|
|
|
|||
|
|
@ -108,4 +108,31 @@ sub createMergeHash {
|
|||
}
|
||||
}
|
||||
|
||||
=head2 stripWhitespaceChars
|
||||
|
||||
$record = Koha::MetadataRecord::stripWhitespaceChars( $record );
|
||||
|
||||
Strip leading and trailing whitespace characters from input fields.
|
||||
|
||||
=cut
|
||||
|
||||
sub stripWhitespaceChars {
|
||||
my ( $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;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
use Modern::Perl;
|
||||
|
||||
return {
|
||||
bug_number => "30358",
|
||||
description => "Add new system preference StripWhitespaceChars",
|
||||
up => sub {
|
||||
my ($args) = @_;
|
||||
my ($dbh, $out) = @$args{qw(dbh out)};
|
||||
|
||||
$dbh->do(q{INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES ('StripWhitespaceChars', '0', NULL, 'Strip leading and trailing whitespace characters and inner newlines from input fields when cataloguing bibliographic and authority records.', 'YesNo') });
|
||||
},
|
||||
};
|
||||
|
|
@ -699,6 +699,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
|
|||
('StatisticsFields','location|itype|ccode', NULL, 'Define Fields (from the items table) used for statistics members','Free'),
|
||||
('StockRotation','0',NULL,'If ON, enables the stock rotation module','YesNo'),
|
||||
('StoreLastBorrower','0','','If ON, the last borrower to return an item will be stored in items.last_returned_by','YesNo'),
|
||||
('StripWhitespaceChars','0',NULL,'Strip leading and trailing whitespace characters and inner newlines from input fields when cataloguing bibliographic and authority records.','YesNo'),
|
||||
('SubfieldsToAllowForRestrictedBatchmod','','Define a list of subfields for which edition is authorized when items_batchmod_restricted permission is enabled, separated by spaces. Example: 995\$f 995\$h 995\$j',NULL,'Free'),
|
||||
('SubfieldsToAllowForRestrictedEditing','','Define a list of subfields for which edition is authorized when edit_items_restricted permission is enabled, separated by spaces. Example: 995\$f 995\$h 995\$j',NULL,'Free'),
|
||||
('SubfieldsToUseWhenPrefill','','','Define a list of subfields to use when prefilling items (separated by space)','Free'),
|
||||
|
|
|
|||
|
|
@ -205,6 +205,12 @@ Cataloging:
|
|||
- pref: ContentWarningField
|
||||
- for storing content warnings.
|
||||
- "<br/><strong>NOTE:</strong> The field needs to appear in the MARC frameworks to be accessible."
|
||||
-
|
||||
- pref: StripWhitespaceChars
|
||||
choices:
|
||||
1: Strip
|
||||
0: "Don't strip"
|
||||
- leading and trailing whitespace characters (including spaces, tabs, line breaks and carriage returns) and inner newlines from data fields when cataloguing bibliographic and authority records. The leader and control fields will not be affected.
|
||||
Display:
|
||||
-
|
||||
- 'Separate main entry and subdivisions with '
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
use Modern::Perl;
|
||||
|
||||
use Test::More tests => 5;
|
||||
use Test::More tests => 6;
|
||||
use Test::Warn;
|
||||
|
||||
use MARC::Record;
|
||||
|
|
@ -143,3 +143,24 @@ subtest "new() tests" => sub {
|
|||
is( $metadata_record, undef, 'record object mandatory')
|
||||
};
|
||||
|
||||
subtest "stripWhitespaceChars() tests" => sub {
|
||||
plan tests => 2;
|
||||
|
||||
# Test default values with a MARC::Record record
|
||||
my $record = MARC::Record->new();
|
||||
|
||||
$record->add_fields(
|
||||
[ '001', '1234' ],
|
||||
[ '150', ' ', ' ', a => 'Test' ],
|
||||
[ '520', ' ', ' ', a => "This is\na test!\t" ],
|
||||
[ '521', ' ', ' ', a => "This is a\t test!\t" ],
|
||||
);
|
||||
|
||||
$record = Koha::MetadataRecord::stripWhitespaceChars( $record );
|
||||
|
||||
my $get520a = $record->subfield('520','a');
|
||||
is( $get520a, "This is a test!", "Whitespace characters are appropriately stripped or replaced with spaces" );
|
||||
|
||||
my $get521a = $record->subfield('521','a');
|
||||
is( $get521a, "This is a\t test!", "Trailing tabs are stripped while inner tabs are kept" );
|
||||
};
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
use Modern::Perl;
|
||||
|
||||
use Test::More tests => 1;
|
||||
use Test::More tests => 2;
|
||||
use t::lib::Mocks;
|
||||
use t::lib::TestBuilder;
|
||||
use MARC::Record;
|
||||
|
|
@ -47,4 +47,38 @@ subtest "Check MARC field length calculation" => sub {
|
|||
like( substr($savedrec->leader,12,5), qr/^\d{5}$/, 'Base address found' );
|
||||
};
|
||||
|
||||
subtest "StripWhitespaceChars tests" => sub {
|
||||
plan tests => 4;
|
||||
|
||||
t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
|
||||
t::lib::Mocks::mock_preference('StripWhitespaceChars', 0);
|
||||
|
||||
my $biblio = t::lib::TestBuilder->new->build_sample_biblio;
|
||||
my $record = MARC::Record->new;
|
||||
$record->append_fields(
|
||||
MARC::Field->new( '003', "abcdefg\n" ),
|
||||
MARC::Field->new( '245', '', '', a => " My\ntitle\n" ),
|
||||
);
|
||||
|
||||
my $title = $record->title;
|
||||
is( $title, " My\ntitle\n", 'Title has whitespace characters' );
|
||||
|
||||
C4::Biblio::ModBiblioMarc( $record, $biblio->biblionumber );
|
||||
$biblio = Koha::Biblios->find( $biblio->biblionumber );
|
||||
my $savedrec = $biblio->metadata->record;
|
||||
my $savedtitle = $savedrec->title;
|
||||
is( $savedtitle, " My\ntitle\n", "Title still has whitespace characters because StripWhitespaceChars is disabled" );
|
||||
|
||||
t::lib::Mocks::mock_preference('StripWhitespaceChars', 1);
|
||||
|
||||
C4::Biblio::ModBiblioMarc( $record, $biblio->biblionumber );
|
||||
$biblio = Koha::Biblios->find( $biblio->biblionumber );
|
||||
my $amendedrec = $biblio->metadata->record;
|
||||
my $amendedtitle = $amendedrec->title;
|
||||
is( $amendedtitle, "My title", "Whitespace characters removed from title because StripWhitespaceChars is enabled" );
|
||||
|
||||
my $f003 = $record->field('003')->data;
|
||||
is( $f003, "abcdefg\n", "Whitespace characters are not stripped from control fields" );
|
||||
};
|
||||
|
||||
$schema->storage->txn_rollback;
|
||||
|
|
|
|||
Loading…
Reference in New Issue