View | Details | Raw Unified | Return to bug 19096
Collapse All | Expand All

(-)a/Koha/MarcSubfieldStructures.pm (-61 lines)
Lines 34-100 Koha::MarcSubfieldStructures - Koha MarcSubfieldStructure Object set class Link Here
34
34
35
=head2 Class methods
35
=head2 Class methods
36
36
37
=head3 sync_kohafield
38
39
    Synchronizes the used kohafields in the Default framework to all other
40
    frameworks.
41
42
=cut
43
44
sub sync_kohafield {
45
    my ( $class ) = @_;
46
47
    # Clear the destination frameworks first
48
    Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, kohafield => { '>', q{} } })->update({ kohafield => undef });
49
50
    # Now copy from Default
51
    my $rs = Koha::MarcSubfieldStructures->search({ frameworkcode => q{}, kohafield => { '>', q{} } });
52
    while( my $rec = $rs->next ) {
53
        Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, tagfield => $rec->tagfield, tagsubfield => $rec->tagsubfield })->update({ kohafield => $rec->kohafield });
54
    }
55
56
    # Clear the cache
57
    my @fw = Koha::BiblioFrameworks->search({ frameworkcode => { '>', q{} } })->get_column('frameworkcode');
58
    foreach( @fw ) {
59
        Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$_" );
60
    }
61
    Koha::Caches->get_instance->clear_from_cache("default_value_for_mod_marc-");
62
}
63
64
=head3 get_kohafield_exceptions
65
66
    Get all kohafield exceptions (deviations from the Default framework)
67
68
    Returns an arrayref of [ framework, field, subfield, kohafield ]
69
    where kohafield is the original (deviating) value.
70
71
=cut
72
73
sub get_kohafield_exceptions {
74
    my ( $class, $params ) = @_;
75
76
    # Too bad that DBIx is not very helpful here
77
    my $default = Koha::MarcSubfieldStructures->search({
78
        frameworkcode => q{},
79
        kohafield => { '>' => '' },
80
    });
81
    my $other = Koha::MarcSubfieldStructures->search({
82
        frameworkcode => { '!=' => q{} },
83
    }, { order_by => { -asc => [ qw/tagfield tagsubfield/ ] } });
84
85
    # Compare
86
    my @return;
87
    while( my $rec = $other->next ) {
88
        my @tags = ( $rec->tagfield, $rec->tagsubfield );
89
        my $defa = $default->find( q{}, @tags );
90
        if( $rec->kohafield ) {
91
            push @return, [ $rec->frameworkcode, @tags, $rec->kohafield ] if !$defa || !$defa->kohafield || $defa->kohafield ne $rec->kohafield;
92
        } else {
93
            push @return, [ $rec->frameworkcode, @tags, q{} ] if $defa && $defa->kohafield;
94
        }
95
    }
96
    return \@return;
97
}
98
37
99
=head3 type
38
=head3 type
100
39
(-)a/Koha/Util/Dbrev.pm (+119 lines)
Line 0 Link Here
1
package Koha::Util::Dbrev;
2
3
# Module contains subroutines used in database revisions (call in OO style)
4
#
5
# Copyright 2017 Koha Development Team
6
#
7
# This file is part of Koha.
8
#
9
# Koha is free software; you can redistribute it and/or modify it under the
10
# terms of the GNU General Public License as published by the Free Software
11
# Foundation; either version 3 of the License, or (at your option) any later
12
# version.
13
#
14
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License along
19
# with Koha; if not, write to the Free Software Foundation, Inc.,
20
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
#
22
23
use Modern::Perl;
24
25
use Koha::BiblioFrameworks;
26
use Koha::Caches;
27
use Koha::MarcSubfieldStructures;
28
29
=head1 NAME
30
31
Koha::Util::Dbrev - utility class with routines used in db revisions
32
33
=head1 SYNOPSIS
34
35
    use Koha::Util::Dbrev;
36
37
    Koha::Util::Dbrev->sync_kohafield;
38
39
=head1 DESCRIPTION
40
41
The routines in this modules are called in OO style as class methods.
42
43
=head1 METHODS
44
45
=head2 sync_kohafield
46
47
    Synchronizes the used kohafields in the Default framework to all other
48
    frameworks.
49
    Used in the dbrev for bug 19096 (Koha to MARC mappings (Part 2): Make Default authoritative. Tested in t/db_dependent/Koha/MarcSubfieldStructures.t.
50
    If needed somehow later on, we should move it to Koha::MarcSubfieldStructures
51
52
=cut
53
54
sub sync_kohafield {
55
    my ( $class ) = @_;
56
57
    # Clear the destination frameworks first
58
    Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, kohafield => { '>', q{} } })->update({ kohafield => undef });
59
60
    # Now copy from Default
