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
26 use C4::AuthoritiesMarc::MARC21;
27 use C4::AuthoritiesMarc::UNIMARC;
29 use vars qw($VERSION @ISA @EXPORT);
31 # set the version for version checking
39 &GetAuthMARCFromKohaField
53 &BuildUnimarcHierarchies
54 &BuildUnimarcHierarchy
57 &FindDuplicateAuthority
60 =head2 GetAuthMARCFromKohaField
64 ( $tag, $subfield ) = &GetAuthMARCFromKohaField ($kohafield,$authtypecode);
65 returns tag and subfield linked to kohafield
68 Suppose Kohafield is only linked to ONE subfield
74 sub GetAuthMARCFromKohaField {
75 #AUTHfind_marc_from_kohafield
76 my ( $kohafield,$authtypecode ) = @_;
77 my $dbh=C4::Context->dbh;
78 return 0, 0 unless $kohafield;
79 $authtypecode="" unless $authtypecode;
80 my $marcfromkohafield;
81 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
82 $sth->execute($kohafield,$authtypecode);
83 my ($tagfield,$tagsubfield) = $sth->fetchrow;
85 return ($tagfield,$tagsubfield);
88 =head2 SearchAuthorities
92 (\@finalresult, $nbresults)= &SearchAuthorities($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby)
93 returns ref to array result and count of results returned
98 sub SearchAuthorities {
99 my ($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby) = @_;
100 # warn "CALL : $tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby";
101 my $dbh=C4::Context->dbh;
102 if (C4::Context->preference('NoZebra')) {
108 my @auths=split / /,$authtypecode ;
109 foreach my $auth (@auths){
110 $query .="AND auth_type= $auth ";
114 for(my $i = 0 ; $i <= $#{$value} ; $i++)
117 if (@$tags[$i] eq "mainmainentry") {
118 $query .=" AND mainmainentry";
119 }elsif (@$tags[$i] eq "mainentry") {
120 $query .=" AND mainentry";
124 if (@$operator[$i] eq 'is') {
125 $query.=(@$tags[$i]?"=":""). '"'.@$value[$i].'"';
126 }elsif (@$operator[$i] eq "="){
127 $query.=(@$tags[$i]?"=":""). '"'.@$value[$i].'"';
128 }elsif (@$operator[$i] eq "start"){
129 $query.=(@$tags[$i]?"=":"").'"'.@$value[$i].'%"';
131 $query.=(@$tags[$i]?"=":"").'"'.@$value[$i].'%"';
137 # do the query (if we had some search term
140 # warn "QUERY : $query";
141 my $result = C4::Search::NZanalyse($query,'authorityserver');
142 # warn "result : $result";
144 foreach (split /;/,$result) {
145 my ($authid,$title) = split /,/,$_;
146 # hint : the result is sorted by title.biblionumber because we can have X biblios with the same title
147 # and we don't want to get only 1 result for each of them !!!
148 # hint & speed improvement : we can order without reading the record
149 # so order, and read records only for the requested page !
150 $result{$title.$authid}=$authid;
152 # sort the hash and return the same structure as GetRecords (Zebra querying)
155 if ($sortby eq 'HeadingDsc') { # sort by mainmainentry desc
156 foreach my $key (sort {$b cmp $a} (keys %result)) {
157 push @listresult, $result{$key};
158 # warn "push..."$#finalresult;
161 } else { # sort by mainmainentry ASC
162 foreach my $key (sort (keys %result)) {
163 push @listresult, $result{$key};
164 # warn "push..."$#finalresult;
168 # limit the $results_per_page to result size if it's more
169 $length = $numbers-$offset if $numbers < ($offset+$length);
170 # for the requested page, replace authid by the complete record
171 # speed improvement : avoid reading too much things
173 for (my $counter=$offset;$counter<=$offset+$length-1;$counter++) {
174 # $finalresult[$counter] = GetAuthority($finalresult[$counter])->as_usmarc;
175 my $separator=C4::Context->preference('authoritysep');
176 my $authrecord =GetAuthority($listresult[$counter]);
177 my $authid=$listresult[$counter];
178 my $summary=BuildSummary($authrecord,$authid,$authtypecode);
179 my $query_auth_tag = "SELECT auth_tag_to_report FROM auth_types WHERE authtypecode=?";
180 my $sth = $dbh->prepare($query_auth_tag);
181 $sth->execute($authtypecode);
182 my $auth_tag_to_report = $sth->fetchrow;
184 $newline{used}=CountUsage($authid);
185 $newline{summary} = $summary;
186 $newline{authid} = $authid;
187 $newline{even} = $counter % 2;
188 push @finalresult, \%newline;
190 return (\@finalresult, $numbers);
197 # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
198 # the authtypecode. Then, search on $a of this tag_to_report
199 # also store main entry MARC tag, to extract it at end of search
201 ##first set the authtype search and may be multiple authorities
204 my @auths=split / /,$authtypecode ;
205 foreach my $auth (@auths){
206 $query .=" \@attr 1=authtype \@attr 5=100 ".$auth; ##No truncation on authtype
207 push @authtypecode ,$auth;
211 while ($n>1){$query= "\@or ".$query;$n--;}
217 for(my $i = 0 ; $i <= $#{$value} ; $i++)
220 ##If mainentry search $a tag
221 if (@$tags[$i] eq "mainmainentry") {
222 $attr =" \@attr 1=Heading ";
223 }elsif (@$tags[$i] eq "mainentry") {
224 $attr =" \@attr 1=Heading ";
226 $attr =" \@attr 1=Any ";
228 if (@$operator[$i] eq 'is') {
229 $attr.=" \@attr 4=1 \@attr 5=100 ";##Phrase, No truncation,all of subfield field must match
230 }elsif (@$operator[$i] eq "="){
231 $attr.=" \@attr 4=107 "; #Number Exact match
232 }elsif (@$operator[$i] eq "start"){
233 $attr.=" \@attr 4=1 \@attr 5=1 ";#Phrase, Right truncated
235 $attr .=" \@attr 5=1 \@attr 4=6 ";## Word list, right truncated, anywhere
238 $attr =$attr."\"".@$value[$i]."\"";
243 ##Add how many queries generated
245 $query= $and.$query.$q2
250 #$query=' @or @attr 7=2 @attr 1=Heading 0 @or @attr 7=1 @attr 1=Heading 1'.$query if ($sortby eq "HeadingDsc");
251 $query=' @or @attr 7=1 @attr 1=Heading 0'.$query if ($sortby eq "HeadingAsc");
252 $query=' @or @attr 7=2 @attr 1=Heading 0'.$query if ($sortby eq "HeadingDsc");
254 $offset=0 unless $offset;
255 my $counter = $offset;
256 $length=10 unless $length;
259 $oAuth[0]=C4::Context->Zconn("authorityserver" , 1);
260 my $Anewq= new ZOOM::Query::PQF($query,$oAuth[0]);
262 $oAResult= $oAuth[0]->search($Anewq) ;
263 while (($i = ZOOM::event(\@oAuth)) != 0) {
264 my $ev = $oAuth[$i-1]->last_event();
265 last if $ev == ZOOM::Event::ZEND;
267 my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
269 warn "oAuth error: $errmsg ($error) $addinfo $diagset\n";
274 $nbresults=$oAResult->size();
275 my $nremains=$nbresults;
277 my @finalresult = ();
281 ##Find authid and linkid fields
282 ##we may be searching multiple authoritytypes.
283 ## FIXME this assumes that all authid and linkid fields are the same for all authority types
284 # my ($authidfield,$authidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.authid",$authtypecode[0]);
285 # my ($linkidfield,$linkidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.linkid",$authtypecode[0]);
286 while (($counter < $nbresults) && ($counter < ($offset + $length))) {
288 ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
289 my $rec=$oAResult->record($counter);
290 my $marcdata=$rec->raw();
292 my $separator=C4::Context->preference('authoritysep');
293 $authrecord = MARC::File::USMARC::decode($marcdata);
294 my $authid=$authrecord->field('001')->data();
295 my $summary=BuildSummary($authrecord,$authid,$authtypecode);
296 my $query_auth_tag = "SELECT auth_tag_to_report FROM auth_types WHERE authtypecode=?";
297 my $sth = $dbh->prepare($query_auth_tag);
298 $sth->execute($authtypecode);
299 my $auth_tag_to_report = $sth->fetchrow;
301 my $mainentry = $authrecord->field($auth_tag_to_report);
303 foreach ($mainentry->subfields()) {
304 $reported_tag .='$'.$_->[0].$_->[1];
308 $newline{summary} = $summary;
309 $newline{authid} = $authid;
310 $newline{even} = $counter % 2;
311 $newline{reported_tag} = $reported_tag;
313 push @finalresult, \%newline;
316 for (my $z=0; $z<@finalresult; $z++){
317 my $count=CountUsage($finalresult[$z]{authid});
318 $finalresult[$z]{used}=$count;
323 # $oAResult->destroy();
324 # $oAuth[0]->destroy();
326 return (\@finalresult, $nbresults);
334 $count= &CountUsage($authid)
335 counts Usage of Authid in bibliorecords.
343 if (C4::Context->preference('NoZebra')) {
344 # Read the index Koha-Auth-Number for this authid and count the lines
345 my $result = C4::Search::NZanalyse("an=$authid");
346 my @tab = split /;/,$result;
350 my $oConnection=C4::Context->Zconn("biblioserver",1);
352 $query= "an=".$authid;
353 my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
355 while ((my $i = ZOOM::event([ $oConnection ])) != 0) {
356 my $ev = $oConnection->last_event();
357 if ($ev == ZOOM::Event::ZEND) {
358 $result = $oResult->size();
365 =head2 CountUsageChildren
369 $count= &CountUsageChildren($authid)
370 counts Usage of narrower terms of Authid in bibliorecords.
375 sub CountUsageChildren {
379 =head2 GetAuthTypeCode
383 $authtypecode= &GetAuthTypeCode($authid)
384 returns authtypecode of an authid
389 sub GetAuthTypeCode {
390 #AUTHfind_authtypecode
392 my $dbh=C4::Context->dbh;
393 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
394 $sth->execute($authid);
395 my ($authtypecode) = $sth->fetchrow;
396 return $authtypecode;
403 $tagslabel= &GetTagsLabels($forlibrarian,$authtypecode)
404 returns a ref to hashref of authorities tag and subfield structure.
407 $tagslabel->{$tag}->{$subfield}->{'attribute'}
408 where attribute takes values in :
426 my ($forlibrarian,$authtypecode)= @_;
427 my $dbh=C4::Context->dbh;
428 $authtypecode="" unless $authtypecode;
430 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
433 # check that authority exists
434 $sth=$dbh->prepare("SELECT count(*) FROM auth_tag_structure WHERE authtypecode=?");
435 $sth->execute($authtypecode);
436 my ($total) = $sth->fetchrow;
437 $authtypecode="" unless ($total >0);
439 "SELECT auth_tag_structure.tagfield,auth_tag_structure.liblibrarian,auth_tag_structure.libopac,auth_tag_structure.mandatory,auth_tag_structure.repeatable
440 FROM auth_tag_structure
445 $sth->execute($authtypecode);
446 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
448 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
449 $res->{$tag}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
450 $res->{$tag}->{tab} = " "; # XXX
451 $res->{$tag}->{mandatory} = $mandatory;
452 $res->{$tag}->{repeatable} = $repeatable;
455 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,frameworkcode as authtypecode,value_builder,kohafield,seealso,hidden,isurl
456 FROM auth_subfield_structure
458 ORDER BY tagfield,tagsubfield"
460 $sth->execute($authtypecode);
463 my $authorised_value;
472 ( $tag, $subfield, $liblibrarian, , $libopac, $tab,
473 $mandatory, $repeatable, $authorised_value, $authtypecode,
474 $value_builder, $kohafield, $seealso, $hidden,
479 $res->{$tag}->{$subfield}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
480 $res->{$tag}->{$subfield}->{tab} = $tab;
481 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
482 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
483 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
484 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
485 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
486 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
487 $res->{$tag}->{$subfield}->{seealso} = $seealso;
488 $res->{$tag}->{$subfield}->{hidden} = $hidden;
489 $res->{$tag}->{$subfield}->{isurl} = $isurl;
490 $res->{$tag}->{$subfield}->{link} = $link;
499 $authid= &AddAuthority($record, $authid,$authtypecode)
500 returns authid of the newly created authority
502 Either Create Or Modify existing authority.
508 # pass the MARC::Record to this function, and it will create the records in the authority table
509 my ($record,$authid,$authtypecode) = @_;
510 my $dbh=C4::Context->dbh;
511 my $leader=' a ';##Fixme correct leader as this one just adds utf8 to MARC21
513 # if authid empty => true add, find a new authid number
514 my $format= 'UNIMARCAUTH' if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC');
515 $format= 'MARC21' if (uc(C4::Context->preference('marcflavour')) ne 'UNIMARC');
516 if (($format eq "UNIMARCAUTH") && (!$record->subfield('100','a'))){
517 $record->leader(" nx j22 ");
518 my $date=POSIX::strftime("%Y%m%d",localtime);
519 if ($record->field('100')){
520 $record->field('100')->update('a'=>$date."afrey50 ba0");
522 $record->append_fields(
523 MARC::Field->new('100',' ',' '
524 ,'a'=>$date."afrey50 ba0")
529 my ($auth_type_tag, $auth_type_subfield) = get_auth_type_location($authtypecode);
530 if (!$authid and $format eq "MARC21") {
531 # only need to do this fix when modifying an existing authority
532 C4::AuthoritiesMarc::MARC21::fix_marc21_auth_type_location($record, $auth_type_tag, $auth_type_subfield);
535 unless ($record->field($auth_type_tag) && $record->subfield($auth_type_tag, $auth_type_subfield)) {
536 $record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode);
540 my $sth=$dbh->prepare("select max(authid) from auth_header");
542 ($authid)=$sth->fetchrow;
544 ##Insert the recordID in MARC record
545 unless ($record->field('001') && $record->field('001')->data() eq $authid){
546 $record->delete_field($record->field('001'));
547 $record->insert_fields_ordered(MARC::Field->new('001',$authid));
549 # warn $record->as_formatted;
550 $dbh->do("lock tables auth_header WRITE");
551 $sth=$dbh->prepare("insert into auth_header (authid,datecreated,authtypecode,marc,marcxml) values (?,now(),?,?,?)");
552 $sth->execute($authid,$authtypecode,$record->as_usmarc,$record->as_xml_record($format));
555 $record->add_fields('001',$authid) unless ($record->field('001'));
556 $dbh->do("lock tables auth_header WRITE");
557 my $sth=$dbh->prepare("update auth_header set marc=?,marcxml=? where authid=?");
558 $sth->execute($record->as_usmarc,$record->as_xml_record($format),$authid);
561 $dbh->do("unlock tables");
562 ModZebra($authid,'specialUpdate',"authorityserver",$record);
571 $authid= &DelAuthority($authid)
581 my $dbh=C4::Context->dbh;
583 ModZebra($authid,"recordDelete","authorityserver",GetAuthority($authid));
584 $dbh->do("delete from auth_header where authid=$authid") ;
589 my ($authid,$record,$authtypecode,$merge)=@_;
590 my $dbh=C4::Context->dbh;
591 # my ($oldrecord)=&GetAuthority($authid);
592 # if ($oldrecord eq $record) {
595 # my $sth=$dbh->prepare("update auth_header set marc=?,marcxml=? where authid=?");
596 #Now rewrite the $record to table with an add
597 $authid=AddAuthority($record,$authid,$authtypecode);
599 ### 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
600 ### they should have a system preference "dontmerge=1" otherwise by default biblios will be updated
601 ### the $merge flag is now depreceated and will be removed at code cleaning
602 if (C4::Context->preference('dontmerge') ){
603 # save the file in tmp/modified_authorities
604 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
605 unless (opendir(DIR,"$cgidir")) {
606 $cgidir = C4::Context->intranetdir."/";
610 my $filename = $cgidir."/tmp/modified_authorities/$authid.authid";
611 open AUTH, "> $filename";
615 # &merge($authid,$record,$authid,$record);
620 =head2 GetAuthorityXML
624 $marcxml= &GetAuthorityXML( $authid)
625 returns xml form of record $authid
630 sub GetAuthorityXML {
631 # Returns MARC::XML of the authority passed in parameter.
633 my $format= 'UNIMARCAUTH' if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC');
634 $format= 'MARC21' if (uc(C4::Context->preference('marcflavour')) ne 'UNIMARC');
635 if ($format eq "MARC21") {
636 # for MARC21, call GetAuthority instead of
637 # getting the XML directly since we may
638 # need to fix up the location of the authority
639 # code -- note that this is reasonably safe
640 # because GetAuthorityXML is used only by the
641 # indexing processes like zebraqueue_start.pl
642 my $record = GetAuthority($authid);
643 return $record->as_xml_record($format);
645 my $dbh=C4::Context->dbh;
646 my $sth = $dbh->prepare("select marcxml from auth_header where authid=? " );
647 $sth->execute($authid);
648 my ($marcxml)=$sth->fetchrow;
657 $record= &GetAuthority( $authid)
658 Returns MARC::Record of the authority passed in parameter.
665 my $dbh=C4::Context->dbh;
666 my $sth=$dbh->prepare("select authtypecode, marcxml from auth_header where authid=?");
667 $sth->execute($authid);
668 my ($authtypecode, $marcxml) = $sth->fetchrow;
669 my $record=MARC::Record->new_from_xml($marcxml,'UTF-8',(C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")));
670 $record->encoding('UTF-8');
671 if (C4::Context->preference("marcflavour") eq "MARC21") {
672 my ($auth_type_tag, $auth_type_subfield) = get_auth_type_location($authtypecode);
673 C4::AuthoritiesMarc::MARC21::fix_marc21_auth_type_location($record, $auth_type_tag, $auth_type_subfield);
682 $result= &GetAuthType( $authtypecode)
683 If $authtypecode is not "" then
684 Returns hashref to authtypecode information
686 returns ref to array of hashref information of all Authtypes
692 my ($authtypecode) = @_;
693 my $dbh=C4::Context->dbh;
695 if (defined $authtypecode){ # NOTE - in MARC21 framework, '' is a valid authority
697 $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
698 $sth->execute($authtypecode);
700 $sth=$dbh->prepare("select * from auth_types");
703 my $res=$sth->fetchall_arrayref({});
704 if (scalar(@$res)==1){
713 my ($rtags,$rsubfields,$rvalues,%indicators) = @_;
714 my $dbh=C4::Context->dbh;
716 my $record = MARC::Record->new();
717 #---- TODO : the leader is missing
719 # my %subfieldlist=();
720 my $prevvalue; # if tag <10
721 my $field; # if tag >=10
722 for (my $i=0; $i< @$rtags; $i++) {
723 # rebuild MARC::Record
724 if (@$rtags[$i] ne $prevtag) {
727 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
731 $record->add_fields($field);
734 $indicators{@$rtags[$i]}.=' ';
735 if (@$rtags[$i] <10) {
736 $prevvalue= @$rvalues[$i];
740 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
742 $prevtag = @$rtags[$i];
744 if (@$rtags[$i] <10) {
745 $prevvalue=@$rvalues[$i];
747 if (length(@$rvalues[$i])>0) {
748 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
751 $prevtag= @$rtags[$i];
754 # the last has not been included inside the loop... do it now !
755 $record->add_fields($field) if $field;
759 =head2 FindDuplicateAuthority
763 $record= &FindDuplicateAuthority( $record, $authtypecode)
764 return $authid,Summary if duplicate is found.
766 Comments : an improvement would be to return All the records that match.
772 sub FindDuplicateAuthority {
774 my ($record,$authtypecode)=@_;
775 # warn "IN for ".$record->as_formatted;
776 my $dbh = C4::Context->dbh;
777 # warn "".$record->as_formatted;
778 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
779 $sth->execute($authtypecode);
780 my ($auth_tag_to_report) = $sth->fetchrow;
782 # warn "record :".$record->as_formatted." auth_tag_to_report :$auth_tag_to_report";
783 # build a request for SearchAuthorities
784 my $query='at='.$authtypecode.' ';
785 map {$query.= " and he=\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/)} $record->field($auth_tag_to_report)->subfields() if $record->field($auth_tag_to_report);
786 my ($error,$results)=SimpleSearch($query,"authorityserver");
787 # there is at least 1 result => return the 1st one
789 my $marcrecord = MARC::File::USMARC::decode($results->[0]);
790 return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode);
792 # no result, returns nothing
800 $text= &BuildSummary( $record, $authid, $authtypecode)
801 return HTML encoded Summary
803 Comment : authtypecode can be infered from both record and authid.
804 Moreover, authid can also be inferred from $record.
805 Would it be interesting to delete those things.
812 ## give this a Marc record to return summary
813 my ($record,$authid,$authtypecode)=@_;
814 my $dbh=C4::Context->dbh;
816 # handle $authtypecode is NULL or eq ""
818 my $authref = GetAuthType($authtypecode);
819 $summary = $authref->{summary};
821 # FIXME: should use I18N.pm
823 $language{'fre'}="Français";
824 $language{'eng'}="Anglais";
825 $language{'ger'}="Allemand";
826 $language{'ita'}="Italien";
827 $language{'spa'}="Espagnol";
829 $thesaurus{'1'}="Peuples";
830 $thesaurus{'2'}="Anthroponymes";
831 $thesaurus{'3'}="Oeuvres";
832 $thesaurus{'4'}="Chronologie";
833 $thesaurus{'5'}="Lieux";
834 $thesaurus{'6'}="Sujets";
836 my @fields = $record->fields();
838 # if the library has a summary defined, use it. Otherwise, build a standard one
839 # FIXME - it appears that the summary field in the authority frameworks
840 # can work as a display template. However, this doesn't
841 # suit the MARC21 version, so for now the "templating"
842 # feature will be enabled only for UNIMARC for backwards
844 if ($summary and C4::Context->preference('marcflavour') eq 'UNIMARC') {
845 my @fields = $record->fields();
846 # $reported_tag = '$9'.$result[$counter];
847 foreach my $field (@fields) {
848 my $tag = $field->tag();
849 my $tagvalue = $field->as_string();
850 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
853 $reported_tag.='$3'.$field->data();
856 my @subf = $field->subfields;
857 for my $i (0..$#subf) {
858 my $subfieldcode = $subf[$i][0];
859 my $subfieldvalue = $subf[$i][1];
860 my $tagsubf = $tag.$subfieldcode;
861 $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
865 $summary =~ s/\[(.*?)]//g;
866 $summary =~ s/\n/<br>/g;
877 my @fields = $record->fields();
878 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
879 # construct UNIMARC summary, that is quite different from MARC21 one
881 foreach my $field ($record->field('2..')) {
882 $heading.= $field->subfield('a');
883 $authid=$field->subfield('3');
886 foreach my $field ($record->field('3..')) {
887 $notes.= '<span class="note">'.$field->subfield('a')."</span>\n";
889 foreach my $field ($record->field('4..')) {
890 my $thesaurus = "thes. : ".$thesaurus{"$field->subfield('2')"}." : " if ($field->subfield('2'));
891 $see.= '<span class="UF">'.$thesaurus.$field->subfield('a')."</span> -- \n";
894 foreach my $field ($record->field('5..')) {
896 if (($field->subfield('5')) && ($field->subfield('a')) && ($field->subfield('5') eq 'g')) {
897 $broaderterms.= '<span class="BT"> <a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
898 } elsif (($field->subfield('5')) && ($field->subfield('a')) && ($field->subfield('5') eq 'h')){
899 $narrowerterms.= '<span class="NT"><a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
900 } elsif ($field->subfield('a')) {
901 $seealso.= '<span class="RT"><a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
905 foreach my $field ($record->field('7..')) {
906 my $lang = substr($field->subfield('8'),3,3);
907 $seeheading.= '<span class="langue"> En '.$language{$lang}.' : </span><span class="OT"> '.$field->subfield('a')."</span><br />\n";
909 $broaderterms =~s/-- \n$//;
910 $narrowerterms =~s/-- \n$//;
911 $seealso =~s/-- \n$//;
913 $summary = "<b><a href=\"detail.pl?authid=$authid\">".$heading."</a></b><br />".($notes?"$notes <br />":"");
914 $summary.= '<p><div class="label">TG : '.$broaderterms.'</div></p>' if ($broaderterms);
915 $summary.= '<p><div class="label">TS : '.$narrowerterms.'</div></p>' if ($narrowerterms);
916 $summary.= '<p><div class="label">TA : '.$seealso.'</div></p>' if ($seealso);
917 $summary.= '<p><div class="label">EP : '.$see.'</div></p>' if ($see);
918 $summary.= '<p><div class="label">'.$seeheading.'</div></p>' if ($seeheading);
920 # construct MARC21 summary
921 # FIXME - looping over 1XX is questionable
922 # since MARC21 authority should have only one 1XX
923 foreach my $field ($record->field('1..')) {
924 next if "152" eq $field->tag(); # FIXME - 152 is not a good tag to use
925 # in MARC21 -- purely local tags really ought to be
927 if ($record->field('100')) {
928 $heading.= $field->as_string('abcdefghjklmnopqrstvxyz68');
929 } elsif ($record->field('110')) {
930 $heading.= $field->as_string('abcdefghklmnoprstvxyz68');
931 } elsif ($record->field('111')) {
932 $heading.= $field->as_string('acdefghklnpqstvxyz68');
933 } elsif ($record->field('130')) {
934 $heading.= $field->as_string('adfghklmnoprstvxyz68');
935 } elsif ($record->field('148')) {
936 $heading.= $field->as_string('abvxyz68');
937 } elsif ($record->field('150')) {
938 # $heading.= $field->as_string('abvxyz68');
939 $heading.= $field->as_formatted();
940 my $tag=$field->tag();
941 $heading=~s /^$tag//g;
942 $heading =~s /\_/\$/g;
943 } elsif ($record->field('151')) {
944 $heading.= $field->as_string('avxyz68');
945 } elsif ($record->field('155')) {
946 $heading.= $field->as_string('abvxyz68');
947 } elsif ($record->field('180')) {
948 $heading.= $field->as_string('vxyz68');
949 } elsif ($record->field('181')) {
950 $heading.= $field->as_string('vxyz68');
951 } elsif ($record->field('182')) {
952 $heading.= $field->as_string('vxyz68');
953 } elsif ($record->field('185')) {
954 $heading.= $field->as_string('vxyz68');
956 $heading.= $field->as_string();
959 foreach my $field ($record->field('4..')) {
960 $seeheading.= "<br /> <i>used for/see from:</i> ".$field->as_string();
962 foreach my $field ($record->field('5..')) {
963 $altheading.= "<br /> <i>see also:</i> ".$field->as_string();
965 $summary .= ": " if $summary;
966 $summary.=$heading.$seeheading.$altheading;
972 =head2 BuildUnimarcHierarchies
976 $text= &BuildUnimarcHierarchies( $authid, $force)
977 return text containing trees for hierarchies
978 for them to be stored in auth_header
981 122,1314,2452;1324,2342,3,2452
986 sub BuildUnimarcHierarchies{
987 my $authid = shift @_;
988 # warn "authid : $authid";
989 my $force = shift @_;
991 my $dbh=C4::Context->dbh;
993 my $data = GetHeaderAuthority($authid);
994 if ($data->{'authtrees'} and not $force){
995 return $data->{'authtrees'};
996 } elsif ($data->{'authtrees'}){
997 $hierarchies=$data->{'authtrees'};
999 my $record = GetAuthority($authid);
1001 foreach my $field ($record->field('550')){
1002 if ($field->subfield('5') && $field->subfield('5') eq 'g'){
1003 my $parentrecord = GetAuthority($field->subfield('3'));
1004 my $localresult=$hierarchies;
1006 $trees = BuildUnimarcHierarchies($field->subfield('3'));
1009 @trees = split(/;/,$trees);
1011 push @trees, $trees;
1016 @globalresult = (@globalresult,@trees);
1019 $hierarchies=join(";",@globalresult);
1021 #Unless there is no ancestor, I am alone.
1022 $hierarchies="$authid" unless ($hierarchies);
1024 AddAuthorityTrees($authid,$hierarchies);
1025 return $hierarchies;
1028 =head2 BuildUnimarcHierarchy
1032 $ref= &BuildUnimarcHierarchy( $record, $class,$authid)
1033 return a hashref in order to display hierarchy for record and final Authid $authid
1044 Those two latest ones should disappear soon.
1049 sub BuildUnimarcHierarchy{
1050 my $record = shift @_;
1051 my $class = shift @_;
1052 my $authid_constructed = shift @_;
1053 my $authid=$record->subfield('250','3');
1055 my $parents=""; my $children="";
1056 my (@loopparents,@loopchildren);
1057 foreach my $field ($record->field('550')){
1058 if ($field->subfield('5') && $field->subfield('a')){
1059 if ($field->subfield('5') eq 'h'){
1060 push @loopchildren, { "childauthid"=>$field->subfield('3'),"childvalue"=>$field->subfield('a')};
1061 }elsif ($field->subfield('5') eq 'g'){
1062 push @loopparents, { "parentauthid"=>$field->subfield('3'),"parentvalue"=>$field->subfield('a')};
1064 # brothers could get in there with an else
1067 $cell{"ifparents"}=1 if (scalar(@loopparents)>0);
1068 $cell{"ifchildren"}=1 if (scalar(@loopchildren)>0);
1069 $cell{"loopparents"}=\@loopparents if (scalar(@loopparents)>0);
1070 $cell{"loopchildren"}=\@loopchildren if (scalar(@loopchildren)>0);
1071 $cell{"class"}=$class;
1072 $cell{"loopauthid"}=$authid;
1073 $cell{"current_value"} =1 if $authid eq $authid_constructed;
1074 $cell{"value"}=$record->subfield('250',"a");
1078 =head2 GetHeaderAuthority
1082 $ref= &GetHeaderAuthority( $authid)
1083 return a hashref in order auth_header table data
1088 sub GetHeaderAuthority{
1089 my $authid = shift @_;
1090 my $sql= "SELECT * from auth_header WHERE authid = ?";
1091 my $dbh=C4::Context->dbh;
1092 my $rq= $dbh->prepare($sql);
1093 $rq->execute($authid);
1094 my $data= $rq->fetchrow_hashref;
1098 =head2 AddAuthorityTrees
1102 $ref= &AddAuthorityTrees( $authid, $trees)
1103 return success or failure
1109 sub AddAuthorityTrees{
1110 my $authid = shift @_;
1111 my $trees = shift @_;
1112 my $sql= "UPDATE IGNORE auth_header set authtrees=? WHERE authid = ?";
1113 my $dbh=C4::Context->dbh;
1114 my $rq= $dbh->prepare($sql);
1115 return $rq->execute($trees,$authid);
1122 $ref= &merge(mergefrom,$MARCfrom,$mergeto,$MARCto)
1125 Could add some feature : Migrating from a typecode to an other for instance.
1126 Then we should add some new parameter : bibliotargettag, authtargettag
1132 my ($mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
1133 my $dbh=C4::Context->dbh;
1134 my $authtypecodefrom = GetAuthTypeCode($mergefrom);
1135 my $authtypecodeto = GetAuthTypeCode($mergeto);
1136 # return if authority does not exist
1137 my @X = $MARCfrom->fields();
1138 return if $#X == -1;
1139 @X = $MARCto->fields();
1140 return if $#X == -1;
1141 # search the tag to report
1142 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
1143 $sth->execute($authtypecodefrom);
1144 my ($auth_tag_to_report) = $sth->fetchrow;
1147 @record_to = $MARCto->field($auth_tag_to_report)->subfields() if $MARCto->field($auth_tag_to_report);
1149 @record_from = $MARCfrom->field($auth_tag_to_report)->subfields() if $MARCfrom->field($auth_tag_to_report);
1151 # search all biblio tags using this authority.
1152 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1153 $sth->execute($authtypecodefrom);
1154 my @tags_using_authtype;
1155 while (my ($tagfield) = $sth->fetchrow) {
1156 push @tags_using_authtype,$tagfield."9" ;
1159 if (C4::Context->preference('NoZebra')) {
1162 # now, find every biblio using this authority
1163 my $oConnection=C4::Context->Zconn("biblioserver");
1165 $query= "an= ".$mergefrom;
1166 my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1167 my $count=$oResult->size() if ($oResult);
1170 while ( $z<$count ) {
1172 $rec=$oResult->record($z);
1173 my $marcdata = $rec->raw();
1174 push @reccache, $marcdata;
1177 $oResult->destroy();
1178 foreach my $marc(@reccache){
1181 $marcrecord = MARC::File::USMARC::decode($marc);
1182 foreach my $tagfield (@tags_using_authtype){
1183 $tagfield=substr($tagfield,0,3);
1184 my @tags = $marcrecord->field($tagfield);
1185 foreach my $tag (@tags){
1186 my $tagsubs=$tag->subfield("9");
1187 #warn "$tagfield:$tagsubs:$mergefrom";
1188 if ($tagsubs== $mergefrom) {
1189 $tag->update("9" =>$mergeto);
1190 foreach my $subfield (@record_to) {
1191 # warn "$subfield,$subfield->[0],$subfield->[1]";
1192 $tag->update($subfield->[0] =>$subfield->[1]);
1195 $marcrecord->delete_field($tag);
1196 $marcrecord->add_fields($tag);
1200 my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,"") ;
1202 &ModBiblio($marcrecord,$oldbiblio->{'biblionumber'},GetFrameworkCode($oldbiblio->{'biblionumber'})) ;
1207 # now, find every other authority linked with this authority
1208 # my $oConnection=C4::Context->Zconn("authorityserver");
1210 # # att 9210 Auth-Internal-authtype
1211 # # att 9220 Auth-Internal-LN
1212 # # ccl.properties to add for authorities
1213 # $query= "= ".$mergefrom;
1214 # my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1215 # my $count=$oResult->size() if ($oResult);
1218 # while ( $z<$count ) {
1220 # $rec=$oResult->record($z);
1221 # my $marcdata = $rec->raw();
1222 # push @reccache, $marcdata;
1225 # $oResult->destroy();
1226 # foreach my $marc(@reccache){
1229 # $marcrecord = MARC::File::USMARC::decode($marc);
1230 # foreach my $tagfield (@tags_using_authtype){
1231 # $tagfield=substr($tagfield,0,3);
1232 # my @tags = $marcrecord->field($tagfield);
1233 # foreach my $tag (@tags){
1234 # my $tagsubs=$tag->subfield("9");
1235 # #warn "$tagfield:$tagsubs:$mergefrom";
1236 # if ($tagsubs== $mergefrom) {
1237 # $tag->update("9" =>$mergeto);
1238 # foreach my $subfield (@record_to) {
1239 # # warn "$subfield,$subfield->[0],$subfield->[1]";
1240 # $tag->update($subfield->[0] =>$subfield->[1]);
1243 # $marcrecord->delete_field($tag);
1244 # $marcrecord->add_fields($tag);
1247 # }#foreach tagfield
1248 # my $authoritynumber = TransformMarcToKoha($dbh,$marcrecord,"") ;
1250 # &ModAuthority($marcrecord,$authoritynumber,GetAuthTypeCode($authoritynumber)) ;
1256 =head2 get_auth_type_location
1260 my ($tag, $subfield) = get_auth_type_location($auth_type_code);
1264 Get the tag and subfield used to store the heading type
1265 for indexing purposes. The C<$auth_type> parameter is
1266 optional; if it is not supplied, assume ''.
1268 This routine searches the MARC authority framework
1269 for the tag and subfield whose kohafield is
1270 C<auth_header.authtypecode>; if no such field is
1271 defined in the framework, default to the hardcoded value
1272 specific to the MARC format.
1276 sub get_auth_type_location {
1277 my $auth_type_code = @_ ? shift : '';
1279 my ($tag, $subfield) = GetAuthMARCFromKohaField('auth_header.authtypecode', $auth_type_code);
1280 if (defined $tag and defined $subfield and $tag != 0 and $subfield != 0) {
1281 return ($tag, $subfield);
1283 if (C4::Context->preference('marcflavour') eq "MARC21") {
1284 return C4::AuthoritiesMarc::MARC21::default_auth_type_location();
1286 return C4::AuthoritiesMarc::UNIMARC::default_auth_type_location();
1291 END { } # module clean-up code here (global destructor)
1295 Koha Developement team <info@koha.org>
1297 Paul POULAIN paul.poulain@free.fr