-sub FindDuplicate {
- my ($record,$authtypecode)=@_;
- my $dbh = C4::Context->dbh;
-
-# warn "".$record->as_formatted;
- # search duplicate on ISBN, easy and fast...
- my $sth = $dbh->prepare("select auth_tag_to_report,summary from auth_types where authtypecode=?");
- $sth->execute($authtypecode);
- my ($auth_tag_to_report,$taglist) = $sth->fetchrow;
- $sth->finish;
- # a more complex search : build a request for authoritysearch
- my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
- # search on biblio.title
-# warn " tag a reporter : $auth_tag_to_report";
- warn "taglist ".$taglist;
- my @subfield = split /\[/, $taglist;
- my $max = @subfield;
- for (my $i=1; $i<$max;$i++){
- warn " ".$subfield[$i];
- $subfield[$i]=substr($subfield[$i],3,1);
- warn " ".$subfield[$i];
- }
-
- if ($record->fields($auth_tag_to_report)) {
-# foreach my $subfieldcount (1..$#subfield){
-# if ($record->field($auth_tag_to_report)->subfields($subfield[$subfieldcount])) {
-# # warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
-# push @tags, $auth_tag_to_report.$subfield[$subfieldcount];
-# # warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
-# push @and_or, "and";
-# push @excluding, "";
-# push @operator, "contains";
-# push @value, $record->field($auth_tag_to_report)->subfield($subfield[$subfieldcount]);
-# }
-# }
-
- my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where tagfield=? and authtypecode=? ");
- $sth->execute($auth_tag_to_report,$authtypecode);
- # warn " field $auth_tag_to_report exists";
- while (my ($tag,$subfield) = $sth->fetchrow){
- if ($record->field($tag)->subfield($subfield)) {
- # warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
- push @tags, $tag.$subfield;
- # warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
- push @and_or, "and";
- push @excluding, "";
- push @operator, "contains";
- push @value, $record->field($tag)->subfield($subfield);
- }
- }
- }
-
- my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
- # there is at least 1 result => return the 1st one
- if ($nbresult) {
- warn "$nbresult => ".@$finalresult[0]->{authid},$record->field($auth_tag_to_report)->subfield('a');
- return @$finalresult[0]->{authid},@$finalresult[0]->{authid},$record->field($auth_tag_to_report)->subfield('a');
- }
- # no result, returns nothing
- return;
+=head2 merge
+
+=over 4
+
+$ref= &merge(mergefrom,$MARCfrom,$mergeto,$MARCto)
+
+
+Could add some feature : Migrating from a typecode to an other for instance.
+Then we should add some new parameter : bibliotargettag, authtargettag
+
+=back
+
+=cut
+
+sub merge {
+ my ($mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
+ my ($counteditedbiblio,$countunmodifiedbiblio,$counterrors)=(0,0,0);
+ my $dbh=C4::Context->dbh;
+ my $authtypecodefrom = GetAuthTypeCode($mergefrom);
+ my $authtypecodeto = GetAuthTypeCode($mergeto);
+# warn "mergefrom : $authtypecodefrom $mergefrom mergeto : $authtypecodeto $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;
+ # search the tag to report
+ my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
+ $sth->execute($authtypecodefrom);
+ my ($auth_tag_to_report_from) = $sth->fetchrow;
+ $sth->execute($authtypecodeto);
+ my ($auth_tag_to_report_to) = $sth->fetchrow;
+
+ my @record_to;
+ @record_to = $MARCto->field($auth_tag_to_report_to)->subfields() if $MARCto->field($auth_tag_to_report_to);
+ my @record_from;
+ @record_from = $MARCfrom->field($auth_tag_to_report_from)->subfields() if $MARCfrom->field($auth_tag_to_report_from);
+
+ my @reccache;
+ # search all biblio tags using this authority.
+ #Getting marcbiblios impacted by the change.
+ if (C4::Context->preference('NoZebra')) {
+ #nozebra way
+ my $dbh=C4::Context->dbh;
+ my $rq=$dbh->prepare(qq(SELECT biblionumbers from nozebra where indexname="an" and server="biblioserver" and value="$mergefrom" ));
+ $rq->execute;
+ while (my $biblionumbers=$rq->fetchrow){
+ my @biblionumbers=split /;/,$biblionumbers;
+ foreach (@biblionumbers) {
+ if ($_=~/(\d+),.*/) {
+ my $marc=GetMarcBiblio($1);
+ push @reccache,$marc;
+ }
+ }
+ }
+ } else {
+ #zebra connection
+ my $oConnection=C4::Context->Zconn("biblioserver",0);
+ $oConnection->option("preferredRecordSyntax"=>"XML");
+ my $query;
+ $query= "an=".$mergefrom;
+ my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
+ my $count = 0;
+ if ($oResult) {
+ $count=$oResult->size();
+ }
+ my $z=0;
+ while ( $z<$count ) {
+ my $rec;
+ $rec=$oResult->record($z);
+ my $marcdata = $rec->raw();
+ push @reccache, $marcdata;
+ $z++;
+ }
+ $oConnection->destroy();
+ }
+ #warn scalar(@reccache)." biblios to update";
+ # Get All candidate Tags for the change
+ # (This will reduce the search scope in marc records).
+ $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
+ $sth->execute($authtypecodefrom);
+ my @tags_using_authtype;
+ while (my ($tagfield) = $sth->fetchrow) {
+ push @tags_using_authtype,$tagfield ;
+ }
+ my $tag_to=0;
+ if ($authtypecodeto ne $authtypecodefrom){
+ # If many tags, take the first
+ $sth->execute($authtypecodeto);
+ $tag_to=$sth->fetchrow;
+ #warn $tag_to;
+ }
+ # BulkEdit marc records
+ # May be used as a template for a bulkedit field
+ foreach my $marcrecord(@reccache){
+ my $update;
+ $marcrecord= MARC::Record->new_from_xml($marcrecord,"utf8",C4::Context->preference("marcflavour")) unless(C4::Context->preference('NoZebra'));
+ foreach my $tagfield (@tags_using_authtype){
+# warn "tagfield : $tagfield ";
+ foreach my $field ($marcrecord->field($tagfield)){
+ 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);
+ foreach my $subfield (@record_to) {
+ $field_to->add_subfields($subfield->[0] =>$subfield->[1]);
+ }
+ $marcrecord->delete_field($field);
+ $marcrecord->insert_grouped_field($field_to);
+ $update=1;
+ }
+ }#for each tag
+ }#foreach tagfield
+ my ($bibliotag,$bibliosubf) = GetMarcFromKohaField("biblio.biblionumber","") ;
+ my $biblionumber;
+ if ($bibliotag<10){
+ $biblionumber=$marcrecord->field($bibliotag)->data;
+ }
+ else {
+ $biblionumber=$marcrecord->subfield($bibliotag,$bibliosubf);
+ }
+ unless ($biblionumber){
+ warn "pas de numéro de notice bibliographique dans : ".$marcrecord->as_formatted;
+ next;
+ }
+ if ($update==1){
+ &ModBiblio($marcrecord,$biblionumber,GetFrameworkCode($biblionumber)) ;
+ $counteditedbiblio++;
+ warn $counteditedbiblio if (($counteditedbiblio % 10) and $ENV{DEBUG});
+ }
+ }#foreach $marc
+ return $counteditedbiblio;
+ # now, find every other authority linked with this authority
+ # now, find every other authority linked with this authority
+# my $oConnection=C4::Context->Zconn("authorityserver");
+# my $query;
+# # att 9210 Auth-Internal-authtype
+# # att 9220 Auth-Internal-LN
+# # ccl.properties to add for authorities
+# $query= "= ".$mergefrom;
+# my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
+# my $count=$oResult->size() if ($oResult);
+# my @reccache;
+# my $z=0;
+# while ( $z<$count ) {
+# my $rec;
+# $rec=$oResult->record($z);
+# my $marcdata = $rec->raw();
+# push @reccache, $marcdata;
+# $z++;
+# }
+# $oResult->destroy();
+# foreach my $marc(@reccache){
+# my $update;
+# my $marcrecord;
+# $marcrecord = MARC::File::USMARC::decode($marc);
+# foreach my $tagfield (@tags_using_authtype){
+# $tagfield=substr($tagfield,0,3);
+# my @tags = $marcrecord->field($tagfield);
+# foreach my $tag (@tags){
+# my $tagsubs=$tag->subfield("9");
+# #warn "$tagfield:$tagsubs:$mergefrom";
+# if ($tagsubs== $mergefrom) {
+# $tag->update("9" =>$mergeto);
+# foreach my $subfield (@record_to) {
+# # warn "$subfield,$subfield->[0],$subfield->[1]";
+# $tag->update($subfield->[0] =>$subfield->[1]);
+# }#for $subfield
+# }
+# $marcrecord->delete_field($tag);
+# $marcrecord->add_fields($tag);
+# $update=1;
+# }#for each tag
+# }#foreach tagfield
+# my $authoritynumber = TransformMarcToKoha($dbh,$marcrecord,"") ;
+# if ($update==1){
+# &ModAuthority($marcrecord,$authoritynumber,GetAuthTypeCode($authoritynumber)) ;
+# }
+#
+# }#foreach $marc
+}#sub
+
+=head2 get_auth_type_location
+
+=over 4
+
+my ($tag, $subfield) = get_auth_type_location($auth_type_code);
+
+=back
+
+Get the tag and subfield used to store the heading type
+for indexing purposes. The C<$auth_type> parameter is
+optional; if it is not supplied, assume ''.
+
+This routine searches the MARC authority framework
+for the tag and subfield whose kohafield is
+C<auth_header.authtypecode>; if no such field is
+defined in the framework, default to the hardcoded value
+specific to the MARC format.
+
+=cut
+
+sub get_auth_type_location {
+ my $auth_type_code = @_ ? shift : '';
+
+ my ($tag, $subfield) = GetAuthMARCFromKohaField('auth_header.authtypecode', $auth_type_code);
+ if (defined $tag and defined $subfield and $tag != 0 and $subfield != 0) {
+ return ($tag, $subfield);
+ } else {
+ if (C4::Context->preference('marcflavour') eq "MARC21") {
+ return C4::AuthoritiesMarc::MARC21::default_auth_type_location();
+ } else {
+ return C4::AuthoritiesMarc::UNIMARC::default_auth_type_location();
+ }
+ }