1 package C4::AuthoritiesMarc;
2 # Copyright 2000-2002 Katipo Communications
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA 02111-1307 USA
27 use vars qw($VERSION @ISA @EXPORT);
29 # set the version for version checking
36 &AUTHfind_authtypecode
43 &AUTHfind_marc_from_kohafield
56 sub AUTHfind_marc_from_kohafield {
57 my ( $dbh, $kohafield,$authtypecode ) = @_;
58 return 0, 0 unless $kohafield;
59 $authtypecode="" unless $authtypecode;
60 my $marcfromkohafield;
61 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
62 $sth->execute($kohafield,$authtypecode);
63 my ($tagfield,$tagsubfield) = $sth->fetchrow;
65 return ($tagfield,$tagsubfield);
68 my ($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode) = @_;
71 # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
72 # the authtypecode. Then, search on $a of this tag_to_report
73 # also store main entry MARC tag, to extract it at end of search
75 ##first set the authtype search
76 $query="\@attr 1=1013 \@attr 5=100 ".$authtypecode; ##No truncation on authtype
80 for(my $i = 0 ; $i <= $#{$value} ; $i++)
84 ##If mainentry search $a tag
85 if (@$tags[$i] eq "mainentry") {
86 $attr =" \@attr 1=21 ";
88 $attr =" \@attr 1=47 ";
94 if (@$operator[$i] eq 'phrase') {
95 $attr.=" \@attr 4=1 \@attr 5=100 \@attr 3=1 ";##Phrase, No truncation, first in field###It seems not implemented by indexdata
99 $attr .=" \@attr 4=6 \@attr 5=1 ";## Word list, right truncated, anywhere
104 $attr =$attr."\"".@$value[$i]."\"";
110 ##Add how many queries generated
111 $query= $and.$query.$q2;
114 $offset=0 unless $offset;
115 my $counter = $offset;
116 $length=10 unless $length;
118 my $oAuth=C4::Context->Zconnauth("authorityserver");
119 if ($oAuth eq "error"){
120 warn "Error/CONNECTING \n";
121 return("error",undef);
125 my $Anewq= new ZOOM::Query::PQF($query);
126 $Anewq->sortby("1=21 i< 1=47 i<");
129 $oAResult= $oAuth->search($Anewq) ;
132 warn " /CODE:", $@->code()," /MSG:",$@->message(),"\n";
133 return("error",undef);
138 $nbresults=$oAResult->size() if ($oAResult);
143 my @finalresult = ();
145 ##fIND tags using authority
147 my $newsth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
148 $newsth->execute($authtypecode);
149 my $tags_using_authtype;
150 while (my ($tagfield) = $newsth->fetchrow) {
151 $tags_using_authtype.= "'".$tagfield."9',";
153 ##Find authid and linkid fields
154 my ($authidfield,$authidsubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.authid",$authtypecode);
155 my ($linkidfield,$linkidsubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.linkid",$authtypecode);
156 while (($counter < $nbresults) && ($counter < ($offset + $length))) {
158 ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
159 my $rec=$oAResult->record($counter);
160 my $marcdata=$rec->raw();
164 my $separator=C4::Context->preference('authoritysep');
165 my $linksummary=" ".$separator;
167 $authrecord = MARC::File::USMARC::decode($marcdata);
168 my $authid=$authrecord->field($authidfield)->subfield($authidsubfield); ## we could have these defined in system pref.
169 if ($authrecord->field($linkidfield)){
170 my @fields=$authrecord->field($linkidfield);
172 foreach my $field (@fields){
173 $linkid=$field->subfield($linkidsubfield) ;
174 if ($linkid){ ##There is a linked record add fields to produce summary
175 my $linktype=AUTHfind_authtypecode($dbh,$linkid);
176 my $linkrecord=AUTHgetauthority($dbh,$linkid);
177 $linksummary.=getsummary($dbh,$linkrecord,$linkid,$linktype).$separator;
182 my $summary=getsummary($dbh,$authrecord,$authid,$authtypecode);
183 if ($linkid && $linksummary ne " ".$separator){
184 $summary="<b>".$summary."</b>".$linksummary;
186 ## Fix Async search and move Zconn to here
188 $newline{summary} = $summary;
189 $newline{authid} = $authid;
190 $newline{linkid} = $linkid;
191 # $newline{used} =$count;
192 $newline{biblio_fields} = $tags_using_authtype;
193 $newline{even} = $counter % 2;
195 push @finalresult, \%newline;
197 $oAResult->destroy();
201 my $oConnection=C4::Context->Zconn("biblioserver");
202 if ($oConnection eq "error"){
203 warn "Error/CONNECTING \n";
206 for (my $z=0; $z<@finalresult; $z++){
209 $nquery= "\@attr GILS 1=2057 ".$finalresult[$z]{authid};
210 $nquery="\@or ".$nquery." \@attr GILS 1=2057 ".$finalresult[$z]{linkid} if $finalresult[$z]{linkid};
213 $oResult = $oConnection->search_pqf($nquery);
216 warn " /CODE:", $@->code()," /MSG:",$@->message(),"\n";
218 my $count=$oResult->size() if ($oResult);
219 $finalresult[$z]{used}=$count;
222 # $oConnection->destroy();
224 return (\@finalresult, $nbresults);
227 # Creates the SQL Request
230 my ($dbh,$tags, $and_or, $operator, $value) = @_;
232 my $sql_tables; # will contain marc_subfield_table as m1,...
233 my $sql_where1; # will contain the "true" where
234 my $sql_where2 = "("; # will contain m1.authid=m2.authid
235 my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
236 my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR is provided.
239 for(my $i=0; $i<=@$value;$i++) {
244 $sql_tables = "auth_subfield_table as m$nb_table,";
245 $sql_where1 .= "( m$nb_table.subfieldvalue like '@$value[$i]' ";
247 $sql_where1 .=" and concat(m$nb_table.tag,m$nb_table.subfieldcode) IN (@$tags[$i])";
257 $sql_tables .= "auth_subfield_table as m$nb_table,";
258 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like '@$value[$i]' ";
260 $sql_where1 .=" and concat(m$nb_table.tag,m$nb_table.subfieldcode) IN (@$tags[$i])";
263 $sql_where2.="m1.authid=m$nb_table.authid and ";
270 if($sql_where2 ne "(") # some datas added to sql_where2, processing
272 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
275 else # no sql_where2 statement, deleting '('
279 chop $sql_tables; # deletes the trailing ','
281 return ($sql_tables, $sql_where1, $sql_where2);
285 sub AUTHcount_usage {
287 my $dbh = C4::Context->dbh;
288 # find MARC fields using this authtype
289 my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
290 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
291 my $tags_used=$sth->execute($authtypecode);
292 my $tags_using_authtype;
294 while (my($tagfield) = $sth->fetchrow){
295 # warn "TAG : $tagfield";
296 $tags_using_authtype.= "'".$tagfield."9',";
300 chop $tags_using_authtype;
301 ### try ZOOM search here
302 my $oConnection=C4::Context->Zconn("biblioserver");
305 $query= "\@attr GILS 1=2057 ".$authid;
307 my $oResult = $oConnection->search_pqf($query);
309 my $result=$oResult->size() if ($oResult);
312 # if ($tags_using_authtype) {
313 # $sth = $dbh->prepare("select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and MATCH(subfieldvalue) AGAINST(? IN BOOLEAN MODE)");
315 # $sth = $dbh->prepare("select count(*) from marc_subfield_table where subfieldvalue=?");
317 # warn "Q : select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and d MATCH(subfieldvalue) AGAINST($authid IN BOOLEAN MODE) ";
318 # $sth->execute($authid);
319 # my ($result) = $sth->fetchrow;
320 # warn "Authority $authid TOTAL USED : $result";
327 sub AUTHfind_authtypecode {
328 my ($dbh,$authid) = @_;
329 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
330 $sth->execute($authid);
331 my ($authtypecode) = $sth->fetchrow;
332 return $authtypecode;
337 my ($dbh,$forlibrarian,$authtypecode)= @_;
338 $authtypecode="" unless $authtypecode;
340 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
343 # check that authority exists
344 $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
345 $sth->execute($authtypecode);
346 my ($total) = $sth->fetchrow;
347 $authtypecode="" unless ($total >0);
349 "select tagfield,liblibrarian,libopac,mandatory,repeatable from auth_tag_structure where authtypecode=? order by tagfield"
352 $sth->execute($authtypecode);
353 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
355 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
356 $res->{$tag}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
357 $res->{$tab}->{tab} = ""; # XXX
358 $res->{$tag}->{mandatory} = $mandatory;
359 $res->{$tag}->{repeatable} = $repeatable;
361 $sth= $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link from auth_subfield_structure where authtypecode=? order by tagfield,tagsubfield"
363 $sth->execute($authtypecode);
366 my $authorised_value;
376 ( $tag, $subfield, $liblibrarian, , $libopac, $tab,
377 $mandatory, $repeatable, $authorised_value, $authtypecode,
378 $value_builder, $kohafield, $seealso, $hidden,
383 $res->{$tag}->{$subfield}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
384 $res->{$tag}->{$subfield}->{tab} = $tab;
385 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
386 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
387 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
388 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
389 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
390 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
391 $res->{$tag}->{$subfield}->{seealso} = $seealso;
392 $res->{$tag}->{$subfield}->{hidden} = $hidden;
393 $res->{$tag}->{$subfield}->{isurl} = $isurl;
394 $res->{$tag}->{$subfield}->{link} = $link;
399 sub AUTHaddauthority {
400 # pass the MARC::Record to this function, and it will create the records in the authority table
401 my ($dbh,$record,$authid,$authtypecode) = @_;
403 #my $leadercode=AUTHfind_leader($dbh,$authtypecode);
404 my $leader=' a ';##Fixme correct leader as this one just adds utf8 to MARC21
405 #substr($leader,8,1)=$leadercode;
406 # $record->leader($leader);
407 my ($authfield,$authidsubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.authid",$authtypecode);
408 my ($authfield2,$authtypesubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.authtypecode",$authtypecode);
409 my ($linkidfield,$linkidsubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.linkid",$authtypecode);
411 # if authid empty => true add, find a new authid number
413 my $sth=$dbh->prepare("select max(authid) from auth_header");
415 ($authid)=$sth->fetchrow;
418 ##Insert the recordID in MARC record
420 ##Both authid and authtypecode is expected to be in the same field. Modify if other requirements arise
421 $record->add_fields($authfield,'','',$authidsubfield=>$authid,$authtypesubfield=>$authtypecode);
423 $dbh->do("lock tables auth_header WRITE");
424 $sth=$dbh->prepare("insert into auth_header (authid,datecreated,authtypecode,marc) values (?,now(),?,?)");
425 $sth->execute($authid,$authtypecode,$record->as_usmarc);
429 ##Modified record reinsertid
430 $record->delete_field($authfield);
431 $record->add_fields($authfield,'','',$authidsubfield=>$authid,$authtypesubfield=>$authtypecode);
433 $dbh->do("lock tables auth_header WRITE");
434 my $sth=$dbh->prepare("update auth_header set marc=? where authid=?");
435 $sth->execute($record->as_usmarc,$authid);
438 $dbh->do("unlock tables");
439 zebraopauth($dbh,$authid,'specialUpdate');
441 if ($record->field($linkidfield)){
442 my @fields=$record->field($linkidfield);
444 foreach my $field (@fields){
445 my $linkid=$field->subfield($linkidsubfield) ;
447 ##Modify the record of linked
448 AUTHaddlink($dbh,$linkid,$authid);
456 my ($dbh,$linkid,$authid)=@_;
457 my $record=AUTHgetauthority($dbh,$linkid);
458 my $authtypecode=AUTHfind_authtypecode($dbh,$linkid);
459 #warn "adding l:$linkid,a:$authid,auth:$authtypecode";
460 $record=AUTH2marcOnefieldlink($dbh,$record,"auth_header.linkid",$authid,$authtypecode);
461 $dbh->do("lock tables auth_header WRITE");
462 my $sth=$dbh->prepare("update auth_header set marc=? where authid=?");
463 $sth->execute($record->as_usmarc,$linkid);
465 $dbh->do("unlock tables");
466 zebraopauth($dbh,$linkid,'specialUpdate');
469 sub AUTH2marcOnefieldlink {
470 my ( $dbh, $record, $kohafieldname, $newvalue,$authtypecode ) = @_;
471 my $sth = $dbh->prepare(
472 "select tagfield,tagsubfield from auth_subfield_structure where authtypecode=? and kohafield=?"
474 $sth->execute($authtypecode,$kohafieldname);
475 my ($tagfield,$tagsubfield)=$sth->fetchrow;
476 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $newvalue );
481 my ($dbh,$authid,$op)=@_;
486 $Zconnauthority=C4::Context->Zconnauth("authorityserver");
487 if ($Zconnauthority ne "error"){
488 my $record = AUTHgetauthority($dbh,$authid);
489 my $Zpackage = $Zconnauthority->package();
490 $Zpackage->option(action => $op);
491 $Zpackage->option(record => $record->as_xml_record);
494 $Zpackage->send("update");
497 if($@->code()==10007 && $tried==0){ ##Timedout -retry
500 }elsif($@->code()==10004 && $recon==0){##Lost connection -reconnect
504 warn "Error-authority updating $authid $op /CODE:", $@->code()," /MSG:",$@->message(),"\n";
505 zebrafiles($dbh,$authid,$op);
509 $Zpackage->("commit") if (C4::Context->shadow);
512 zebrafiles($dbh,$authid,$op);
518 my ($dbh,$authid,$folder)=@_;
519 my $record=AUTHgetauthority($dbh,$authid);
520 my $zebradir = C4::Context->zebraconfig("authorityserver")->{directory}."/".$folder."/";
522 #my $zebradir = C4::Context->authoritydir."/".$folder."/";
523 unless (opendir(DIR, "$zebradir")) {
524 warn "$zebradir not found";
528 my $filename = $zebradir.$authid;
530 open (OUTPUT,">", $filename.".xml");
531 print OUTPUT $record->as_xml_record;
541 ##Hard coded for NEU auth types
542 my($dbh,$authtypecode)=@_;
545 if ($authtypecode eq "AUTH"){
547 }elsif ($authtypecode eq "ESUB"){
549 }elsif ($authtypecode eq "TSUB"){
557 sub AUTHgetauthority {
558 # Returns MARC::Record of the biblio passed in parameter.
559 my ($dbh,$authid)=@_;
560 my $sth=$dbh->prepare("select marc from auth_header where authid=?");
561 $sth->execute($authid);
562 my ($marc) = $sth->fetchrow;
563 my $record=MARC::File::USMARC::decode($marc);
568 sub AUTHgetauth_type {
569 my ($authtypecode) = @_;
570 my $dbh=C4::Context->dbh;
571 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
572 $sth->execute($authtypecode);
573 return $sth->fetchrow_hashref;
575 sub AUTHmodauthority {
577 my ($dbh,$authid,$record,$authtypecode,$merge)=@_;
578 my ($oldrecord)=&AUTHgetauthority($dbh,$authid);
579 if ($oldrecord eq $record) {
582 my $sth=$dbh->prepare("update auth_header set marc=? where authid=?");
583 #warn find if linked records exist and delete them
584 my($linkidfield,$linkidsubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.linkid",$authtypecode);
586 if ($oldrecord->field($linkidfield)){
587 my @fields=$oldrecord->field($linkidfield);
588 foreach my $field (@fields){
589 my $linkid=$field->subfield($linkidsubfield) ;
591 ##Modify the record of linked
592 my $linkrecord=AUTHgetauthority($dbh,$linkid);
593 my $linktypecode=AUTHfind_authtypecode($dbh,$linkid);
594 my ( $linkidfield2,$linkidsubfield2)=AUTHfind_marc_from_kohafield($dbh,"auth_header.linkid",$linktypecode);
595 my @linkfields=$linkrecord->field($linkidfield2);
596 foreach my $linkfield (@linkfields){
597 if ($linkfield->subfield($linkidsubfield2) eq $authid){
598 $linkrecord->delete_field($linkfield);
599 $sth->execute($linkrecord->as_usmarc,$linkid);
600 zebraopauth($dbh,$linkid,'specialUpdate');
606 #Now rewrite the $record to table with an add
607 $authid=AUTHaddauthority($dbh,$record,$authid,$authtypecode);
609 ##Uncomment below and all biblios will get updated with modified authority-- To be used with $merge flag
610 # &merge($dbh,$authid,$record,$authid,$record);
614 sub AUTHdelauthority {
615 my ($dbh,$authid,$keep_biblio) = @_;
616 # if the keep_biblio is set to 1, then authority entries in biblio are preserved.
618 zebraopauth($dbh,$authid,"recordDelete");
619 $dbh->do("delete from auth_header where authid=$authid") ;
621 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag)
626 sub AUTHfind_authtypecode {
627 my ($dbh,$authid) = @_;
628 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
629 $sth->execute($authid);
630 my ($authtypecode) = $sth->fetchrow;
631 return $authtypecode;
637 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
639 my $record = MARC::Record->new();
640 #---- TODO : the leader is missing
642 # my %subfieldlist=();
643 my $prevvalue; # if tag <10
644 my $field; # if tag >=10
645 for (my $i=0; $i< @$rtags; $i++) {
646 # rebuild MARC::Record
647 if (@$rtags[$i] ne $prevtag) {
650 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
654 $record->add_fields($field);
657 $indicators{@$rtags[$i]}.=' ';
658 if (@$rtags[$i] <10) {
659 $prevvalue= @$rvalues[$i];
663 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
665 $prevtag = @$rtags[$i];
667 if (@$rtags[$i] <10) {
668 $prevvalue=@$rvalues[$i];
670 if (length(@$rvalues[$i])>0) {
671 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
674 $prevtag= @$rtags[$i];
677 # the last has not been included inside the loop... do it now !
678 $record->add_fields($field) if $field;
687 my ($record,$authtypecode)=@_;
688 # warn "IN for ".$record->as_formatted;
689 my $dbh = C4::Context->dbh;
690 # warn "".$record->as_formatted;
691 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
692 $sth->execute($authtypecode);
693 my ($auth_tag_to_report) = $sth->fetchrow;
695 # build a request for authoritysearch
696 my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
697 if ($record->field($auth_tag_to_report)) {
698 push @tags, $auth_tag_to_report;
701 push @operator, "all";
702 push @value, $record->field($auth_tag_to_report)->as_string();
705 my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
706 # there is at least 1 result => return the 1st one
708 return @$finalresult[0]->{authid},@$finalresult[0]->{summary};
710 # no result, returns nothing
715 ## give this a Marc record to return summary
716 my ($dbh,$record,$authid,$authtypecode)=@_;
718 # my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
719 my $authref = getauthtype($authtypecode);
720 my $summary = $authref->{summary};
721 my @fields = $record->fields();
722 # chop $tags_using_authtype;
723 # if the library has a summary defined, use it. Otherwise, build a standard one
725 my @fields = $record->fields();
726 foreach my $field (@fields) {
727 my $tag = $field->tag();
728 my $tagvalue = $field->as_string();
729 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
732 my @subf = $field->subfields;
733 for my $i (0..$#subf) {
734 my $subfieldcode = $subf[$i][0];
735 my $subfieldvalue = $subf[$i][1];
736 my $tagsubf = $tag.$subfieldcode;
737 $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
741 $summary =~ s/\[(.*?)]//g;
742 $summary =~ s/\n/<br>/g;
744 my $heading; # = $authref->{summary};
748 my @fields = $record->fields();
749 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
750 # construct UNIMARC summary, that is quite different from MARC21 one
752 foreach my $field ($record->field('2..')) {
753 $heading.= $field->as_string();
756 foreach my $field ($record->field('4..')) {
757 $summary.= " <i>".$field->as_string()."</i><br/>";
758 $summary.= " <i>see:</i> ".$heading."<br/>";
761 foreach my $field ($record->field('5..')) {
762 $summary.= " <i>".$field->as_string()."</i><br/>";
763 $summary.= " <i>see:</i> ".$heading."<br/>";
766 foreach my $field ($record->field('7..')) {
767 $seeheading.= " <i>see also:</i> ".$field->as_string()."<br />";
768 $altheading.= " ".$field->as_string()."<br />";
769 $altheading.= " <i>see also:</i> ".$heading."<br />";
771 $summary = "<b>".$heading."</b><br />".$seeheading.$altheading.$summary;
773 # construct MARC21 summary
774 foreach my $field ($record->field('1..')) {
775 if ($record->field('100')) {
776 $heading.= $field->as_string('abcdefghjklmnopqrstvxyz68');
777 } elsif ($record->field('110')) {
778 $heading.= $field->as_string('abcdefghklmnoprstvxyz68');
779 } elsif ($record->field('111')) {
780 $heading.= $field->as_string('acdefghklnpqstvxyz68');
781 } elsif ($record->field('130')) {
782 $heading.= $field->as_string('adfghklmnoprstvxyz68');
783 } elsif ($record->field('148')) {
784 $heading.= $field->as_string('abvxyz68');
785 } elsif ($record->field('150')) {
786 $heading.= $field->as_string('abvxyz68');
787 } elsif ($record->field('151')) {
788 $heading.= $field->as_string('avxyz68');
789 } elsif ($record->field('155')) {
790 $heading.= $field->as_string('abvxyz68');
791 } elsif ($record->field('180')) {
792 $heading.= $field->as_string('vxyz68');
793 } elsif ($record->field('181')) {
794 $heading.= $field->as_string('vxyz68');
795 } elsif ($record->field('182')) {
796 $heading.= $field->as_string('vxyz68');
797 } elsif ($record->field('185')) {
798 $heading.= $field->as_string('vxyz68');
800 $heading.= $field->as_string();
803 foreach my $field ($record->field('4..')) {
804 $seeheading.= " ".$field->as_string()."<br />";
805 $seeheading.= " <i>see:</i> ".$seeheading."<br />";
807 foreach my $field ($record->field('5..')) {
808 $altheading.= " <i>see also:</i> ".$field->as_string()."<br />";
809 $altheading.= " ".$field->as_string()."<br />";
810 $altheading.= " <i>see also:</i> ".$altheading."<br />";
812 $summary.=$heading.$seeheading.$altheading;
818 my ($dbh,$mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
819 my $authtypecodefrom = AUTHfind_authtypecode($dbh,$mergefrom);
820 my $authtypecodeto = AUTHfind_authtypecode($dbh,$mergeto);
821 # return if authority does not exist
822 my @X = $MARCfrom->fields();
824 my @X = $MARCto->fields();
828 # search the tag to report
829 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
830 $sth->execute($authtypecodefrom);
831 my ($auth_tag_to_report) = $sth->fetchrow;
834 @record_to = $MARCto->field($auth_tag_to_report)->subfields() if $MARCto->field($auth_tag_to_report);
836 @record_from = $MARCfrom->field($auth_tag_to_report)->subfields() if $MARCfrom->field($auth_tag_to_report);
838 # search all biblio tags using this authority.
839 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
840 $sth->execute($authtypecodefrom);
841 my @tags_using_authtype;
842 while (my ($tagfield) = $sth->fetchrow) {
843 push @tags_using_authtype,$tagfield."9" ;
846 # now, find every biblio using this authority
847 ### try ZOOM search here
848 my $oConnection=C4::Context->Zconn("biblioserver");
853 $query= "\@attr GILS 1=2057 ".$mergefrom;
855 my $oResult = $oConnection->search_pqf($query);
857 my $count=$oResult->size() if ($oResult);
860 while ( $z<$count ) {
864 $rec=$oResult->record($z);
867 my $marcdata = $rec->raw();
868 push @reccache, $marcdata;
872 foreach my $marc(@reccache){
876 $marcrecord = MARC::File::USMARC::decode($marc);
877 foreach my $tagfield (@tags_using_authtype){
878 $tagfield=substr($tagfield,0,3);
879 my @tags = $marcrecord->field($tagfield);
880 foreach my $tag (@tags){
881 my $tagsubs=$tag->subfield("9");
882 #warn "$tagfield:$tagsubs:$mergefrom";
883 if ($tagsubs== $mergefrom) {
885 $tag->update("9" =>$mergeto);
886 foreach my $subfield (@record_to) {
887 # warn "$subfield,$subfield->[0],$subfield->[1]";
888 $tag->update($subfield->[0] =>$subfield->[1]);
891 $marcrecord->delete_field($tag);
892 $marcrecord->add_fields($tag);
896 my $oldbiblio = MARCmarc2koha($dbh,$marcrecord,"") ;
898 &NEWmodbiblio($dbh,$marcrecord,$oldbiblio->{'biblionumber'},undef,"0000") ;
903 END { } # module clean-up code here (global destructor)
909 Koha Developement team <info@koha.org>
911 Paul POULAIN paul.poulain@free.fr
917 # Revision 1.25 2006/05/19 18:09:39 tgarip1957
918 # All support for auth_subfield_tables is removed. All search is now with zebra authorities. New authority structure allows multiple linking of authorities of differnet types to one another.
919 # Authority tables are modified to be compatible with new MARC frameworks. This change is part of Authority Linking & Zebra authorities. Requires change in Mysql database. It will break head unless all changes regarding this is implemented. This warning will take place on all commits regarding this
921 # Revision 1.9.2.6 2005/06/07 10:02:00 tipaul
922 # porting dictionnary search from head to 2.2. there is now a ... facing titles, author & subject, to search in biblio & authorities existing values.
924 # Revision 1.9.2.5 2005/05/31 14:50:46 tipaul
925 # fix for authority merging. There was a bug on official installs
927 # Revision 1.9.2.4 2005/05/30 11:24:15 tipaul
928 # fixing a bug : when a field was repeated, the last field was also repeated. (Was due to the "empty" field in html between fields : to separate fields, in html, an empty field is automatically added. in AUTHhtml2marc, this empty field was not discarded correctly)
930 # Revision 1.9.2.3 2005/04/28 08:45:33 tipaul
931 # porting FindDuplicate feature for authorities from HEAD to rel_2_2, works correctly now.
933 # Revision 1.9.2.2 2005/02/28 14:03:13 tipaul
934 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
935 # * adding a select box to requet "contain" or "begin with" search.
936 # * fixing some bug in authority search (related to "main entry" search)
938 # Revision 1.9.2.1 2005/02/24 13:12:13 tipaul
939 # saving authority modif in a text file. This will be used soon with another script (in crontab). The script in crontab will retrieve every authorityid in the directory localfile/authorities and modify every biblio using this authority. Those modifs may be long. So they can't be done through http, because we may encounter a webserver timeout, and kill the process before end of the job.
940 # So, it will be done through a cron job.
941 # (/me agree we need some doc for command line scripts)
943 # Revision 1.9 2004/12/23 09:48:11 tipaul
944 # Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
946 # Revision 1.8 2004/11/05 10:11:39 tipaul
947 # export auth_count_usage (bugfix)
949 # Revision 1.7 2004/09/23 16:13:00 tipaul
950 # Bugfix in modification
952 # Revision 1.6 2004/08/18 16:00:24 tipaul
953 # fixes for authorities management
955 # Revision 1.5 2004/07/05 13:37:22 doxulting
956 # First step for working authorities
958 # Revision 1.4 2004/06/22 11:35:37 tipaul
959 # removing % at the beginning of a string to avoid loooonnnngggg searchs
961 # Revision 1.3 2004/06/17 08:02:13 tipaul
962 # merging tag & subfield in auth_word for better perfs
964 # Revision 1.2 2004/06/10 08:29:01 tipaul
965 # MARC authority management (continued)
967 # Revision 1.1 2004/06/07 07:35:01 tipaul
968 # MARC authority management package