61
    my $rs = Koha::MarcSubfieldStructures->search({ frameworkcode => q{}, kohafield => { '>', q{} } });
62
    while( my $rec = $rs->next ) {
63
        Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, tagfield => $rec->tagfield, tagsubfield => $rec->tagsubfield })->update({ kohafield => $rec->kohafield });
64
    }
65
66
    # Clear the cache
67
    my @fw = Koha::BiblioFrameworks->search({ frameworkcode => { '>', q{} } })->get_column('frameworkcode');
68
    foreach( @fw ) {
69
        Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$_" );
70
    }
71
    Koha::Caches->get_instance->clear_from_cache("default_value_for_mod_marc-");
72
}
73
74
=head2 get_kohafield_exceptions
75
76
    Get all kohafield exceptions (deviations from the Default framework)
77
78
    Returns an arrayref of [ framework, field, subfield, kohafield ]
79
    where kohafield is the original (deviating) value.
80
81
    Used in the dbrev for bug 19096 (Koha to MARC mappings (Part 2): Make Default authoritative. Tested in t/db_dependent/Koha/MarcSubfieldStructures.t.
82
    If needed somehow later on, we should move it to Koha::MarcSubfieldStructures
83
84
=cut
85
86
sub get_kohafield_exceptions {
87
    my ( $class, $params ) = @_;
88
89
    # Too bad that DBIx is not very helpful here
90
    my $default = Koha::MarcSubfieldStructures->search({
91
        frameworkcode => q{},
92
        kohafield => { '>' => '' },
93
    });
94
    my $other = Koha::MarcSubfieldStructures->search({
95
        frameworkcode => { '!=' => q{} },
96
    }, { order_by => { -asc => [ qw/tagfield tagsubfield/ ] } });
97
98
    # Compare
99
    my @return;
100
    while( my $rec = $other->next ) {
101
        my @tags = ( $rec->tagfield, $rec->tagsubfield );
102
        my $defa = $default->find( q{}, @tags );
103
        if( $rec->kohafield ) {
104
            push @return, [ $rec->frameworkcode, @tags, $rec->kohafield ] if !$defa || !$defa->kohafield || $defa->kohafield ne $rec->kohafield;
105
        } else {
106
            push @return, [ $rec->frameworkcode, @tags, q{} ] if $defa && $defa->kohafield;
107
        }
108
    }
109
    return \@return;
110
}
111
112
=head1 AUTHOR
113
114
    Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
115
    Koha Development Team
116
117
=cut
118
119
1;
(-)a/installer/data/mysql/atomicupdate/bug19096.perl (-3 / +3 lines)
Lines 1-8 Link Here
1
$DBversion = 'XXX';  # will be replaced by the RM
1
$DBversion = 'XXX';  # will be replaced by the RM
2
if( CheckVersion( $DBversion ) ) {
2
if( CheckVersion( $DBversion ) ) {
3
    require Koha::MarcSubfieldStructures;
3
    require Koha::Util::Dbrev;
4
4
5
    my $diff = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
5
    my $diff = Koha::Util::Dbrev->get_kohafield_exceptions;
6
    if( @$diff ) {
6
    if( @$diff ) {
7
        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";
7
        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";
8
        foreach( @$diff ) {
8
        foreach( @$diff ) {
Lines 13-19 if( CheckVersion( $DBversion ) ) { Link Here
13
                print "Mapping has been reset.\n";
13
                print "Mapping has been reset.\n";
14
            }
14
            }
15
        }
15
        }
16
        Koha::MarcSubfieldStructures->sync_kohafield;
16
        Koha::Util::Dbrev->sync_kohafield;
17
    }
17
    }
18
    SetVersion( $DBversion );
18
    SetVersion( $DBversion );
19
    print "Upgrade to $DBversion done (Bug 19096 - Make Default authoritative for Koha to MARC mappings)\n";
19
    print "Upgrade to $DBversion done (Bug 19096 - Make Default authoritative for Koha to MARC mappings)\n";
(-)a/t/db_dependent/Koha/MarcSubfieldStructures.t (-10 / +10 lines)
Lines 24-29 use Test::More tests => 2; Link Here
24
use Koha::MarcSubfieldStructure;
24
use Koha::MarcSubfieldStructure;
25
use Koha::MarcSubfieldStructures;
25
use Koha::MarcSubfieldStructures;
26
use Koha::Database;
26
use Koha::Database;
27
use Koha::Util::Dbrev;
27
28
28
use t::lib::TestBuilder;
29
use t::lib::TestBuilder;
29
30
Lines 67-77 subtest 'get_kohafield_exceptions / sync_kohafield' => sub { Link Here
67
    # 100a mapped in Default, unmapped in other
68
    # 100a mapped in Default, unmapped in other
68
    Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '100', tagsubfield => 'a', kohafield => 'biblio.author' })->store;
69
    Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '100', tagsubfield => 'a', kohafield => 'biblio.author' })->store;
