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

(-)a/C4/Biblio.pm (-37 / +1 lines)
Lines 65-71 BEGIN { Link Here
65
        TransformHtmlToMarc
65
        TransformHtmlToMarc
66
        TransformHtmlToXml
66
        TransformHtmlToXml
67
        prepare_host_field
67
        prepare_host_field
68
        TransformMarcToKohaOneField
69
    );
68
    );
70
69
71
    # Internal functions
70
    # Internal functions
Lines 2396-2439 sub _disambiguate { Link Here
2396
2395
2397
}
2396
}
2398
2397
2399
=head2 TransformMarcToKohaOneField
2400
2401
    $val = TransformMarcToKohaOneField( 'biblio.title', $marc );
2402
2403
    Note: The authoritative Default framework is used implicitly.
2404
2405
=cut
2406
2407
sub TransformMarcToKohaOneField {
2408
    my ( $kohafield, $marc ) = @_;
2409
2410
    my ( @rv, $retval );
2411
    my @mss = GetMarcSubfieldStructureFromKohaField($kohafield);
2412
    foreach my $fldhash ( @mss ) {
2413
        my $tag = $fldhash->{tagfield};
2414
        my $sub = $fldhash->{tagsubfield};
2415
        foreach my $fld ( $marc->field($tag) ) {
2416
            if( $sub eq '@' || $fld->is_control_field ) {
2417
                push @rv, $fld->data if $fld->data;
2418
            } else {
2419
                push @rv, grep { $_ } $fld->subfield($sub);
2420
            }
2421
        }
2422
    }
2423
    return unless @rv;
2424
    $retval = join ' | ', uniq(@rv);
2425
2426
    # Additional polishing for individual kohafields
2427
    if( $kohafield =~ /copyrightdate|publicationyear/ ) {
2428
        $retval = _adjust_pubyear( $retval );
2429
    }
2430
2431
    return $retval;
2432
}
2433
2434
=head2 _adjust_pubyear
2398
=head2 _adjust_pubyear
2435
2399
2436
    Helper routine for TransformMarcToKohaOneField
2400
    Helper routine for TransformMarcToKoha
2437
2401
2438
=cut
2402
=cut
2439
2403
(-)a/C4/Breeding.pm (-31 / +7 lines)
Lines 304-319 sub _handle_one_result { Link Here
304
    my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0);
304
    my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0);
305
        #Last zero indicates: no update for batch record counts
305
        #Last zero indicates: no update for batch record counts
306
306
307
308
    #call to TransformMarcToKoha replaced by next call
309
    #we only need six fields from the marc record
310
    my $row;
307
    my $row;
311
    $row = _add_rowdata(
308
    if( $breedingid ){
312
        {
309
        my @kohafields = ('biblio.title','biblio.author','biblioitems.isbn','biblioitems.lccn','biblioitems.editionstatement');
313
            biblionumber => $bib,
310
        push @kohafields, C4::Context->preference('marcflavour') eq "MARC21" ? 'biblio.copyrightdate' : 'biblioitems.publicationyear';
314
            server       => $servhref->{servername},
311
        $row = TransformMarcToKoha({ record => $marcrecord, kohafields => \@kohafields, limit_table => 'no_items' });
315
            breedingid   => $breedingid,
312
        $row->{isbn}=_isbn_replace($row->{isbn});
316
        }, $marcrecord) if $breedingid;
313
        $row = _add_custom_field_rowdata($row, $marcrecord);
314
    }
317
    return ( $row, $error );
315
    return ( $row, $error );
318
}
316
}
319
317
Lines 341-368 sub _do_xslt_proc { Link Here
341
    }
339
    }
