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

(-)a/C4/AuthoritiesMarc.pm (-2 / +12 lines)
Lines 1471-1480 sub merge { Link Here
1471
                my $newtag = $tags_new && @$tags_new
1471
                my $newtag = $tags_new && @$tags_new
1472
                  ? _merge_newtag( $tag, $tags_new )
1472
                  ? _merge_newtag( $tag, $tags_new )
1473
                  : $tag;
1473
                  : $tag;
1474
                my $controlled_ind = Koha::Authority->new({ authtypecode => $authtypeto ? $authtypeto->authtypecode : undef })->controlled_indicators({ record => $MARCto, biblio_tag => $newtag }); #FIXME Replace this tric with new when refactoring
1474
                my $field_to = MARC::Field->new(
1475
                my $field_to = MARC::Field->new(
1475
                    $newtag,
1476
                    $newtag,
1476
                    $field->indicator(1),
1477
                    $controlled_ind->{ind1} // $field->indicator(1),
1477
                    $field->indicator(2),
1478
                    $controlled_ind->{ind2} // $field->indicator(2),
1478
                    9 => $mergeto, # Needed to create field, will be moved
1479
                    9 => $mergeto, # Needed to create field, will be moved
1479
                );
1480
                );
1480
                my ( @prefix, @postfix );
1481
                my ( @prefix, @postfix );
Lines 1500-1505 sub merge { Link Here
1500
                foreach my $subfield ( @prefix, @record_to, @postfix ) {
1501
                foreach my $subfield ( @prefix, @record_to, @postfix ) {
1501
                    $field_to->add_subfields($subfield->[0] => $subfield->[1]);
1502
                    $field_to->add_subfields($subfield->[0] => $subfield->[1]);
1502
                }
1503
                }
1504
                if( exists $controlled_ind->{sub2} ) { # thesaurus info
1505
                    if( defined $controlled_ind->{sub2} ) {
1506
                        # Add or replace
1507
                        $field_to->update( 2 => $controlled_ind->{sub2} );
1508
                    } else {
1509
                        # Key alerts us here to remove $2
1510
                        $field_to->delete_subfield( code => '2' );
1511
                    }
1512
                }
1503
                # Move $9 to the end
1513
                # Move $9 to the end
1504
                $field_to->delete_subfield( code => '9' );
1514
                $field_to->delete_subfield( code => '9' );
1505
                $field_to->add_subfields( 9 => $mergeto );
1515
                $field_to->add_subfields( 9 => $mergeto );
(-)a/Koha/Authority.pm (+51 lines)
Lines 20-25 package Koha::Authority; Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use base qw(Koha::Object);
22
use base qw(Koha::Object);
23
24
use Koha::Authority::ControlledIndicators;
23
use Koha::SearchEngine::Search;
25
use Koha::SearchEngine::Search;
24
26
25
=head1 NAME
27
=head1 NAME
Lines 59-64 sub linked_biblionumbers { Link Here
59
    return Koha::Authorities->linked_biblionumbers( $params );
61
    return Koha::Authorities->linked_biblionumbers( $params );
60
}
62
}
61
63
64
=head3 controlled_indicators
65
66
    Some authority types control the indicators of some corresponding
67
    biblio fields (especially in MARC21).
68
    For example, if you have a PERSO_NAME authority (report tag 100), the
69
    first indicator of biblio field 600 directly comes from the authority,
70
    and the second indicator depends on thesaurus settings in the authority
71
    record. Use this method to obtain such controlled values. In this example
72
    you should pass 600 in the biblio_tag parameter.
73
74
    my $result = $self->controlled_indicators({
75
        record => $auth_marc, biblio_tag => $bib_tag
76
    });
77
    my $ind1 = $result->{ind1};
78
    my $ind2 = $result->{ind2};
79
    my $subfield_2 = $result->{sub2}; # Optional subfield 2 when ind==7
80
81
    If an indicator is not controlled, the result hash does not contain a key
82
    for its value. (Same for the sub2 key for an optional subfield $2.)
83
84
    Note: The record parameter is a temporary bypass in order to prevent
85
    needless conversion of $self->marcxml.
86
87
=cut
88
89
sub controlled_indicators {
90
    my ( $self, $params ) = @_;
91
    my $tag = $params->{biblio_tag} // q{};
92
    my $record = $params->{record};
93
94
    my $flavour = C4::Context->preference('marcflavour') eq 'UNIMARC'
95
        ? 'UNIMARCAUTH'
96
        : 'MARC21';
97
    if( !$record ) {
98
        $record = MARC::Record->new_from_xml(
99
            $self->marcxml, 'UTF-8', $flavour );
100
    }
101
102
    my $authtype = Koha::Authority::Types->find( $self->authtypecode );
103
    return {} if !$authtype;
104
105
    return Koha::Authority::ControlledIndicators->new->get({
106
        auth_record => $record,
107
        report_tag  => $authtype->auth_tag_to_report,
108
        biblio_tag  => $tag,
109
        flavour     => $flavour,
110
    });
111
}
112
62
=head2 Class Methods
113
=head2 Class Methods
63
114
64
=head3 type
115
=head3 type
(-)a/authorities/blinddetail-biblio-search.pl (-47 / +14 lines)
Lines 70-80 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
70
    }
