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

(-)a/Koha/Util/Dbrev.pm (-145 lines)
Lines 1-145 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 C4::Context;
26
use Koha::Caches;
27
28
=head1 NAME
29
30
Koha::Util::Dbrev - utility class with routines used in db revisions
31
32
=head1 SYNOPSIS
33
34
    use Koha::Util::Dbrev;
35
36
    Koha::Util::Dbrev->sync_kohafield;
37
38
=head1 DESCRIPTION
39
40
The routines in this modules are called in OO style as class methods.
41
42
=head1 METHODS
43
44
=head2 sync_kohafield
45
46
    Synchronizes the used kohafields in the Default framework to all other
47
    frameworks.
48
    Used in the dbrev for bug 19096 (Koha to MARC mappings (Part 2): Make Default authoritative. Tested in t/db_dependent/Koha/MarcSubfieldStructures.t.
49
    If needed somehow later on, we should move it to Koha::MarcSubfieldStructures
50
51
=cut
52
53
sub sync_kohafield {
54
    my ( $class ) = @_;
55
56
    my $dbh = C4::Context->dbh;
57
58
    # Clear the destination frameworks first
59
    $dbh->do(q|
60
        UPDATE marc_subfield_structure
61
        SET kohafield = NULL
62
        WHERE   frameworkcode > ''
63
            AND     Kohafield > ''
64
    |);
65
66
    # Now copy from Default
67
    my $msss = $dbh->selectall_arrayref(q|
68
        SELECT kohafield, tagfield, tagsubfield
69
        FROM marc_subfield_structure
70
        WHERE   frameworkcode = ''
71
            AND     kohafield > ''
72
    |, { Slice => {} });
73
    my $sth = $dbh->prepare(q|
74
        UPDATE marc_subfield_structure
75
        SET kohafield = ?
76
        WHERE frameworkcode > ''
77
        AND tagfield = ?
78
        AND tagsubfield = ?
79
    |);
80
    for my $mss ( @$msss ) {
81
        $sth->execute( $mss->{kohafield}, $mss->{tagfield}, $mss->{tagsubfield} );
82
    }
83
84
    my @frameworkcodes = $dbh->selectall_arrayref(q|
85
    # Clear the cache
86
        SELECT frameworkcode FROM biblio_framework WHERE frameworkcode > ''
87
    |);
88
    for my $frameworkcode ( @frameworkcodes ) {
89
        Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$frameworkcode" );
90
    }
91
    Koha::Caches->get_instance->clear_from_cache("default_value_for_mod_marc-");
92
}
93
94
=head2 get_kohafield_exceptions
95
96
    Get all kohafield exceptions (deviations from the Default framework)
97
98
    Returns an arrayref of [ framework, field, subfield, kohafield ]
99
    where kohafield is the original (deviating) value.
100
101
    Used in the dbrev for bug 19096 (Koha to MARC mappings (Part 2): Make Default authoritative. Tested in t/db_dependent/Koha/MarcSubfieldStructures.t.
102
    If needed somehow later on, we should move it to Koha::MarcSubfieldStructures
103
104
=cut
105
106
sub get_kohafield_exceptions {
107
    my ( $class ) = @_;
108
109
    my $dbh = C4::Context->dbh;
110
    my $msss = $dbh->selectall_arrayref(q|
111
        SELECT kohafield, tagfield, tagsubfield
112
        FROM marc_subfield_structure
113
        WHERE   frameworkcode != ''
114
    |, { Slice => {} });
115
116
117
    my $sth = $dbh->prepare(q|
118
        SELECT kohafield
119
        FROM marc_subfield_structure
120
        WHERE frameworkcode = ''
121
        AND tagfield = ?
122
        AND tagsubfield = ?
123
    |);
124
125
    my @exceptions;
126
    for my $mss ( @$msss ) {
127
        $sth->execute($mss->{tagfield}, $mss->{tagsubfield} );
128
        my ( $default_kohafield ) = $sth->fetchrow_array();
129
        if( $mss->{kohafield} ) {
130
            push @exceptions, [ $mss->{frameworkcode}, $mss->{tagfield}, $mss->{tagsubfield}, $mss->{kohafield} ] if not $default_kohafield or $default_kohafield ne $mss->{kohafield};
131
        } else {
132
            push @exceptions, [ $mss->{frameworkcode}, $mss->{tagfield}, $mss->{tagsubfield}, q{} ] if $default_kohafield;
133
        }
134
    }
135
    return \@exceptions;
136
}
137
138
=head1 AUTHOR
139
140
    Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
141
    Koha Development Team
142
143
=cut
144
145
1;
(-)a/installer/data/mysql/atomicupdate/bug19096.perl (-11 / +85 lines)
Lines 1-20 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::Util::Dbrev;
3
4
4
    my $dbh = C4::Context->dbh;
5
    my $diff = Koha::Util::Dbrev->get_kohafield_exceptions;
5
6
    if( @$diff ) {
6
    my $msss = $dbh->selectall_arrayref(q|
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
        SELECT kohafield, tagfield, tagsubfield, frameworkcode
8
        foreach( @$diff ) {
8
        FROM marc_subfield_structure
9
            print "Field ". $_->[1].$_->[2]. " in framework ". $_->[0]. ": ";
9
        WHERE   frameworkcode != ''
10
            if( $_->[3] ) {
10
    |, { Slice => {} });
11
                print "Mapping to ". $_->[3]. " has been adjusted.\n";
11
12
            } else {
12
13
    my $sth = $dbh->prepare(q|
14
        SELECT kohafield
15
        FROM marc_subfield_structure
16
        WHERE frameworkcode = ''
17
        AND tagfield = ?
18
        AND tagsubfield = ?
19
    |);
20
21
    my @exceptions;
22
    for my $mss ( @$msss ) {
23
        $sth->execute($mss->{tagfield}, $mss->{tagsubfield} );
24
        my ( $default_kohafield ) = $sth->fetchrow_array();
25
        if( $mss->{kohafield} ) {
26
            push @exceptions, { frameworkcode => $mss->{frameworkcode}, tagfield => $mss->{tagfield}, tagsubfield => $mss->{tagsubfield}, kohafield => $mss->{kohafield} } if not $default_kohafield or $default_kohafield ne $mss->{kohafield};
27
        } else {
28
            push @exceptions, { frameworkcode => $mss->{frameworkcode}, tagfield => $mss->{tagfield}, tagsubfield => $mss->{tagsubfield}, kohafield => q{} } if $default_kohafield;
29
        }
30
    }
31
32
    if (@exceptions) {
33
        print
34
"WARNING: The Default framework is now considered as authoritative for Koha to MARC mappings. We have found that your additional frameworks contained "
35
          . scalar(@exceptions)
36
          . " 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";
37
        for my $exception (@exceptions) {
38
            print "Field "
39
              . $exception->{tagfield} . '.'
40
              . $exception->{tagsubfield}
41
              . " in framework "
42
              . $exception->{frameworkcode} . ': ';
43
            if ( $exception->{kohafield} ) {
44
                print "Mapping to "
45
                  . $exception->{kohafield}
46
                  . " has been adjusted.\n";
47
            }
48
            else {
13
                print "Mapping has been reset.\n";
49
                print "Mapping has been reset.\n";
14
            }
50
            }
15
        }
51
        }
16
        Koha::Util::Dbrev->sync_kohafield;
52
53
        # Sync kohafield
54
55
        # Clear the destination frameworks first
56
        $dbh->do(q|
57
            UPDATE marc_subfield_structure
58
            SET kohafield = NULL
59
            WHERE   frameworkcode > ''
60
                AND     Kohafield > ''
61
        |);
62
63
        # Now copy from Default
64
        my $msss = $dbh->selectall_arrayref(q|
65
            SELECT kohafield, tagfield, tagsubfield
66
            FROM marc_subfield_structure
67
            WHERE   frameworkcode = ''
68
                AND     kohafield > ''
69
        |, { Slice => {} });
70
        my $sth = $dbh->prepare(q|
71
            UPDATE marc_subfield_structure
72
            SET kohafield = ?
73
            WHERE frameworkcode > ''
74
            AND tagfield = ?
75
            AND tagsubfield = ?
76
        |);
77
        for my $mss (@$msss) {
78
            $sth->execute( $mss->{kohafield}, $mss->{tagfield},
79
                $mss->{tagsubfield} );
80
        }
81
82
        # Clear the cache
83
        my @frameworkcodes = $dbh->selectall_arrayref(q|
84
            SELECT frameworkcode FROM biblio_framework WHERE frameworkcode > ''
85
        |);
86
        for my $frameworkcode (@frameworkcodes) {
87
            Koha::Caches->get_instance->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
88
        }
89
        Koha::Caches->get_instance->clear_from_cache("default_value_for_mod_marc-");
17
    }
90
    }
91
18
    SetVersion( $DBversion );
92
    SetVersion( $DBversion );
19
    print "Upgrade to $DBversion done (Bug 19096 - Make Default authoritative for Koha to MARC mappings)\n";
93
    print "Upgrade to $DBversion done (Bug 19096 - Make Default authoritative for Koha to MARC mappings)\n";
20
}
94
}
(-)a/t/db_dependent/Koha/MarcSubfieldStructures.t (-54 / +1 lines)
Lines 19-30 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 1;
23
23
24
use Koha::MarcSubfieldStructure;
25
use Koha::MarcSubfieldStructures;
24
use Koha::MarcSubfieldStructures;
26
use Koha::Database;
25
use Koha::Database;
27
use Koha::Util::Dbrev;
28
26
29
use t::lib::TestBuilder;
27
use t::lib::TestBuilder;
30
28
Lines 57-110 subtest 'Trivial tests' => sub { Link Here
57
    is( Koha::MarcSubfieldStructures->search->count, $nb_of_fields + 1, 'Delete should have deleted the field' );
55
    is( Koha::MarcSubfieldStructures->search->count, $nb_of_fields + 1, 'Delete should have deleted the field' );
58
};
56
};
59
57
60
subtest 'get_kohafield_exceptions / sync_kohafield' => sub {
61
    plan tests => 14;
62
63
    # start from scratch again, add a new framework
64
    Koha::MarcSubfieldStructures->delete;
65
    my $builder = t::lib::TestBuilder->new;
66
    my $fwc = $builder->build({ source => 'BiblioFramework' })->{frameworkcode};
67
68
    # 100a mapped in Default, unmapped in other
69
    Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '100', tagsubfield => 'a', kohafield => 'biblio.author' })->store;
