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

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

Return to bug 11700