70
    }
71
);
71
);
72
72
73
# Extract the tag number from the index
74
my $tag_number = $index;
75
$tag_number =~ s/^tag_(\d*)_.*$/$1/;
76
73
# fill arrays
77
# fill arrays
74
my @subfield_loop;
78
my @subfield_loop;
75
my ($indicator1, $indicator2);
79
my ($indicator1, $indicator2);
76
if ($authid) {
80
if ($authid) {
77
    my $authtypecode = Koha::Authorities->find($authid)->authtypecode;
81
    my $auth = Koha::Authorities->find( $authid );
82
    my $authtypecode = $auth ? $auth->authtypecode : q{};
78
    my $auth_type = Koha::Authority::Types->find($authtypecode);
83
    my $auth_type = Koha::Authority::Types->find($authtypecode);
79
    my $record = GetAuthority($authid);
84
    my $record = GetAuthority($authid);
80
    my @fields = $record->field( $auth_type->auth_tag_to_report );
85
    my @fields = $record->field( $auth_type->auth_tag_to_report );
Lines 98-153 if ($authid) { Link Here
98
    }
103
    }
99
104
100
    push( @subfield_loop, { marc_subfield => 'w', marc_values => $relationship } ) if ( $relationship );
105
    push( @subfield_loop, { marc_subfield => 'w', marc_values => $relationship } ) if ( $relationship );
101
    if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
106
102
        $indicator1 = $field->indicator('1');
107
    my $controlled_ind = $auth->controlled_indicators({ record => $record, biblio_tag => $tag_number });
103
        $indicator2 = $field->indicator('2');
108
    $indicator1 = $controlled_ind->{ind1} // q{};
104
    } elsif (C4::Context->preference('marcflavour') eq 'MARC21') {
109
    $indicator2 = $controlled_ind->{ind2} // q{};
105
        my $tag_from = $auth_type->auth_tag_to_report;
110
    if( defined $controlled_ind->{sub2} ) {
106
        my $tag_to = $index;
111
        my $v = $controlled_ind->{sub2};
107
        $tag_to =~ s/^tag_(\d*)_.*$/$1/;
112
        push @subfield_loop, { marc_subfield => '2', marc_values => [ $v ] };
108
        if ($tag_to =~ /^6/) {  # subject heading
109
            my %thes_mapping = qw / a 0
110
                                    b 1
111
                                    c 2
112
                                    d 3
113
                                    k 5
114
                                    n 4
115
                                    r 7
116
                                    s 7
117
                                    v 6
118
                                    z 7
119
                                    | 4 /;
120
            my $thes_008_11 = '';
121
            $thes_008_11 = substr($record->field('008')->data(), 11, 1) if $record->field('008')->data();
122
            $indicator2 = defined $thes_mapping{$thes_008_11} ? $thes_mapping{$thes_008_11} : $thes_008_11;
123
            if ($indicator2 eq '7') {
124
                if ($thes_008_11 eq 'r') {
125
                    push @subfield_loop, { marc_subfield => '2', marc_values => [ 'aat' ] };
126
                } elsif ($thes_008_11 eq 's') {
127
                    push @subfield_loop, { marc_subfield => '2', marc_values => [ 'sears' ] };
128
                }
129
            }
130
        }
131
        if ($tag_from eq '130') {  # unified title -- the special case
132
            if ($tag_to eq '830' || $tag_to eq '240') {
133
                $indicator2 = $field->indicator('2');
134
            } else {
135
                $indicator1 = $field->indicator('2');
136
            }
137
        } else {
138
            $indicator1 = $field->indicator('1');
139
        }
140
    }
113
    }
