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
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 use C4::AuthoritiesMarc::MARC21;
26 use C4::AuthoritiesMarc::UNIMARC;
31 use vars qw($VERSION @ISA @EXPORT);
34 # set the version for version checking
35 $VERSION = 3.07.00.049;
43 &GetAuthMARCFromKohaField
61 &FindDuplicateAuthority
73 =head2 GetAuthMARCFromKohaField
75 ( $tag, $subfield ) = &GetAuthMARCFromKohaField ($kohafield,$authtypecode);
77 returns tag and subfield linked to kohafield
80 Suppose Kohafield is only linked to ONE subfield
84 sub GetAuthMARCFromKohaField {
85 #AUTHfind_marc_from_kohafield
86 my ( $kohafield,$authtypecode ) = @_;
87 my $dbh=C4::Context->dbh;
88 return 0, 0 unless $kohafield;
89 $authtypecode="" unless $authtypecode;
90 my $marcfromkohafield;
91 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
92 $sth->execute($kohafield,$authtypecode);
93 my ($tagfield,$tagsubfield) = $sth->fetchrow;
95 return ($tagfield,$tagsubfield);
98 =head2 SearchAuthorities
100 (\@finalresult, $nbresults)= &SearchAuthorities($tags, $and_or,
101 $excluding, $operator, $value, $offset,$length,$authtypecode,
102 $sortby[, $skipmetadata])
104 returns ref to array result and count of results returned
108 sub SearchAuthorities {
109 my ($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby,$skipmetadata) = @_;
110 # warn Dumper($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby);
111 my $dbh=C4::Context->dbh;
115 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
117 # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
118 # the authtypecode. Then, search on $a of this tag_to_report
119 # also store main entry MARC tag, to extract it at end of search
121 ##first set the authtype search and may be multiple authorities
125 my @auths=split / /,$authtypecode ;
126 foreach my $auth (@auths){
127 $query .=" \@attr 1=authtype \@attr 5=100 ".$auth; ##No truncation on authtype
128 push @authtypecode ,$auth;
132 while ($n>1){$query= "\@or ".$query;$n--;}
135 $qpquery .= '(authtype:' . join('|| authtype:', @auths) . ')';
143 for(my $i = 0 ; $i <= $#{$value} ; $i++)
146 if ( @$tags[$i] eq "mainmainentry" ) {
147 $attr = " \@attr 1=Heading-Main ";
149 elsif ( @$tags[$i] eq "mainentry" ) {
150 $attr = " \@attr 1=Heading ";
152 elsif ( @$tags[$i] eq "match" ) {
153 $attr = " \@attr 1=Match ";
155 elsif ( @$tags[$i] eq "match-heading" ) {
156 $attr = " \@attr 1=Match-heading ";
158 elsif ( @$tags[$i] eq "see-from" ) {
159 $attr = " \@attr 1=Match-heading-see-from ";
161 elsif ( @$tags[$i] eq "thesaurus" ) {
162 $attr = " \@attr 1=Subject-heading-thesaurus ";
164 else { # Assume any if no index was specified
165 $attr = " \@attr 1=Any ";
167 if ( @$operator[$i] eq 'is' ) {
168 $attr .= " \@attr 4=1 \@attr 5=100 "
169 ; ##Phrase, No truncation,all of subfield field must match
171 elsif ( @$operator[$i] eq "=" ) {
172 $attr .= " \@attr 4=107 "; #Number Exact match
174 elsif ( @$operator[$i] eq "start" ) {
175 $attr .= " \@attr 3=2 \@attr 4=1 \@attr 5=1 "
176 ; #Firstinfield Phrase, Right truncated
178 elsif ( @$operator[$i] eq "exact" ) {
179 $attr .= " \@attr 4=1 \@attr 5=100 \@attr 6=3 "
180 ; ##Phrase, No truncation,all of subfield field must match
183 $attr .= " \@attr 5=1 \@attr 4=6 "
184 ; ## Word list, right truncated, anywhere
185 if ($sortby eq 'Relevance') {
186 $attr .= "\@attr 2=102 ";
189 @$value[$i] =~ s/"/\\"/g; # Escape the double-quotes in the search value
190 $attr =$attr."\"".@$value[$i]."\"";
195 $qpquery .= " $tags->[$i]:\"$value->[$i]\"";
199 ##Add how many queries generated
200 if (defined $query && $query=~/\S+/){
201 $query= $and x $attr_cnt . $query . (defined $q2 ? $q2 : '');
206 #$query=' @or @attr 7=2 @attr 1=Heading 0 @or @attr 7=1 @attr 1=Heading 1'.$query if ($sortby eq "HeadingDsc");
208 if ($sortby eq 'HeadingAsc') {
209 $orderstring = '@attr 7=1 @attr 1=Heading 0';
210 } elsif ($sortby eq 'HeadingDsc') {
211 $orderstring = '@attr 7=2 @attr 1=Heading 0';
212 } elsif ($sortby eq 'AuthidAsc') {
213 $orderstring = '@attr 7=1 @attr 4=109 @attr 1=Local-Number 0';
214 } elsif ($sortby eq 'AuthidDsc') {
215 $orderstring = '@attr 7=2 @attr 4=109 @attr 1=Local-Number 0';
218 $qpquery .= ' all:all' unless $value->[0];
220 if ( $value->[0] =~ m/^qp=(.*)$/ ) {
224 $qpquery .= " #$sortby" unless $sortby eq '';
226 $QParser->parse( $qpquery );
227 $query = $QParser->target_syntax('authorityserver');
229 $query=($query?$query:"\@attr 1=_ALLRECORDS \@attr 2=103 ''");
230 $query="\@or $orderstring $query" if $orderstring;
233 $offset=0 unless $offset;
234 my $counter = $offset;
235 $length=10 unless $length;
238 $oAuth[0]=C4::Context->Zconn("authorityserver" , 1);
239 my $Anewq= new ZOOM::Query::PQF($query,$oAuth[0]);
241 $oAResult= $oAuth[0]->search($Anewq) ;
242 while (($i = ZOOM::event(\@oAuth)) != 0) {
243 my $ev = $oAuth[$i-1]->last_event();
244 last if $ev == ZOOM::Event::ZEND;
246 my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
248 warn "oAuth error: $errmsg ($error) $addinfo $diagset\n";
253 $nbresults=$oAResult->size();
254 my $nremains=$nbresults;
256 my @finalresult = ();
260 ##Find authid and linkid fields
261 ##we may be searching multiple authoritytypes.
262 ## FIXME this assumes that all authid and linkid fields are the same for all authority types
263 # my ($authidfield,$authidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.authid",$authtypecode[0]);
264 # my ($linkidfield,$linkidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.linkid",$authtypecode[0]);
265 while (($counter < $nbresults) && ($counter < ($offset + $length))) {
267 ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
268 my $rec=$oAResult->record($counter);
269 my $separator=C4::Context->preference('authoritysep');
270 my $authrecord = C4::Search::new_record_from_zebra(
275 if ( !defined $authrecord or !defined $authrecord->field('001') ) {
280 my $authid=$authrecord->field('001')->data();
282 $newline{authid} = $authid;
283 if ( !$skipmetadata ) {
285 "SELECT auth_tag_to_report FROM auth_types WHERE authtypecode=?";
286 my $sth = $dbh->prepare($query_auth_tag);
287 $sth->execute($authtypecode);
288 my $auth_tag_to_report = $sth->fetchrow;
290 my $mainentry = $authrecord->field($auth_tag_to_report);
292 foreach ( $mainentry->subfields() ) {
293 $reported_tag .= '$' . $_->[0] . $_->[1];
296 my $thisauthtypecode = GetAuthTypeCode($authid);
297 my $thisauthtype = GetAuthType($thisauthtypecode);
298 unless (defined $thisauthtype) {
299 $thisauthtypecode = $authtypecode;
300 $thisauthtype = GetAuthType($authtypecode);
302 my $summary = BuildSummary( $authrecord, $authid, $thisauthtypecode );
304 $newline{authtype} = defined($thisauthtype) ?
305 $thisauthtype->{'authtypetext'} : '';
306 $newline{summary} = $summary;
307 $newline{even} = $counter % 2;
308 $newline{reported_tag} = $reported_tag;
311 push @finalresult, \%newline;
314 if (! $skipmetadata) {
315 for (my $z=0; $z<@finalresult; $z++){
316 my $count=CountUsage($finalresult[$z]{authid});
317 $finalresult[$z]{used}=$count;
323 $oAResult->destroy();
324 # $oAuth[0]->destroy();
326 return (\@finalresult, $nbresults);
331 $count= &CountUsage($authid)
333 counts Usage of Authid in bibliorecords.
341 $query= "an:".$authid;
342 my ($err,$res,$result) = C4::Search::SimpleSearch($query,0,10);
344 warn "Error: $err from search $query";
351 =head2 CountUsageChildren
353 $count= &CountUsageChildren($authid)
355 counts Usage of narrower terms of Authid in bibliorecords.
359 sub CountUsageChildren {
363 =head2 GetAuthTypeCode
365 $authtypecode= &GetAuthTypeCode($authid)
367 returns authtypecode of an authid
371 sub GetAuthTypeCode {
372 #AUTHfind_authtypecode
374 my $dbh=C4::Context->dbh;
375 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
376 $sth->execute($authid);
377 my $authtypecode = $sth->fetchrow;
378 return $authtypecode;
381 =head2 GuessAuthTypeCode
383 my $authtypecode = GuessAuthTypeCode($record);
385 Get the record and tries to guess the adequate authtypecode from its content.
389 sub GuessAuthTypeCode {
390 my ($record, $heading_fields) = @_;
391 return unless defined $record;
392 $heading_fields //= {
394 '100'=>{authtypecode=>'PERSO_NAME'},
395 '110'=>{authtypecode=>'CORPO_NAME'},
396 '111'=>{authtypecode=>'MEETI_NAME'},
397 '130'=>{authtypecode=>'UNIF_TITLE'},
398 '148'=>{authtypecode=>'CHRON_TERM'},
399 '150'=>{authtypecode=>'TOPIC_TERM'},
400 '151'=>{authtypecode=>'GEOGR_NAME'},
401 '155'=>{authtypecode=>'GENRE/FORM'},
402 '180'=>{authtypecode=>'GEN_SUBDIV'},
403 '181'=>{authtypecode=>'GEO_SUBDIV'},
404 '182'=>{authtypecode=>'CHRON_SUBD'},
405 '185'=>{authtypecode=>'FORM_SUBD'},
407 #200 Personal name 700, 701, 702 4-- with embedded 700, 701, 702 600
408 # 604 with embedded 700, 701, 702
409 #210 Corporate or meeting name 710, 711, 712 4-- with embedded 710, 711, 712 601 604 with embedded 710, 711, 712
410 #215 Territorial or geographic name 710, 711, 712 4-- with embedded 710, 711, 712 601, 607 604 with embedded 710, 711, 712
411 #216 Trademark 716 [Reserved for future use]
412 #220 Family name 720, 721, 722 4-- with embedded 720, 721, 722 602 604 with embedded 720, 721, 722
413 #230 Title 500 4-- with embedded 500 605
414 #240 Name and title (embedded 200, 210, 215, or 220 and 230) 4-- with embedded 7-- and 500 7-- 604 with embedded 7-- and 500 500
415 #245 Name and collective title (embedded 200, 210, 215, or 220 and 235) 4-- with embedded 7-- and 501 604 with embedded 7-- and 501 7-- 501
416 #250 Topical subject 606
417 #260 Place access 620
418 #280 Form, genre or physical characteristics 608
421 # Could also be represented with :
423 #a = personal name entry
424 #b = corporate name entry
425 #c = territorial or geographical name
429 #g = collective uniform title
431 #i = name/collective uniform title
434 #l = form, genre or physical characteristics
436 '200'=>{authtypecode=>'NP'},
437 '210'=>{authtypecode=>'CO'},
438 '215'=>{authtypecode=>'SNG'},
439 '216'=>{authtypecode=>'TM'},
440 '220'=>{authtypecode=>'FAM'},
441 '230'=>{authtypecode=>'TU'},
442 '235'=>{authtypecode=>'CO_UNI_TI'},
443 '240'=>{authtypecode=>'SAUTTIT'},
444 '245'=>{authtypecode=>'NAME_COL'},
445 '250'=>{authtypecode=>'SNC'},
446 '260'=>{authtypecode=>'PA'},
447 '280'=>{authtypecode=>'GENRE/FORM'},
450 foreach my $field (keys %{$heading_fields->{uc(C4::Context->preference('marcflavour'))} }) {
451 return $heading_fields->{uc(C4::Context->preference('marcflavour'))}->{$field}->{'authtypecode'} if (defined $record->field($field));
458 my $authtid = GuessAuthId($record);
460 Get the record and tries to guess the adequate authtypecode from its content.
466 return unless ($record && $record->field('001'));
467 # my $authtypecode=GuessAuthTypeCode($record);
468 # my ($tag,$subfield)=GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
469 # if ($tag > 010) {return $record->subfield($tag,$subfield)}
470 # else {return $record->field($tag)->data}
471 return $record->field('001')->data;
476 $tagslabel= &GetTagsLabels($forlibrarian,$authtypecode)
478 returns a ref to hashref of authorities tag and subfield structure.
482 $tagslabel->{$tag}->{$subfield}->{'attribute'}
484 where attribute takes values in :
502 my ($forlibrarian,$authtypecode)= @_;
503 my $dbh=C4::Context->dbh;
504 $authtypecode="" unless $authtypecode;
506 my $libfield = ($forlibrarian == 1)? 'liblibrarian' : 'libopac';
509 # check that authority exists
510 $sth=$dbh->prepare("SELECT count(*) FROM auth_tag_structure WHERE authtypecode=?");
511 $sth->execute($authtypecode);
512 my ($total) = $sth->fetchrow;
513 $authtypecode="" unless ($total >0);
515 "SELECT auth_tag_structure.tagfield,auth_tag_structure.liblibrarian,auth_tag_structure.libopac,auth_tag_structure.mandatory,auth_tag_structure.repeatable
516 FROM auth_tag_structure
521 $sth->execute($authtypecode);
522 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
524 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
525 $res->{$tag}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
526 $res->{$tag}->{tab} = " "; # XXX
527 $res->{$tag}->{mandatory} = $mandatory;
528 $res->{$tag}->{repeatable} = $repeatable;
531 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,frameworkcode as authtypecode,value_builder,kohafield,seealso,hidden,isurl,defaultvalue
532 FROM auth_subfield_structure
534 ORDER BY tagfield,tagsubfield"
536 $sth->execute($authtypecode);
539 my $authorised_value;
549 ( $tag, $subfield, $liblibrarian, , $libopac, $tab,
550 $mandatory, $repeatable, $authorised_value, $authtypecode,
551 $value_builder, $kohafield, $seealso, $hidden,
552 $isurl, $defaultvalue, $link )
556 $res->{$tag}->{$subfield}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
557 $res->{$tag}->{$subfield}->{tab} = $tab;
558 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
559 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
560 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
561 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
562 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
563 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
564 $res->{$tag}->{$subfield}->{seealso} = $seealso;
565 $res->{$tag}->{$subfield}->{hidden} = $hidden;
566 $res->{$tag}->{$subfield}->{isurl} = $isurl;
567 $res->{$tag}->{$subfield}->{link} = $link;
568 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
575 $authid= &AddAuthority($record, $authid,$authtypecode)
577 Either Create Or Modify existing authority.
578 returns authid of the newly created authority
583 # pass the MARC::Record to this function, and it will create the records in the authority table
584 my ($record,$authid,$authtypecode) = @_;
585 my $dbh=C4::Context->dbh;
586 my $leader=' nz a22 o 4500';#Leader for incomplete MARC21 record
588 # if authid empty => true add, find a new authid number
590 if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') {
591 $format= 'UNIMARCAUTH';
597 #update date/time to 005 for marc and unimarc
598 my $time=POSIX::strftime("%Y%m%d%H%M%S",localtime);
599 my $f5=$record->field('005');
601 $record->insert_fields_ordered( MARC::Field->new('005',$time.".0") );
604 $f5->update($time.".0");
607 SetUTF8Flag($record);
608 if ($format eq "MARC21") {
609 if (!$record->leader) {
610 $record->leader($leader);
612 if (!$record->field('003')) {
613 $record->insert_fields_ordered(
614 MARC::Field->new('003',C4::Context->preference('MARCOrgCode'))
617 my $date=POSIX::strftime("%y%m%d",localtime);
618 if (!$record->field('008')) {
619 # Get a valid default value for field 008
620 my $default_008 = C4::Context->preference('MARCAuthorityControlField008');
621 if(!$default_008 or length($default_008)<34) {
622 $default_008 = '|| aca||aabn | a|a d';
625 $default_008 = substr($default_008,0,34);
628 $record->insert_fields_ordered( MARC::Field->new('008',$date.$default_008) );
630 if (!$record->field('040')) {
631 $record->insert_fields_ordered(
632 MARC::Field->new('040','','',
633 'a' => C4::Context->preference('MARCOrgCode'),
634 'c' => C4::Context->preference('MARCOrgCode')
640 if ($format eq "UNIMARCAUTH") {
641 $record->leader(" nx j22 ") unless ($record->leader());
642 my $date=POSIX::strftime("%Y%m%d",localtime);
643 my $defaultfield100 = C4::Context->preference('UNIMARCAuthorityField100');
644 if (my $string=$record->subfield('100',"a")){
645 $string=~s/fre50/frey50/;
646 $record->field('100')->update('a'=>$string);
648 elsif ($record->field('100')){
649 $record->field('100')->update('a'=>$date.$defaultfield100);
651 $record->append_fields(
652 MARC::Field->new('100',' ',' '
653 ,'a'=>$date.$defaultfield100)
657 my ($auth_type_tag, $auth_type_subfield) = get_auth_type_location($authtypecode);
658 if (!$authid and $format eq "MARC21") {
659 # only need to do this fix when modifying an existing authority
660 C4::AuthoritiesMarc::MARC21::fix_marc21_auth_type_location($record, $auth_type_tag, $auth_type_subfield);
662 if (my $field=$record->field($auth_type_tag)){
663 $field->update($auth_type_subfield=>$authtypecode);
666 $record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode);
672 my $sth=$dbh->prepare("select max(authid) from auth_header");
674 ($authid)=$sth->fetchrow;
676 ##Insert the recordID in MARC record
677 unless ($record->field('001') && $record->field('001')->data() eq $authid){
678 $record->delete_field($record->field('001'));
679 $record->insert_fields_ordered(MARC::Field->new('001',$authid));
682 $auth_exists=$dbh->do(qq(select authid from auth_header where authid=?),undef,$authid);
683 # warn "auth_exists = $auth_exists";
686 $oldRecord=GetAuthority($authid);
687 $record->add_fields('001',$authid) unless ($record->field('001'));
688 # warn "\n\n\n enregistrement".$record->as_formatted;
689 my $sth=$dbh->prepare("update auth_header set authtypecode=?,marc=?,marcxml=? where authid=?");
690 $sth->execute($authtypecode,$record->as_usmarc,$record->as_xml_record($format),$authid) or die $sth->errstr;
694 my $sth=$dbh->prepare("insert into auth_header (authid,datecreated,authtypecode,marc,marcxml) values (?,now(),?,?,?)");
695 $sth->execute($authid,$authtypecode,$record->as_usmarc,$record->as_xml_record($format));
697 logaction( "AUTHORITIES", "ADD", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
699 ModZebra($authid,'specialUpdate',"authorityserver",$oldRecord,$record);
706 $authid= &DelAuthority($authid)
714 my $dbh=C4::Context->dbh;
716 logaction( "AUTHORITIES", "DELETE", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
717 ModZebra($authid,"recordDelete","authorityserver",GetAuthority($authid),undef);
718 my $sth = $dbh->prepare("DELETE FROM auth_header WHERE authid=?");
719 $sth->execute($authid);
724 $authid= &ModAuthority($authid,$record,$authtypecode)
726 Modifies authority record, optionally updates attached biblios.
731 my ($authid,$record,$authtypecode)=@_; # deprecated $merge parameter removed
733 my $dbh=C4::Context->dbh;
734 #Now rewrite the $record to table with an add
735 my $oldrecord=GetAuthority($authid);
736 $authid=AddAuthority($record,$authid,$authtypecode);
738 # If a library thinks that updating all biblios is a long process and wishes
739 # to leave that to a cron job, use misc/migration_tools/merge_authority.pl.
740 # In that case set system preference "dontmerge" to 1. Otherwise biblios will
742 unless(C4::Context->preference('dontmerge') eq '1'){
743 &merge($authid,$oldrecord,$authid,$record);
745 # save a record in need_merge_authorities table
746 my $sqlinsert="INSERT INTO need_merge_authorities (authid, done) ".
748 $dbh->do($sqlinsert,undef,($authid,0));
750 logaction( "AUTHORITIES", "MODIFY", $authid, "authority BEFORE=>" . $oldrecord->as_formatted ) if C4::Context->preference("AuthoritiesLog");
754 =head2 GetAuthorityXML
756 $marcxml= &GetAuthorityXML( $authid)
758 returns xml form of record $authid
762 sub GetAuthorityXML {
763 # Returns MARC::XML of the authority passed in parameter.
765 if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') {
766 my $dbh=C4::Context->dbh;
767 my $sth = $dbh->prepare("select marcxml from auth_header where authid=? " );
768 $sth->execute($authid);
769 my ($marcxml)=$sth->fetchrow;
773 # for MARC21, call GetAuthority instead of
774 # getting the XML directly since we may
775 # need to fix up the location of the authority
776 # code -- note that this is reasonably safe
777 # because GetAuthorityXML is used only by the
778 # indexing processes like zebraqueue_start.pl
779 my $record = GetAuthority($authid);
780 return $record->as_xml_record('MARC21');
786 $record= &GetAuthority( $authid)
788 Returns MARC::Record of the authority passed in parameter.
794 my $authority = Koha::Authority->get_from_authid($authid);
795 return unless $authority;
796 return ($authority->record);
801 $result = &GetAuthType($authtypecode)
803 If the authority type specified by C<$authtypecode> exists,
804 returns a hashref of the type's fields. If the type
805 does not exist, returns undef.
810 my ($authtypecode) = @_;
811 my $dbh=C4::Context->dbh;
813 if (defined $authtypecode){ # NOTE - in MARC21 framework, '' is a valid authority
814 # type (FIXME but why?)
815 $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
816 $sth->execute($authtypecode);
817 if (my $res = $sth->fetchrow_hashref) {
825 =head2 FindDuplicateAuthority
827 $record= &FindDuplicateAuthority( $record, $authtypecode)
829 return $authid,Summary if duplicate is found.
831 Comments : an improvement would be to return All the records that match.
835 sub FindDuplicateAuthority {
837 my ($record,$authtypecode)=@_;
838 # warn "IN for ".$record->as_formatted;
839 my $dbh = C4::Context->dbh;
840 # warn "".$record->as_formatted;
841 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
842 $sth->execute($authtypecode);
843 my ($auth_tag_to_report) = $sth->fetchrow;
845 # warn "record :".$record->as_formatted." auth_tag_to_report :$auth_tag_to_report";
846 # build a request for SearchAuthorities
848 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
855 my $query='at:'.$authtypecode.' ';
856 my $filtervalues=qr([\001-\040\!\'\"\`\#\$\%\&\*\+,\-\./:;<=>\?\@\(\)\{\[\]\}_\|\~]);
857 if ($record->field($auth_tag_to_report)) {
858 foreach ($record->field($auth_tag_to_report)->subfields()) {
859 $_->[1]=~s/$filtervalues/ /g; $query.= " $op he:\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/);
862 my ($error, $results, $total_hits) = C4::Search::SimpleSearch( $query, 0, 1, [ "authorityserver" ] );
863 # there is at least 1 result => return the 1st one
864 if (!defined $error && @{$results} ) {
865 my $marcrecord = C4::Search::new_record_from_zebra(
869 return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode);
871 # no result, returns nothing
877 $summary= &BuildSummary( $record, $authid, $authtypecode)
879 Returns a hashref with a summary of the specified record.
881 Comment : authtypecode can be infered from both record and authid.
882 Moreover, authid can also be inferred from $record.
883 Would it be interesting to delete those things.
888 ## give this a Marc record to return summary
889 my ($record,$authid,$authtypecode)=@_;
890 my $dbh=C4::Context->dbh;
892 my $summary_template;
893 # handle $authtypecode is NULL or eq ""
895 my $authref = GetAuthType($authtypecode);
896 $summary{authtypecode} = $authref->{authtypecode};
897 $summary{type} = $authref->{authtypetext};
898 $summary_template = $authref->{summary};
900 my $marc21subfields = 'abcdfghjklmnopqrstuvxyz68';
901 my %marc21controlrefs = ( 'a' => 'earlier',
907 'n' => 'notapplicable',
911 my %unimarc_relation_from_code = (
917 $thesaurus{'1'}="Peuples";
918 $thesaurus{'2'}="Anthroponymes";
919 $thesaurus{'3'}="Oeuvres";
920 $thesaurus{'4'}="Chronologie";
921 $thesaurus{'5'}="Lieux";
922 $thesaurus{'6'}="Sujets";
925 # if the library has a summary defined, use it. Otherwise, build a standard one
926 # FIXME - it appears that the summary field in the authority frameworks
927 # can work as a display template. However, this doesn't
928 # suit the MARC21 version, so for now the "templating"
929 # feature will be enabled only for UNIMARC for backwards
931 if ($summary_template and C4::Context->preference('marcflavour') eq 'UNIMARC') {
932 my @fields = $record->fields();
933 # $reported_tag = '$9'.$result[$counter];
935 foreach my $field (@fields) {
936 my $tag = $field->tag();
937 my $tagvalue = $field->as_string();
938 my $localsummary= $summary_template;
939 $localsummary =~ s/\[(.?.?.?.?)$tag\*(.*?)\]/$1$tagvalue$2\[$1$tag$2\]/g;
942 $reported_tag.='$3'.$field->data();
945 my @subf = $field->subfields;
946 for my $i (0..$#subf) {
947 my $subfieldcode = $subf[$i][0];
948 my $subfieldvalue = $subf[$i][1];
949 my $tagsubf = $tag.$subfieldcode;
950 $localsummary =~ s/\[(.?.?.?.?)$tagsubf(.*?)\]/$1$subfieldvalue$2\[$1$tagsubf$2\]/g;
953 if ($localsummary ne $summary_template) {
954 $localsummary =~ s/\[(.*?)\]//g;
955 $localsummary =~ s/\n/<br>/g;
956 push @repets, $localsummary;
959 $summary{repets} = \@repets;
966 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
967 # construct UNIMARC summary, that is quite different from MARC21 one
969 foreach my $field ($record->field('2..')) {
971 heading => $field->as_string('abcdefghijlmnopqrstuvwxyz'),
972 hemain => ( $field->subfield('a') // undef ),
973 field => $field->tag(),
977 foreach my $field ($record->field('3..')) {
978 push @notes, { note => $field->subfield('a'), field => $field->tag() };
980 foreach my $field ($record->field('4..')) {
981 my $thesaurus = $field->subfield('2') ? "thes. : ".$thesaurus{"$field->subfield('2')"}." : " : '';
983 heading => $thesaurus . $field->as_string('abcdefghijlmnopqrstuvwxyz'),
984 hemain => ( $field->subfield('a') // undef ),
986 field => $field->tag(),
992 my $type = $unimarc_relation_from_code{$_->subfield('5') || 'a'};
993 my $heading = $_->as_string('abcdefgjxyz');
998 hemain => ( $_->subfield('a') // undef ),
1000 authid => ( $_->subfield('9') // undef ),
1002 } $record->field('5..');
1005 @otherscript = map { {
1006 lang => length ($_->subfield('8')) == 6 ? substr ($_->subfield('8'), 3, 3) : $_->subfield('8') || '',
1007 term => $_->subfield('a') . ($_->subfield('b') ? ', ' . $_->subfield('b') : ''),
1010 } } $record->field('7..');
1013 # construct MARC21 summary
1014 # FIXME - looping over 1XX is questionable
1015 # since MARC21 authority should have only one 1XX
1016 my $subfields_to_report;
1017 foreach my $field ($record->field('1..')) {
1018 my $tag = $field->tag();
1019 next if "152" eq $tag;
1020 # FIXME - 152 is not a good tag to use
1021 # in MARC21 -- purely local tags really ought to be
1023 if ($tag eq '100') {
1024 $subfields_to_report = 'abcdefghjklmnopqrstvxyz';
1025 } elsif ($tag eq '110') {
1026 $subfields_to_report = 'abcdefghklmnoprstvxyz';
1027 } elsif ($tag eq '111') {
1028 $subfields_to_report = 'acdefghklnpqstvxyz';
1029 } elsif ($tag eq '130') {
1030 $subfields_to_report = 'adfghklmnoprstvxyz';
1031 } elsif ($tag eq '148') {
1032 $subfields_to_report = 'abvxyz';
1033 } elsif ($tag eq '150') {
1034 $subfields_to_report = 'abvxyz';
1035 } elsif ($tag eq '151') {
1036 $subfields_to_report = 'avxyz';
1037 } elsif ($tag eq '155') {
1038 $subfields_to_report = 'abvxyz';
1039 } elsif ($tag eq '180') {
1040 $subfields_to_report = 'vxyz';
1041 } elsif ($tag eq '181') {
1042 $subfields_to_report = 'vxyz';
1043 } elsif ($tag eq '182') {
1044 $subfields_to_report = 'vxyz';
1045 } elsif ($tag eq '185') {
1046 $subfields_to_report = 'vxyz';
1048 if ($subfields_to_report) {
1050 heading => $field->as_string($subfields_to_report),
1051 hemain => ( $field->subfield( substr($subfields_to_report, 0, 1) ) // undef ),
1056 heading => $field->as_string(),
1057 hemain => ( $field->subfield( 'a' ) // undef ),
1062 foreach my $field ($record->field('4..')) { #See From
1063 my $type = 'seefrom';
1064 $type = ($marc21controlrefs{substr $field->subfield('w'), 0, 1} || '') if ($field->subfield('w'));
1065 if ($type eq 'notapplicable') {
1066 $type = substr $field->subfield('w'), 2, 1;
1067 $type = 'earlier' if $type && $type ne 'n';
1069 if ($type eq 'subfi') {
1071 heading => $field->as_string($marc21subfields),
1072 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1073 type => ($field->subfield('i') || ''),
1074 field => $field->tag(),
1078 heading => $field->as_string($marc21subfields),
1079 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1081 field => $field->tag(),
1085 foreach my $field ($record->field('5..')) { #See Also
1086 my $type = 'seealso';
1087 $type = ($marc21controlrefs{substr $field->subfield('w'), 0, 1} || '') if ($field->subfield('w'));
1088 if ($type eq 'notapplicable') {
1089 $type = substr $field->subfield('w'), 2, 1;
1090 $type = 'earlier' if $type && $type ne 'n';
1092 if ($type eq 'subfi') {
1094 heading => $field->as_string($marc21subfields),
1095 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1096 type => $field->subfield('i'),
1097 field => $field->tag(),
1098 search => $field->as_string($marc21subfields) || '',
1099 authid => $field->subfield('9') || ''
1103 heading => $field->as_string($marc21subfields),
1104 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1106 field => $field->tag(),
1107 search => $field->as_string($marc21subfields) || '',
1108 authid => $field->subfield('9') || ''
1112 foreach my $field ($record->field('6..')) {
1113 push @notes, { note => $field->as_string(), field => $field->tag() };
1115 foreach my $field ($record->field('880')) {
1116 my $linkage = $field->subfield('6');
1117 my $category = substr $linkage, 0, 1;
1118 if ($category eq '1') {
1119 $category = 'preferred';
1120 } elsif ($category eq '4') {
1121 $category = 'seefrom';
1122 } elsif ($category eq '5') {
1123 $category = 'seealso';
1126 if ($field->subfield('w')) {
1127 $type = $marc21controlrefs{substr $field->subfield('w'), '0'};
1131 my $direction = $linkage =~ m#/r$# ? 'rtl' : 'ltr';
1132 push @otherscript, { term => $field->as_string($subfields_to_report), category => $category, type => $type, direction => $direction, linkage => $linkage };
1135 $summary{mainentry} = $authorized[0]->{heading};
1136 $summary{mainmainentry} = $authorized[0]->{hemain};
1137 $summary{authorized} = \@authorized;
1138 $summary{notes} = \@notes;
1139 $summary{seefrom} = \@seefrom;
1140 $summary{seealso} = \@seealso;
1141 $summary{otherscript} = \@otherscript;
1145 =head2 GetAuthorizedHeading
1147 $heading = &GetAuthorizedHeading({ record => $record, authid => $authid })
1149 Takes a MARC::Record object describing an authority record or an authid, and
1150 returns a string representation of the first authorized heading. This routine
1151 should be considered a temporary shim to ease the future migration of authority
1152 data from C4::AuthoritiesMarc to the object-oriented Koha::*::Authority.
1156 sub GetAuthorizedHeading {
1159 unless ($record = $args->{record}) {
1160 return unless $args->{authid};
1161 $record = GetAuthority($args->{authid});
1163 return unless (ref $record eq 'MARC::Record');
1164 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
1165 # construct UNIMARC summary, that is quite different from MARC21 one
1167 foreach my $field ($record->field('2..')) {
1168 return $field->as_string('abcdefghijlmnopqrstuvwxyz');
1171 foreach my $field ($record->field('1..')) {
1172 my $tag = $field->tag();
1173 next if "152" eq $tag;
1174 # FIXME - 152 is not a good tag to use
1175 # in MARC21 -- purely local tags really ought to be
1177 if ($tag eq '100') {
1178 return $field->as_string('abcdefghjklmnopqrstvxyz68');
1179 } elsif ($tag eq '110') {
1180 return $field->as_string('abcdefghklmnoprstvxyz68');
1181 } elsif ($tag eq '111') {
1182 return $field->as_string('acdefghklnpqstvxyz68');
1183 } elsif ($tag eq '130') {
1184 return $field->as_string('adfghklmnoprstvxyz68');
1185 } elsif ($tag eq '148') {
1186 return $field->as_string('abvxyz68');
1187 } elsif ($tag eq '150') {
1188 return $field->as_string('abvxyz68');
1189 } elsif ($tag eq '151') {
1190 return $field->as_string('avxyz68');
1191 } elsif ($tag eq '155') {
1192 return $field->as_string('abvxyz68');
1193 } elsif ($tag eq '180') {
1194 return $field->as_string('vxyz68');
1195 } elsif ($tag eq '181') {
1196 return $field->as_string('vxyz68');
1197 } elsif ($tag eq '182') {
1198 return $field->as_string('vxyz68');
1199 } elsif ($tag eq '185') {
1200 return $field->as_string('vxyz68');
1202 return $field->as_string();
1209 =head2 BuildAuthHierarchies
1211 $text= &BuildAuthHierarchies( $authid, $force)
1213 return text containing trees for hierarchies
1214 for them to be stored in auth_header
1217 122,1314,2452;1324,2342,3,2452
1221 sub BuildAuthHierarchies{
1222 my $authid = shift @_;
1223 # warn "authid : $authid";
1224 my $force = shift @_ || (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 0 : 1);
1226 my $dbh=C4::Context->dbh;
1228 my $data = GetHeaderAuthority($authid);
1229 if ($data->{'authtrees'} and not $force){
1230 return $data->{'authtrees'};
1231 # } elsif ($data->{'authtrees'}){
1232 # $hierarchies=$data->{'authtrees'};
1234 my $record = GetAuthority($authid);
1236 return unless $record;
1237 foreach my $field ($record->field('5..')){
1240 (C4::Context->preference('marcflavour') eq 'UNIMARC' && $field->subfield('5') && $field->subfield('5') eq 'g') ||
1241 (C4::Context->preference('marcflavour') ne 'UNIMARC' && $field->subfield('w') && substr($field->subfield('w'), 0, 1) eq 'g'));
1243 my $subfauthid=_get_authid_subfield($field) || '';
1244 next if ($subfauthid eq $authid);
1245 my $parentrecord = GetAuthority($subfauthid);
1246 next unless $parentrecord;
1247 my $localresult=$hierarchies;
1249 $trees = BuildAuthHierarchies($subfauthid);
1252 @trees = split(/;/,$trees);
1254 push @trees, $trees;
1259 @globalresult = (@globalresult,@trees);
1262 $hierarchies=join(";",@globalresult);
1264 #Unless there is no ancestor, I am alone.
1265 $hierarchies="$authid" unless ($hierarchies);
1267 AddAuthorityTrees($authid,$hierarchies);
1268 return $hierarchies;
1271 =head2 BuildAuthHierarchy
1273 $ref= &BuildAuthHierarchy( $record, $class,$authid)
1275 return a hashref in order to display hierarchy for record and final Authid $authid
1286 sub BuildAuthHierarchy{
1287 my $record = shift @_;
1288 my $class = shift @_;
1289 my $authid_constructed = shift @_;
1290 return unless ($record && $record->field('001'));
1291 my $authid=$record->field('001')->data();
1293 my $parents=""; my $children="";
1294 my (@loopparents,@loopchildren);
1295 my $marcflavour = C4::Context->preference('marcflavour');
1296 my $relationshipsf = $marcflavour eq 'UNIMARC' ? '5' : 'w';
1297 foreach my $field ($record->field('5..')){
1298 my $subfauthid=_get_authid_subfield($field);
1299 if ($subfauthid && $field->subfield($relationshipsf) && $field->subfield('a')){
1300 my $relationship = substr($field->subfield($relationshipsf), 0, 1);
1301 if ($relationship eq 'h'){
1302 push @loopchildren, { "authid"=>$subfauthid,"value"=>$field->subfield('a')};
1304 elsif ($relationship eq 'g'){
1305 push @loopparents, { "authid"=>$subfauthid,"value"=>$field->subfield('a')};
1307 # brothers could get in there with an else
1310 $cell{"parents"}=\@loopparents;
1311 $cell{"children"}=\@loopchildren;
1312 $cell{"class"}=$class;
1313 $cell{"authid"}=$authid;
1314 $cell{"current_value"} =1 if ($authid eq $authid_constructed);
1315 $cell{"value"}=C4::Context->preference('marcflavour') eq 'UNIMARC' ? $record->subfield('2..',"a") : $record->subfield('1..', 'a');
1319 =head2 BuildAuthHierarchyBranch
1321 $branch = &BuildAuthHierarchyBranch( $tree, $authid[, $cnt])
1323 Return a data structure representing an authority hierarchy
1324 given a list of authorities representing a single branch in
1325 an authority hierarchy tree. $authid is the current node in
1326 the tree (which may or may not be somewhere in the middle).
1327 $cnt represents the level of the upper-most item, and is only
1328 used when BuildAuthHierarchyBranch is called recursively (i.e.,
1329 don't ever pass in anything but zero to it).
1333 sub BuildAuthHierarchyBranch {
1334 my ($tree, $authid, $cnt) = @_;
1336 my $elementdata = GetAuthority(shift @$tree);
1337 my $branch = BuildAuthHierarchy($elementdata,"child".$cnt, $authid);
1338 if (scalar @$tree > 0) {
1339 my $nextBranch = BuildAuthHierarchyBranch($tree, $authid, ++$cnt);
1340 my $nextAuthid = $nextBranch->{authid};
1342 # If we already have the next branch listed as a child, let's
1343 # replace the old listing with the new one. If not, we will add
1344 # the branch at the end.
1345 foreach my $cell (@{$branch->{children}}) {
1346 if ($cell->{authid} eq $nextAuthid) {
1347 $cell = $nextBranch;
1352 push @{$branch->{children}}, $nextBranch unless $found;
1357 =head2 GenerateHierarchy
1359 $hierarchy = &GenerateHierarchy($authid);
1361 Return an arrayref holding one or more "trees" representing
1362 authority hierarchies.
1366 sub GenerateHierarchy {
1368 my $trees = BuildAuthHierarchies($authid);
1369 my @trees = split /;/,$trees ;
1370 push @trees,$trees unless (@trees);
1371 my @loophierarchies;
1372 foreach my $tree (@trees){
1373 my @tree=split /,/,$tree;
1374 push @tree, $tree unless (@tree);
1375 my $branch = BuildAuthHierarchyBranch(\@tree, $authid);
1376 push @loophierarchies, [ $branch ];
1378 return \@loophierarchies;
1381 sub _get_authid_subfield{
1383 return $field->subfield('9')||$field->subfield('3');
1385 =head2 GetHeaderAuthority
1387 $ref= &GetHeaderAuthority( $authid)
1389 return a hashref in order auth_header table data
1393 sub GetHeaderAuthority{
1394 my $authid = shift @_;
1395 my $sql= "SELECT * from auth_header WHERE authid = ?";
1396 my $dbh=C4::Context->dbh;
1397 my $rq= $dbh->prepare($sql);
1398 $rq->execute($authid);
1399 my $data= $rq->fetchrow_hashref;
1403 =head2 AddAuthorityTrees
1405 $ref= &AddAuthorityTrees( $authid, $trees)
1407 return success or failure
1411 sub AddAuthorityTrees{
1412 my $authid = shift @_;
1413 my $trees = shift @_;
1414 my $sql= "UPDATE IGNORE auth_header set authtrees=? WHERE authid = ?";
1415 my $dbh=C4::Context->dbh;
1416 my $rq= $dbh->prepare($sql);
1417 return $rq->execute($trees,$authid);
1422 $ref= &merge(mergefrom,$MARCfrom,$mergeto,$MARCto)
1424 Could add some feature : Migrating from a typecode to an other for instance.
1425 Then we should add some new parameter : bibliotargettag, authtargettag
1430 my ($mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
1431 my ($counteditedbiblio,$countunmodifiedbiblio,$counterrors)=(0,0,0);
1432 my $dbh=C4::Context->dbh;
1433 my $authtypecodefrom = GetAuthTypeCode($mergefrom);
1434 my $authtypecodeto = GetAuthTypeCode($mergeto);
1435 # warn "mergefrom : $authtypecodefrom $mergefrom mergeto : $authtypecodeto $mergeto ";
1436 # return if authority does not exist
1437 return "error MARCFROM not a marcrecord ".Data::Dumper::Dumper($MARCfrom) if scalar($MARCfrom->fields()) == 0;
1438 return "error MARCTO not a marcrecord".Data::Dumper::Dumper($MARCto) if scalar($MARCto->fields()) == 0;
1439 # search the tag to report
1440 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
1441 $sth->execute($authtypecodefrom);
1442 my ($auth_tag_to_report_from) = $sth->fetchrow;
1443 $sth->execute($authtypecodeto);
1444 my ($auth_tag_to_report_to) = $sth->fetchrow;
1447 @record_to = $MARCto->field($auth_tag_to_report_to)->subfields() if $MARCto->field($auth_tag_to_report_to);
1449 @record_from = $MARCfrom->field($auth_tag_to_report_from)->subfields() if $MARCfrom->field($auth_tag_to_report_from);
1452 # search all biblio tags using this authority.
1453 #Getting marcbiblios impacted by the change.
1455 my $oConnection=C4::Context->Zconn("biblioserver",0);
1456 # We used to use XML syntax here, but that no longer works.
1457 # Thankfully, we don't need it.
1459 $query= "an=".$mergefrom;
1460 my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1463 $count=$oResult->size();
1466 while ( $z<$count ) {
1467 my $marcrecordzebra = C4::Search::new_record_from_zebra(
1469 $oResult->record($z)->raw()
1471 my ( $biblionumbertagfield, $biblionumbertagsubfield ) = &GetMarcFromKohaField( "biblio.biblionumber", '' );
1472 my $i = ($biblionumbertagfield < 10)
1473 ? $marcrecordzebra->field( $biblionumbertagfield )->data
1474 : $marcrecordzebra->subfield( $biblionumbertagfield, $biblionumbertagsubfield );
1475 my $marcrecorddb = GetMarcBiblio($i);
1476 push @reccache, $marcrecorddb;
1479 $oResult->destroy();
1480 #warn scalar(@reccache)." biblios to update";
1481 # Get All candidate Tags for the change
1482 # (This will reduce the search scope in marc records).
1483 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1484 $sth->execute($authtypecodefrom);
1485 my @tags_using_authtype;
1486 while (my ($tagfield) = $sth->fetchrow) {
1487 push @tags_using_authtype,$tagfield ;
1490 if ($authtypecodeto ne $authtypecodefrom){
1491 # If many tags, take the first
1492 $sth->execute($authtypecodeto);
1493 $tag_to=$sth->fetchrow;
1496 # BulkEdit marc records
1497 # May be used as a template for a bulkedit field
1498 foreach my $marcrecord(@reccache){
1500 foreach my $tagfield (@tags_using_authtype){
1501 # warn "tagfield : $tagfield ";
1502 foreach my $field ($marcrecord->field($tagfield)){
1503 # biblio is linked to authority with $9 subfield containing authid
1504 my $auth_number=$field->subfield("9");
1505 my $tag=$field->tag();
1506 if ($auth_number==$mergefrom) {
1507 my $field_to=MARC::Field->new(($tag_to?$tag_to:$tag),$field->indicator(1),$field->indicator(2),"9"=>$mergeto);
1509 foreach my $subfield (grep {$_->[0] ne '9'} @record_to) {
1510 $field_to->add_subfields($subfield->[0] =>$subfield->[1]);
1511 $exclude.= $subfield->[0];
1513 $exclude='['.$exclude.']';
1514 # add subfields in $field not included in @record_to
1515 my @restore= grep {$_->[0]!~/$exclude/} $field->subfields();
1516 foreach my $subfield (@restore) {
1517 $field_to->add_subfields($subfield->[0] =>$subfield->[1]);
1519 $marcrecord->delete_field($field);
1520 $marcrecord->insert_grouped_field($field_to);
1525 my ($bibliotag,$bibliosubf) = GetMarcFromKohaField("biblio.biblionumber","") ;
1528 $biblionumber=$marcrecord->field($bibliotag)->data;
1531 $biblionumber=$marcrecord->subfield($bibliotag,$bibliosubf);
1533 unless ($biblionumber){
1534 warn "pas de numéro de notice bibliographique dans : ".$marcrecord->as_formatted;
1538 &ModBiblio($marcrecord,$biblionumber,GetFrameworkCode($biblionumber)) ;
1539 $counteditedbiblio++;
1540 warn $counteditedbiblio if (($counteditedbiblio % 10) and $ENV{DEBUG});
1543 return $counteditedbiblio;
1544 # now, find every other authority linked with this authority
1545 # now, find every other authority linked with this authority
1546 # my $oConnection=C4::Context->Zconn("authorityserver");
1548 # # att 9210 Auth-Internal-authtype
1549 # # att 9220 Auth-Internal-LN
1550 # # ccl.properties to add for authorities
1551 # $query= "= ".$mergefrom;
1552 # my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1553 # my $count=$oResult->size() if ($oResult);
1556 # while ( $z<$count ) {
1558 # $rec=$oResult->record($z);
1559 # my $marcdata = $rec->raw();
1560 # push @reccache, $marcdata;
1563 # $oResult->destroy();
1564 # foreach my $marc(@reccache){
1567 # $marcrecord = MARC::File::USMARC::decode($marc);
1568 # foreach my $tagfield (@tags_using_authtype){
1569 # $tagfield=substr($tagfield,0,3);
1570 # my @tags = $marcrecord->field($tagfield);
1571 # foreach my $tag (@tags){
1572 # my $tagsubs=$tag->subfield("9");
1573 # #warn "$tagfield:$tagsubs:$mergefrom";
1574 # if ($tagsubs== $mergefrom) {
1575 # $tag->update("9" =>$mergeto);
1576 # foreach my $subfield (@record_to) {
1577 # # warn "$subfield,$subfield->[0],$subfield->[1]";
1578 # $tag->update($subfield->[0] =>$subfield->[1]);
1581 # $marcrecord->delete_field($tag);
1582 # $marcrecord->add_fields($tag);
1585 # }#foreach tagfield
1586 # my $authoritynumber = TransformMarcToKoha($dbh,$marcrecord,"") ;
1588 # &ModAuthority($marcrecord,$authoritynumber,GetAuthTypeCode($authoritynumber)) ;
1594 =head2 get_auth_type_location
1596 my ($tag, $subfield) = get_auth_type_location($auth_type_code);
1598 Get the tag and subfield used to store the heading type
1599 for indexing purposes. The C<$auth_type> parameter is
1600 optional; if it is not supplied, assume ''.
1602 This routine searches the MARC authority framework
1603 for the tag and subfield whose kohafield is
1604 C<auth_header.authtypecode>; if no such field is
1605 defined in the framework, default to the hardcoded value
1606 specific to the MARC format.
1610 sub get_auth_type_location {
1611 my $auth_type_code = @_ ? shift : '';
1613 my ($tag, $subfield) = GetAuthMARCFromKohaField('auth_header.authtypecode', $auth_type_code);
1614 if (defined $tag and defined $subfield and $tag != 0 and $subfield ne '' and $subfield ne ' ') {
1615 return ($tag, $subfield);
1617 if (C4::Context->preference('marcflavour') eq "MARC21") {
1618 return C4::AuthoritiesMarc::MARC21::default_auth_type_location();
1620 return C4::AuthoritiesMarc::UNIMARC::default_auth_type_location();
1625 END { } # module clean-up code here (global destructor)
1632 Koha Development Team <http://koha-community.org/>
1634 Paul POULAIN paul.poulain@free.fr