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->authorityservershadow);
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);
610 ### If a library thinks that updating all biblios is a long process and wishes to leave that to a cron job to use merge_authotities.p
611 ### they should have a system preference "dontmerge=1" otherwise by default biblios will be updated
612 ### the $merge flag is now depreceated and will be removed at code cleaning
614 if (C4::Context->preference('dontmerge')){
615 # save the file in localfile/modified_authorities
616 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
617 unless (opendir(DIR, "$cgidir")) {
618 $cgidir = C4::Context->intranetdir."/";
621 my $filename = $cgidir."/localfile/modified_authorities/$authid.authid";
622 open AUTH, "> $filename";
626 &merge($dbh,$authid,$record,$authid,$record);
631 sub AUTHdelauthority {
632 my ($dbh,$authid,$keep_biblio) = @_;
633 # if the keep_biblio is set to 1, then authority entries in biblio are preserved.
635 zebraopauth($dbh,$authid,"recordDelete");
636 $dbh->do("delete from auth_header where authid=$authid") ;
638 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag)
643 sub AUTHfind_authtypecode {
644 my ($dbh,$authid) = @_;
645 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
646 $sth->execute($authid);
647 my ($authtypecode) = $sth->fetchrow;
648 return $authtypecode;
654 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
656 my $record = MARC::Record->new();
657 #---- TODO : the leader is missing
659 # my %subfieldlist=();
660 my $prevvalue; # if tag <10
661 my $field; # if tag >=10
662 for (my $i=0; $i< @$rtags; $i++) {
663 # rebuild MARC::Record
664 if (@$rtags[$i] ne $prevtag) {
667 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
671 $record->add_fields($field);
674 $indicators{@$rtags[$i]}.=' ';
675 if (@$rtags[$i] <10) {
676 $prevvalue= @$rvalues[$i];
680 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
682 $prevtag = @$rtags[$i];
684 if (@$rtags[$i] <10) {
685 $prevvalue=@$rvalues[$i];
687 if (length(@$rvalues[$i])>0) {
688 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
691 $prevtag= @$rtags[$i];
694 # the last has not been included inside the loop... do it now !
695 $record->add_fields($field) if $field;
704 my ($record,$authtypecode)=@_;
705 # warn "IN for ".$record->as_formatted;
706 my $dbh = C4::Context->dbh;
707 # warn "".$record->as_formatted;
708 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
709 $sth->execute($authtypecode);
710 my ($auth_tag_to_report) = $sth->fetchrow;
712 # build a request for authoritysearch
713 my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
714 if ($record->field($auth_tag_to_report)) {
715 push @tags, $auth_tag_to_report;
718 push @operator, "all";
719 push @value, $record->field($auth_tag_to_report)->as_string();
722 my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
723 # there is at least 1 result => return the 1st one
725 return @$finalresult[0]->{authid},@$finalresult[0]->{summary};
727 # no result, returns nothing
732 ## give this a Marc record to return summary
733 my ($dbh,$record,$authid,$authtypecode)=@_;
735 # my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
736 my $authref = getauthtype($authtypecode);
737 my $summary = $authref->{summary};
738 my @fields = $record->fields();
739 # chop $tags_using_authtype;
740 # if the library has a summary defined, use it. Otherwise, build a standard one
742 my @fields = $record->fields();
743 foreach my $field (@fields) {
744 my $tag = $field->tag();
745 my $tagvalue = $field->as_string();
746 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
749 my @subf = $field->subfields;
750 for my $i (0..$#subf) {
751 my $subfieldcode = $subf[$i][0];
752 my $subfieldvalue = $subf[$i][1];
753 my $tagsubf = $tag.$subfieldcode;
754 $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
758 $summary =~ s/\[(.*?)]//g;
759 $summary =~ s/\n/<br>/g;
761 my $heading; # = $authref->{summary};
765 my @fields = $record->fields();
766 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
767 # construct UNIMARC summary, that is quite different from MARC21 one
769 foreach my $field ($record->field('2..')) {
770 $heading.= $field->as_string();
773 foreach my $field ($record->field('4..')) {
774 $summary.= " <i>".$field->as_string()."</i><br/>";
775 $summary.= " <i>see:</i> ".$heading."<br/>";
778 foreach my $field ($record->field('5..')) {
779 $summary.= " <i>".$field->as_string()."</i><br/>";
780 $summary.= " <i>see:</i> ".$heading."<br/>";
783 foreach my $field ($record->field('7..')) {
784 $seeheading.= " <i>see also:</i> ".$field->as_string()."<br />";
785 $altheading.= " ".$field->as_string()."<br />";
786 $altheading.= " <i>see also:</i> ".$heading."<br />";
788 $summary = "<b>".$heading."</b><br />".$seeheading.$altheading.$summary;
790 # construct MARC21 summary
791 foreach my $field ($record->field('1..')) {
792 if ($record->field('100')) {
793 $heading.= $field->as_string('abcdefghjklmnopqrstvxyz68');
794 } elsif ($record->field('110')) {
795 $heading.= $field->as_string('abcdefghklmnoprstvxyz68');
796 } elsif ($record->field('111')) {
797 $heading.= $field->as_string('acdefghklnpqstvxyz68');
798 } elsif ($record->field('130')) {
799 $heading.= $field->as_string('adfghklmnoprstvxyz68');
800 } elsif ($record->field('148')) {
801 $heading.= $field->as_string('abvxyz68');
802 } elsif ($record->field('150')) {
803 $heading.= $field->as_string('abvxyz68');
804 } elsif ($record->field('151')) {
805 $heading.= $field->as_string('avxyz68');
806 } elsif ($record->field('155')) {
807 $heading.= $field->as_string('abvxyz68');
808 } elsif ($record->field('180')) {
809 $heading.= $field->as_string('vxyz68');
810 } elsif ($record->field('181')) {
811 $heading.= $field->as_string('vxyz68');
812 } elsif ($record->field('182')) {
813 $heading.= $field->as_string('vxyz68');
814 } elsif ($record->field('185')) {
815 $heading.= $field->as_string('vxyz68');
817 $heading.= $field->as_string();
820 foreach my $field ($record->field('4..')) {
821 $seeheading.= " ".$field->as_string()."<br />";
822 $seeheading.= " <i>see:</i> ".$seeheading."<br />";
824 foreach my $field ($record->field('5..')) {
825 $altheading.= " <i>see also:</i> ".$field->as_string()."<br />";
826 $altheading.= " ".$field->as_string()."<br />";
827 $altheading.= " <i>see also:</i> ".$altheading."<br />";
829 $summary.=$heading.$seeheading.$altheading;
835 my ($dbh,$mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
836 my $authtypecodefrom = AUTHfind_authtypecode($dbh,$mergefrom);
837 my $authtypecodeto = AUTHfind_authtypecode($dbh,$mergeto);
838 # return if authority does not exist
839 my @X = $MARCfrom->fields();
841 my @X = $MARCto->fields();
845 # search the tag to report
846 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
847 $sth->execute($authtypecodefrom);
848 my ($auth_tag_to_report) = $sth->fetchrow;
851 @record_to = $MARCto->field($auth_tag_to_report)->subfields() if $MARCto->field($auth_tag_to_report);
853 @record_from = $MARCfrom->field($auth_tag_to_report)->subfields() if $MARCfrom->field($auth_tag_to_report);
855 # search all biblio tags using this authority.
856 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
857 $sth->execute($authtypecodefrom);
858 my @tags_using_authtype;
859 while (my ($tagfield) = $sth->fetchrow) {
860 push @tags_using_authtype,$tagfield."9" ;
863 # now, find every biblio using this authority
864 ### try ZOOM search here
865 my $oConnection=C4::Context->Zconn("biblioserver");
870 $query= "\@attr GILS 1=2057 ".$mergefrom;
872 my $oResult = $oConnection->search_pqf($query);
874 my $count=$oResult->size() if ($oResult);
877 while ( $z<$count ) {
881 $rec=$oResult->record($z);
884 my $marcdata = $rec->raw();
885 push @reccache, $marcdata;
889 foreach my $marc(@reccache){
893 $marcrecord = MARC::File::USMARC::decode($marc);
894 foreach my $tagfield (@tags_using_authtype){
895 $tagfield=substr($tagfield,0,3);
896 my @tags = $marcrecord->field($tagfield);
897 foreach my $tag (@tags){
898 my $tagsubs=$tag->subfield("9");
899 #warn "$tagfield:$tagsubs:$mergefrom";
900 if ($tagsubs== $mergefrom) {
902 $tag->update("9" =>$mergeto);
903 foreach my $subfield (@record_to) {
904 # warn "$subfield,$subfield->[0],$subfield->[1]";
905 $tag->update($subfield->[0] =>$subfield->[1]);
908 $marcrecord->delete_field($tag);
909 $marcrecord->add_fields($tag);
913 my $oldbiblio = MARCmarc2koha($dbh,$marcrecord,"") ;
915 &NEWmodbiblio($dbh,$marcrecord,$oldbiblio->{'biblionumber'},undef,"0000") ;
920 END { } # module clean-up code here (global destructor)
926 Koha Developement team <info@koha.org>
928 Paul POULAIN paul.poulain@free.fr
934 # Revision 1.26 2006/05/20 14:32:54 tgarip1957
935 # If an authority is modified biblios related to this authority were not updated but a list of modified authorities was written to disk. Now by defult they get modified as well unless a system preference 'dontmerge' is defined. dontmerge=1 will keep the previous behaviour.
937 # Authority zebra server may have different shadow settings. Support is added
939 # Revision 1.25 2006/05/19 18:09:39 tgarip1957
940 # 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.
941 # 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
943 # Revision 1.9.2.6 2005/06/07 10:02:00 tipaul
944 # porting dictionnary search from head to 2.2. there is now a ... facing titles, author & subject, to search in biblio & authorities existing values.
946 # Revision 1.9.2.5 2005/05/31 14:50:46 tipaul
947 # fix for authority merging. There was a bug on official installs
949 # Revision 1.9.2.4 2005/05/30 11:24:15 tipaul
950 # 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)
952 # Revision 1.9.2.3 2005/04/28 08:45:33 tipaul
953 # porting FindDuplicate feature for authorities from HEAD to rel_2_2, works correctly now.
955 # Revision 1.9.2.2 2005/02/28 14:03:13 tipaul
956 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
957 # * adding a select box to requet "contain" or "begin with" search.
958 # * fixing some bug in authority search (related to "main entry" search)
960 # Revision 1.9.2.1 2005/02/24 13:12:13 tipaul
961 # 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.
962 # So, it will be done through a cron job.
963 # (/me agree we need some doc for command line scripts)
965 # Revision 1.9 2004/12/23 09:48:11 tipaul
966 # Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
968 # Revision 1.8 2004/11/05 10:11:39 tipaul
969 # export auth_count_usage (bugfix)
971 # Revision 1.7 2004/09/23 16:13:00 tipaul
972 # Bugfix in modification
974 # Revision 1.6 2004/08/18 16:00:24 tipaul
975 # fixes for authorities management
977 # Revision 1.5 2004/07/05 13:37:22 doxulting
978 # First step for working authorities
980 # Revision 1.4 2004/06/22 11:35:37 tipaul
981 # removing % at the beginning of a string to avoid loooonnnngggg searchs
983 # Revision 1.3 2004/06/17 08:02:13 tipaul
984 # merging tag & subfield in auth_word for better perfs
986 # Revision 1.2 2004/06/10 08:29:01 tipaul
987 # MARC authority management (continued)
989 # Revision 1.1 2004/06/07 07:35:01 tipaul
990 # MARC authority management package