141
}
114
} else {
142
else {
143
    # authid is empty => the user want to empty the entry.
115
    # authid is empty => the user want to empty the entry.
144
    $template->param( "clear" => 1 );
116
    $template->param( "clear" => 1 );
145
}
117
}
146
118
147
# Extract the tag number from the index
148
my $tag_number = $index;
149
$tag_number =~ s/^tag_(\d*)_.*$/$1/;
150
151
# Remove spaces in indicators
119
# Remove spaces in indicators
152
$indicator1 =~ s/\s//g;
120
$indicator1 =~ s/\s//g;
153
$indicator2 =~ s/\s//g;
121
$indicator2 =~ s/\s//g;
Lines 163-166 $template->param( Link Here
163
);
131
);
164
132
165
output_html_with_http_headers $query, $cookie, $template->output;
133
output_html_with_http_headers $query, $cookie, $template->output;
166
(-)a/t/db_dependent/Authority/Merge.t (-1 / +38 lines)
Lines 4-10 Link Here
4
4
5
use Modern::Perl;
5
use Modern::Perl;
6
6
7
use Test::More tests => 9;
7
use Test::More tests => 10;
8
8
9
use Getopt::Long;
9
use Getopt::Long;
10
use MARC::Record;
10
use MARC::Record;
Lines 15-20 use t::lib::TestBuilder; Link Here
15
15
16
use C4::Biblio;
16
use C4::Biblio;
17
use Koha::Authorities;
17
use Koha::Authorities;
18
use Koha::Authority::ControlledIndicators;
18
use Koha::Authority::MergeRequests;
19
use Koha::Authority::MergeRequests;
19
use Koha::Database;
20
use Koha::Database;
20
21
Lines 385-390 subtest 'merge should not reorder too much' => sub { Link Here
385
    is_deeply( $subfields, [ 'i', 'a', 'b', 'c', '9' ], 'Order kept' );
386
    is_deeply( $subfields, [ 'i', 'a', 'b', 'c', '9' ], 'Order kept' );
386
};
387
};
387
388
389
subtest 'Test how merge handles controlled indicators' => sub {
390
    plan tests => 4;
391
392
    # Note: See more detailed tests in t/Koha/Authority/ControlledIndicators.t
393
394
    # Testing MARC21 because thesaurus code makes it more interesting
395
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
396
    t::lib::Mocks::mock_preference('AuthorityControlledIndicators', q|marc21,*,ind1:auth1,ind2:thesaurus|);
397
398
    my $authmarc = MARC::Record->new;
399
    $authmarc->append_fields(
400
        MARC::Field->new( '008', (' 'x11).'r' ), # thesaurus code
401
        MARC::Field->new( '109', '7', '', 'a' => 'a' ),
402
    );
403
    my $id = AddAuthority( $authmarc, undef, $authtype1 );
404
    my $biblio = MARC::Record->new;
405
    $biblio->append_fields(
406
        MARC::Field->new( '609', '8', '4', a => 'a', 2 => '2', 9 => $id ),
407
    );
408
    my ( $biblionumber ) = C4::Biblio::AddBiblio( $biblio, '' );
409
410
    # Merge and check indicators and $2
411
    merge({ mergefrom => $id, MARCfrom => $authmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
412
    my $biblio2 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
413
    is( $biblio2->field('609')->indicator(1), '7', 'Indicator1 OK' );
414
    is( $biblio2->field('609')->indicator(2), '7', 'Indicator2 OK' );
415
    is( $biblio2->subfield('609', '2'), 'aat', 'Subfield $2 OK' );
416
417
    # Test $2 removal now
418
    $authmarc->field('008')->update( (' 'x11).'a' ); # LOC, no $2 needed
419
    AddAuthority( $authmarc, $id, $authtype1 ); # modify
420
    merge({ mergefrom => $id, MARCfrom => $authmarc, mergeto => $id, MARCto => $authmarc, biblionumbers => [ $biblionumber ] });
421
    $biblio2 = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
422
    is( $biblio2->subfield('609', '2'), undef, 'No subfield $2 left' );
423
};
424
388
sub set_mocks {
425
sub set_mocks {
389
    # After we removed the Zebra code from merge, we only need to mock
426
    # After we removed the Zebra code from merge, we only need to mock
390
    # get_usage_count and linked_biblionumbers here.
427
    # get_usage_count and linked_biblionumbers here.
(-)a/t/db_dependent/Koha/Authorities.t (-2 / +34 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 6;
22
use Test::More tests => 7;
23
use MARC::Field;
23
use MARC::Field;
24
use MARC::File::XML;
24
use MARC::File::XML;
25
use MARC::Record;
25
use MARC::Record;
Lines 29-35 use Test::MockObject; Link Here
29
use Test::Warn;
29
use Test::Warn;
30
30
31
use C4::Context;
31
use C4::Context;
32
use C4::AuthoritiesMarc;
32
use Koha::Authority;
33
use Koha::Authority;
34
use Koha::Authority::ControlledIndicators;
33
use Koha::Authorities;
35
use Koha::Authorities;
34
use Koha::Authority::MergeRequest;
36
use Koha::Authority::MergeRequest;
35
use Koha::Authority::Type;
37
use Koha::Authority::Type;
Lines 164-169 subtest 'Trivial tests for get_usage_count and linked_biblionumbers' => sub { Link Here
164
    t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
166
    t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
165
    cmp_deeply( [ $auth1->linked_biblionumbers ], [ 2001 ],
167
    cmp_deeply( [ $auth1->linked_biblionumbers ], [ 2001 ],
166
        'linked_biblionumbers with Elasticsearch' );
168
        'linked_biblionumbers with Elasticsearch' );
169
    t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
170
};
171
172
subtest 'Simple test for controlled_indicators' => sub {
173
    plan tests => 4;
174
175
    # NOTE: See more detailed tests in t/Koha/Authority/ControlledIndicators.t
176
177
    # Mock pref so that authority indicators are swapped for marc21/unimarc
178
    # The biblio tag is actually made irrelevant here
179
    t::lib::Mocks::mock_preference('AuthorityControlledIndicators', q|marc21,*,ind1:auth2,ind2:auth1
180
unimarc,*,ind1:auth2,ind2:auth1|);
181
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
182
183
    my $record = MARC::Record->new;
184
    $record->append_fields( MARC::Field->new( '100', '1', '2', a => 'Name' ) );
185
    my $type = $builder->build({ source => 'AuthType', value => { auth_tag_to_report => '100'} });
186
    my $authid = C4::AuthoritiesMarc::AddAuthority( $record, undef, $type->{authtypecode} );
187
    my $auth = Koha::Authorities->find( $authid );
188
    is( $auth->controlled_indicators({ biblio_tag => '123' })->{ind1}, '2', 'MARC21: Swapped ind2' );
189
    is( $auth->controlled_indicators({ biblio_tag => '234' })->{ind2}, '1', 'MARC21: Swapped ind1' );
190
191
    # try UNIMARC too
192
    t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
193
    $record = MARC::Record->new;
194
    $record->append_fields( MARC::Field->new( '210', '1', '2', a => 'Name' ) );
195
    $type = $builder->build({ source => 'AuthType', value => { auth_tag_to_report => '210'} });
196
    $authid = C4::AuthoritiesMarc::AddAuthority( $record, undef, $type->{authtypecode} );
197
    $auth = Koha::Authorities->find( $authid );
198
    is( $auth->controlled_indicators({ biblio_tag => '345' })->{ind1}, '2', 'UNIMARC: Swapped ind2' );
199
    is( $auth->controlled_indicators({ biblio_tag => '456' })->{ind2}, '1', 'UNIMARC: Swapped ind1' );
167
};
200
};
168
201
169
sub simple_search_compat {
202
sub simple_search_compat {
170
- 

Return to bug 14769