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

(-)a/C4/AuthoritiesMarc.pm (-48 / +73 lines)
Lines 660-670 sub AddAuthority { Link Here
660
  my $auth_exists=0;
660
  my $auth_exists=0;
661
  my $oldRecord;
661
  my $oldRecord;
662
  if (!$authid) {
662
  if (!$authid) {
663
    my $sth=$dbh->prepare("select max(authid) from auth_header");
663
    my $sth = $dbh->prepare("select max(authid) from auth_header");
664
    $sth->execute;
664
    $sth->execute;
665
    ($authid)=$sth->fetchrow;
665
    ($authid) = $sth->fetchrow || 0;
666
    $authid=$authid+1;
666
    $authid++;
667
  ##Insert the recordID in MARC record 
667
668
    # Insert the recordID in MARC record 
668
    unless ($record->field('001') && $record->field('001')->data() eq $authid){
669
    unless ($record->field('001') && $record->field('001')->data() eq $authid){
669
        $record->delete_field($record->field('001'));
670
        $record->delete_field($record->field('001'));
670
        $record->insert_fields_ordered(MARC::Field->new('001',$authid));
671
        $record->insert_fields_ordered(MARC::Field->new('001',$authid));
Lines 1400-1416 Returns a list of biblionumbers of records that were modified. Link Here
1400
=cut
1401
=cut
1401
1402
1402
sub merge {
1403
sub merge {
1403
    my ($mergefrom, $MARCfrom, $mergeto, $MARCto) = @_;
1404
    my ( $mergefrom, $MARCfrom, $mergeto, $MARCto ) = @_;
1405
1404
    my @editedbiblios;
1406
    my @editedbiblios;
1405
    my $dbh = C4::Context->dbh;
1407
    my $dbh = C4::Context->dbh;
1406
    my $authtypefrom = Koha::Authority::Types->find($mergefrom);
1408
    my $authtypefrom = Koha::Authority::Types->find($mergefrom);
1407
    my $authtypeto   = Koha::Authority::Types->find($mergeto);
1409
    my $authtypeto   = Koha::Authority::Types->find($mergeto);
1408
1410
1409
    $MARCfrom ||= GetAuthority($mergefrom);
1411
    $MARCfrom ||= GetAuthority($mergefrom);
1410
    $MARCto ||= GetAuthority($mergeto);
1412
    $MARCto   ||= GetAuthority($mergeto);
1413
1414
    # return if authority does not exist
1415
    return "error MARCFROM not a marcrecord " . Data::Dumper::Dumper($MARCfrom)
1416
      if scalar( $MARCfrom->fields() ) == 0;
1417
    return "error MARCTO not a marcrecord" . Data::Dumper::Dumper($MARCto)
1418
      if scalar( $MARCto->fields() ) == 0;
1411
1419
1412
    return "error MARCFROM not a marcrecord ".Data::Dumper::Dumper($MARCfrom) if scalar($MARCfrom->fields()) == 0;
1413
    return "error MARCTO not a marcrecord".Data::Dumper::Dumper($MARCto) if scalar($MARCto->fields()) == 0;
1414
    # search the tag to report
1420
    # search the tag to report
1415
    my $auth_tag_to_report_from = $authtypefrom->auth_tag_to_report;
1421
    my $auth_tag_to_report_from = $authtypefrom->auth_tag_to_report;
1416
    my $auth_tag_to_report_to   = $authtypeto->auth_tag_to_report;
1422
    my $auth_tag_to_report_to   = $authtypeto->auth_tag_to_report;
Lines 1422-1516 sub merge { Link Here
1422
1428
1423
    my @reccache;
1429
    my @reccache;
1424
    # search all biblio tags using this authority.
1430
    # search all biblio tags using this authority.
1425
    #Getting marcbiblios impacted by the change.
1431
    # Getting marcbiblios impacted by the change.
1426
    #zebra connection
1432
    # zebra connection
1427
    my $oConnection=C4::Context->Zconn("biblioserver",0);
1433
    my $oConnection = C4::Context->Zconn( "biblioserver", 0 );
1428
    # We used to use XML syntax here, but that no longer works.
1434
    # We used to use XML syntax here, but that no longer works.
1429
    # Thankfully, we don't need it.
1435
    # Thankfully, we don't need it.
1430
    my $query;
1436
    my $query = "an=" . $mergefrom;
1431
    $query= "an=".$mergefrom;
1437
1432
    my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1438
    my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1433
    my $count = 0;
1439
    my $count = 0;
1434
    if  ($oResult) {
1440
    if ($oResult) {
1435
        $count=$oResult->size();
1441
        $count = $oResult->size();
1436
    }
1442
    }
1437
    my $z=0;
1443
1438
    while ( $z<$count ) {
1444
    my $z = 0;
1445
    while ( $z < $count ) {
1439
        my $marcrecordzebra = C4::Search::new_record_from_zebra(
1446
        my $marcrecordzebra = C4::Search::new_record_from_zebra(
1440
            'biblioserver',
1447
            'biblioserver',
1441
            $oResult->record($z)->raw()
1448
            $oResult->record($z)->raw()
1442
        );
1449
        );
1450
1443
        my ( $biblionumbertagfield, $biblionumbertagsubfield ) = &GetMarcFromKohaField( "biblio.biblionumber", '' );
1451
        my ( $biblionumbertagfield, $biblionumbertagsubfield ) = &GetMarcFromKohaField( "biblio.biblionumber", '' );
1452
1444
        my $i = ($biblionumbertagfield < 10)
1453
        my $i = ($biblionumbertagfield < 10)
1445
            ? $marcrecordzebra->field( $biblionumbertagfield )->data
1454
            ? $marcrecordzebra->field( $biblionumbertagfield )->data
1446
            : $marcrecordzebra->subfield( $biblionumbertagfield, $biblionumbertagsubfield );
1455
            : $marcrecordzebra->subfield( $biblionumbertagfield, $biblionumbertagsubfield );
1456
1447
        my $marcrecorddb = GetMarcBiblio($i);
1457
        my $marcrecorddb = GetMarcBiblio($i);
1458
1448
        push @reccache, $marcrecorddb;
1459
        push @reccache, $marcrecorddb;
1449
        $z++;
1460
        $z++;
1450
    }
1461
    }
1462
1451
    $oResult->destroy();
1463
    $oResult->destroy();
1464
1452
    #warn scalar(@reccache)." biblios to update";
1465
    #warn scalar(@reccache)." biblios to update";
1453
    # Get All candidate Tags for the change 
1466
    # Get All candidate Tags for the change 
1454
    # (This will reduce the search scope in marc records).
1467
    # (This will reduce the search scope in marc records).
1455
    my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1468
    my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1456
    $sth->execute($authtypefrom->authtypecode);
1469
    $sth->execute($authtypefrom->authtypecode);
1457
    my @tags_using_authtype;
1470
    my @tags_using_authtype;
1458
    while (my ($tagfield) = $sth->fetchrow) {
1471
    while ( my ($tagfield) = $sth->fetchrow ) {
1459
        push @tags_using_authtype,$tagfield ;
1472
        push @tags_using_authtype, $tagfield;
1460
    }
1473
    }
1461
    my $tag_to=0;
1474
    my $tag_to = 0;
1475
1462
    if ($authtypeto->authtypecode ne $authtypefrom->authtypecode){
1476
    if ($authtypeto->authtypecode ne $authtypefrom->authtypecode){
1463
        # If many tags, take the first
1477
        # If many tags, take the first
1464
        $sth->execute($authtypeto->authtypecode);
1478
        $sth->execute($authtypeto->authtypecode);
1465
        $tag_to=$sth->fetchrow;
1479
        $tag_to = $sth->fetchrow;
1466
    }
1480
    }
1481
1467
    # BulkEdit marc records
1482
    # BulkEdit marc records
1468
    # May be used as a template for a bulkedit field
1483
    # May be used as a template for a bulkedit field
1469
    foreach my $marcrecord(@reccache){
1484
    foreach my $marcrecord(@reccache){
1470
        my $update = 0;
1485
        my $update = 0;
1471
        foreach my $tagfield (@tags_using_authtype){
1486
        foreach my $tagfield (@tags_using_authtype) {
1472
            foreach my $field ($marcrecord->field($tagfield)){
1487
            foreach my $field ( $marcrecord->field($tagfield) ) {
1473
                # biblio is linked to authority with $9 subfield containing authid
1488
1474
                my $auth_number=$field->subfield("9");
1489
              # biblio is linked to authority with $9 subfield containing authid
1475
                my $tag=$field->tag();
1490
                my $auth_number = $field->subfield("9");
1476
                if ($auth_number==$mergefrom) {
1491
                my $tag         = $field->tag();
1477
                    my $field_to = MARC::Field->new(($tag_to?$tag_to:$tag),
1492
                if ( $auth_number == $mergefrom ) {
1478
                        $field->indicator(1),$field->indicator(2),"9"=>$mergeto);
1493
                    my $field_to = MARC::Field->new(
1479
                    my $exclude='9';
1494
                        ( $tag_to ? $tag_to : $tag ), $field->indicator(1),
1480
                    foreach my $subfield (grep {$_->[0] ne '9'} @record_to) {
1495
                        $field->indicator(2), "9" => $mergeto
1481
                        $field_to->add_subfields($subfield->[0] =>$subfield->[1]);
1496
                    );
1482
                        $exclude.= $subfield->[0];
1497
                    my $exclude = '9';
1498
                    foreach my $subfield ( grep { $_->[0] ne '9' } @record_to )
1499
                    {
1500
                        $field_to->add_subfields(
1501
                            $subfield->[0] => $subfield->[1] );
1502
                        $exclude .= $subfield->[0];
1483
                    }
1503
                    }
1484
                    $exclude='['.$exclude.']';
1504
                    $exclude = '[' . $exclude . ']';
1505
1485
                    #add subfields in $field not included in @record_to
1506
                    #add subfields in $field not included in @record_to
1486
                    my @restore= grep {$_->[0]!~/$exclude/} $field->subfields();
1507
                    my @restore =
1508
                      grep { $_->[0] !~ /$exclude/ } $field->subfields();
1487
                    foreach my $subfield (@restore) {
1509
                    foreach my $subfield (@restore) {
1488
                       $field_to->add_subfields($subfield->[0] =>$subfield->[1]);
1510
                        $field_to->add_subfields(
1511
                            $subfield->[0] => $subfield->[1] );
1489
                    }
1512
                    }
1490
                    $marcrecord->delete_field($field);
1513
                    $marcrecord->delete_field($field);
1491
                    $marcrecord->insert_grouped_field($field_to);
1514
                    $marcrecord->insert_grouped_field($field_to);
1492
                    $update=1;
1515
                    $update = 1;
1493
                }
1516
                }
1494
            }#for each tag
1517
            }    #for each tag
1495
        }#foreach tagfield
1518
        }    #foreach tagfield
1496
1519
1497
        my ($bibliotag,$bibliosubf) = GetMarcFromKohaField("biblio.biblionumber","") ;
1520
        my ($bibliotag,$bibliosubf) = GetMarcFromKohaField("biblio.biblionumber","") ;
1498
        my $biblionumber;
1521
        my $biblionumber;
1499
        if ($bibliotag<10){
1522
1500
            $biblionumber=$marcrecord->field($bibliotag)->data;
1523
        if ( $bibliotag < 10 ) {
1524
            $biblionumber = $marcrecord->field($bibliotag)->data;
1501
        }
1525
        }
1502
        else {
1526
        else {
1503
            $biblionumber=$marcrecord->subfield($bibliotag,$bibliosubf);
1527
            $biblionumber = $marcrecord->subfield( $bibliotag, $bibliosubf );
1504
        }
1528
        }
1505
        unless ($biblionumber){
1529
1506
            warn "pas de numéro de notice bibliographique dans : ".$marcrecord->as_formatted;
1530
        unless ($biblionumber) {
1531
            warn "No biblionumber found for record: " . $marcrecord->as_formatted;
1507
            next;
1532
            next;
1508
        }
1533
        }
1509
        if ($update==1){
1534
1510
            &ModBiblio($marcrecord,$biblionumber,GetFrameworkCode($biblionumber)) ;
1535
        if ( $update == 1 ) {
1536
            &ModBiblio( $marcrecord, $biblionumber, GetFrameworkCode($biblionumber) );
1511
            push @editedbiblios, $biblionumber;
1537
            push @editedbiblios, $biblionumber;
1512
        }
1538
        }
1513
    }#foreach $marc
1539
    }   #foreach $marc
1514
1540
1515
    if ( $mergefrom != $mergeto ) {
1541
    if ( $mergefrom != $mergeto ) {
1516
        my $number_of_authorities_deleted = DelAuthority($mergefrom);
1542
        my $number_of_authorities_deleted = DelAuthority($mergefrom);
1517
- 

Return to bug 11700