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)
151 if ($sortby eq 'HeadingDsc') { # sort by mainmainentry desc
152 foreach my $key (sort {$b cmp $a} (keys %result)) {
153 push @listresult, $result{$key};
154 # warn "push..."$#finalresult;
157 } else { # sort by mainmainentry ASC
158 foreach my $key (sort (keys %result)) {
159 push @listresult, $result{$key};
160 # warn "push..."$#finalresult;
164 # limit the $results_per_page to result size if it's more
165 $length = $numbers-$offset if $numbers < ($offset+$length);
166 # for the requested page, replace authid by the complete record
167 # speed improvement : avoid reading too much things
169 for (my $counter=$offset;$counter<=$offset+$length-1;$counter++) {
170 # $finalresult[$counter] = GetAuthority($finalresult[$counter])->as_usmarc;
171 my $separator=C4::Context->preference('authoritysep');
172 my $authrecord =GetAuthority($listresult[$counter]);
173 my $authid=$listresult[$counter];
174 my $summary=BuildSummary($authrecord,$authid,$authtypecode);
175 my $query_auth_tag = "SELECT auth_tag_to_report FROM auth_types WHERE authtypecode=?";
176 my $sth = $dbh->prepare($query_auth_tag);
177 $sth->execute($authtypecode);
178 my $auth_tag_to_report = $sth->fetchrow;
180 $newline{used}=CountUsage($authid);
181 $newline{summary} = $summary;
182 $newline{authid} = $authid;
183 $newline{even} = $counter % 2;
184 push @finalresult, \%newline;
186 return (\@finalresult, $numbers);
193 # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
194 # the authtypecode. Then, search on $a of this tag_to_report
195 # also store main entry MARC tag, to extract it at end of search
197 ##first set the authtype search and may be multiple authorities
200 my @auths=split / /,$authtypecode ;
201 foreach my $auth (@auths){
202 $query .=" \@attr 1=Authority/format-id \@attr 5=100 ".$auth; ##No truncation on authtype
203 push @authtypecode ,$auth;
207 while ($n>1){$query= "\@or ".$query;$n--;}
213 for(my $i = 0 ; $i <= $#{$value} ; $i++)
216 ##If mainentry search $a tag
217 if (@$tags[$i] eq "mainmainentry") {
218 $attr =" \@attr 1=Heading ";
219 }elsif (@$tags[$i] eq "mainentry") {
220 $attr =" \@attr 1=Heading-Entity ";
222 $attr =" \@attr 1=Any ";
224 if (@$operator[$i] eq 'is') {
225 $attr.=" \@attr 4=1 \@attr 5=100 ";##Phrase, No truncation,all of subfield field must match
226 }elsif (@$operator[$i] eq "="){
227 $attr.=" \@attr 4=107 "; #Number Exact match
228 }elsif (@$operator[$i] eq "start"){
229 $attr.=" \@attr 4=1 \@attr 5=1 ";#Phrase, Right truncated
231 $attr .=" \@attr 5=1 \@attr 4=6 ";## Word list, right truncated, anywhere
234 $attr =$attr."\"".@$value[$i]."\"";
239 ##Add how many queries generated
241 $query= $and.$query.$q2
246 # I get an error 207 from zebra 'cannot sort according to sequence', so I'm modifying as below - JF
247 #$query=' @or @attr 7=1 @attr 1=Heading 0 @or @attr 7=1 @attr 1=Heading-Entity 1'.$query if ($sortby eq "HeadingAsc");
248 #$query=' @or @attr 7=2 @attr 1=Heading 0 @or @attr 7=1 @attr 1=Heading-Entity 1'.$query if ($sortby eq "HeadingDsc");
249 $query=' @or @attr 7=1 @attr 1=Heading 0'.$query if ($sortby eq "HeadingAsc");
250 $query=' @or @attr 7=2 @attr 1=Heading 0'.$query if ($sortby eq "HeadingDsc");
252 $offset=0 unless $offset;
253 my $counter = $offset;
254 $length=10 unless $length;
257 $oAuth[0]=C4::Context->Zconn("authorityserver" , 1);
258 my $Anewq= new ZOOM::Query::PQF($query,$oAuth[0]);
260 $oAResult= $oAuth[0]->search($Anewq) ;
261 while (($i = ZOOM::event(\@oAuth)) != 0) {
262 my $ev = $oAuth[$i-1]->last_event();
263 last if $ev == ZOOM::Event::ZEND;
265 my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
267 warn "oAuth error: $errmsg ($error) $addinfo $diagset\n";
272 $nbresults=$oAResult->size();
273 my $nremains=$nbresults;
275 my @finalresult = ();
279 ##Find authid and linkid fields
280 ##we may be searching multiple authoritytypes.
281 ## FIXME this assumes that all authid and linkid fields are the same for all authority types
282 # my ($authidfield,$authidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.authid",$authtypecode[0]);
283 # my ($linkidfield,$linkidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.linkid",$authtypecode[0]);
284 while (($counter < $nbresults) && ($counter < ($offset + $length))) {
286 ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
287 my $rec=$oAResult->record($counter);
288 my $marcdata=$rec->raw();
290 my $separator=C4::Context->preference('authoritysep');
291 $authrecord = MARC::File::USMARC::decode($marcdata);
292 my $authid=$authrecord->field('001')->data();
293 my $summary=BuildSummary($authrecord,$authid,$authtypecode);
294 my $query_auth_tag = "SELECT auth_tag_to_report FROM auth_types WHERE authtypecode=?";
295 my $sth = $dbh->prepare($query_auth_tag);
296 $sth->execute($authtypecode);
297 my $auth_tag_to_report = $sth->fetchrow;
299 my $mainentry = $authrecord->field($auth_tag_to_report);
301 foreach ($mainentry->subfields()) {
302 $reported_tag .='$'.$_->[0].$_->[1];
306 $newline{summary} = $summary;
307 $newline{authid} = $authid;
308 $newline{even} = $counter % 2;
309 $newline{reported_tag} = $reported_tag;
311 push @finalresult, \%newline;
314 for (my $z=0; $z<@finalresult; $z++){
315 my $count=CountUsage($finalresult[$z]{authid});
316 $finalresult[$z]{used}=$count;
321 # $oAResult->destroy();
322 # $oAuth[0]->destroy();
324 return (\@finalresult, $nbresults);
332 $count= &CountUsage($authid)
333 counts Usage of Authid in bibliorecords.
341 if (C4::Context->preference('NoZebra')) {
342 # Read the index Koha-Auth-Number for this authid and count the lines
343 my $result = C4::Search::NZanalyse("an=$authid");
344 my @tab = split /;/,$result;
348 my $oConnection=C4::Context->Zconn("biblioserver",1);
350 $query= "an=".$authid;
351 my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
353 while ((my $i = ZOOM::event([ $oConnection ])) != 0) {
354 my $ev = $oConnection->last_event();
355 if ($ev == ZOOM::Event::ZEND) {
356 $result = $oResult->size();
363 =head2 CountUsageChildren
367 $count= &CountUsageChildren($authid)
368 counts Usage of narrower terms of Authid in bibliorecords.
373 sub CountUsageChildren {
377 =head2 GetAuthTypeCode
381 $authtypecode= &GetAuthTypeCode($authid)
382 returns authtypecode of an authid
387 sub GetAuthTypeCode {
388 #AUTHfind_authtypecode
390 my $dbh=C4::Context->dbh;
391 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
392 $sth->execute($authid);
393 my ($authtypecode) = $sth->fetchrow;
394 return $authtypecode;
401 $tagslabel= &GetTagsLabels($forlibrarian,$authtypecode)
402 returns a ref to hashref of authorities tag and subfield structure.
405 $tagslabel->{$tag}->{$subfield}->{'attribute'}
406 where attribute takes values in :
424 my ($forlibrarian,$authtypecode)= @_;
425 my $dbh=C4::Context->dbh;
426 $authtypecode="" unless $authtypecode;
428 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
431 # check that authority exists
432 $sth=$dbh->prepare("SELECT count(*) FROM auth_tag_structure WHERE authtypecode=?");
433 $sth->execute($authtypecode);
434 my ($total) = $sth->fetchrow;
435 $authtypecode="" unless ($total >0);
437 "SELECT auth_tag_structure.tagfield,auth_tag_structure.liblibrarian,auth_tag_structure.libopac,auth_tag_structure.mandatory,auth_tag_structure.repeatable
438 FROM auth_tag_structure
443 $sth->execute($authtypecode);
444 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
446 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
447 $res->{$tag}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
448 $res->{$tag}->{tab} = " "; # XXX
449 $res->{$tag}->{mandatory} = $mandatory;
450 $res->{$tag}->{repeatable} = $repeatable;
453 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,frameworkcode as authtypecode,value_builder,kohafield,seealso,hidden,isurl
454 FROM auth_subfield_structure
456 ORDER BY tagfield,tagsubfield"
458 $sth->execute($authtypecode);
461 my $authorised_value;
470 ( $tag, $subfield, $liblibrarian, , $libopac, $tab,
471 $mandatory, $repeatable, $authorised_value, $authtypecode,
472 $value_builder, $kohafield, $seealso, $hidden,
477 $res->{$tag}->{$subfield}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
478 $res->{$tag}->{$subfield}->{tab} = $tab;
479 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
480 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
481 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
482 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
483 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
484 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
485 $res->{$tag}->{$subfield}->{seealso} = $seealso;
486 $res->{$tag}->{$subfield}->{hidden} = $hidden;
487 $res->{$tag}->{$subfield}->{isurl} = $isurl;
488 $res->{$tag}->{$subfield}->{link} = $link;
497 $authid= &AddAuthority($record, $authid,$authtypecode)
498 returns authid of the newly created authority
500 Either Create Or Modify existing authority.
506 # pass the MARC::Record to this function, and it will create the records in the authority table
507 my ($record,$authid,$authtypecode) = @_;
508 my $dbh=C4::Context->dbh;
509 my $leader=' a ';##Fixme correct leader as this one just adds utf8 to MARC21
511 # if authid empty => true add, find a new authid number
512 my $format= 'UNIMARCAUTH' if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC');
513 $format= 'MARC21' if (uc(C4::Context->preference('marcflavour')) ne 'UNIMARC');
514 if (($format eq "UNIMARCAUTH") && (!$record->subfield('100','a'))){
515 $record->leader(" nx j22 ");
516 my $date=POSIX::strftime("%Y%m%d",localtime);
517 if ($record->field('100')){
518 $record->field('100')->update('a'=>$date."afrey50 ba0");
520 $record->append_fields(
521 MARC::Field->new('100',' ',' '
522 ,'a'=>$date."afrey50 ba0")
526 $record->add_fields('152','','','b'=>$authtypecode) unless ($record->field('152') && $record->subfield('152','b'));
528 my $sth=$dbh->prepare("select max(authid) from auth_header");
530 ($authid)=$sth->fetchrow;
532 ##Insert the recordID in MARC record
533 unless ($record->field('001') && $record->field('001')->data() eq $authid){
534 $record->delete_field($record->field('001'));
535 $record->insert_fields_ordered(MARC::Field->new('001',$authid));
537 # warn $record->as_formatted;
538 $dbh->do("lock tables auth_header WRITE");
539 $sth=$dbh->prepare("insert into auth_header (authid,datecreated,authtypecode,marc,marcxml) values (?,now(),?,?,?)");
540 $sth->execute($authid,$authtypecode,$record->as_usmarc,$record->as_xml_record($format));
543 $record->add_fields('001',$authid) unless ($record->field('001'));
544 $dbh->do("lock tables auth_header WRITE");
545 my $sth=$dbh->prepare("update auth_header set marc=?,marcxml=? where authid=?");
546 $sth->execute($record->as_usmarc,$record->as_xml_record($format),$authid);
549 $dbh->do("unlock tables");
550 ModZebra($authid,'specialUpdate',"authorityserver",$record);
559 $authid= &DelAuthority($authid)
569 my $dbh=C4::Context->dbh;
571 ModZebra($authid,"recordDelete","authorityserver",GetAuthority($authid));
572 $dbh->do("delete from auth_header where authid=$authid") ;
577 my ($authid,$record,$authtypecode,$merge)=@_;
578 my $dbh=C4::Context->dbh;
579 # my ($oldrecord)=&GetAuthority($authid);
580 # if ($oldrecord eq $record) {
583 # my $sth=$dbh->prepare("update auth_header set marc=?,marcxml=? where authid=?");
584 #Now rewrite the $record to table with an add
585 $authid=AddAuthority($record,$authid,$authtypecode);
587 ### 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
588 ### they should have a system preference "dontmerge=1" otherwise by default biblios will be updated
589 ### the $merge flag is now depreceated and will be removed at code cleaning
590 if (C4::Context->preference('dontmerge') ){
591 # save the file in tmp/modified_authorities
592 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
593 unless (opendir(DIR,"$cgidir")) {
594 $cgidir = C4::Context->intranetdir."/";
597 my $filename = $cgidir."/tmp/modified_authorities/$authid.authid";
598 open AUTH, "> $filename";
602 # &merge($authid,$record,$authid,$record);
607 =head2 GetAuthorityXML
611 $marcxml= &GetAuthorityXML( $authid)
612 returns xml form of record $authid
617 sub GetAuthorityXML {
618 # Returns MARC::XML of the authority passed in parameter.
620 my $dbh=C4::Context->dbh;
622 $dbh->prepare("select marcxml from auth_header where authid=? " );
623 $sth->execute($authid);
624 my ($marcxml)=$sth->fetchrow;
633 $record= &GetAuthority( $authid)
634 Returns MARC::Record of the authority passed in parameter.
641 my $dbh=C4::Context->dbh;
642 my $sth=$dbh->prepare("select marcxml from auth_header where authid=?");
643 $sth->execute($authid);
644 my ($marcxml) = $sth->fetchrow;
645 my $record=MARC::Record->new_from_xml($marcxml,'UTF-8',(C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")));
646 $record->encoding('UTF-8');
654 $result= &GetAuthType( $authtypecode)
655 If $authtypecode is not "" then
656 Returns hashref to authtypecode information
658 returns ref to array of hashref information of all Authtypes
664 my ($authtypecode) = @_;
665 my $dbh=C4::Context->dbh;
667 if (defined $authtypecode){ # NOTE - in MARC21 framework, '' is a valid authority
669 $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
670 $sth->execute($authtypecode);
672 $sth=$dbh->prepare("select * from auth_types");
675 my $res=$sth->fetchall_arrayref({});
676 if (scalar(@$res)==1){
685 my ($rtags,$rsubfields,$rvalues,%indicators) = @_;
686 my $dbh=C4::Context->dbh;
688 my $record = MARC::Record->new();
689 #---- TODO : the leader is missing
691 # my %subfieldlist=();
692 my $prevvalue; # if tag <10
693 my $field; # if tag >=10
694 for (my $i=0; $i< @$rtags; $i++) {
695 # rebuild MARC::Record
696 if (@$rtags[$i] ne $prevtag) {
699 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
703 $record->add_fields($field);
706 $indicators{@$rtags[$i]}.=' ';
707 if (@$rtags[$i] <10) {
708 $prevvalue= @$rvalues[$i];
712 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
714 $prevtag = @$rtags[$i];
716 if (@$rtags[$i] <10) {
717 $prevvalue=@$rvalues[$i];
719 if (length(@$rvalues[$i])>0) {
720 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
723 $prevtag= @$rtags[$i];
726 # the last has not been included inside the loop... do it now !
727 $record->add_fields($field) if $field;
731 =head2 FindDuplicateAuthority
735 $record= &FindDuplicateAuthority( $record, $authtypecode)
736 return $authid,Summary if duplicate is found.
738 Comments : an improvement would be to return All the records that match.
744 sub FindDuplicateAuthority {
746 my ($record,$authtypecode)=@_;
747 # warn "IN for ".$record->as_formatted;
748 my $dbh = C4::Context->dbh;
749 # warn "".$record->as_formatted;
750 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
751 $sth->execute($authtypecode);
752 my ($auth_tag_to_report) = $sth->fetchrow;
754 # warn "record :".$record->as_formatted." auth_tag_to_report :$auth_tag_to_report";
755 # build a request for SearchAuthorities
756 my $query='at='.$authtypecode.' ';
757 map {$query.= " and he=\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/)} $record->field($auth_tag_to_report)->subfields() if $record->field($auth_tag_to_report);
758 my ($error,$results)=SimpleSearch($query,"authorityserver");
759 # there is at least 1 result => return the 1st one
761 my $marcrecord = MARC::File::USMARC::decode($results->[0]);
762 return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode);
764 # no result, returns nothing
772 $text= &BuildSummary( $record, $authid, $authtypecode)
773 return HTML encoded Summary
775 Comment : authtypecode can be infered from both record and authid.
776 Moreover, authid can also be inferred from $record.
777 Would it be interesting to delete those things.
784 ## give this a Marc record to return summary
785 my ($record,$authid,$authtypecode)=@_;
786 my $dbh=C4::Context->dbh;
787 my $authref = GetAuthType($authtypecode);
788 my $summary = $authref->{summary};
790 $language{'fre'}="Français";
791 $language{'eng'}="Anglais";
792 $language{'ger'}="Allemand";
793 $language{'ita'}="Italien";
794 $language{'spa'}="Espagnol";
796 $thesaurus{'1'}="Peuples";
797 $thesaurus{'2'}="Anthroponymes";
798 $thesaurus{'3'}="Oeuvres";
799 $thesaurus{'4'}="Chronologie";
800 $thesaurus{'5'}="Lieux";
801 $thesaurus{'6'}="Sujets";
803 my @fields = $record->fields();
805 # if the library has a summary defined, use it. Otherwise, build a standard one
806 # FIXME - it appears that the summary field in the authority frameworks
807 # can work as a display template. However, this doesn't
808 # suit the MARC21 version, so for now the "templating"
809 # feature will be enabled only for UNIMARC for backwards
811 if ($summary and C4::Context->preference('marcflavour') eq 'UNIMARC') {
812 my @fields = $record->fields();
813 # $reported_tag = '$9'.$result[$counter];
814 foreach my $field (@fields) {
815 my $tag = $field->tag();
816 my $tagvalue = $field->as_string();
817 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
820 $reported_tag.='$3'.$field->data();
823 my @subf = $field->subfields;
824 for my $i (0..$#subf) {
825 my $subfieldcode = $subf[$i][0];
826 my $subfieldvalue = $subf[$i][1];
827 my $tagsubf = $tag.$subfieldcode;
828 $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
832 $summary =~ s/\[(.*?)]//g;
833 $summary =~ s/\n/<br>/g;
844 my @fields = $record->fields();
845 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
846 # construct UNIMARC summary, that is quite different from MARC21 one
848 foreach my $field ($record->field('2..')) {
849 $heading.= $field->subfield('a');
850 $authid=$field->subfield('3');
853 foreach my $field ($record->field('3..')) {
854 $notes.= '<span class="note">'.$field->subfield('a')."</span>\n";
856 foreach my $field ($record->field('4..')) {
857 my $thesaurus = "thes. : ".$thesaurus{"$field->subfield('2')"}." : " if ($field->subfield('2'));
858 $see.= '<span class="UF">'.$thesaurus.$field->subfield('a')."</span> -- \n";
861 foreach my $field ($record->field('5..')) {
863 if (($field->subfield('5')) && ($field->subfield('a')) && ($field->subfield('5') eq 'g')) {
864 $broaderterms.= '<span class="BT"> <a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
865 } elsif (($field->subfield('5')) && ($field->subfield('a')) && ($field->subfield('5') eq 'h')){
866 $narrowerterms.= '<span class="NT"><a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
867 } elsif ($field->subfield('a')) {
868 $seealso.= '<span class="RT"><a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
872 foreach my $field ($record->field('7..')) {
873 my $lang = substr($field->subfield('8'),3,3);
874 $seeheading.= '<span class="langue"> En '.$language{$lang}.' : </span><span class="OT"> '.$field->subfield('a')."</span><br />\n";
876 $broaderterms =~s/-- \n$//;
877 $narrowerterms =~s/-- \n$//;
878 $seealso =~s/-- \n$//;
880 $summary = "<b><a href=\"detail.pl?authid=$authid\">".$heading."</a></b><br />".($notes?"$notes <br />":"");
881 $summary.= '<p><div class="label">TG : '.$broaderterms.'</div></p>' if ($broaderterms);
882 $summary.= '<p><div class="label">TS : '.$narrowerterms.'</div></p>' if ($narrowerterms);
883 $summary.= '<p><div class="label">TA : '.$seealso.'</div></p>' if ($seealso);
884 $summary.= '<p><div class="label">EP : '.$see.'</div></p>' if ($see);
885 $summary.= '<p><div class="label">'.$seeheading.'</div></p>' if ($seeheading);
887 # construct MARC21 summary
888 # FIXME - looping over 1XX is questionable
889 # since MARC21 authority should have only one 1XX
890 foreach my $field ($record->field('1..')) {
891 next if "152" eq $field->tag(); # FIXME - 152 is not a good tag to use
892 # in MARC21 -- purely local tags really ought to be
894 if ($record->field('100')) {
895 $heading.= $field->as_string('abcdefghjklmnopqrstvxyz68');
896 } elsif ($record->field('110')) {
897 $heading.= $field->as_string('abcdefghklmnoprstvxyz68');
898 } elsif ($record->field('111')) {
899 $heading.= $field->as_string('acdefghklnpqstvxyz68');
900 } elsif ($record->field('130')) {
901 $heading.= $field->as_string('adfghklmnoprstvxyz68');
902 } elsif ($record->field('148')) {
903 $heading.= $field->as_string('abvxyz68');
904 } elsif ($record->field('150')) {
905 # $heading.= $field->as_string('abvxyz68');
906 $heading.= $field->as_formatted();
907 my $tag=$field->tag();
908 $heading=~s /^$tag//g;
909 $heading =~s /\_/\$/g;
910 } elsif ($record->field('151')) {
911 $heading.= $field->as_string('avxyz68');
912 } elsif ($record->field('155')) {
913 $heading.= $field->as_string('abvxyz68');
914 } elsif ($record->field('180')) {
915 $heading.= $field->as_string('vxyz68');
916 } elsif ($record->field('181')) {
917 $heading.= $field->as_string('vxyz68');
918 } elsif ($record->field('182')) {
919 $heading.= $field->as_string('vxyz68');
920 } elsif ($record->field('185')) {
921 $heading.= $field->as_string('vxyz68');
923 $heading.= $field->as_string();
926 foreach my $field ($record->field('4..')) {
927 $seeheading.= "<br /> <i>used for/see from:</i> ".$field->as_string();
929 foreach my $field ($record->field('5..')) {
930 $altheading.= "<br /> <i>see also:</i> ".$field->as_string();
932 $summary .= ": " if $summary;
933 $summary.=$heading.$seeheading.$altheading;
939 =head2 BuildUnimarcHierarchies
943 $text= &BuildUnimarcHierarchies( $authid, $force)
944 return text containing trees for hierarchies
945 for them to be stored in auth_header
948 122,1314,2452;1324,2342,3,2452
953 sub BuildUnimarcHierarchies{
954 my $authid = shift @_;
955 # warn "authid : $authid";
956 my $force = shift @_;
958 my $dbh=C4::Context->dbh;
960 my $data = GetHeaderAuthority($authid);
961 if ($data->{'authtrees'} and not $force){
962 return $data->{'authtrees'};
963 } elsif ($data->{'authtrees'}){
964 $hierarchies=$data->{'authtrees'};
966 my $record = GetAuthority($authid);
968 foreach my $field ($record->field('550')){
969 if ($field->subfield('5') && $field->subfield('5') eq 'g'){
970 my $parentrecord = GetAuthority($field->subfield('3'));
971 my $localresult=$hierarchies;
973 $trees = BuildUnimarcHierarchies($field->subfield('3'));
976 @trees = split(/;/,$trees);
983 @globalresult = (@globalresult,@trees);
986 $hierarchies=join(";",@globalresult);
988 #Unless there is no ancestor, I am alone.
989 $hierarchies="$authid" unless ($hierarchies);
991 AddAuthorityTrees($authid,$hierarchies);
995 =head2 BuildUnimarcHierarchy
999 $ref= &BuildUnimarcHierarchy( $record, $class,$authid)
1000 return a hashref in order to display hierarchy for record and final Authid $authid
1011 Those two latest ones should disappear soon.
1016 sub BuildUnimarcHierarchy{
1017 my $record = shift @_;
1018 my $class = shift @_;
1019 my $authid_constructed = shift @_;
1020 my $authid=$record->subfield('250','3');
1022 my $parents=""; my $children="";
1023 my (@loopparents,@loopchildren);
1024 foreach my $field ($record->field('550')){
1025 if ($field->subfield('5') && $field->subfield('a')){
1026 if ($field->subfield('5') eq 'h'){
1027 push @loopchildren, { "childauthid"=>$field->subfield('3'),"childvalue"=>$field->subfield('a')};
1028 }elsif ($field->subfield('5') eq 'g'){
1029 push @loopparents, { "parentauthid"=>$field->subfield('3'),"parentvalue"=>$field->subfield('a')};
1031 # brothers could get in there with an else
1034 $cell{"ifparents"}=1 if (scalar(@loopparents)>0);
1035 $cell{"ifchildren"}=1 if (scalar(@loopchildren)>0);
1036 $cell{"loopparents"}=\@loopparents if (scalar(@loopparents)>0);
1037 $cell{"loopchildren"}=\@loopchildren if (scalar(@loopchildren)>0);
1038 $cell{"class"}=$class;
1039 $cell{"loopauthid"}=$authid;
1040 $cell{"current_value"} =1 if $authid eq $authid_constructed;
1041 $cell{"value"}=$record->subfield('250',"a");
1045 =head2 GetHeaderAuthority
1049 $ref= &GetHeaderAuthority( $authid)
1050 return a hashref in order auth_header table data
1055 sub GetHeaderAuthority{
1056 my $authid = shift @_;
1057 my $sql= "SELECT * from auth_header WHERE authid = ?";
1058 my $dbh=C4::Context->dbh;
1059 my $rq= $dbh->prepare($sql);
1060 $rq->execute($authid);
1061 my $data= $rq->fetchrow_hashref;
1065 =head2 AddAuthorityTrees
1069 $ref= &AddAuthorityTrees( $authid, $trees)
1070 return success or failure
1076 sub AddAuthorityTrees{
1077 my $authid = shift @_;
1078 my $trees = shift @_;
1079 my $sql= "UPDATE IGNORE auth_header set authtrees=? WHERE authid = ?";
1080 my $dbh=C4::Context->dbh;
1081 my $rq= $dbh->prepare($sql);
1082 return $rq->execute($trees,$authid);
1089 $ref= &merge(mergefrom,$MARCfrom,$mergeto,$MARCto)
1092 Could add some feature : Migrating from a typecode to an other for instance.
1093 Then we should add some new parameter : bibliotargettag, authtargettag
1099 my ($mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
1100 my $dbh=C4::Context->dbh;
1101 my $authtypecodefrom = GetAuthTypeCode($mergefrom);
1102 my $authtypecodeto = GetAuthTypeCode($mergeto);
1103 # return if authority does not exist
1104 my @X = $MARCfrom->fields();
1105 return if $#X == -1;
1106 @X = $MARCto->fields();
1107 return if $#X == -1;
1108 # search the tag to report
1109 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
1110 $sth->execute($authtypecodefrom);
1111 my ($auth_tag_to_report) = $sth->fetchrow;
1114 @record_to = $MARCto->field($auth_tag_to_report)->subfields() if $MARCto->field($auth_tag_to_report);
1116 @record_from = $MARCfrom->field($auth_tag_to_report)->subfields() if $MARCfrom->field($auth_tag_to_report);
1118 # search all biblio tags using this authority.
1119 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1120 $sth->execute($authtypecodefrom);
1121 my @tags_using_authtype;
1122 while (my ($tagfield) = $sth->fetchrow) {
1123 push @tags_using_authtype,$tagfield."9" ;
1126 if (C4::Context->preference('NoZebra')) {
1129 # now, find every biblio using this authority
1130 my $oConnection=C4::Context->Zconn("biblioserver");
1132 $query= "an= ".$mergefrom;
1133 my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1134 my $count=$oResult->size() if ($oResult);
1137 while ( $z<$count ) {
1139 $rec=$oResult->record($z);
1140 my $marcdata = $rec->raw();
1141 push @reccache, $marcdata;
1144 $oResult->destroy();
1145 foreach my $marc(@reccache){
1148 $marcrecord = MARC::File::USMARC::decode($marc);
1149 foreach my $tagfield (@tags_using_authtype){
1150 $tagfield=substr($tagfield,0,3);
1151 my @tags = $marcrecord->field($tagfield);
1152 foreach my $tag (@tags){
1153 my $tagsubs=$tag->subfield("9");
1154 #warn "$tagfield:$tagsubs:$mergefrom";
1155 if ($tagsubs== $mergefrom) {
1156 $tag->update("9" =>$mergeto);
1157 foreach my $subfield (@record_to) {
1158 # warn "$subfield,$subfield->[0],$subfield->[1]";
1159 $tag->update($subfield->[0] =>$subfield->[1]);
1162 $marcrecord->delete_field($tag);
1163 $marcrecord->add_fields($tag);
1167 my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,"") ;
1169 &ModBiblio($marcrecord,$oldbiblio->{'biblionumber'},GetFrameworkCode($oldbiblio->{'biblionumber'})) ;
1174 # now, find every other authority linked with this authority
1175 # my $oConnection=C4::Context->Zconn("authorityserver");
1177 # # att 9210 Auth-Internal-authtype
1178 # # att 9220 Auth-Internal-LN
1179 # # ccl.properties to add for authorities
1180 # $query= "= ".$mergefrom;
1181 # my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1182 # my $count=$oResult->size() if ($oResult);
1185 # while ( $z<$count ) {
1187 # $rec=$oResult->record($z);
1188 # my $marcdata = $rec->raw();
1189 # push @reccache, $marcdata;
1192 # $oResult->destroy();
1193 # foreach my $marc(@reccache){
1196 # $marcrecord = MARC::File::USMARC::decode($marc);
1197 # foreach my $tagfield (@tags_using_authtype){
1198 # $tagfield=substr($tagfield,0,3);
1199 # my @tags = $marcrecord->field($tagfield);
1200 # foreach my $tag (@tags){
1201 # my $tagsubs=$tag->subfield("9");
1202 # #warn "$tagfield:$tagsubs:$mergefrom";
1203 # if ($tagsubs== $mergefrom) {
1204 # $tag->update("9" =>$mergeto);
1205 # foreach my $subfield (@record_to) {
1206 # # warn "$subfield,$subfield->[0],$subfield->[1]";
1207 # $tag->update($subfield->[0] =>$subfield->[1]);
1210 # $marcrecord->delete_field($tag);
1211 # $marcrecord->add_fields($tag);
1214 # }#foreach tagfield
1215 # my $authoritynumber = TransformMarcToKoha($dbh,$marcrecord,"") ;
1217 # &ModAuthority($marcrecord,$authoritynumber,GetAuthTypeCode($authoritynumber)) ;
1222 END { } # module clean-up code here (global destructor)
1228 Koha Developement team <info@koha.org>
1230 Paul POULAIN paul.poulain@free.fr