70
    Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '100', tagsubfield => 'a', kohafield => '' })->store;
71
    my $res = Koha::Util::Dbrev->get_kohafield_exceptions;
72
    is( @$res, 1, 'Found one exception' );
73
    is( $res->[0]->[1] . $res->[0]->[2], '100a', '100a was reported' );
74
    Koha::Util::Dbrev->sync_kohafield;
75
    $res = Koha::Util::Dbrev->get_kohafield_exceptions;
76
    is( @$res, 0, 'Found no exceptions after syncing' );
77
    is( Koha::MarcSubfieldStructures->find($fwc, '100', 'a')->kohafield,
78
        'biblio.author', '100a in added framework adjusted' );
79
80
    # 245a unmapped in Default, mapped in other
81
    # 300a not in Default, mapped in other
82
    Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '245', tagsubfield => 'a', kohafield => q{} })->store;
83
    Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '245', tagsubfield => 'a', kohafield => 'biblio.title' })->store;
84
    Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '300', tagsubfield => 'a', kohafield => 'biblioitems.pages' })->store;
85
    $res = Koha::Util::Dbrev->get_kohafield_exceptions;
86
    is( @$res, 2, 'Found two exceptions' );
87
    is( $res->[0]->[1] . $res->[0]->[2], '245a', '245a was reported' );