69
    Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '100', tagsubfield => 'a', kohafield => '' })->store;
70
    Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '100', tagsubfield => 'a', kohafield => '' })->store;
70
    my $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
71
    my $res = Koha::Util::Dbrev->get_kohafield_exceptions;
71
    is( @$res, 1, 'Found one exception' );
72
    is( @$res, 1, 'Found one exception' );
72
    is( $res->[0]->[1] . $res->[0]->[2], '100a', '100a was reported' );
73
    is( $res->[0]->[1] . $res->[0]->[2], '100a', '100a was reported' );
73
    Koha::MarcSubfieldStructures->sync_kohafield;
74
    Koha::Util::Dbrev->sync_kohafield;
74
    $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
75
    $res = Koha::Util::Dbrev->get_kohafield_exceptions;
75
    is( @$res, 0, 'Found no exceptions after syncing' );
76
    is( @$res, 0, 'Found no exceptions after syncing' );
76
    is( Koha::MarcSubfieldStructures->find($fwc, '100', 'a')->kohafield,
77
    is( Koha::MarcSubfieldStructures->find($fwc, '100', 'a')->kohafield,
77
        'biblio.author', '100a in added framework adjusted' );
78
        'biblio.author', '100a in added framework adjusted' );
Lines 81-92 subtest 'get_kohafield_exceptions / sync_kohafield' => sub { Link Here
81
    Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '245', tagsubfield => 'a', kohafield => q{} })->store;
82
    Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '245', tagsubfield => 'a', kohafield => q{} })->store;
82
    Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '245', tagsubfield => 'a', kohafield => 'biblio.title' })->store;
83
    Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '245', tagsubfield => 'a', kohafield => 'biblio.title' })->store;
83
    Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '300', tagsubfield => 'a', kohafield => 'biblioitems.pages' })->store;
84
    Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '300', tagsubfield => 'a', kohafield => 'biblioitems.pages' })->store;
84
    $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
85
    $res = Koha::Util::Dbrev->get_kohafield_exceptions;
85
    is( @$res, 2, 'Found two exceptions' );
86
    is( @$res, 2, 'Found two exceptions' );
86
    is( $res->[0]->[1] . $res->[0]->[2], '245a', '245a was reported' );
87
    is( $res->[0]->[1] . $res->[0]->[2], '245a', '245a was reported' );
87
    is( $res->[1]->[1] . $res->[1]->[2], '300a', '300a was reported' );
88
    is( $res->[1]->[1] . $res->[1]->[2], '300a', '300a was reported' );
88
    Koha::MarcSubfieldStructures->sync_kohafield;
89
    Koha::Util::Dbrev->sync_kohafield;
89
    $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
90
    $res = Koha::Util::Dbrev->get_kohafield_exceptions;
90
    is( @$res, 0, 'Found no exceptions after syncing again' );
91
    is( @$res, 0, 'Found no exceptions after syncing again' );
91
    is( Koha::MarcSubfieldStructures->find($fwc, '245', 'a')->kohafield,
92
    is( Koha::MarcSubfieldStructures->find($fwc, '245', 'a')->kohafield,
92
        undef, '245a in added framework cleared' );
93
        undef, '245a in added framework cleared' );
Lines 96-106 subtest 'get_kohafield_exceptions / sync_kohafield' => sub { Link Here
96
    # 300a mapped in Default to another field than in other framework
97
    # 300a mapped in Default to another field than in other framework
97
    Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '300', tagsubfield => 'a', kohafield => 'something_else' })->store;
98
    Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '300', tagsubfield => 'a', kohafield => 'something_else' })->store;
98
    Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->update({ kohafield => 'biblioitems.pages' });
99
    Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->update({ kohafield => 'biblioitems.pages' });
99
    $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
100
    $res = Koha::Util::Dbrev->get_kohafield_exceptions;
100
    is( @$res, 1, 'Found one exception' );
101
    is( @$res, 1, 'Found one exception' );
101
    is( $res->[0]->[1] . $res->[0]->[2], '300a', '300a was reported again' );
102
    is( $res->[0]->[1] . $res->[0]->[2], '300a', '300a was reported again' );
102
    Koha::MarcSubfieldStructures->sync_kohafield;
103
    Koha::Util::Dbrev->sync_kohafield;
103
    $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
104
    $res = Koha::Util::Dbrev->get_kohafield_exceptions;
104
    is( @$res, 0, 'Found no exceptions after third syncing' );
105
    is( @$res, 0, 'Found no exceptions after third syncing' );
105
    is( Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->kohafield,
106
    is( Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->kohafield,
106
        'something_else', '300a in added framework adjusted' );
107
        'something_else', '300a in added framework adjusted' );
107
- 

Return to bug 19096