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
37 &GetAuthMARCFromKohaField
51 &BuildUnimarcHierarchies
52 &BuildUnimarcHierarchy
55 &FindDuplicateAuthority
58 =head2 GetAuthMARCFromKohaField
62 ( $tag, $subfield ) = &GetAuthMARCFromKohaField ($kohafield,$authtypecode);
63 returns tag and subfield linked to kohafield
66 Suppose Kohafield is only linked to ONE subfield
70 sub GetAuthMARCFromKohaField {
71 #AUTHfind_marc_from_kohafield
72 my ( $kohafield,$authtypecode ) = @_;
73 my $dbh=C4::Context->dbh;
74 return 0, 0 unless $kohafield;
75 $authtypecode="" unless $authtypecode;
76 my $marcfromkohafield;
77 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
78 $sth->execute($kohafield,$authtypecode);
79 my ($tagfield,$tagsubfield) = $sth->fetchrow;
81 return ($tagfield,$tagsubfield);
84 =head2 SearchAuthorities
88 (\@finalresult, $nbresults)= &SearchAuthorities($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby)
89 returns ref to array result and count of results returned
94 sub SearchAuthorities {
95 my ($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby) = @_;
96 # warn "CALL : $tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby";
97 my $dbh=C4::Context->dbh;
98 if (C4::Context->preference('NoZebra')) {
104 my @auths=split / /,$authtypecode ;
105 foreach my $auth (@auths){
106 $query .="AND auth_type= $auth ";
110 for(my $i = 0 ; $i <= $#{$value} ; $i++)
113 if (@$tags[$i] eq "mainmainentry") {
114 $query .=" AND mainmainentry";
115 }elsif (@$tags[$i] eq "mainentry") {
116 $query .=" AND mainentry";
120 if (@$operator[$i] eq 'is') {
121 $query.=(@$tags[$i]?"=":""). '"'.@$value[$i].'"';
122 }elsif (@$operator[$i] eq "="){
123 $query.=(@$tags[$i]?"=":""). '"'.@$value[$i].'"';
124 }elsif (@$operator[$i] eq "start"){
125 $query.=(@$tags[$i]?"=":"").'"'.@$value[$i].'%"';
127 $query.=(@$tags[$i]?"=":"").'"'.@$value[$i].'%"';
133 # do the query (if we had some search term
136 # warn "QUERY : $query";
137 my $result = C4::Search::NZanalyse($query,'authorityserver');
138 # warn "result : $result";
140 foreach (split /;/,$result) {
141 my ($authid,$title) = split /,/,$_;
142 # hint : the result is sorted by title.biblionumber because we can have X biblios with the same title
143 # and we don't want to get only 1 result for each of them !!!
144 # hint & speed improvement : we can order without reading the record
145 # so order, and read records only for the requested page !
146 $result{$title.$authid}=$authid;
148 # sort the hash and return the same structure as GetRecords (Zebra querying)
149 my @finalresult = ();
151 if ($sortby eq 'HeadingDsc') { # sort by mainmainentry desc
152 foreach my $key (sort {$b cmp $a} (keys %result)) {
153 push @finalresult, $result{$key};
154 # warn "push..."$#finalresult;
157 } else { # sort by mainmainentry ASC
158 foreach my $key (sort (keys %result)) {
159 push @finalresult, $result{$key};
160 # warn "push..."$#finalresult;
164 # limit the $results_per_page to result size if it's more
165 $length = $numbers-1 if $numbers < $length;
166 # for the requested page, replace authid by the complete record
167 # speed improvement : avoid reading too much things
168 for (my $counter=$offset;$counter<=$offset+$length;$counter++) {
169 # $finalresult[$counter] = GetAuthority($finalresult[$counter])->as_usmarc;
170 my $separator=C4::Context->preference('authoritysep');
171 my $authrecord = MARC::File::USMARC::decode(GetAuthority($finalresult[$counter])->as_usmarc);
172 my $authid=$authrecord->field('001')->data();
173 my $summary=BuildSummary($authrecord,$authid,$authtypecode);
174 my $query_auth_tag = "SELECT auth_tag_to_report FROM auth_types WHERE authtypecode=?";
175 my $sth = $dbh->prepare($query_auth_tag);
176 $sth->execute($authtypecode);
177 my $auth_tag_to_report = $sth->fetchrow;
179 $newline{used}=CountUsage($authid);
180 $newline{summary} = $summary;
181 $newline{authid} = $authid;
182 $newline{even} = $counter % 2;
183 $finalresult[$counter]= \%newline;
185 return (\@finalresult, $numbers);
192 # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
193 # the authtypecode. Then, search on $a of this tag_to_report
194 # also store main entry MARC tag, to extract it at end of search
196 ##first set the authtype search and may be multiple authorities
199 my @auths=split / /,$authtypecode ;
200 foreach my $auth (@auths){
201 $query .=" \@attr 1=Authority/format-id \@attr 5=100 ".$auth; ##No truncation on authtype
202 push @authtypecode ,$auth;
206 $query= "\@or ".$query;
212 for(my $i = 0 ; $i <= $#{$value} ; $i++)
215 ##If mainentry search $a tag
216 if (@$tags[$i] eq "mainmainentry") {
217 $attr =" \@attr 1=Heading ";
218 }elsif (@$tags[$i] eq "mainentry") {
219 $attr =" \@attr 1=Heading-Entity ";
221 $attr =" \@attr 1=Any ";
223 if (@$operator[$i] eq 'is') {
224 $attr.=" \@attr 4=1 \@attr 5=100 ";##Phrase, No truncation,all of subfield field must match
225 }elsif (@$operator[$i] eq "="){
226 $attr.=" \@attr 4=107 "; #Number Exact match
227 }elsif (@$operator[$i] eq "start"){
228 $attr.=" \@attr 4=1 \@attr 5=1 ";#Phrase, Right truncated
230 $attr .=" \@attr 5=1 ";## Word list, right truncated, anywhere
233 $attr =$attr."\"".@$value[$i]."\"";
238 ##Add how many queries generated
239 $query= $and.$query.$q2;
241 $query=' @or @attr 7=1 @attr 1=Heading 0 @or @attr 7=1 @attr 1=Heading-Entity 1'.$query if ($sortby eq "HeadingAsc");
242 $query=' @or @attr 7=2 @attr 1=Heading 0 @or @attr 7=1 @attr 1=Heading-Entity 1'.$query if ($sortby eq "HeadingDsc");
244 $offset=0 unless $offset;
245 my $counter = $offset;
246 $length=10 unless $length;
249 $oAuth[0]=C4::Context->Zconn("authorityserver" , 1);
250 my $Anewq= new ZOOM::Query::PQF($query,$oAuth[0]);
252 $oAResult= $oAuth[0]->search($Anewq) ;
253 while (($i = ZOOM::event(\@oAuth)) != 0) {
254 my $ev = $oAuth[$i-1]->last_event();
255 last if $ev == ZOOM::Event::ZEND;
257 my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
259 warn "oAuth error: $errmsg ($error) $addinfo $diagset\n";
264 $nbresults=$oAResult->size();
265 my $nremains=$nbresults;
267 my @finalresult = ();
271 ##Find authid and linkid fields
272 ##we may be searching multiple authoritytypes.
273 ## FIXME this assumes that all authid and linkid fields are the same for all authority types
274 # my ($authidfield,$authidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.authid",$authtypecode[0]);
275 # my ($linkidfield,$linkidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.linkid",$authtypecode[0]);
276 while (($counter < $nbresults) && ($counter < ($offset + $length))) {
278 ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
279 my $rec=$oAResult->record($counter);
280 my $marcdata=$rec->raw();
282 my $separator=C4::Context->preference('authoritysep');
283 $authrecord = MARC::File::USMARC::decode($marcdata);
284 my $authid=$authrecord->field('001')->data();
285 my $summary=BuildSummary($authrecord,$authid,$authtypecode);
286 my $query_auth_tag = "SELECT auth_tag_to_report FROM auth_types WHERE authtypecode=?";
287 my $sth = $dbh->prepare($query_auth_tag);
288 $sth->execute($authtypecode);
289 my $auth_tag_to_report = $sth->fetchrow;
291 $newline{summary} = $summary;
292 $newline{authid} = $authid;
293 $newline{even} = $counter % 2;
295 push @finalresult, \%newline;
298 for (my $z=0; $z<@finalresult; $z++){
299 my $count=CountUsage($finalresult[$z]{authid});
300 $finalresult[$z]{used}=$count;
305 # $oAResult->destroy();
306 # $oAuth[0]->destroy();
308 return (\@finalresult, $nbresults);
316 $count= &CountUsage($authid)
317 counts Usage of Authid in bibliorecords.
324 if (C4::Context->preference('NoZebra')) {
325 # Read the index Koha-Auth-Number for this authid and count the lines
326 my $result = C4::Search::NZanalyse("an=$authid");
327 return scalar split /;/,$result;
330 my $oConnection=C4::Context->Zconn("biblioserver",1);
332 $query= "an=".$authid;
333 my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
335 while ((my $i = ZOOM::event([ $oConnection ])) != 0) {
336 my $ev = $oConnection->last_event();
337 if ($ev == ZOOM::Event::ZEND) {
338 $result = $oResult->size();
345 =head2 CountUsageChildren
349 $count= &CountUsageChildren($authid)
350 counts Usage of narrower terms of Authid in bibliorecords.
355 sub CountUsageChildren {
359 =head2 GetAuthTypeCode
363 $authtypecode= &GetAuthTypeCode($authid)
364 returns authtypecode of an authid
369 sub GetAuthTypeCode {
370 #AUTHfind_authtypecode
372 my $dbh=C4::Context->dbh;
373 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
374 $sth->execute($authid);
375 my ($authtypecode) = $sth->fetchrow;
376 return $authtypecode;
383 $tagslabel= &GetTagsLabels($forlibrarian,$authtypecode)
384 returns a ref to hashref of authorities tag and subfield structure.
387 $tagslabel->{$tag}->{$subfield}->{'attribute'}
388 where attribute takes values in :
406 my ($forlibrarian,$authtypecode)= @_;
407 my $dbh=C4::Context->dbh;
408 $authtypecode="" unless $authtypecode;
410 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
413 # check that authority exists
414 $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
415 $sth->execute($authtypecode);
416 my ($total) = $sth->fetchrow;
417 $authtypecode="" unless ($total >0);
419 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
420 FROM auth_tag_structure
425 $sth->execute($authtypecode);
426 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
428 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
429 $res->{$tag}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
430 $res->{$tag}->{tab} = " "; # XXX
431 $res->{$tag}->{mandatory} = $mandatory;
432 $res->{$tag}->{repeatable} = $repeatable;
435 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl
436 FROM auth_subfield_structure
438 ORDER BY tagfield,tagsubfield"
440 $sth->execute($authtypecode);
443 my $authorised_value;
452 ( $tag, $subfield, $liblibrarian, , $libopac, $tab,
453 $mandatory, $repeatable, $authorised_value, $authtypecode,
454 $value_builder, $kohafield, $seealso, $hidden,
459 $res->{$tag}->{$subfield}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
460 $res->{$tag}->{$subfield}->{tab} = $tab;
461 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
462 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
463 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
464 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
465 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
466 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
467 $res->{$tag}->{$subfield}->{seealso} = $seealso;
468 $res->{$tag}->{$subfield}->{hidden} = $hidden;
469 $res->{$tag}->{$subfield}->{isurl} = $isurl;
470 $res->{$tag}->{$subfield}->{link} = $link;
479 $authid= &AddAuthority($record, $authid,$authtypecode)
480 returns authid of the newly created authority
482 Either Create Or Modify existing authority.
488 # pass the MARC::Record to this function, and it will create the records in the authority table
489 my ($record,$authid,$authtypecode) = @_;
490 my $dbh=C4::Context->dbh;
491 my $leader=' a ';##Fixme correct leader as this one just adds utf8 to MARC21
493 # if authid empty => true add, find a new authid number
495 my $sth=$dbh->prepare("select max(authid) from auth_header");
497 ($authid)=$sth->fetchrow;
499 ##Insert the recordID in MARC record
500 ##Both authid and authtypecode is expected to be in the same field. Modify if other requirements arise
501 $record->add_fields('001',$authid) unless $record->field('001');
502 $record->add_fields('152','','','b'=>$authtypecode) unless $record->field('152');
503 # warn $record->as_formatted;
504 $dbh->do("lock tables auth_header WRITE");
505 $sth=$dbh->prepare("insert into auth_header (authid,datecreated,authtypecode,marc) values (?,now(),?,?)");
506 $sth->execute($authid,$authtypecode,$record->as_usmarc);
509 $record->add_fields('001',$authid) unless ($record->field('001'));
510 $record->add_fields('100',$authid) unless ($record->field('100'));
511 $record->add_fields('152','','','b'=>$authtypecode) unless ($record->field('152'));
512 $dbh->do("lock tables auth_header WRITE");
513 my $sth=$dbh->prepare("update auth_header set marc=? where authid=?");
514 $sth->execute($record->as_usmarc,$authid);
517 $dbh->do("unlock tables");
518 ModZebra($authid,'specialUpdate',"authorityserver",$record);
527 $authid= &DelAuthority($authid)
537 my $dbh=C4::Context->dbh;
539 ModZebra($authid,"recordDelete","authorityserver",GetAuthority($authid));
540 $dbh->do("delete from auth_header where authid=$authid") ;
545 my ($authid,$record,$authtypecode,$merge)=@_;
546 my $dbh=C4::Context->dbh;
547 my ($oldrecord)=&GetAuthority($authid);
548 if ($oldrecord eq $record) {
551 my $sth=$dbh->prepare("update auth_header set marc=? where authid=?");
552 #Now rewrite the $record to table with an add
553 $authid=AddAuthority($record,$authid,$authtypecode);
556 ### 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
557 ### they should have a system preference "dontmerge=1" otherwise by default biblios will be updated
558 ### the $merge flag is now depreceated and will be removed at code cleaning
559 if (C4::Context->preference('dontmerge') ){
560 # save the file in localfile/modified_authorities
561 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
562 unless (opendir(DIR,"$cgidir")) {
563 $cgidir = C4::Context->intranetdir."/";
566 my $filename = $cgidir."/localfile/modified_authorities/$authid.authid";
567 open AUTH, "> $filename";
571 &merge($authid,$record,$authid,$record);
576 =head2 GetAuthorityXML
580 $marcxml= &GetAuthorityXML( $authid)
581 returns xml form of record $authid
586 sub GetAuthorityXML {
587 # Returns MARC::XML of the authority passed in parameter.
589 my $dbh=C4::Context->dbh;
591 $dbh->prepare("select marc from auth_header where authid=? " );
592 $sth->execute($authid);
593 my ($marc)=$sth->fetchrow;
594 $marc=MARC::File::USMARC::decode($marc);
595 my $marcxml=$marc->as_xml_record();
604 $record= &GetAuthority( $authid)
605 Returns MARC::Record of the authority passed in parameter.
612 my $dbh=C4::Context->dbh;
613 my $sth=$dbh->prepare("select marc from auth_header where authid=?");
614 $sth->execute($authid);
615 my ($marc) = $sth->fetchrow;
616 my $record=MARC::File::USMARC::decode($marc);
624 $result= &GetAuthType( $authtypecode)
625 If $authtypecode is not "" then
626 Returns hashref to authtypecode information
628 returns ref to array of hashref information of all Authtypes
634 my ($authtypecode) = @_;
635 my $dbh=C4::Context->dbh;
638 $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
639 $sth->execute($authtypecode);
641 $sth=$dbh->prepare("select * from auth_types");
644 my $res=$sth->fetchall_arrayref({});
645 if (scalar(@$res)==1){
654 my ($rtags,$rsubfields,$rvalues,%indicators) = @_;
655 my $dbh=C4::Context->dbh;
657 my $record = MARC::Record->new();
658 #---- TODO : the leader is missing
660 # my %subfieldlist=();
661 my $prevvalue; # if tag <10
662 my $field; # if tag >=10
663 for (my $i=0; $i< @$rtags; $i++) {
664 # rebuild MARC::Record
665 if (@$rtags[$i] ne $prevtag) {
668 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
672 $record->add_fields($field);
675 $indicators{@$rtags[$i]}.=' ';
676 if (@$rtags[$i] <10) {
677 $prevvalue= @$rvalues[$i];
681 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
683 $prevtag = @$rtags[$i];
685 if (@$rtags[$i] <10) {
686 $prevvalue=@$rvalues[$i];
688 if (length(@$rvalues[$i])>0) {
689 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
692 $prevtag= @$rtags[$i];
695 # the last has not been included inside the loop... do it now !
696 $record->add_fields($field) if $field;
700 =head2 FindDuplicateAuthority
704 $record= &FindDuplicateAuthority( $record, $authtypecode)
705 return $authid,Summary if duplicate is found.
707 Comments : an improvement would be to return All the records that match.
713 sub FindDuplicateAuthority {
715 my ($record,$authtypecode)=@_;
716 # warn "IN for ".$record->as_formatted;
717 my $dbh = C4::Context->dbh;
718 # warn "".$record->as_formatted;
719 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
720 $sth->execute($authtypecode);
721 my ($auth_tag_to_report) = $sth->fetchrow;
723 # warn "record :".$record->as_formatted." auth_tag_to_report :$auth_tag_to_report";
724 # build a request for SearchAuthorities
725 my $query='at='.$authtypecode.' ';
726 map {$query.= " and he=\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/)} $record->field($auth_tag_to_report)->subfields() if $record->field($auth_tag_to_report);
727 my ($error,$results)=SimpleSearch($query,"authorityserver");
728 # there is at least 1 result => return the 1st one
730 my $marcrecord = MARC::File::USMARC::decode($results->[0]);
731 return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode);
733 # no result, returns nothing
741 $text= &BuildSummary( $record, $authid, $authtypecode)
742 return HTML encoded Summary
744 Comment : authtypecode can be infered from both record and authid.
745 Moreover, authid can also be inferred from $record.
746 Would it be interesting to delete those things.
753 ## give this a Marc record to return summary
754 my ($record,$authid,$authtypecode)=@_;
755 my $dbh=C4::Context->dbh;
756 my $authref = GetAuthType($authtypecode);
757 my $summary = $authref->{summary};
758 my @fields = $record->fields();
760 # if the library has a summary defined, use it. Otherwise, build a standard one
762 my @fields = $record->fields();
763 # $reported_tag = '$9'.$result[$counter];
764 foreach my $field (@fields) {
765 my $tag = $field->tag();
766 my $tagvalue = $field->as_string();
767 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
770 $reported_tag.='$3'.$field->data();
773 my @subf = $field->subfields;
774 for my $i (0..$#subf) {
775 my $subfieldcode = $subf[$i][0];
776 my $subfieldvalue = $subf[$i][1];
777 my $tagsubf = $tag.$subfieldcode;
778 $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
782 $summary =~ s/\[(.*?)]//g;
783 $summary =~ s/\n/<br>/g;
785 my $heading; # = $authref->{summary};
789 my @fields = $record->fields();
790 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
791 # construct UNIMARC summary, that is quite different from MARC21 one
793 foreach my $field ($record->field('2..')) {
794 $heading.= $field->as_string();
797 foreach my $field ($record->field('4..')) {
798 $summary.= " <i>".$field->as_string()."</i><br/>";
799 $summary.= " <i>see:</i> ".$heading."<br/>";
802 foreach my $field ($record->field('5..')) {
803 $summary.= " <i>".$field->as_string()."</i><br/>";
804 $summary.= " <i>see:</i> ".$heading."<br/>";
807 foreach my $field ($record->field('7..')) {
808 $seeheading.= " <i>see also:</i> ".$field->as_string()."<br />";
809 $altheading.= " ".$field->as_string()."<br />";
810 $altheading.= " <i>see also:</i> ".$heading."<br />";
812 $summary = "<b>".$heading."</b><br />".$seeheading.$altheading.$summary;
814 # construct MARC21 summary
815 foreach my $field ($record->field('1..')) {
816 if ($record->field('100')) {
817 $heading.= $field->as_string('abcdefghjklmnopqrstvxyz68');
818 } elsif ($record->field('110')) {
819 $heading.= $field->as_string('abcdefghklmnoprstvxyz68');
820 } elsif ($record->field('111')) {
821 $heading.= $field->as_string('acdefghklnpqstvxyz68');
822 } elsif ($record->field('130')) {
823 $heading.= $field->as_string('adfghklmnoprstvxyz68');
824 } elsif ($record->field('148')) {
825 $heading.= $field->as_string('abvxyz68');
826 } elsif ($record->field('150')) {
827 # $heading.= $field->as_string('abvxyz68');
828 $heading.= $field->as_formatted();
829 my $tag=$field->tag();
830 $heading=~s /^$tag//g;
831 $heading =~s /\_/\$/g;
832 } elsif ($record->field('151')) {
833 $heading.= $field->as_string('avxyz68');
834 } elsif ($record->field('155')) {
835 $heading.= $field->as_string('abvxyz68');
836 } elsif ($record->field('180')) {
837 $heading.= $field->as_string('vxyz68');
838 } elsif ($record->field('181')) {
839 $heading.= $field->as_string('vxyz68');
840 } elsif ($record->field('182')) {
841 $heading.= $field->as_string('vxyz68');
842 } elsif ($record->field('185')) {
843 $heading.= $field->as_string('vxyz68');
845 $heading.= $field->as_string();
848 foreach my $field ($record->field('4..')) {
849 $seeheading.= " ".$field->as_string()."<br />";
850 $seeheading.= " <i>see:</i> ".$seeheading."<br />";
852 foreach my $field ($record->field('5..')) {
853 $altheading.= " <i>see also:</i> ".$field->as_string()."<br />";
854 $altheading.= " ".$field->as_string()."<br />";
855 $altheading.= " <i>see also:</i> ".$altheading."<br />";
857 $summary.=$heading.$seeheading.$altheading;
863 =head2 BuildUnimarcHierarchies
867 $text= &BuildUnimarcHierarchies( $authid, $force)
868 return text containing trees for hierarchies
869 for them to be stored in auth_header
872 122,1314,2452;1324,2342,3,2452
877 sub BuildUnimarcHierarchies{
878 my $authid = shift @_;
879 # warn "authid : $authid";
880 my $force = shift @_;
882 my $dbh=C4::Context->dbh;
884 my $data = GetHeaderAuthority($authid);
885 if ($data->{'authtrees'} and not $force){
886 return $data->{'authtrees'};
887 } elsif ($data->{'authtrees'}){
888 $hierarchies=$data->{'authtrees'};
890 my $record = GetAuthority($authid);
892 foreach my $field ($record->field('550')){
893 if ($field->subfield('5') && $field->subfield('5') eq 'g'){
894 my $parentrecord = GetAuthority($field->subfield('3'));
895 my $localresult=$hierarchies;
897 $trees = BuildUnimarcHierarchies($field->subfield('3'));
900 @trees = split(/;/,$trees);
907 @globalresult = (@globalresult,@trees);
910 $hierarchies=join(";",@globalresult);
912 #Unless there is no ancestor, I am alone.
913 $hierarchies="$authid" unless ($hierarchies);
915 AddAuthorityTrees($authid,$hierarchies);
919 =head2 BuildUnimarcHierarchy
923 $ref= &BuildUnimarcHierarchy( $record, $class,$authid)
924 return a hashref in order to display hierarchy for record and final Authid $authid
935 Those two latest ones should disappear soon.
940 sub BuildUnimarcHierarchy{
941 my $record = shift @_;
942 my $class = shift @_;
943 my $authid_constructed = shift @_;
944 my $authid=$record->subfield('250','3');
946 my $parents=""; my $children="";
947 my (@loopparents,@loopchildren);
948 foreach my $field ($record->field('550')){
949 if ($field->subfield('5') && $field->subfield('a')){
950 if ($field->subfield('5') eq 'h'){
951 push @loopchildren, { "childauthid"=>$field->subfield('3'),"childvalue"=>$field->subfield('a')};
952 }elsif ($field->subfield('5') eq 'g'){
953 push @loopparents, { "parentauthid"=>$field->subfield('3'),"parentvalue"=>$field->subfield('a')};
955 # brothers could get in there with an else
958 $cell{"ifparents"}=1 if (scalar(@loopparents)>0);
959 $cell{"ifchildren"}=1 if (scalar(@loopchildren)>0);
960 $cell{"loopparents"}=\@loopparents if (scalar(@loopparents)>0);
961 $cell{"loopchildren"}=\@loopchildren if (scalar(@loopchildren)>0);
962 $cell{"class"}=$class;
963 $cell{"loopauthid"}=$authid;
964 $cell{"current_value"} =1 if $authid eq $authid_constructed;
965 $cell{"value"}=$record->subfield('250',"a");
969 =head2 GetHeaderAuthority
973 $ref= &GetHeaderAuthority( $authid)
974 return a hashref in order auth_header table data
979 sub GetHeaderAuthority{
980 my $authid = shift @_;
981 my $sql= "SELECT * from auth_header WHERE authid = ?";
982 my $dbh=C4::Context->dbh;
983 my $rq= $dbh->prepare($sql);
984 $rq->execute($authid);
985 my $data= $rq->fetchrow_hashref;
989 =head2 AddAuthorityTrees
993 $ref= &AddAuthorityTrees( $authid, $trees)
994 return success or failure
1000 sub AddAuthorityTrees{
1001 my $authid = shift @_;
1002 my $trees = shift @_;
1003 my $sql= "UPDATE IGNORE auth_header set authtrees=? WHERE authid = ?";
1004 my $dbh=C4::Context->dbh;
1005 my $rq= $dbh->prepare($sql);
1006 return $rq->execute($trees,$authid);
1013 $ref= &merge(mergefrom,$MARCfrom,$mergeto,$MARCto)
1016 Could add some feature : Migrating from a typecode to an other for instance.
1017 Then we should add some new parameter : bibliotargettag, authtargettag
1023 my ($mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
1024 my $dbh=C4::Context->dbh;
1025 my $authtypecodefrom = GetAuthTypeCode($mergefrom);
1026 my $authtypecodeto = GetAuthTypeCode($mergeto);
1027 # return if authority does not exist
1028 my @X = $MARCfrom->fields();
1029 return if $#X == -1;
1030 @X = $MARCto->fields();
1031 return if $#X == -1;
1032 # search the tag to report
1033 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
1034 $sth->execute($authtypecodefrom);
1035 my ($auth_tag_to_report) = $sth->fetchrow;
1038 @record_to = $MARCto->field($auth_tag_to_report)->subfields() if $MARCto->field($auth_tag_to_report);
1040 @record_from = $MARCfrom->field($auth_tag_to_report)->subfields() if $MARCfrom->field($auth_tag_to_report);
1042 # search all biblio tags using this authority.
1043 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1044 $sth->execute($authtypecodefrom);
1045 my @tags_using_authtype;
1046 while (my ($tagfield) = $sth->fetchrow) {
1047 push @tags_using_authtype,$tagfield."9" ;
1050 if (C4::Context->preference('NoZebra')) {
1053 # now, find every biblio using this authority
1054 my $oConnection=C4::Context->Zconn("biblioserver");
1056 $query= "an= ".$mergefrom;
1057 my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1058 my $count=$oResult->size() if ($oResult);
1061 while ( $z<$count ) {
1063 $rec=$oResult->record($z);
1064 my $marcdata = $rec->raw();
1065 push @reccache, $marcdata;
1068 $oResult->destroy();
1069 foreach my $marc(@reccache){
1072 $marcrecord = MARC::File::USMARC::decode($marc);
1073 foreach my $tagfield (@tags_using_authtype){
1074 $tagfield=substr($tagfield,0,3);
1075 my @tags = $marcrecord->field($tagfield);
1076 foreach my $tag (@tags){
1077 my $tagsubs=$tag->subfield("9");
1078 #warn "$tagfield:$tagsubs:$mergefrom";
1079 if ($tagsubs== $mergefrom) {
1080 $tag->update("9" =>$mergeto);
1081 foreach my $subfield (@record_to) {
1082 # warn "$subfield,$subfield->[0],$subfield->[1]";
1083 $tag->update($subfield->[0] =>$subfield->[1]);
1086 $marcrecord->delete_field($tag);
1087 $marcrecord->add_fields($tag);
1091 my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,"") ;
1093 &ModBiblio($marcrecord,$oldbiblio->{'biblionumber'},GetFrameworkCode($oldbiblio->{'biblionumber'})) ;
1098 # now, find every other authority linked with this authority
1099 # my $oConnection=C4::Context->Zconn("authorityserver");
1101 # # att 9210 Auth-Internal-authtype
1102 # # att 9220 Auth-Internal-LN
1103 # # ccl.properties to add for authorities
1104 # $query= "= ".$mergefrom;
1105 # my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1106 # my $count=$oResult->size() if ($oResult);
1109 # while ( $z<$count ) {
1111 # $rec=$oResult->record($z);
1112 # my $marcdata = $rec->raw();
1113 # push @reccache, $marcdata;
1116 # $oResult->destroy();
1117 # foreach my $marc(@reccache){
1120 # $marcrecord = MARC::File::USMARC::decode($marc);
1121 # foreach my $tagfield (@tags_using_authtype){
1122 # $tagfield=substr($tagfield,0,3);
1123 # my @tags = $marcrecord->field($tagfield);
1124 # foreach my $tag (@tags){
1125 # my $tagsubs=$tag->subfield("9");
1126 # #warn "$tagfield:$tagsubs:$mergefrom";
1127 # if ($tagsubs== $mergefrom) {
1128 # $tag->update("9" =>$mergeto);
1129 # foreach my $subfield (@record_to) {
1130 # # warn "$subfield,$subfield->[0],$subfield->[1]";
1131 # $tag->update($subfield->[0] =>$subfield->[1]);
1134 # $marcrecord->delete_field($tag);
1135 # $marcrecord->add_fields($tag);
1138 # }#foreach tagfield
1139 # my $authoritynumber = TransformMarcToKoha($dbh,$marcrecord,"") ;
1141 # &ModAuthority($marcrecord,$authoritynumber,GetAuthTypeCode($authoritynumber)) ;
1146 END { } # module clean-up code here (global destructor)
1152 Koha Developement team <info@koha.org>
1154 Paul POULAIN paul.poulain@free.fr
1160 # Revision 1.46 2007/05/10 14:45:15 tipaul
1162 # - support for authorities
1163 # - some bugfixes in ordering and "CCL" parsing
1164 # - support for authorities <=> biblios walking
1166 # Seems I can do what I want now, so I consider its done, except for bugfixes that will be needed i m sure !
1168 # Revision 1.45 2007/04/06 14:48:45 hdl
1169 # Code Cleaning : AuthoritiesMARC.
1171 # Revision 1.44 2007/04/05 12:17:55 btoumi
1172 # add "sort by" with heading-entity in authorities search
1174 # Revision 1.43 2007/03/30 11:59:16 tipaul
1175 # some cleaning (minor, the main one will come later) : removing some unused subs
1177 # Revision 1.42 2007/03/29 16:45:53 tipaul
1178 # Code cleaning of Biblio.pm (continued)
1180 # All subs have be cleaned :
1183 # - reordering Biblio.pm completly
1184 # - using only naming conventions
1186 # Seems to have broken nothing, but it still has to be heavily tested.
1187 # Note that Biblio.pm is now much more efficient than previously & probably more reliable as well.
1189 # Revision 1.41 2007/03/29 13:30:31 tipaul
1191 # == Biblio.pm cleaning (useless) ==
1192 # * some sub declaration dropped
1193 # * removed modbiblio sub
1194 # * removed moditem sub
1195 # * removed newitems. It was used only in finishrecieve. Replaced by a TransformKohaToMarc+AddItem, that is better.
1196 # * removed MARCkoha2marcItem
1197 # * removed MARCdelsubfield declaration
1198 # * removed MARCkoha2marcBiblio
1200 # == Biblio.pm cleaning (naming conventions) ==
1201 # * MARCgettagslib renamed to GetMarcStructure
1202 # * MARCgetitems renamed to GetMarcItem
1203 # * MARCfind_frameworkcode renamed to GetFrameworkCode
1204 # * MARCmarc2koha renamed to TransformMarcToKoha
1205 # * MARChtml2marc renamed to TransformHtmlToMarc
1206 # * MARChtml2xml renamed to TranformeHtmlToXml
1207 # * zebraop renamed to ModZebra
1210 # * removing MARC=OFF related scripts (in cataloguing directory)
1211 # * removed checkitems (function related to MARC=off feature, that is completly broken in head. If someone want to reintroduce it, hard work coming...)
1212 # * removed getitemsbybiblioitem (used only by MARC=OFF scripts, that is removed as well)
1214 # Revision 1.40 2007/03/28 10:39:16 hdl
1215 # removing $dbh as a parameter in AuthoritiesMarc functions
1216 # And reporting all differences into the scripts taht relies on those functions.
1218 # Revision 1.39 2007/03/16 01:25:08 kados
1219 # Using my precrash CVS copy I did the following:
1221 # cvs -z3 -d:ext:kados@cvs.savannah.nongnu.org:/sources/koha co -P koha
1222 # find koha.precrash -type d -name "CVS" -exec rm -v {} \;
1223 # cp -r koha.precrash/* koha/
1227 # This should in theory put us right back where we were before the crash
1229 # Revision 1.39 2007/03/12 22:16:31 kados
1230 # chcking for field before calling subfields
1232 # Revision 1.38 2007/03/09 14:31:47 tipaul
1233 # rel_3_0 moved to HEAD
1235 # Revision 1.28.2.17 2007/02/05 13:16:08 hdl
1236 # Removing Link from AuthoritiesMARC summary (caused a problem owed to the API differences between opac and intranet)
1237 # + removing $dbh in SearchAuthorities
1238 # + adding links in templates on summaries to go to full view.
1239 # (no more links in popup authorities. or should we add it ?)
1241 # Revision 1.28.2.16 2007/02/02 18:07:42 hdl
1242 # Sorting and searching for exact term now works.
1244 # Revision 1.28.2.15 2007/01/24 10:17:47 hdl
1245 # FindDuplicate Now works.
1246 # Be AWARE that it needs a change ccl.properties.
1248 # Revision 1.28.2.14 2007/01/10 14:40:11 hdl
1249 # Adding Authorities tree.
1251 # Revision 1.28.2.13 2007/01/09 15:18:09 hdl
1252 # Adding an to ccl.properties to allow ccl search for authority-numbers.
1253 # Fixing Some problems with the previous modification to allow pqf search to work for more than one page.
1254 # Using search for an= for an authority-Number.
1256 # Revision 1.28.2.12 2007/01/09 13:51:31 hdl
1257 # Bug Fixing : CountUsage used *synchronous* connection where biblio used ****asynchronous**** one.
1258 # First try to get it work.
1260 # Revision 1.28.2.11 2007/01/05 14:37:26 btoumi
1261 # bug fix : remove wrong field in sql syntaxe from auth_subfield_structure table
1263 # Revision 1.28.2.10 2007/01/04 13:11:08 tipaul
1264 # commenting 2 zconn destroy
1266 # Revision 1.28.2.9 2006/12/22 15:09:53 toins
1267 # removing C4::Database;
1269 # Revision 1.28.2.8 2006/12/20 17:13:19 hdl
1270 # modifying use of GILS into use of @attr 1=Koha-Auth-Number
1272 # Revision 1.28.2.7 2006/12/18 16:45:38 tipaul
1275 # Revision 1.28.2.6 2006/12/07 16:45:43 toins
1276 # removing warn compilation. (perl -wc)
1278 # Revision 1.28.2.5 2006/12/06 14:19:59 hdl
1279 # ABugFixing : Authority count Management.
1281 # Revision 1.28.2.4 2006/11/17 13:18:58 tipaul
1282 # code cleaning : removing use of "bib", and replacing with "biblionumber"
1284 # WARNING : I tried to do carefully, but there are probably some mistakes.
1285 # So if you encounter a problem you didn't have before, look for this change !!!
1286 # anyway, I urge everybody to use only "biblionumber", instead of "bib", "bi", "biblio" or anything else. will be easier to maintain !!!
1288 # Revision 1.28.2.3 2006/11/17 11:17:30 tipaul
1289 # code cleaning : removing use of "bib", and replacing with "biblionumber"
1291 # WARNING : I tried to do carefully, but there are probably some mistakes.
1292 # So if you encounter a problem you didn't have before, look for this change !!!
1293 # anyway, I urge everybody to use only "biblionumber", instead of "bib", "bi", "biblio" or anything else. will be easier to maintain !!!
1295 # Revision 1.28.2.2 2006/10/12 22:04:47 hdl
1296 # Authorities working with zebra.
1297 # zebra Configuration files are comitted next.