88
    is( $res->[1]->[1] . $res->[1]->[2], '300a', '300a was reported' );
89
    Koha::Util::Dbrev->sync_kohafield;
90
    $res = Koha::Util::Dbrev->get_kohafield_exceptions;
91
    is( @$res, 0, 'Found no exceptions after syncing again' );
92
    is( Koha::MarcSubfieldStructures->find($fwc, '245', 'a')->kohafield,
93
        undef, '245a in added framework cleared' );
94
    is( Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->kohafield,
95
        undef, '300a in added framework cleared' );
96
97
    # 300a mapped in Default to another field than in other framework
98
    Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '300', tagsubfield => 'a', kohafield => 'something_else' })->store;
99
    Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->update({ kohafield => 'biblioitems.pages' });
100
    $res = Koha::Util::Dbrev->get_kohafield_exceptions;
101
    is( @$res, 1, 'Found one exception' );
102
    is( $res->[0]->[1] . $res->[0]->[2], '300a', '300a was reported again' );
103
    Koha::Util::Dbrev->sync_kohafield;
104
    $res = Koha::Util::Dbrev->get_kohafield_exceptions;
105
    is( @$res, 0, 'Found no exceptions after third syncing' );
106
    is( Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->kohafield,
107
        'something_else', '300a in added framework adjusted' );
108
};
109
110
$schema->storage->txn_rollback;
58
$schema->storage->txn_rollback;
111
- 

Return to bug 19096