From b2215dfb3db4710e355fb0e325323a0ba2d6fd83 Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Fri, 25 Mar 2022 03:12:31 +0000 Subject: [PATCH] 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 --- C4/AuthoritiesMarc.pm | 13 +++++++- C4/Biblio.pm | 11 +++++++ ...0358_-_add_StripWhitespaceChars_syspref.pl | 12 ++++++++ installer/data/mysql/mandatory/sysprefs.sql | 1 + .../admin/preferences/cataloguing.pref | 6 ++++ t/db_dependent/Biblio/ModBiblioMarc.t | 30 ++++++++++++++++++- 6 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_30358_-_add_StripWhitespaceChars_syspref.pl diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm index 863ca3d6104..079f246099d 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -638,9 +638,20 @@ 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') ) { + foreach my $field ( $record->fields ) { + foreach my $subfield ( $field->subfields ) { + my $key = $subfield->[0]; + my $value = $subfield->[1]; + $value =~ s/^\s+|\s+$|^\n+|\n+$|^\r+|\r+$|^\t+|\t+$//g; + $field->update( $key => $value ); + } + } + } + # Save record into auth_header, update 001 my $action; my $authority; diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 9790febdaa2..ab20d075662 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -2751,6 +2751,17 @@ sub ModBiblioMarc { $f005->update(sprintf("%4d%02d%02d%02d%02d%04.1f",@a)) if $f005; } + if ( C4::Context->preference('StripWhitespaceChars') ) { + foreach my $field ( $record->fields ) { + foreach my $subfield ( $field->subfields ) { + my $key = $subfield->[0]; + my $value = $subfield->[1]; + $value =~ s/^\s+|\s+$|^\n+|\n+$|^\r+|\r+$|^\t+|\t+$//g; + $field->update( $key => $value ); + } + } + } + my $metadata = { biblionumber => $biblionumber, format => 'marcxml', diff --git a/installer/data/mysql/atomicupdate/bug_30358_-_add_StripWhitespaceChars_syspref.pl b/installer/data/mysql/atomicupdate/bug_30358_-_add_StripWhitespaceChars_syspref.pl new file mode 100644 index 00000000000..80957d8145f --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_30358_-_add_StripWhitespaceChars_syspref.pl @@ -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 from input fields when cataloguing bibliographic and authority records.', 'YesNo') }); + }, +}; diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index 8ff883c7ed0..534cc21bf61 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -691,6 +691,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 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'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref index b28bf50099e..36165b707ac 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref @@ -186,6 +186,12 @@ Cataloging: duplicate: "when editing records as new (duplicating)" changed: "when changing the framework while editing the existing record" imported: "when importing a record via z39.50" + - + - pref: StripWhitespaceChars + choices: + 1: Strip + 0: "Don't strip" + - leading and trailing whitespace characters from input fields when cataloguing bibliographic and authority records. Whitespace characters include spaces, tabs, newlines and carriage returns. Display: - - 'Separate main entry and subdivisions with ' diff --git a/t/db_dependent/Biblio/ModBiblioMarc.t b/t/db_dependent/Biblio/ModBiblioMarc.t index ac56746d2d6..5312b85e82c 100755 --- a/t/db_dependent/Biblio/ModBiblioMarc.t +++ b/t/db_dependent/Biblio/ModBiblioMarc.t @@ -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,32 @@ 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 => 3; + + 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( '245', '', '', a => " My title\n" ), + ); + + my $title = $record->title; + is( $title, " My title\n", 'Title has whitespace characters' ); + + C4::Biblio::ModBiblioMarc( $record, $biblio->biblionumber ); + my $savedrec = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber }); + my $savedtitle = $savedrec->title; + is( $savedtitle, " My title\n", "Title still has whitespace characters because StripWhitespaceChars is disabled" ); + + t::lib::Mocks->mock_preference('StripWhitespaceChars', 1); + + C4::Biblio::ModBiblioMarc( $record, $biblio->biblionumber ); + my $amendedrec = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber }); + my $amendedtitle = $amendedrec->title; + is( $amendedtitle, "My title", "Whitespace characters removed from title because StripWhitespaceChars is enabled" ); +}; + $schema->storage->txn_rollback; -- 2.20.1