From 28229feffd575015e9f3f1b26c1013bdf9d07143 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 | 11 +++++++ 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, 70 insertions(+), 1 deletion(-) create mode 100644 installer/data/mysql/atomicupdate/bug_30358_-_add_StripWhitespaceChars_syspref.pl diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm index d6f2e381b3..8f0d35b4fb 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -641,6 +641,17 @@ sub AddAuthority { $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; if (!$authid ) { diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 41346511e7..ee81b98a57 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -2909,6 +2909,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 108007b3df..460efb10f2 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -659,6 +659,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 6f66761c37..bad5c409c3 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 @@ -165,6 +165,12 @@ Cataloging: - and record's last modifier name in MARC subfield - pref: MarcFieldForModifierName - ".
NOTE: Use a dollar sign between field and subfield like 123$a." + - + - 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 8f7e28b49c..2e3e926904 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; @@ -46,4 +46,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