From 66a6f563d95ebeb7830915680534c21027ab3114 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 863ca3d610..079f246099 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 006c359e17..50fb61a344 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -2752,6 +2752,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 0000000000..80957d8145 --- /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 c1ff4a2f38..bc61f7bd4d 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -683,6 +683,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 c3ef5c7c68..3b3db3e4d2 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 @@ -178,6 +178,12 @@ Cataloging: 1: Display 0: "Don't display" - OCLC defined values for MARC encoding level in leader value builder for position 17. + - + - 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 ac56746d2d..5312b85e82 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.30.2