From 7ca7e214f6660108602e30ab2ce9dacf0d7be9cf Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 8 Dec 2017 08:18:39 +0100 Subject: [PATCH] Bug 19096: (QA follow-up) Move two routines out of Koha::MSS Content-Type: text/plain; charset=utf-8 As requested by RM, this patch moves sync_kohafield and get_kohafield_exceptions from Koha/MarcSubfieldStructures.pm. At this moment they are only used in a database revision; it is not clear if they may be of use later on. In order to keep them in a module and not remove the unit tests, this patch adds a Koha::Util module Dbrev.pm. It is now required in the atomic update, but could be added in a use statement in updatedatabase.pl. Test plan: [1] Run updatedatabase.pl [2] Run t/db_dependent/Koha/MarcSubfieldStructures.t Signed-off-by: Marcel de Rooy --- Koha/MarcSubfieldStructures.pm | 61 ------------ Koha/Util/Dbrev.pm | 119 ++++++++++++++++++++++++ installer/data/mysql/atomicupdate/bug19096.perl | 6 +- t/db_dependent/Koha/MarcSubfieldStructures.t | 19 ++-- 4 files changed, 132 insertions(+), 73 deletions(-) create mode 100644 Koha/Util/Dbrev.pm diff --git a/Koha/MarcSubfieldStructures.pm b/Koha/MarcSubfieldStructures.pm index d9c839f..500d951 100644 --- a/Koha/MarcSubfieldStructures.pm +++ b/Koha/MarcSubfieldStructures.pm @@ -34,67 +34,6 @@ Koha::MarcSubfieldStructures - Koha MarcSubfieldStructure Object set class =head2 Class methods -=head3 sync_kohafield - - Synchronizes the used kohafields in the Default framework to all other - frameworks. - -=cut - -sub sync_kohafield { - my ( $class ) = @_; - - # Clear the destination frameworks first - Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, kohafield => { '>', q{} } })->update({ kohafield => undef }); - - # Now copy from Default - my $rs = Koha::MarcSubfieldStructures->search({ frameworkcode => q{}, kohafield => { '>', q{} } }); - while( my $rec = $rs->next ) { - Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, tagfield => $rec->tagfield, tagsubfield => $rec->tagsubfield })->update({ kohafield => $rec->kohafield }); - } - - # Clear the cache - my @fw = Koha::BiblioFrameworks->search({ frameworkcode => { '>', q{} } })->get_column('frameworkcode'); - foreach( @fw ) { - Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$_" ); - } - Koha::Caches->get_instance->clear_from_cache("default_value_for_mod_marc-"); -} - -=head3 get_kohafield_exceptions - - Get all kohafield exceptions (deviations from the Default framework) - - Returns an arrayref of [ framework, field, subfield, kohafield ] - where kohafield is the original (deviating) value. - -=cut - -sub get_kohafield_exceptions { - my ( $class, $params ) = @_; - - # Too bad that DBIx is not very helpful here - my $default = Koha::MarcSubfieldStructures->search({ - frameworkcode => q{}, - kohafield => { '>' => '' }, - }); - my $other = Koha::MarcSubfieldStructures->search({ - frameworkcode => { '!=' => q{} }, - }, { order_by => { -asc => [ qw/tagfield tagsubfield/ ] } }); - - # Compare - my @return; - while( my $rec = $other->next ) { - my @tags = ( $rec->tagfield, $rec->tagsubfield ); - my $defa = $default->find( q{}, @tags ); - if( $rec->kohafield ) { - push @return, [ $rec->frameworkcode, @tags, $rec->kohafield ] if !$defa || !$defa->kohafield || $defa->kohafield ne $rec->kohafield; - } else { - push @return, [ $rec->frameworkcode, @tags, q{} ] if $defa && $defa->kohafield; - } - } - return \@return; -} =head3 type diff --git a/Koha/Util/Dbrev.pm b/Koha/Util/Dbrev.pm new file mode 100644 index 0000000..7346558 --- /dev/null +++ b/Koha/Util/Dbrev.pm @@ -0,0 +1,119 @@ +package Koha::Util::Dbrev; + +# Module contains subroutines used in database revisions (call in OO style) +# +# Copyright 2017 Koha Development Team +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +use Modern::Perl; + +use Koha::BiblioFrameworks; +use Koha::Caches; +use Koha::MarcSubfieldStructures; + +=head1 NAME + +Koha::Util::Dbrev - utility class with routines used in db revisions + +=head1 SYNOPSIS + + use Koha::Util::Dbrev; + + Koha::Util::Dbrev->sync_kohafield; + +=head1 DESCRIPTION + +The routines in this modules are called in OO style as class methods. + +=head1 METHODS + +=head2 sync_kohafield + + Synchronizes the used kohafields in the Default framework to all other + frameworks. + Used in the dbrev for bug 19096 (Koha to MARC mappings (Part 2): Make Default authoritative. Tested in t/db_dependent/Koha/MarcSubfieldStructures.t. + If needed somehow later on, we should move it to Koha::MarcSubfieldStructures + +=cut + +sub sync_kohafield { + my ( $class ) = @_; + + # Clear the destination frameworks first + Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, kohafield => { '>', q{} } })->update({ kohafield => undef }); + + # Now copy from Default + my $rs = Koha::MarcSubfieldStructures->search({ frameworkcode => q{}, kohafield => { '>', q{} } }); + while( my $rec = $rs->next ) { + Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, tagfield => $rec->tagfield, tagsubfield => $rec->tagsubfield })->update({ kohafield => $rec->kohafield }); + } + + # Clear the cache + my @fw = Koha::BiblioFrameworks->search({ frameworkcode => { '>', q{} } })->get_column('frameworkcode'); + foreach( @fw ) { + Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$_" ); + } + Koha::Caches->get_instance->clear_from_cache("default_value_for_mod_marc-"); +} + +=head2 get_kohafield_exceptions + + Get all kohafield exceptions (deviations from the Default framework) + + Returns an arrayref of [ framework, field, subfield, kohafield ] + where kohafield is the original (deviating) value. + + Used in the dbrev for bug 19096 (Koha to MARC mappings (Part 2): Make Default authoritative. Tested in t/db_dependent/Koha/MarcSubfieldStructures.t. + If needed somehow later on, we should move it to Koha::MarcSubfieldStructures + +=cut + +sub get_kohafield_exceptions { + my ( $class, $params ) = @_; + + # Too bad that DBIx is not very helpful here + my $default = Koha::MarcSubfieldStructures->search({ + frameworkcode => q{}, + kohafield => { '>' => '' }, + }); + my $other = Koha::MarcSubfieldStructures->search({ + frameworkcode => { '!=' => q{} }, + }, { order_by => { -asc => [ qw/tagfield tagsubfield/ ] } }); + + # Compare + my @return; + while( my $rec = $other->next ) { + my @tags = ( $rec->tagfield, $rec->tagsubfield ); + my $defa = $default->find( q{}, @tags ); + if( $rec->kohafield ) { + push @return, [ $rec->frameworkcode, @tags, $rec->kohafield ] if !$defa || !$defa->kohafield || $defa->kohafield ne $rec->kohafield; + } else { + push @return, [ $rec->frameworkcode, @tags, q{} ] if $defa && $defa->kohafield; + } + } + return \@return; +} + +=head1 AUTHOR + + Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands + Koha Development Team + +=cut + +1; diff --git a/installer/data/mysql/atomicupdate/bug19096.perl b/installer/data/mysql/atomicupdate/bug19096.perl index 72a2c46..a4f0331 100644 --- a/installer/data/mysql/atomicupdate/bug19096.perl +++ b/installer/data/mysql/atomicupdate/bug19096.perl @@ -1,8 +1,8 @@ $DBversion = 'XXX'; # will be replaced by the RM if( CheckVersion( $DBversion ) ) { - require Koha::MarcSubfieldStructures; + require Koha::Util::Dbrev; - my $diff = Koha::MarcSubfieldStructures->get_kohafield_exceptions; + my $diff = Koha::Util::Dbrev->get_kohafield_exceptions; if( @$diff ) { print "WARNING: The Default framework is now considered as authoritative for Koha to MARC mappings. We have found that your additional frameworks contained ".@$diff." mapping(s) that deviate from the standard mappings. Please look at the following list and consider if you need to add them again in Default (possibly as a second mapping).\n"; foreach( @$diff ) { @@ -13,7 +13,7 @@ if( CheckVersion( $DBversion ) ) { print "Mapping has been reset.\n"; } } - Koha::MarcSubfieldStructures->sync_kohafield; + Koha::Util::Dbrev->sync_kohafield; } SetVersion( $DBversion ); print "Upgrade to $DBversion done (Bug 19096 - Make Default authoritative for Koha to MARC mappings)\n"; diff --git a/t/db_dependent/Koha/MarcSubfieldStructures.t b/t/db_dependent/Koha/MarcSubfieldStructures.t index c962474..65b1e0a 100644 --- a/t/db_dependent/Koha/MarcSubfieldStructures.t +++ b/t/db_dependent/Koha/MarcSubfieldStructures.t @@ -24,6 +24,7 @@ use Test::More tests => 2; use Koha::MarcSubfieldStructure; use Koha::MarcSubfieldStructures; use Koha::Database; +use Koha::Util::Dbrev; use t::lib::TestBuilder; @@ -67,11 +68,11 @@ subtest 'get_kohafield_exceptions / sync_kohafield' => sub { # 100a mapped in Default, unmapped in other Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '100', tagsubfield => 'a', kohafield => 'biblio.author' })->store; Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '100', tagsubfield => 'a', kohafield => '' })->store; - my $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions; + my $res = Koha::Util::Dbrev->get_kohafield_exceptions; is( @$res, 1, 'Found one exception' ); is( $res->[0]->[1] . $res->[0]->[2], '100a', '100a was reported' ); - Koha::MarcSubfieldStructures->sync_kohafield; - $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions; + Koha::Util::Dbrev->sync_kohafield; + $res = Koha::Util::Dbrev->get_kohafield_exceptions; is( @$res, 0, 'Found no exceptions after syncing' ); is( Koha::MarcSubfieldStructures->find($fwc, '100', 'a')->kohafield, 'biblio.author', '100a in added framework adjusted' ); @@ -81,12 +82,12 @@ subtest 'get_kohafield_exceptions / sync_kohafield' => sub { Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '245', tagsubfield => 'a', kohafield => q{} })->store; Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '245', tagsubfield => 'a', kohafield => 'biblio.title' })->store; Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '300', tagsubfield => 'a', kohafield => 'biblioitems.pages' })->store; - $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions; + $res = Koha::Util::Dbrev->get_kohafield_exceptions; is( @$res, 2, 'Found two exceptions' ); is( $res->[0]->[1] . $res->[0]->[2], '245a', '245a was reported' ); is( $res->[1]->[1] . $res->[1]->[2], '300a', '300a was reported' ); - Koha::MarcSubfieldStructures->sync_kohafield; - $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions; + Koha::Util::Dbrev->sync_kohafield; + $res = Koha::Util::Dbrev->get_kohafield_exceptions; is( @$res, 0, 'Found no exceptions after syncing again' ); is( Koha::MarcSubfieldStructures->find($fwc, '245', 'a')->kohafield, undef, '245a in added framework cleared' ); @@ -96,11 +97,11 @@ subtest 'get_kohafield_exceptions / sync_kohafield' => sub { # 300a mapped in Default to another field than in other framework Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '300', tagsubfield => 'a', kohafield => 'something_else' })->store; Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->update({ kohafield => 'biblioitems.pages' }); - $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions; + $res = Koha::Util::Dbrev->get_kohafield_exceptions; is( @$res, 1, 'Found one exception' ); is( $res->[0]->[1] . $res->[0]->[2], '300a', '300a was reported again' ); - Koha::MarcSubfieldStructures->sync_kohafield; - $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions; + Koha::Util::Dbrev->sync_kohafield; + $res = Koha::Util::Dbrev->get_kohafield_exceptions; is( @$res, 0, 'Found no exceptions after third syncing' ); is( Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->kohafield, 'something_else', '300a in added framework adjusted' ); -- 2.1.4