@@ -, +, @@ triggered during unit tests --- C4/AuthoritiesMarc.pm | 120 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 47 deletions(-) --- a/C4/AuthoritiesMarc.pm +++ a/C4/AuthoritiesMarc.pm @@ -660,11 +660,12 @@ sub AddAuthority { my $auth_exists=0; my $oldRecord; if (!$authid) { - my $sth=$dbh->prepare("select max(authid) from auth_header"); + my $sth = $dbh->prepare("select max(authid) from auth_header"); $sth->execute; - ($authid)=$sth->fetchrow; - $authid=$authid+1; - ##Insert the recordID in MARC record + ($authid) = $sth->fetchrow || 0; + $authid++; + + # Insert the recordID in MARC record unless ($record->field('001') && $record->field('001')->data() eq $authid){ $record->delete_field($record->field('001')); $record->insert_fields_ordered(MARC::Field->new('001',$authid)); @@ -1400,17 +1401,22 @@ Returns a list of biblionumbers of records that were modified. =cut sub merge { - my ($mergefrom, $MARCfrom, $mergeto, $MARCto) = @_; + my ( $mergefrom, $MARCfrom, $mergeto, $MARCto ) = @_; + my @editedbiblios; my $dbh = C4::Context->dbh; my $authtypefrom = Koha::Authority::Types->find($mergefrom); my $authtypeto = Koha::Authority::Types->find($mergeto); $MARCfrom ||= GetAuthority($mergefrom); - $MARCto ||= GetAuthority($mergeto); + $MARCto ||= GetAuthority($mergeto); + + # return if authority does not exist + return "error MARCFROM not a marcrecord " . Data::Dumper::Dumper($MARCfrom) + if scalar( $MARCfrom->fields() ) == 0; + return "error MARCTO not a marcrecord" . Data::Dumper::Dumper($MARCto) + if scalar( $MARCto->fields() ) == 0; - return "error MARCFROM not a marcrecord ".Data::Dumper::Dumper($MARCfrom) if scalar($MARCfrom->fields()) == 0; - return "error MARCTO not a marcrecord".Data::Dumper::Dumper($MARCto) if scalar($MARCto->fields()) == 0; # search the tag to report my $auth_tag_to_report_from = $authtypefrom->auth_tag_to_report; my $auth_tag_to_report_to = $authtypeto->auth_tag_to_report; @@ -1422,95 +1428,115 @@ sub merge { my @reccache; # search all biblio tags using this authority. - #Getting marcbiblios impacted by the change. - #zebra connection - my $oConnection=C4::Context->Zconn("biblioserver",0); + # Getting marcbiblios impacted by the change. + # zebra connection + my $oConnection = C4::Context->Zconn( "biblioserver", 0 ); # We used to use XML syntax here, but that no longer works. # Thankfully, we don't need it. - my $query; - $query= "an=".$mergefrom; + my $query = "an=" . $mergefrom; + my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection )); my $count = 0; - if ($oResult) { - $count=$oResult->size(); + if ($oResult) { + $count = $oResult->size(); } - my $z=0; - while ( $z<$count ) { + + my $z = 0; + while ( $z < $count ) { my $marcrecordzebra = C4::Search::new_record_from_zebra( 'biblioserver', $oResult->record($z)->raw() ); + my ( $biblionumbertagfield, $biblionumbertagsubfield ) = &GetMarcFromKohaField( "biblio.biblionumber", '' ); + my $i = ($biblionumbertagfield < 10) ? $marcrecordzebra->field( $biblionumbertagfield )->data : $marcrecordzebra->subfield( $biblionumbertagfield, $biblionumbertagsubfield ); + my $marcrecorddb = GetMarcBiblio($i); + push @reccache, $marcrecorddb; $z++; } + $oResult->destroy(); + #warn scalar(@reccache)." biblios to update"; # Get All candidate Tags for the change # (This will reduce the search scope in marc records). my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?"); $sth->execute($authtypefrom->authtypecode); my @tags_using_authtype; - while (my ($tagfield) = $sth->fetchrow) { - push @tags_using_authtype,$tagfield ; + while ( my ($tagfield) = $sth->fetchrow ) { + push @tags_using_authtype, $tagfield; } - my $tag_to=0; + my $tag_to = 0; + if ($authtypeto->authtypecode ne $authtypefrom->authtypecode){ # If many tags, take the first $sth->execute($authtypeto->authtypecode); - $tag_to=$sth->fetchrow; + $tag_to = $sth->fetchrow; } + # BulkEdit marc records # May be used as a template for a bulkedit field foreach my $marcrecord(@reccache){ my $update = 0; - foreach my $tagfield (@tags_using_authtype){ - foreach my $field ($marcrecord->field($tagfield)){ - # biblio is linked to authority with $9 subfield containing authid - my $auth_number=$field->subfield("9"); - my $tag=$field->tag(); - if ($auth_number==$mergefrom) { - my $field_to = MARC::Field->new(($tag_to?$tag_to:$tag), - $field->indicator(1),$field->indicator(2),"9"=>$mergeto); - my $exclude='9'; - foreach my $subfield (grep {$_->[0] ne '9'} @record_to) { - $field_to->add_subfields($subfield->[0] =>$subfield->[1]); - $exclude.= $subfield->[0]; + foreach my $tagfield (@tags_using_authtype) { + foreach my $field ( $marcrecord->field($tagfield) ) { + + # biblio is linked to authority with $9 subfield containing authid + my $auth_number = $field->subfield("9"); + my $tag = $field->tag(); + if ( $auth_number == $mergefrom ) { + my $field_to = MARC::Field->new( + ( $tag_to ? $tag_to : $tag ), $field->indicator(1), + $field->indicator(2), "9" => $mergeto + ); + my $exclude = '9'; + foreach my $subfield ( grep { $_->[0] ne '9' } @record_to ) + { + $field_to->add_subfields( + $subfield->[0] => $subfield->[1] ); + $exclude .= $subfield->[0]; } - $exclude='['.$exclude.']'; + $exclude = '[' . $exclude . ']'; + #add subfields in $field not included in @record_to - my @restore= grep {$_->[0]!~/$exclude/} $field->subfields(); + my @restore = + grep { $_->[0] !~ /$exclude/ } $field->subfields(); foreach my $subfield (@restore) { - $field_to->add_subfields($subfield->[0] =>$subfield->[1]); + $field_to->add_subfields( + $subfield->[0] => $subfield->[1] ); } $marcrecord->delete_field($field); $marcrecord->insert_grouped_field($field_to); - $update=1; + $update = 1; } - }#for each tag - }#foreach tagfield + } #for each tag + } #foreach tagfield my ($bibliotag,$bibliosubf) = GetMarcFromKohaField("biblio.biblionumber","") ; my $biblionumber; - if ($bibliotag<10){ - $biblionumber=$marcrecord->field($bibliotag)->data; + + if ( $bibliotag < 10 ) { + $biblionumber = $marcrecord->field($bibliotag)->data; } else { - $biblionumber=$marcrecord->subfield($bibliotag,$bibliosubf); + $biblionumber = $marcrecord->subfield( $bibliotag, $bibliosubf ); } - unless ($biblionumber){ - warn "pas de numéro de notice bibliographique dans : ".$marcrecord->as_formatted; + + unless ($biblionumber) { + warn "No biblionumber found for record: " . $marcrecord->as_formatted; next; } - if ($update==1){ - &ModBiblio($marcrecord,$biblionumber,GetFrameworkCode($biblionumber)) ; + + if ( $update == 1 ) { + &ModBiblio( $marcrecord, $biblionumber, GetFrameworkCode($biblionumber) ); push @editedbiblios, $biblionumber; } - }#foreach $marc + } #foreach $marc if ( $mergefrom != $mergeto ) { my $number_of_authorities_deleted = DelAuthority($mergefrom); --