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

(-)a/Koha/MarcSubfieldStructures.pm (-2 / +63 lines)
Lines 20-26 use Modern::Perl; Link Here
20
use Carp;
20
use Carp;
21
21
22
use Koha::Database;
22
use Koha::Database;
23
23
use Koha::BiblioFrameworks;
24
use Koha::Caches;
24
use Koha::MarcSubfieldStructure;
25
use Koha::MarcSubfieldStructure;
25
26
26
use base qw(Koha::Objects);
27
use base qw(Koha::Objects);
Lines 31-40 Koha::MarcSubfieldStructures - Koha MarcSubfieldStructure Object set class Link Here
31
32
32
=head1 API
33
=head1 API
33
34
34
=head2 Class Methods
35
=head2 Class methods
36
37
=head3 sync_kohafield
38
39
    Synchronizes the used kohafields in the Default framework to all other
40
    frameworks.
35
41
36
=cut
42
=cut
37
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
38
=head3 type
99
=head3 type
39
100
40
=cut
101
=cut
(-)a/installer/data/mysql/atomicupdate/bug19096.perl (+20 lines)
Line 0 Link Here
1
$DBversion = 'XXX';  # will be replaced by the RM
2
if( CheckVersion( $DBversion ) ) {
3
    require Koha::MarcSubfieldStructures;
4
5
    my $diff = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
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";
8
        foreach( @$diff ) {
9
            print "Field ". $_->[1].$_->[2]. " in framework ". $_->[0]. ": ";
10
            if( $_->[3] ) {
11
                print "Mapping to ". $_->[3]. " has been adjusted.\n";
12
            } else {
13
                print "Mapping has been reset.\n";
14
            }
15
        }
16
        Koha::MarcSubfieldStructures->sync_kohafield;
17
    }
18
    SetVersion( $DBversion );
19
    print "Upgrade to $DBversion done (Bug 19096 - Make Default authoritative for Koha to MARC mappings)\n";
20
}
(-)a/t/db_dependent/Koha/MarcSubfieldStructures.t (-2 / +51 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 1;
22
use Test::More tests => 2;
23
23
24
use Koha::MarcSubfieldStructure;
24
use Koha::MarcSubfieldStructure;
25
use Koha::MarcSubfieldStructures;
25
use Koha::MarcSubfieldStructures;
Lines 56-59 subtest 'Trivial tests' => sub { Link Here
56
    is( Koha::MarcSubfieldStructures->search->count, $nb_of_fields + 1, 'Delete should have deleted the field' );
56
    is( Koha::MarcSubfieldStructures->search->count, $nb_of_fields + 1, 'Delete should have deleted the field' );
57
};
57
};
58
58
59
subtest 'get_kohafield_exceptions / sync_kohafield' => sub {
60
    plan tests => 14;
61
62
    # start from scratch again, add a new framework
63
    Koha::MarcSubfieldStructures->delete;
64
    my $builder = t::lib::TestBuilder->new;
65
    my $fwc = $builder->build({ source => 'BiblioFramework' })->{frameworkcode};
66
67
    # 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 => $fwc, tagfield => '100', tagsubfield => 'a', kohafield => '' })->store;
70
    my $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
71
    is( @$res, 1, 'Found one exception' );
72
    is( $res->[0]->[1] . $res->[0]->[2], '100a', '100a was reported' );
73
    Koha::MarcSubfieldStructures->sync_kohafield;
74
    $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
75
    is( @$res, 0, 'Found no exceptions after syncing' );
76
    is( Koha::MarcSubfieldStructures->find($fwc, '100', 'a')->kohafield,
77
        'biblio.author', '100a in added framework adjusted' );
78
79
    # 245a unmapped in Default, mapped in other
80
    # 300a not in Default, mapped in other
81
    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 => '300', tagsubfield => 'a', kohafield => 'biblioitems.pages' })->store;
84
    $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
85
    is( @$res, 2, 'Found two exceptions' );
86
    is( $res->[0]->[1] . $res->[0]->[2], '245a', '245a was reported' );
87
    is( $res->[1]->[1] . $res->[1]->[2], '300a', '300a was reported' );
88
    Koha::MarcSubfieldStructures->sync_kohafield;
89
    $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
90
    is( @$res, 0, 'Found no exceptions after syncing again' );
91
    is( Koha::MarcSubfieldStructures->find($fwc, '245', 'a')->kohafield,
92
        undef, '245a in added framework cleared' );
93
    is( Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->kohafield,
94
        undef, '300a in added framework cleared' );
95
96
    # 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::MarcSubfieldStructures->find($fwc, '300', 'a')->update({ kohafield => 'biblioitems.pages' });
99
    $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
100
    is( @$res, 1, 'Found one exception' );
101
    is( $res->[0]->[1] . $res->[0]->[2], '300a', '300a was reported again' );
102
    Koha::MarcSubfieldStructures->sync_kohafield;
103
    $res = Koha::MarcSubfieldStructures->get_kohafield_exceptions;
104
    is( @$res, 0, 'Found no exceptions after third syncing' );
105
    is( Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->kohafield,
106
        'something_else', '300a in added framework adjusted' );
107
};
108
59
$schema->storage->txn_rollback;
109
$schema->storage->txn_rollback;
60
- 

Return to bug 19096