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
51 &BuildUnimarcHierarchies
52 &BuildUnimarcHierarchy
57 sub AUTHfind_marc_from_kohafield {
58 my ( $kohafield,$authtypecode ) = @_;
59 my $dbh=C4::Context->dbh;
60 return 0, 0 unless $kohafield;
61 $authtypecode="" unless $authtypecode;
62 my $marcfromkohafield;
63 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
64 $sth->execute($kohafield,$authtypecode);
65 my ($tagfield,$tagsubfield) = $sth->fetchrow;
67 return ($tagfield,$tagsubfield);
70 my ($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby) = @_;
71 my $dbh=C4::Context->dbh;
74 # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
75 # the authtypecode. Then, search on $a of this tag_to_report
76 # also store main entry MARC tag, to extract it at end of search
78 ##first set the authtype search and may be multiple authorities
81 my @auths=split / /,$authtypecode ;
82 foreach my $auth (@auths){
83 $query .=" \@attr 1=Authority/format-id \@attr 5=100 ".$auth; ##No truncation on authtype
84 push @authtypecode ,$auth;
88 $query= "\@or ".$query;
94 for(my $i = 0 ; $i <= $#{$value} ; $i++)
97 ##If mainentry search $a tag
98 if (@$tags[$i] eq "mainmainentry") {
99 $attr =" \@attr 1=Heading ";
100 }elsif (@$tags[$i] eq "mainentry") {
101 $attr =" \@attr 1=Heading-Entity ";
103 $attr =" \@attr 1=Any ";
105 if (@$operator[$i] eq 'is') {
106 $attr.=" \@attr 4=1 \@attr 5=100 ";##Phrase, No truncation,all of subfield field must match
107 }elsif (@$operator[$i] eq "="){
108 $attr.=" \@attr 4=107 "; #Number Exact match
109 }elsif (@$operator[$i] eq "start"){
110 $attr.=" \@attr 4=1 \@attr 5=1 ";#Phrase, Right truncated
112 $attr .=" \@attr 5=1 ";## Word list, right truncated, anywhere
115 $attr =$attr."\"".@$value[$i]."\"";
120 ##Add how many queries generated
121 $query= $and.$query.$q2;
122 $query=' @or @attr 7=1 @attr 1=Heading 0 '.$query if ($sortby eq "HeadingAsc");
123 $query=' @or @attr 7=2 @attr 1=Heading 0 '.$query if ($sortby eq "HeadingDsc");
126 $offset=0 unless $offset;
127 my $counter = $offset;
128 $length=10 unless $length;
131 $oAuth[0]=C4::Context->Zconn("authorityserver" , 1);
132 my $Anewq= new ZOOM::Query::PQF($query,$oAuth[0]);
133 # $Anewq->sortby("1=Heading i< 1=Heading-Entity i< ");
134 # $Anewq->sortby("1=Heading i< 1=Heading-Entity i< ");
136 $oAResult= $oAuth[0]->search($Anewq) ;
137 while (($i = ZOOM::event(\@oAuth)) != 0) {
138 my $ev = $oAuth[$i-1]->last_event();
139 # warn("Authority ", $i-1, ": event $ev (", ZOOM::event_str($ev), ")\n");
140 last if $ev == ZOOM::Event::ZEND;
142 my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
144 warn "oAuth error: $errmsg ($error) $addinfo $diagset\n";
149 $nbresults=$oAResult->size();
150 my $nremains=$nbresults;
152 my @finalresult = ();
156 ##Find authid and linkid fields
157 ##we may be searching multiple authoritytypes.
158 ## FIXME this assumes that all authid and linkid fields are the same for all authority types
159 # my ($authidfield,$authidsubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.authid",$authtypecode[0]);
160 # my ($linkidfield,$linkidsubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.linkid",$authtypecode[0]);
161 while (($counter < $nbresults) && ($counter < ($offset + $length))) {
163 ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
164 my $rec=$oAResult->record($counter);
165 my $marcdata=$rec->raw();
169 my $separator=C4::Context->preference('authoritysep');
170 my $linksummary=" ".$separator;
172 $authrecord = MARC::File::USMARC::decode($marcdata);
174 my $authid=$authrecord->field('001')->data();
175 # if ($authrecord->field($linkidfield)){
176 # my @fields=$authrecord->field($linkidfield);
178 # # foreach my $field (@fields){
179 # # # $linkid=$field->subfield($linkidsubfield) ;
180 # # # if ($linkid){ ##There is a linked record add fields to produce summary
181 # # # my $linktype=AUTHfind_authtypecode($dbh,$linkid);
182 # # # my $linkrecord=AUTHgetauthority($dbh,$linkid);
183 # # # $linksummary.="<br> <a href='detail.pl?authid=$linkid'>".getsummary($dbh,$linkrecord,$linkid,$linktype).".</a>".$separator;
188 my $summary=getsummary($authrecord,$authid,$authtypecode);
189 # $summary="<a href='detail.pl?authid=$authid'>".$summary.".</a>" if ($intranet);
190 # $summary="<a href='detail.pl?authid=$authid'>".$summary.".</a>" if ($intranet);
191 # if ($linkid && $linksummary ne " ".$separator){
192 # $summary="<b>".$summary."</b>".$linksummary;
194 my $query_auth_tag = "SELECT auth_tag_to_report FROM auth_types WHERE authtypecode=?";
195 my $sth = $dbh->prepare($query_auth_tag);
196 $sth->execute($authtypecode);
197 my $auth_tag_to_report = $sth->fetchrow;
199 $newline{summary} = $summary;
200 $newline{authid} = $authid;
201 # $newline{linkid} = $linkid;
202 # $newline{reported_tag} = $reported_tag;
204 # $newline{biblio_fields} = $tags_using_authtype;
205 $newline{even} = $counter % 2;
207 push @finalresult, \%newline;
210 for (my $z=0; $z<@finalresult; $z++){
211 my $count=AUTHcount_usage($finalresult[$z]{authid});
212 $finalresult[$z]{used}=$count;
217 # $oAResult->destroy();
218 # $oAuth[0]->destroy();
220 return (\@finalresult, $nbresults);
223 # Creates the SQL Request
226 my ($tags, $and_or, $operator, $value) = @_;
227 my $dbh=C4::Context->dbh;
229 my $sql_tables; # will contain marc_subfield_table as m1,...
230 my $sql_where1; # will contain the "true" where
231 my $sql_where2 = "("; # will contain m1.authid=m2.authid
232 my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
233 my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR is provided.
235 for(my $i=0; $i<=@$value;$i++) {
239 $sql_tables = "auth_subfield_table as m$nb_table,";
240 $sql_where1 .= "( m$nb_table.subfieldvalue like '@$value[$i]' ";
242 $sql_where1 .=" and concat(m$nb_table.tag,m$nb_table.subfieldcode) IN (@$tags[$i])";
247 $sql_tables .= "auth_subfield_table as m$nb_table,";
248 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like '@$value[$i]' ";
250 $sql_where1 .=" and concat(m$nb_table.tag,m$nb_table.subfieldcode) IN (@$tags[$i])";
253 $sql_where2.="m1.authid=m$nb_table.authid and ";
258 if($sql_where2 ne "(") # some datas added to sql_where2, processing
260 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
263 else # no sql_where2 statement, deleting '('
267 chop $sql_tables; # deletes the trailing ','
269 return ($sql_tables, $sql_where1, $sql_where2);
273 sub AUTHcount_usage {
275 ### try ZOOM search here
276 my $oConnection=C4::Context->Zconn("biblioserver",1);
278 $query= "an=".$authid;
280 my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
282 while ((my $i = ZOOM::event([ $oConnection ])) != 0) {
283 my $ev = $oConnection->last_event();
284 if ($ev == ZOOM::Event::ZEND) {
285 $result = $oResult->size();
293 sub AUTHfind_authtypecode {
295 my $dbh=C4::Context->dbh;
296 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
297 $sth->execute($authid);
298 my ($authtypecode) = $sth->fetchrow;
299 return $authtypecode;
304 my ($forlibrarian,$authtypecode)= @_;
305 my $dbh=C4::Context->dbh;
306 $authtypecode="" unless $authtypecode;
308 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
311 # check that authority exists
312 $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
313 $sth->execute($authtypecode);
314 my ($total) = $sth->fetchrow;
315 $authtypecode="" unless ($total >0);
317 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
318 FROM auth_tag_structure
323 $sth->execute($authtypecode);
324 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
326 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
327 $res->{$tag}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
328 $res->{$tag}->{tab} = " "; # XXX
329 $res->{$tag}->{mandatory} = $mandatory;
330 $res->{$tag}->{repeatable} = $repeatable;
333 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl
334 FROM auth_subfield_structure
336 ORDER BY tagfield,tagsubfield"
338 $sth->execute($authtypecode);
341 my $authorised_value;
350 ( $tag, $subfield, $liblibrarian, , $libopac, $tab,
351 $mandatory, $repeatable, $authorised_value, $authtypecode,
352 $value_builder, $kohafield, $seealso, $hidden,
357 $res->{$tag}->{$subfield}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
358 $res->{$tag}->{$subfield}->{tab} = $tab;
359 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
360 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
361 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
362 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
363 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
364 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
365 $res->{$tag}->{$subfield}->{seealso} = $seealso;
366 $res->{$tag}->{$subfield}->{hidden} = $hidden;
367 $res->{$tag}->{$subfield}->{isurl} = $isurl;
368 $res->{$tag}->{$subfield}->{link} = $link;
373 sub AUTHaddauthority {
374 # pass the MARC::Record to this function, and it will create the records in the authority table
375 my ($record,$authid,$authtypecode) = @_;
376 my $dbh=C4::Context->dbh;
377 #my $leadercode=AUTHfind_leader($dbh,$authtypecode);
378 my $leader=' a ';##Fixme correct leader as this one just adds utf8 to MARC21
379 #substr($leader,8,1)=$leadercode;
380 # $record->leader($leader);
381 # my ($authfield,$authidsubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.authid",$authtypecode);
382 # my ($authfield2,$authtypesubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.authtypecode",$authtypecode);
383 # my ($linkidfield,$linkidsubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.linkid",$authtypecode);
385 # if authid empty => true add, find a new authid number
387 my $sth=$dbh->prepare("select max(authid) from auth_header");
389 ($authid)=$sth->fetchrow;
391 ##Insert the recordID in MARC record
392 ##Both authid and authtypecode is expected to be in the same field. Modify if other requirements arise
393 $record->add_fields('001',$authid) unless $record->field('001');
394 $record->add_fields('152','','','b'=>$authtypecode) unless $record->field('152');
395 # $record->add_fields('100','','','b'=>$authtypecode);
396 warn $record->as_formatted;
397 $dbh->do("lock tables auth_header WRITE");
398 $sth=$dbh->prepare("insert into auth_header (authid,datecreated,authtypecode,marc) values (?,now(),?,?)");
399 $sth->execute($authid,$authtypecode,$record->as_usmarc);
403 ##Modified record reinsertid
404 # my $idfield=$record->field('001');
405 # $record->delete_field($idfield);
406 $record->add_fields('001',$authid) unless ($record->field('001'));
407 $record->add_fields('152','','','b'=>$authtypecode) unless ($record->field('152'));
408 # $record->add_fields($authfield,$authid);
409 # $record->add_fields($authfield2,'','',$authtypesubfield=>$authtypecode);
410 warn $record->as_formatted;
411 $dbh->do("lock tables auth_header WRITE");
412 my $sth=$dbh->prepare("update auth_header set marc=? where authid=?");
413 $sth->execute($record->as_usmarc,$authid);
416 $dbh->do("unlock tables");
417 ModZebra($authid,'specialUpdate',"authorityserver");
419 # if ($record->field($linkidfield)){
420 # my @fields=$record->field($linkidfield);
422 # foreach my $field (@fields){
423 # my $linkid=$field->subfield($linkidsubfield) ;
425 # ##Modify the record of linked
426 # AUTHaddlink($dbh,$linkid,$authid);
434 my ($linkid,$authid)=@_;
435 my $dbh=C4::Context->dbh;
436 my $record=AUTHgetauthority($linkid);
437 my $authtypecode=AUTHfind_authtypecode($linkid);
438 #warn "adding l:$linkid,a:$authid,auth:$authtypecode";
439 $record=AUTH2marcOnefieldlink($record,"auth_header.linkid",$authid,$authtypecode);
440 $dbh->do("lock tables auth_header WRITE");
441 my $sth=$dbh->prepare("update auth_header set marc=? where authid=?");
442 $sth->execute($record->as_usmarc,$linkid);
444 $dbh->do("unlock tables");
445 ModZebra($linkid,'specialUpdate',"authorityserver");
448 sub AUTH2marcOnefieldlink {
449 my ( $record, $kohafieldname, $newvalue,$authtypecode ) = @_;
450 my $dbh=C4::Context->dbh;
451 my $sth = $dbh->prepare(
452 "select tagfield,tagsubfield from auth_subfield_structure where authtypecode=? and kohafield=?"
454 $sth->execute($authtypecode,$kohafieldname);
455 my ($tagfield,$tagsubfield)=$sth->fetchrow;
456 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $newvalue );
460 sub XMLgetauthority {
462 # Returns MARC::XML of the authority passed in parameter.
464 my $dbh=C4::Context->dbh;
466 $dbh->prepare("select marc from auth_header where authid=? " );
467 $sth->execute($authid);
468 my ($marc)=$sth->fetchrow;
469 $marc=MARC::File::USMARC::decode($marc);
470 my $marcxml=$marc->as_xml_record();
477 ##Hard coded for NEU auth types
478 my($authtypecode)=@_;
481 if ($authtypecode eq "AUTH"){
483 }elsif ($authtypecode eq "ESUB"){
485 }elsif ($authtypecode eq "TSUB"){
493 sub AUTHgetauthority {
494 # Returns MARC::Record of the biblio passed in parameter.
496 my $dbh=C4::Context->dbh;
497 my $sth=$dbh->prepare("select marc from auth_header where authid=?");
498 $sth->execute($authid);
499 my ($marc) = $sth->fetchrow;
500 my $record=MARC::File::USMARC::decode($marc);
505 sub AUTHgetauth_type {
506 my ($authtypecode) = @_;
507 my $dbh=C4::Context->dbh;
508 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
509 $sth->execute($authtypecode);
510 return $sth->fetchrow_hashref;
512 sub AUTHmodauthority {
514 my ($authid,$record,$authtypecode,$merge)=@_;
515 my $dbh=C4::Context->dbh;
516 my ($oldrecord)=&AUTHgetauthority($authid);
517 if ($oldrecord eq $record) {
520 my $sth=$dbh->prepare("update auth_header set marc=? where authid=?");
521 #warn find if linked records exist and delete them
522 my($linkidfield,$linkidsubfield)=AUTHfind_marc_from_kohafield("auth_header.linkid",$authtypecode);
524 if ($oldrecord->field($linkidfield)){
525 my @fields=$oldrecord->field($linkidfield);
526 foreach my $field (@fields){
527 my $linkid=$field->subfield($linkidsubfield) ;
529 ##Modify the record of linked
530 my $linkrecord=AUTHgetauthority($linkid);
531 my $linktypecode=AUTHfind_authtypecode($linkid);
532 my ( $linkidfield2,$linkidsubfield2)=AUTHfind_marc_from_kohafield("auth_header.linkid",$linktypecode);
533 my @linkfields=$linkrecord->field($linkidfield2);
534 foreach my $linkfield (@linkfields){
535 if ($linkfield->subfield($linkidsubfield2) eq $authid){
536 $linkrecord->delete_field($linkfield);
537 $sth->execute($linkrecord->as_usmarc,$linkid);
538 ModZebra($linkid,'specialUpdate',"authorityserver");
544 #Now rewrite the $record to table with an add
545 $authid=AUTHaddauthority($record,$authid,$authtypecode);
548 ### 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
549 ### they should have a system preference "dontmerge=1" otherwise by default biblios will be updated
550 ### the $merge flag is now depreceated and will be removed at code cleaning
552 if (C4::Context->preference('dontmerge') ){
553 # save the file in localfile/modified_authorities
554 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
555 unless (opendir(DIR,"$cgidir")) {
556 $cgidir = C4::Context->intranetdir."/";
559 my $filename = $cgidir."/localfile/modified_authorities/$authid.authid";
560 open AUTH, "> $filename";
564 &merge($authid,$record,$authid,$record);
569 sub AUTHdelauthority {
570 my ($authid,$keep_biblio) = @_;
571 my $dbh=C4::Context->dbh;
572 # if the keep_biblio is set to 1, then authority entries in biblio are preserved.
574 ModZebra($authid,"recordDelete","authorityserver");
575 $dbh->do("delete from auth_header where authid=$authid") ;
577 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag)
581 my ($rtags,$rsubfields,$rvalues,%indicators) = @_;
582 my $dbh=C4::Context->dbh;
584 my $record = MARC::Record->new();
585 #---- TODO : the leader is missing
587 # my %subfieldlist=();
588 my $prevvalue; # if tag <10
589 my $field; # if tag >=10
590 for (my $i=0; $i< @$rtags; $i++) {
591 # rebuild MARC::Record
592 if (@$rtags[$i] ne $prevtag) {
595 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
599 $record->add_fields($field);
602 $indicators{@$rtags[$i]}.=' ';
603 if (@$rtags[$i] <10) {
604 $prevvalue= @$rvalues[$i];
608 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
610 $prevtag = @$rtags[$i];
612 if (@$rtags[$i] <10) {
613 $prevvalue=@$rvalues[$i];
615 if (length(@$rvalues[$i])>0) {
616 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
619 $prevtag= @$rtags[$i];
622 # the last has not been included inside the loop... do it now !
623 $record->add_fields($field) if $field;
631 my ($record,$authtypecode)=@_;
632 # warn "IN for ".$record->as_formatted;
633 my $dbh = C4::Context->dbh;
634 # warn "".$record->as_formatted;
635 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
636 $sth->execute($authtypecode);
637 my ($auth_tag_to_report) = $sth->fetchrow;
639 # warn "record :".$record->as_formatted." authtattoreport :$auth_tag_to_report";
640 # build a request for authoritysearch
641 my $query='at='.$authtypecode.' ';
642 map {$query.= " and he=\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/)} $record->field($auth_tag_to_report)->subfields() if $record->field($auth_tag_to_report);
643 my ($error,$results)=SimpleSearch($query,"authorityserver");
644 # there is at least 1 result => return the 1st one
646 my $marcrecord = MARC::File::USMARC::decode($results->[0]);
647 return $marcrecord->field('001')->data,getsummary($marcrecord,$marcrecord->field('001')->data,$authtypecode);
649 # no result, returns nothing
654 ## give this a Marc record to return summary
655 my ($record,$authid,$authtypecode)=@_;
657 my $dbh=C4::Context->dbh;
658 # my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
659 my $authref = getauthtype($authtypecode);
660 my $summary = $authref->{summary};
661 my @fields = $record->fields();
662 # chop $tags_using_authtype; # FIXME: why commented out?
665 # if the library has a summary defined, use it. Otherwise, build a standard one
667 my @fields = $record->fields();
668 # $reported_tag = '$9'.$result[$counter];
669 foreach my $field (@fields) {
670 my $tag = $field->tag();
671 my $tagvalue = $field->as_string();
672 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
675 $reported_tag.='$3'.$field->data();
679 my @subf = $field->subfields;
680 for my $i (0..$#subf) {
681 my $subfieldcode = $subf[$i][0];
682 my $subfieldvalue = $subf[$i][1];
683 my $tagsubf = $tag.$subfieldcode;
684 $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
685 # if ($tag eq $auth_tag_to_report) {
686 # $reported_tag.='$'.$subfieldcode.$subfieldvalue;
691 $summary =~ s/\[(.*?)]//g;
692 $summary =~ s/\n/<br>/g;
694 my $heading; # = $authref->{summary};
698 my @fields = $record->fields();
699 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
700 # construct UNIMARC summary, that is quite different from MARC21 one
702 foreach my $field ($record->field('2..')) {
703 $heading.= $field->as_string();
706 foreach my $field ($record->field('4..')) {
707 $summary.= " <i>".$field->as_string()."</i><br/>";
708 $summary.= " <i>see:</i> ".$heading."<br/>";
711 foreach my $field ($record->field('5..')) {
712 $summary.= " <i>".$field->as_string()."</i><br/>";
713 $summary.= " <i>see:</i> ".$heading."<br/>";
716 foreach my $field ($record->field('7..')) {
717 $seeheading.= " <i>see also:</i> ".$field->as_string()."<br />";
718 $altheading.= " ".$field->as_string()."<br />";
719 $altheading.= " <i>see also:</i> ".$heading."<br />";
721 $summary = "<b>".$heading."</b><br />".$seeheading.$altheading.$summary;
723 # construct MARC21 summary
724 foreach my $field ($record->field('1..')) {
725 if ($record->field('100')) {
726 $heading.= $field->as_string('abcdefghjklmnopqrstvxyz68');
727 } elsif ($record->field('110')) {
728 $heading.= $field->as_string('abcdefghklmnoprstvxyz68');
729 } elsif ($record->field('111')) {
730 $heading.= $field->as_string('acdefghklnpqstvxyz68');
731 } elsif ($record->field('130')) {
732 $heading.= $field->as_string('adfghklmnoprstvxyz68');
733 } elsif ($record->field('148')) {
734 $heading.= $field->as_string('abvxyz68');
735 } elsif ($record->field('150')) {
736 # $heading.= $field->as_string('abvxyz68');
737 $heading.= $field->as_formatted();
738 my $tag=$field->tag();
739 $heading=~s /^$tag//g;
740 $heading =~s /\_/\$/g;
741 } elsif ($record->field('151')) {
742 $heading.= $field->as_string('avxyz68');
743 } elsif ($record->field('155')) {
744 $heading.= $field->as_string('abvxyz68');
745 } elsif ($record->field('180')) {
746 $heading.= $field->as_string('vxyz68');
747 } elsif ($record->field('181')) {
748 $heading.= $field->as_string('vxyz68');
749 } elsif ($record->field('182')) {
750 $heading.= $field->as_string('vxyz68');
751 } elsif ($record->field('185')) {
752 $heading.= $field->as_string('vxyz68');
754 $heading.= $field->as_string();
757 foreach my $field ($record->field('4..')) {
758 $seeheading.= " ".$field->as_string()."<br />";
759 $seeheading.= " <i>see:</i> ".$seeheading."<br />";
761 foreach my $field ($record->field('5..')) {
762 $altheading.= " <i>see also:</i> ".$field->as_string()."<br />";
763 $altheading.= " ".$field->as_string()."<br />";
764 $altheading.= " <i>see also:</i> ".$altheading."<br />";
766 $summary.=$heading.$seeheading.$altheading;
771 sub BuildUnimarcHierarchies{
772 my $authid = shift @_;
773 # warn "authid : $authid";
774 my $force = shift @_;
776 my $dbh=C4::Context->dbh;
778 my $data = AUTHgetheader($authid);
780 if ($data->{'authtrees'} and not $force){
781 return $data->{'authtrees'};
782 } elsif ($data->{'authtrees'}){
783 $hierarchies=$data->{'authtrees'};
785 my $record = AUTHgetauthority($authid);
787 foreach my $field ($record->field('550')){
788 if ($field->subfield('5') && $field->subfield('5') eq 'g'){
789 my $parentrecord = AUTHgetauthority($field->subfield('3'));
790 my $localresult=$hierarchies;
792 $trees = BuildUnimarcHierarchies($field->subfield('3'));
795 @trees = split(/;/,$trees);
802 @globalresult = (@globalresult,@trees);
805 $hierarchies=join(";",@globalresult);
807 #Unless there is no ancestor, I am alone.
808 $hierarchies="$authid" unless ($hierarchies);
810 AUTHsavetrees($authid,$hierarchies);
814 sub BuildUnimarcHierarchy{
815 my $record = shift @_;
816 my $class = shift @_;
817 my $authid_constructed = shift @_;
818 my $authid=$record->subfield('250','3');
820 my $parents=""; my $children="";
821 my (@loopparents,@loopchildren);
822 foreach my $field ($record->field('550')){
823 if ($field->subfield('5') && $field->subfield('a')){
824 if ($field->subfield('5') eq 'h'){
825 push @loopchildren, { "childauthid"=>$field->subfield('3'),"childvalue"=>$field->subfield('a')};
826 }elsif ($field->subfield('5') eq 'g'){
827 push @loopparents, { "parentauthid"=>$field->subfield('3'),"parentvalue"=>$field->subfield('a')};
829 # brothers could get in there with an else
832 $cell{"ifparents"}=1 if (scalar(@loopparents)>0);
833 $cell{"ifchildren"}=1 if (scalar(@loopchildren)>0);
834 $cell{"loopparents"}=\@loopparents if (scalar(@loopparents)>0);
835 $cell{"loopchildren"}=\@loopchildren if (scalar(@loopchildren)>0);
836 $cell{"class"}=$class;
837 $cell{"loopauthid"}=$authid;
838 $cell{"current_value"} =1 if $authid eq $authid_constructed;
839 $cell{"value"}=$record->subfield('250',"a");
844 my $authid = shift @_;
845 my $sql= "SELECT * from auth_header WHERE authid = ?";
846 my $dbh=C4::Context->dbh;
847 my $rq= $dbh->prepare($sql);
848 $rq->execute($authid);
849 my $data= $rq->fetchrow_hashref;
854 my $authid = shift @_;
855 my $trees = shift @_;
856 my $sql= "UPDATE IGNORE auth_header set authtrees=? WHERE authid = ?";
857 my $dbh=C4::Context->dbh;
858 my $rq= $dbh->prepare($sql);
859 $rq->execute($trees,$authid);
864 my ($mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
865 my $dbh=C4::Context->dbh;
866 my $authtypecodefrom = AUTHfind_authtypecode($mergefrom);
867 my $authtypecodeto = AUTHfind_authtypecode($mergeto);
868 # return if authority does not exist
869 my @X = $MARCfrom->fields();
871 @X = $MARCto->fields();
875 # search the tag to report
876 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
877 $sth->execute($authtypecodefrom);
878 my ($auth_tag_to_report) = $sth->fetchrow;
881 @record_to = $MARCto->field($auth_tag_to_report)->subfields() if $MARCto->field($auth_tag_to_report);
883 @record_from = $MARCfrom->field($auth_tag_to_report)->subfields() if $MARCfrom->field($auth_tag_to_report);
885 # search all biblio tags using this authority.
886 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
887 $sth->execute($authtypecodefrom);
888 my @tags_using_authtype;
889 while (my ($tagfield) = $sth->fetchrow) {
890 push @tags_using_authtype,$tagfield."9" ;
893 # now, find every biblio using this authority
894 ### try ZOOM search here
895 my $oConnection=C4::Context->Zconn("biblioserver");
897 $query= "an= ".$mergefrom;
898 my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
899 my $count=$oResult->size() if ($oResult);
902 while ( $z<$count ) {
904 $rec=$oResult->record($z);
905 my $marcdata = $rec->raw();
906 push @reccache, $marcdata;
910 foreach my $marc(@reccache){
914 $marcrecord = MARC::File::USMARC::decode($marc);
915 foreach my $tagfield (@tags_using_authtype){
916 $tagfield=substr($tagfield,0,3);
917 my @tags = $marcrecord->field($tagfield);
918 foreach my $tag (@tags){
919 my $tagsubs=$tag->subfield("9");
920 #warn "$tagfield:$tagsubs:$mergefrom";
921 if ($tagsubs== $mergefrom) {
923 $tag->update("9" =>$mergeto);
924 foreach my $subfield (@record_to) {
925 # warn "$subfield,$subfield->[0],$subfield->[1]";
926 $tag->update($subfield->[0] =>$subfield->[1]);
929 $marcrecord->delete_field($tag);
930 $marcrecord->add_fields($tag);
934 my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,"") ;
936 # FIXME : this NEWmodbiblio does not exist anymore...
937 &ModBiblio($marcrecord,$oldbiblio->{'biblionumber'},GetFrameworkCode($oldbiblio->{'biblionumber'})) ;
942 END { } # module clean-up code here (global destructor)
948 Koha Developement team <info@koha.org>
950 Paul POULAIN paul.poulain@free.fr
956 # Revision 1.41 2007/03/29 13:30:31 tipaul
958 # == Biblio.pm cleaning (useless) ==
959 # * some sub declaration dropped
960 # * removed modbiblio sub
961 # * removed moditem sub
962 # * removed newitems. It was used only in finishrecieve. Replaced by a Koha2Marc+AddItem, that is better.
963 # * removed MARCkoha2marcItem
964 # * removed MARCdelsubfield declaration
965 # * removed MARCkoha2marcBiblio
967 # == Biblio.pm cleaning (naming conventions) ==
968 # * MARCgettagslib renamed to GetMarcStructure
969 # * MARCgetitems renamed to GetMarcItem
970 # * MARCfind_frameworkcode renamed to GetFrameworkCode
971 # * MARCmarc2koha renamed to TransformMarcToKoha
972 # * MARChtml2marc renamed to TransformHtmlToMarc
973 # * MARChtml2xml renamed to TranformeHtmlToXml
974 # * zebraop renamed to ModZebra
977 # * removing MARC=OFF related scripts (in cataloguing directory)
978 # * removed checkitems (function related to MARC=off feature, that is completly broken in head. If someone want to reintroduce it, hard work coming...)
979 # * removed getitemsbybiblioitem (used only by MARC=OFF scripts, that is removed as well)
981 # Revision 1.40 2007/03/28 10:39:16 hdl
982 # removing $dbh as a parameter in AuthoritiesMarc functions
983 # And reporting all differences into the scripts taht relies on those functions.
985 # Revision 1.39 2007/03/16 01:25:08 kados
986 # Using my precrash CVS copy I did the following:
988 # cvs -z3 -d:ext:kados@cvs.savannah.nongnu.org:/sources/koha co -P koha
989 # find koha.precrash -type d -name "CVS" -exec rm -v {} \;
990 # cp -r koha.precrash/* koha/
994 # This should in theory put us right back where we were before the crash
996 # Revision 1.39 2007/03/12 22:16:31 kados
997 # chcking for field before calling subfields
999 # Revision 1.38 2007/03/09 14:31:47 tipaul
1000 # rel_3_0 moved to HEAD
1002 # Revision 1.28.2.17 2007/02/05 13:16:08 hdl
1003 # Removing Link from AuthoritiesMARC summary (caused a problem owed to the API differences between opac and intranet)
1004 # + removing $dbh in authoritysearch
1005 # + adding links in templates on summaries to go to full view.
1006 # (no more links in popup authorities. or should we add it ?)
1008 # Revision 1.28.2.16 2007/02/02 18:07:42 hdl
1009 # Sorting and searching for exact term now works.
1011 # Revision 1.28.2.15 2007/01/24 10:17:47 hdl
1012 # FindDuplicate Now works.
1013 # Be AWARE that it needs a change ccl.properties.
1015 # Revision 1.28.2.14 2007/01/10 14:40:11 hdl
1016 # Adding Authorities tree.
1018 # Revision 1.28.2.13 2007/01/09 15:18:09 hdl
1019 # Adding an to ccl.properties to allow ccl search for authority-numbers.
1020 # Fixing Some problems with the previous modification to allow pqf search to work for more than one page.
1021 # Using search for an= for an authority-Number.
1023 # Revision 1.28.2.12 2007/01/09 13:51:31 hdl
1024 # Bug Fixing : AUTHcount_usage used *synchronous* connection where biblio used ****asynchronous**** one.
1025 # First try to get it work.
1027 # Revision 1.28.2.11 2007/01/05 14:37:26 btoumi
1028 # bug fix : remove wrong field in sql syntaxe from auth_subfield_structure table
1030 # Revision 1.28.2.10 2007/01/04 13:11:08 tipaul
1031 # commenting 2 zconn destroy
1033 # Revision 1.28.2.9 2006/12/22 15:09:53 toins
1034 # removing C4::Database;
1036 # Revision 1.28.2.8 2006/12/20 17:13:19 hdl
1037 # modifying use of GILS into use of @attr 1=Koha-Auth-Number
1039 # Revision 1.28.2.7 2006/12/18 16:45:38 tipaul
1042 # Revision 1.28.2.6 2006/12/07 16:45:43 toins
1043 # removing warn compilation. (perl -wc)
1045 # Revision 1.28.2.5 2006/12/06 14:19:59 hdl
1046 # ABugFixing : Authority count Management.
1048 # Revision 1.28.2.4 2006/11/17 13:18:58 tipaul
1049 # code cleaning : removing use of "bib", and replacing with "biblionumber"
1051 # WARNING : I tried to do carefully, but there are probably some mistakes.
1052 # So if you encounter a problem you didn't have before, look for this change !!!
1053 # anyway, I urge everybody to use only "biblionumber", instead of "bib", "bi", "biblio" or anything else. will be easier to maintain !!!
1055 # Revision 1.28.2.3 2006/11/17 11:17:30 tipaul
1056 # code cleaning : removing use of "bib", and replacing with "biblionumber"
1058 # WARNING : I tried to do carefully, but there are probably some mistakes.
1059 # So if you encounter a problem you didn't have before, look for this change !!!
1060 # anyway, I urge everybody to use only "biblionumber", instead of "bib", "bi", "biblio" or anything else. will be easier to maintain !!!
1062 # Revision 1.28.2.2 2006/10/12 22:04:47 hdl
1063 # Authorities working with zebra.
1064 # zebra Configuration files are comitted next.
1066 # Revision 1.9.2.17.2.2 2006/07/27 16:34:56 kados
1067 # syncing with rel_2_2 .. .untested.
1069 # Revision 1.9.2.17.2.1 2006/05/28 18:49:12 tgarip1957
1070 # This is an unusual commit. The main purpose is a working model of Zebra on a modified rel2_2.
1071 # Any questions regarding these commits should be asked to Joshua Ferraro unless you are Joshua whom I'll report to
1073 # Revision 1.9.2.6 2005/06/07 10:02:00 tipaul
1074 # porting dictionnary search from head to 2.2. there is now a ... facing titles, author & subject, to search in biblio & authorities existing values.
1076 # Revision 1.9.2.5 2005/05/31 14:50:46 tipaul
1077 # fix for authority merging. There was a bug on official installs
1079 # Revision 1.9.2.4 2005/05/30 11:24:15 tipaul
1080 # 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)
1082 # Revision 1.9.2.3 2005/04/28 08:45:33 tipaul
1083 # porting FindDuplicate feature for authorities from HEAD to rel_2_2, works correctly now.
1085 # Revision 1.9.2.2 2005/02/28 14:03:13 tipaul
1086 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
1087 # * adding a select box to requet "contain" or "begin with" search.
1088 # * fixing some bug in authority search (related to "main entry" search)
1090 # Revision 1.9.2.1 2005/02/24 13:12:13 tipaul
1091 # 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.
1092 # So, it will be done through a cron job.
1093 # (/me agree we need some doc for command line scripts)
1095 # Revision 1.9 2004/12/23 09:48:11 tipaul
1096 # Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
1098 # Revision 1.8 2004/11/05 10:11:39 tipaul
1099 # export auth_count_usage (bugfix)
1101 # Revision 1.7 2004/09/23 16:13:00 tipaul
1102 # Bugfix in modification
1104 # Revision 1.6 2004/08/18 16:00:24 tipaul
1105 # fixes for authorities management
1107 # Revision 1.5 2004/07/05 13:37:22 doxulting
1108 # First step for working authorities
1110 # Revision 1.4 2004/06/22 11:35:37 tipaul
1111 # removing % at the beginning of a string to avoid loooonnnngggg searchs
1113 # Revision 1.3 2004/06/17 08:02:13 tipaul
1114 # merging tag & subfield in auth_word for better perfs
1116 # Revision 1.2 2004/06/10 08:29:01 tipaul
1117 # MARC authority management (continued)
1119 # Revision 1.1 2004/06/07 07:35:01 tipaul
1120 # MARC authority management package