342
}
340
}
343
341
344
sub _add_rowdata {
345
    my ($row, $record)=@_;
346
    my %fetch= (
347
        title => 'biblio.title',
348
        author => 'biblio.author',
349
        isbn =>'biblioitems.isbn',
350
        lccn =>'biblioitems.lccn', #LC control number (not call number)
351
        edition =>'biblioitems.editionstatement'
352
    );
353
    $fetch{date} = C4::Context->preference('marcflavour') eq "MARC21" ? 'biblio.copyrightdate' : 'biblioitems.publicationyear';
354
355
    foreach my $k (keys %fetch) {
356
        $row->{$k} = C4::Biblio::TransformMarcToKohaOneField( $fetch{$k}, $record );
357
    }
358
    $row->{date}//= $row->{date2};
359
    $row->{isbn}=_isbn_replace($row->{isbn});
360
361
    $row = _add_custom_field_rowdata($row, $record);
362
363
    return $row;
364
}
365
366
sub _add_custom_field_rowdata
342
sub _add_custom_field_rowdata
367
{
343
{
368
    my ( $row, $record ) = @_;
344
    my ( $row, $record ) = @_;
(-)a/C4/XISBN.pm (-1 / +4 lines)
Lines 58-64 sub _get_biblio_from_xisbn { Link Here
58
    return unless ( !$errors && scalar @$results );
58
    return unless ( !$errors && scalar @$results );
59
59
60
    my $record = C4::Search::new_record_from_zebra( 'biblioserver', $results->[0] );
60
    my $record = C4::Search::new_record_from_zebra( 'biblioserver', $results->[0] );
61
    my $biblionumber = C4::Biblio::TransformMarcToKohaOneField( 'biblio.biblionumber', $record );
61
    my $biblionumber = C4::Biblio::TransformMarcToKoha({
62
        kohafields => ['biblio.biblionumber'],
63
        record => $record
64
    })->{biblionumber};
62
    return unless $biblionumber;
65
    return unless $biblionumber;
63
66
64
    my $biblio = Koha::Biblios->find( $biblionumber );
67
    my $biblio = Koha::Biblios->find( $biblionumber );
(-)a/Koha/MetaSearcher.pm (-20 / +7 lines)
Lines 58-83 sub new { Link Here
58
sub handle_hit {
58
sub handle_hit {
59
    my ( $self, $index, $server, $marcrecord ) = @_;
59
    my ( $self, $index, $server, $marcrecord ) = @_;
60
60
61
    my $record = Koha::MetadataRecord->new( { schema => 'marc', record => $marcrecord } );
61
    my @kohafields = ('biblio.title','biblio.subtitle','biblio.seriestitle','biblio.author',
62
62
            'biblioitems.isbn','biblioitems.issn','biblioitems.lccn','biblioitems.editionstatement',
63
    my %fetch = (
63
            'biblio.copyrightdate','biblioitems.publicationyear');
64
        title => 'biblio.title',
64
    my $metadata =  C4::Biblio::TransformMarcToKoha({ kohafields => \@kohafields, record => $marcrecord});
65
        subtitle => 'biblio.subtitle',
65
    $metadata->{edition} = delete $metadata->{editionstatement};
66
        seriestitle => 'biblio.seriestitle',
66
    $metadata->{date} = delete $metadata->{copyrightdate};
67
        author => 'biblio.author',
67
    $metadata->{date} //= delete $metadata->{publicationyear};
68
        isbn =>'biblioitems.isbn',
69
        issn =>'biblioitems.issn',
70
        lccn =>'biblioitems.lccn', #LC control number (not call number)
71
        edition =>'biblioitems.editionstatement',
72
        date => 'biblio.copyrightdate', #MARC21
73
        date2 => 'biblioitems.publicationyear', #UNIMARC
74
    );
75
76
    my $metadata = {};
77
    while ( my ( $key, $kohafield ) = each %fetch ) {
78
        $metadata->{$key} = $record->getKohaField($kohafield);
79
    }
80
    $metadata->{date} //= $metadata->{date2};
81
68
82
    push @{ $self->{results} }, {
69
    push @{ $self->{results} }, {
83
        server => $server,
70
        server => $server,
(-)a/Koha/MetadataRecord.pm (-1 / +1 lines)
Lines 118-124 sub createMergeHash { Link Here
118
sub getKohaField {
118
sub getKohaField {
119
    my ($self, $kohafield) = @_;
119
    my ($self, $kohafield) = @_;
120
    if ($self->schema =~ m/marc/) {
120
    if ($self->schema =~ m/marc/) {
121
        return C4::Biblio::TransformMarcToKohaOneField($kohafield, $self->record);
121
        return C4::Biblio::TransformMarcToKoha({ kohafields => [$kohafield], record => $self->record});
122
    }
122
    }
123
}
123
}
124
124
(-)a/t/db_dependent/Biblio/TransformMarcToKoha.t (-8 / +8 lines)
Lines 1-6 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
# Tests for C4::Biblio::TransformMarcToKoha, TransformMarcToKohaOneField
3
# Tests for C4::Biblio::TransformMarcToKoha
4
4
5
# Copyright 2017 Rijksmuseum
5
# Copyright 2017 Rijksmuseum
6
#
6
#
Lines 28-34 use t::lib::TestBuilder; Link Here
28
use Koha::Database;
28
use Koha::Database;
29
use Koha::Caches;
29
use Koha::Caches;
30
use Koha::MarcSubfieldStructures;
30
use Koha::MarcSubfieldStructures;
31
use C4::Biblio qw( TransformMarcToKoha TransformMarcToKohaOneField );
31
use C4::Biblio qw( TransformMarcToKoha );
32
32
33
my $schema  = Koha::Database->new->schema;
33
my $schema  = Koha::Database->new->schema;
34
$schema->storage->txn_begin;
34
$schema->storage->txn_begin;
Lines 57-66 subtest 'Test a few mappings' => sub { Link Here
57
    is( $result->{field2}, 'b1 | b2', 'Check field2 results' );
57
    is( $result->{field2}, 'b1 | b2', 'Check field2 results' );
58
    is( $result->{field3}, 'note1 | note2', 'Check field3 results' );
58
    is( $result->{field3}, 'note1 | note2', 'Check field3 results' );
59
59
60
    is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc ),
60
    is_deeply( C4::Biblio::TransformMarcToKoha({ record => $marc, kohafields => ['biblio.field1'] }),
61
        $result->{field1}, 'TransformMarcToKohaOneField returns biblio.field1');
61
        {field1 => 'a1 | a2'}, 'TransformMarcToKoha returns biblio.field1 if kohafields specified');
62
    is( C4::Biblio::TransformMarcToKohaOneField( 'field4', $marc ),
62
    is_deeply( C4::Biblio::TransformMarcToKoha({ record => $marc, kohafields => ['field4'] }),
63
        undef, 'TransformMarcToKohaOneField returns undef' );
63
            {} , 'TransformMarcToKoha returns empty hashref on unknown kohafields' );
64
64
65
};
65
};
66
66
Lines 83-90 subtest 'Multiple mappings for one kohafield' => sub { Link Here
83
    $result = C4::Biblio::TransformMarcToKoha({ record =>  $marc });
83
    $result = C4::Biblio::TransformMarcToKoha({ record =>  $marc });
84
    is_deeply( $result, { field1 => '3a | 51' }, 'Got 300a and 510a' );
84
    is_deeply( $result, { field1 => '3a | 51' }, 'Got 300a and 510a' );
85
85
86
    is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc ),
86
    is_deeply( C4::Biblio::TransformMarcToKoha({ kohafields => ['biblio.field1'], record => $marc }),
87
        '3a | 51', 'TransformMarcToKohaOneField returns biblio.field1' );
87
        { 'field1' => '3a | 51'}, 'TransformMarcToKoha returns biblio.field1 when kohafields specified' );
88
};
88
};
89
89
90
subtest 'Testing _adjust_pubyear' => sub {
90
subtest 'Testing _adjust_pubyear' => sub {
(-)a/t/db_dependent/Breeding.t (-29 / +13 lines)
Lines 56-65 subtest '_do_xslt_proc' => sub { Link Here
56
    plan tests => 6;
56
    plan tests => 6;
57
    test_do_xslt();
57
    test_do_xslt();
58
};
58
};
59
#Group 4: testing _add_rowdata (part of Z3950Search)
59
#Group 4: testing _add_custom_field_rowdata (part of Z3950Search)
60
subtest '_add_rowdata' => sub {
60
subtest '_add_custom_field_rowdata' => sub {
61
    plan tests => 5;
61
    plan tests => 3;
62
    test_add_rowdata();
62
    test_add_custom_field_rowdata();
63
};
63
};
64
64
65
subtest ImportBreedingAuth => sub {
65
subtest ImportBreedingAuth => sub {
Lines 261-304 sub test_do_xslt { Link Here
261
        'At least the title is the same :)' );
261
        'At least the title is the same :)' );
262
}
262
}
263
263
264
sub test_add_rowdata {
264
sub test_add_custom_field_rowdata {
265
    t::lib::Mocks::mock_preference('AdditionalFieldsInZ3950ResultSearch','');
266
267
    my $row = {
265
    my $row = {
268
       biblionumber => 0,
266
       biblionumber => 0,
269
       server => "testServer",
267
       server => "testServer",
270
       breedingid => 0
268
       breedingid => 0,
269
       title => "Just a title"
271
   };
270
   };
272
271
273
    my $biblio = MARC::Record->new();
272
    my $biblio = MARC::Record->new();
274
    $biblio->append_fields(
273
    $biblio->append_fields(
275
        MARC::Field->new('245', ' ', ' ', a => 'Just a title'), #title
274
        MARC::Field->new('245', ' ', ' ', a => 'Just a title'),
275
        MARC::Field->new('035', ' ', ' ', a => 'First 035'),
276
        MARC::Field->new('035', ' ', ' ', a => 'Second 035')
276
    );
277
    );
277
278
278
    my $returned_row = C4::Breeding::_add_rowdata($row, $biblio);
279
   t::lib::Mocks::mock_preference('AdditionalFieldsInZ3950ResultSearch',"245\$a, 035\$a");
279
280
    is($returned_row->{title}, "Just a title", "_add_rowdata returns the title of a biblio");
281
    is($returned_row->{addnumberfields}[0], undef, "_add_rowdata returns undef if it has no additionnal field");
282
280
283
    t::lib::Mocks::mock_preference('AdditionalFieldsInZ3950ResultSearch',"245\$a, 035\$a");
281
   my $returned_row = C4::Breeding::_add_custom_field_rowdata($row, $biblio);
284
285
    $row = {
286
       biblionumber => 0,
287
       server => "testServer",
288
       breedingid => 0
289
   };
290
   $biblio = MARC::Record->new();
291
   $biblio->append_fields(
292
        MARC::Field->new('245', ' ', ' ', a => 'Just a title'), #title
293
        MARC::Field->new('035', ' ', ' ', a => 'First 035'),
294
        MARC::Field->new('035', ' ', ' ', a => 'Second 035')
295
   );
296
   $returned_row = C4::Breeding::_add_rowdata($row, $biblio);
297
282
298
   is($returned_row->{title}, "Just a title", "_add_rowdata returns the title of a biblio");
283
   is($returned_row->{title}, "Just a title", "_add_rowdata returns the title of a biblio");
299
   is($returned_row->{addnumberfields}[0], "245\$a", "_add_rowdata returns the field number chosen in the AdditionalFieldsInZ3950ResultSearch preference");
284
   is($returned_row->{addnumberfields}[0], "245\$a", "_add_rowdata returns the field number chosen in the AdditionalFieldsInZ3950ResultSearch preference");
300
285
301
   # Test repeatble tags,the trailing whitespace is a normal side-effect of _add_custom_row_data
286
   # Test repeatble tags,the trailing whitespace is a normal side-effect of _add_custom_field_row_data
302
   is_deeply(\$returned_row->{"035\$a"}, \["First 035 ", "Second 035 "],"_add_rowdata supports repeatable tags");
287
   is_deeply(\$returned_row->{"035\$a"}, \["First 035 ", "Second 035 "],"_add_rowdata supports repeatable tags");
303
}
288
}
304
289
305
- 

Return to bug 30813