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
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 use C4::AuthoritiesMarc::MARC21;
26 use C4::AuthoritiesMarc::UNIMARC;
29 use Koha::MetadataRecord::Authority;
30 use Koha::Authority::Types;
32 use vars qw($VERSION @ISA @EXPORT);
35 # set the version for version checking
36 $VERSION = 3.07.00.049;
44 &GetAuthMARCFromKohaField
62 &FindDuplicateAuthority
74 =head2 GetAuthMARCFromKohaField
76 ( $tag, $subfield ) = &GetAuthMARCFromKohaField ($kohafield,$authtypecode);
78 returns tag and subfield linked to kohafield
81 Suppose Kohafield is only linked to ONE subfield
85 sub GetAuthMARCFromKohaField {
86 #AUTHfind_marc_from_kohafield
87 my ( $kohafield,$authtypecode ) = @_;
88 my $dbh=C4::Context->dbh;
89 return 0, 0 unless $kohafield;
90 $authtypecode="" unless $authtypecode;
91 my $marcfromkohafield;
92 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
93 $sth->execute($kohafield,$authtypecode);
94 my ($tagfield,$tagsubfield) = $sth->fetchrow;
96 return ($tagfield,$tagsubfield);
99 =head2 SearchAuthorities
101 (\@finalresult, $nbresults)= &SearchAuthorities($tags, $and_or,
102 $excluding, $operator, $value, $offset,$length,$authtypecode,
103 $sortby[, $skipmetadata])
105 returns ref to array result and count of results returned
109 sub SearchAuthorities {
110 my ($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby,$skipmetadata) = @_;
111 # warn Dumper($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby);
112 my $dbh=C4::Context->dbh;
113 $sortby="" unless $sortby;
117 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
119 # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
120 # the authtypecode. Then, search on $a of this tag_to_report
121 # also store main entry MARC tag, to extract it at end of search
123 ##first set the authtype search and may be multiple authorities
127 my @auths=split / /,$authtypecode ;
128 foreach my $auth (@auths){
129 $query .=" \@attr 1=authtype \@attr 5=100 ".$auth; ##No truncation on authtype
130 push @authtypecode ,$auth;
134 while ($n>1){$query= "\@or ".$query;$n--;}
137 $qpquery .= '(authtype:' . join('|| authtype:', @auths) . ')';
145 for ( my $i = 0 ; $i <= $#{$value} ; $i++ ) {
148 if ( @$tags[$i] eq "mainmainentry" ) {
149 $attr = " \@attr 1=Heading-Main ";
151 elsif ( @$tags[$i] eq "mainentry" ) {
152 $attr = " \@attr 1=Heading ";
154 elsif ( @$tags[$i] eq "match" ) {
155 $attr = " \@attr 1=Match ";
157 elsif ( @$tags[$i] eq "match-heading" ) {
158 $attr = " \@attr 1=Match-heading ";
160 elsif ( @$tags[$i] eq "see-from" ) {
161 $attr = " \@attr 1=Match-heading-see-from ";
163 elsif ( @$tags[$i] eq "thesaurus" ) {
164 $attr = " \@attr 1=Subject-heading-thesaurus ";
166 else { # Assume any if no index was specified
167 $attr = " \@attr 1=Any ";
170 else { # Assume any if no index was specified
171 $attr = " \@attr 1=Any ";
174 my $operator = @$operator[$i];
175 if ( $operator and $operator eq 'is' ) {
176 $attr .= " \@attr 4=1 \@attr 5=100 "
177 ; ##Phrase, No truncation,all of subfield field must match
179 elsif ( $operator and $operator eq "=" ) {
180 $attr .= " \@attr 4=107 "; #Number Exact match
182 elsif ( $operator and $operator eq "start" ) {
183 $attr .= " \@attr 3=2 \@attr 4=1 \@attr 5=1 "
184 ; #Firstinfield Phrase, Right truncated
186 elsif ( $operator and $operator eq "exact" ) {
187 $attr .= " \@attr 4=1 \@attr 5=100 \@attr 6=3 "
188 ; ##Phrase, No truncation,all of subfield field must match
191 $attr .= " \@attr 5=1 \@attr 4=6 "
192 ; ## Word list, right truncated, anywhere
193 if ( $sortby eq 'Relevance' ) {
194 $attr .= "\@attr 2=102 ";
198 s/"/\\"/g; # Escape the double-quotes in the search value
199 $attr = $attr . "\"" . @$value[$i] . "\"";
204 $qpquery .= " $tags->[$i]:\"$value->[$i]\"";
208 ##Add how many queries generated
209 if (defined $query && $query=~/\S+/){
210 $query= $and x $attr_cnt . $query . (defined $q2 ? $q2 : '');
215 #$query=' @or @attr 7=2 @attr 1=Heading 0 @or @attr 7=1 @attr 1=Heading 1'.$query if ($sortby eq "HeadingDsc");
217 if ($sortby eq 'HeadingAsc') {
218 $orderstring = '@attr 7=1 @attr 1=Heading 0';
219 } elsif ($sortby eq 'HeadingDsc') {
220 $orderstring = '@attr 7=2 @attr 1=Heading 0';
221 } elsif ($sortby eq 'AuthidAsc') {
222 $orderstring = '@attr 7=1 @attr 4=109 @attr 1=Local-Number 0';
223 } elsif ($sortby eq 'AuthidDsc') {
224 $orderstring = '@attr 7=2 @attr 4=109 @attr 1=Local-Number 0';
227 $qpquery .= ' all:all' unless $value->[0];
229 if ( $value->[0] =~ m/^qp=(.*)$/ ) {
233 $qpquery .= " #$sortby" unless $sortby eq '';
235 $QParser->parse( $qpquery );
236 $query = $QParser->target_syntax('authorityserver');
238 $query=($query?$query:"\@attr 1=_ALLRECORDS \@attr 2=103 ''");
239 $query="\@or $orderstring $query" if $orderstring;
242 $offset=0 unless $offset;
243 my $counter = $offset;
244 $length=10 unless $length;
247 $oAuth[0]=C4::Context->Zconn("authorityserver" , 1);
248 my $Anewq= new ZOOM::Query::PQF($query,$oAuth[0]);
250 $oAResult= $oAuth[0]->search($Anewq) ;
251 while (($i = ZOOM::event(\@oAuth)) != 0) {
252 my $ev = $oAuth[$i-1]->last_event();
253 last if $ev == ZOOM::Event::ZEND;
255 my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
257 warn "oAuth error: $errmsg ($error) $addinfo $diagset\n";
262 $nbresults=$oAResult->size();
263 my $nremains=$nbresults;
265 my @finalresult = ();
269 ##Find authid and linkid fields
270 ##we may be searching multiple authoritytypes.
271 ## FIXME this assumes that all authid and linkid fields are the same for all authority types
272 # my ($authidfield,$authidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.authid",$authtypecode[0]);
273 # my ($linkidfield,$linkidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.linkid",$authtypecode[0]);
274 while (($counter < $nbresults) && ($counter < ($offset + $length))) {
276 ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
277 my $rec=$oAResult->record($counter);
278 my $separator=C4::Context->preference('AuthoritySeparator');
279 my $authrecord = C4::Search::new_record_from_zebra(
284 if ( !defined $authrecord or !defined $authrecord->field('001') ) {
289 SetUTF8Flag( $authrecord );
291 my $authid=$authrecord->field('001')->data();
293 $newline{authid} = $authid;
294 if ( !$skipmetadata ) {
295 my $auth_tag_to_report = Koha::Authority::Types->find($authtypecode)->auth_tag_to_report;
297 my $mainentry = $authrecord->field($auth_tag_to_report);
299 foreach ( $mainentry->subfields() ) {
300 $reported_tag .= '$' . $_->[0] . $_->[1];
303 my $thisauthtypecode = GetAuthTypeCode($authid);
304 my $thisauthtype = GetAuthType($thisauthtypecode);
305 unless (defined $thisauthtype) {
306 $thisauthtypecode = $authtypecode;
307 $thisauthtype = GetAuthType($authtypecode);
309 my $summary = BuildSummary( $authrecord, $authid, $thisauthtypecode );
311 $newline{authtype} = defined($thisauthtype) ?
312 $thisauthtype->{'authtypetext'} : '';
313 $newline{summary} = $summary;
314 $newline{even} = $counter % 2;
315 $newline{reported_tag} = $reported_tag;
318 push @finalresult, \%newline;
321 if (! $skipmetadata) {
322 for (my $z=0; $z<@finalresult; $z++){
323 my $count=CountUsage($finalresult[$z]{authid});
324 $finalresult[$z]{used}=$count;
330 $oAResult->destroy();
331 # $oAuth[0]->destroy();
333 return (\@finalresult, $nbresults);
338 $count= &CountUsage($authid)
340 counts Usage of Authid in bibliorecords.
348 $query= "an:".$authid;
349 my ($err,$res,$result) = C4::Search::SimpleSearch($query,0,10);
351 warn "Error: $err from search $query";
358 =head2 CountUsageChildren
360 $count= &CountUsageChildren($authid)
362 counts Usage of narrower terms of Authid in bibliorecords.
366 sub CountUsageChildren {
370 =head2 GetAuthTypeCode
372 $authtypecode= &GetAuthTypeCode($authid)
374 returns authtypecode of an authid
378 sub GetAuthTypeCode {
379 #AUTHfind_authtypecode
381 my $dbh=C4::Context->dbh;
382 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
383 $sth->execute($authid);
384 my $authtypecode = $sth->fetchrow;
385 return $authtypecode;
388 =head2 GuessAuthTypeCode
390 my $authtypecode = GuessAuthTypeCode($record);
392 Get the record and tries to guess the adequate authtypecode from its content.
396 sub GuessAuthTypeCode {
397 my ($record, $heading_fields) = @_;
398 return unless defined $record;
399 $heading_fields //= {
401 '100'=>{authtypecode=>'PERSO_NAME'},
402 '110'=>{authtypecode=>'CORPO_NAME'},
403 '111'=>{authtypecode=>'MEETI_NAME'},
404 '130'=>{authtypecode=>'UNIF_TITLE'},
405 '148'=>{authtypecode=>'CHRON_TERM'},
406 '150'=>{authtypecode=>'TOPIC_TERM'},
407 '151'=>{authtypecode=>'GEOGR_NAME'},
408 '155'=>{authtypecode=>'GENRE/FORM'},
409 '180'=>{authtypecode=>'GEN_SUBDIV'},
410 '181'=>{authtypecode=>'GEO_SUBDIV'},
411 '182'=>{authtypecode=>'CHRON_SUBD'},
412 '185'=>{authtypecode=>'FORM_SUBD'},
414 #200 Personal name 700, 701, 702 4-- with embedded 700, 701, 702 600
415 # 604 with embedded 700, 701, 702
416 #210 Corporate or meeting name 710, 711, 712 4-- with embedded 710, 711, 712 601 604 with embedded 710, 711, 712
417 #215 Territorial or geographic name 710, 711, 712 4-- with embedded 710, 711, 712 601, 607 604 with embedded 710, 711, 712
418 #216 Trademark 716 [Reserved for future use]
419 #220 Family name 720, 721, 722 4-- with embedded 720, 721, 722 602 604 with embedded 720, 721, 722
420 #230 Title 500 4-- with embedded 500 605
421 #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
422 #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
423 #250 Topical subject 606
424 #260 Place access 620
425 #280 Form, genre or physical characteristics 608
428 # Could also be represented with :
430 #a = personal name entry
431 #b = corporate name entry
432 #c = territorial or geographical name
436 #g = collective uniform title
438 #i = name/collective uniform title
441 #l = form, genre or physical characteristics
443 '200'=>{authtypecode=>'NP'},
444 '210'=>{authtypecode=>'CO'},
445 '215'=>{authtypecode=>'SNG'},
446 '216'=>{authtypecode=>'TM'},
447 '220'=>{authtypecode=>'FAM'},
448 '230'=>{authtypecode=>'TU'},
449 '235'=>{authtypecode=>'CO_UNI_TI'},
450 '240'=>{authtypecode=>'SAUTTIT'},
451 '245'=>{authtypecode=>'NAME_COL'},
452 '250'=>{authtypecode=>'SNC'},
453 '260'=>{authtypecode=>'PA'},
454 '280'=>{authtypecode=>'GENRE/FORM'},
457 foreach my $field (keys %{$heading_fields->{uc(C4::Context->preference('marcflavour'))} }) {
458 return $heading_fields->{uc(C4::Context->preference('marcflavour'))}->{$field}->{'authtypecode'} if (defined $record->field($field));
465 my $authtid = GuessAuthId($record);
467 Get the record and tries to guess the adequate authtypecode from its content.
473 return unless ($record && $record->field('001'));
474 # my $authtypecode=GuessAuthTypeCode($record);
475 # my ($tag,$subfield)=GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
476 # if ($tag > 010) {return $record->subfield($tag,$subfield)}
477 # else {return $record->field($tag)->data}
478 return $record->field('001')->data;
483 $tagslabel= &GetTagsLabels($forlibrarian,$authtypecode)
485 returns a ref to hashref of authorities tag and subfield structure.
489 $tagslabel->{$tag}->{$subfield}->{'attribute'}
491 where attribute takes values in :
509 my ($forlibrarian,$authtypecode)= @_;
510 my $dbh=C4::Context->dbh;
511 $authtypecode="" unless $authtypecode;
513 my $libfield = ($forlibrarian == 1)? 'liblibrarian' : 'libopac';
516 # check that authority exists
517 $sth=$dbh->prepare("SELECT count(*) FROM auth_tag_structure WHERE authtypecode=?");
518 $sth->execute($authtypecode);
519 my ($total) = $sth->fetchrow;
520 $authtypecode="" unless ($total >0);
522 "SELECT auth_tag_structure.tagfield,auth_tag_structure.liblibrarian,auth_tag_structure.libopac,auth_tag_structure.mandatory,auth_tag_structure.repeatable
523 FROM auth_tag_structure
528 $sth->execute($authtypecode);
529 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
531 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
532 $res->{$tag}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
533 $res->{$tag}->{tab} = " "; # XXX
534 $res->{$tag}->{mandatory} = $mandatory;
535 $res->{$tag}->{repeatable} = $repeatable;
538 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,frameworkcode as authtypecode,value_builder,kohafield,seealso,hidden,isurl,defaultvalue
539 FROM auth_subfield_structure
541 ORDER BY tagfield,tagsubfield"
543 $sth->execute($authtypecode);
546 my $authorised_value;
556 ( $tag, $subfield, $liblibrarian, , $libopac, $tab,
557 $mandatory, $repeatable, $authorised_value, $authtypecode,
558 $value_builder, $kohafield, $seealso, $hidden,
559 $isurl, $defaultvalue, $link )
563 $res->{$tag}->{$subfield}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
564 $res->{$tag}->{$subfield}->{tab} = $tab;
565 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
566 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
567 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
568 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
569 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
570 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
571 $res->{$tag}->{$subfield}->{seealso} = $seealso;
572 $res->{$tag}->{$subfield}->{hidden} = $hidden;
573 $res->{$tag}->{$subfield}->{isurl} = $isurl;
574 $res->{$tag}->{$subfield}->{link} = $link;
575 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
582 $authid= &AddAuthority($record, $authid,$authtypecode)
584 Either Create Or Modify existing authority.
585 returns authid of the newly created authority
590 # pass the MARC::Record to this function, and it will create the records in the authority table
591 my ($record,$authid,$authtypecode) = @_;
592 my $dbh=C4::Context->dbh;
593 my $leader=' nz a22 o 4500';#Leader for incomplete MARC21 record
595 # if authid empty => true add, find a new authid number
597 if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') {
598 $format= 'UNIMARCAUTH';
604 #update date/time to 005 for marc and unimarc
605 my $time=POSIX::strftime("%Y%m%d%H%M%S",localtime);
606 my $f5=$record->field('005');
608 $record->insert_fields_ordered( MARC::Field->new('005',$time.".0") );
611 $f5->update($time.".0");
614 SetUTF8Flag($record);
615 if ($format eq "MARC21") {
616 if (!$record->leader) {
617 $record->leader($leader);
619 if (!$record->field('003')) {
620 $record->insert_fields_ordered(
621 MARC::Field->new('003',C4::Context->preference('MARCOrgCode'))
624 my $date=POSIX::strftime("%y%m%d",localtime);
625 if (!$record->field('008')) {
626 # Get a valid default value for field 008
627 my $default_008 = C4::Context->preference('MARCAuthorityControlField008');
628 if(!$default_008 or length($default_008)<34) {
629 $default_008 = '|| aca||aabn | a|a d';
632 $default_008 = substr($default_008,0,34);
635 $record->insert_fields_ordered( MARC::Field->new('008',$date.$default_008) );
637 if (!$record->field('040')) {
638 $record->insert_fields_ordered(
639 MARC::Field->new('040','','',
640 'a' => C4::Context->preference('MARCOrgCode'),
641 'c' => C4::Context->preference('MARCOrgCode')
647 if ($format eq "UNIMARCAUTH") {
648 $record->leader(" nx j22 ") unless ($record->leader());
649 my $date=POSIX::strftime("%Y%m%d",localtime);
650 my $defaultfield100 = C4::Context->preference('UNIMARCAuthorityField100');
651 if (my $string=$record->subfield('100',"a")){
652 $string=~s/fre50/frey50/;
653 $record->field('100')->update('a'=>$string);
655 elsif ($record->field('100')){
656 $record->field('100')->update('a'=>$date.$defaultfield100);
658 $record->append_fields(
659 MARC::Field->new('100',' ',' '
660 ,'a'=>$date.$defaultfield100)
664 my ($auth_type_tag, $auth_type_subfield) = get_auth_type_location($authtypecode);
665 if (!$authid and $format eq "MARC21") {
666 # only need to do this fix when modifying an existing authority
667 C4::AuthoritiesMarc::MARC21::fix_marc21_auth_type_location($record, $auth_type_tag, $auth_type_subfield);
669 if (my $field=$record->field($auth_type_tag)){
670 $field->update($auth_type_subfield=>$authtypecode);
673 $record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode);
679 my $sth=$dbh->prepare("select max(authid) from auth_header");
681 ($authid)=$sth->fetchrow;
683 ##Insert the recordID in MARC record
684 unless ($record->field('001') && $record->field('001')->data() eq $authid){
685 $record->delete_field($record->field('001'));
686 $record->insert_fields_ordered(MARC::Field->new('001',$authid));
689 $auth_exists=$dbh->do(qq(select authid from auth_header where authid=?),undef,$authid);
690 # warn "auth_exists = $auth_exists";
693 $oldRecord=GetAuthority($authid);
694 $record->add_fields('001',$authid) unless ($record->field('001'));
695 # warn "\n\n\n enregistrement".$record->as_formatted;
696 my $sth=$dbh->prepare("update auth_header set authtypecode=?,marc=?,marcxml=? where authid=?");
697 $sth->execute($authtypecode,$record->as_usmarc,$record->as_xml_record($format),$authid) or die $sth->errstr;
701 my $sth=$dbh->prepare("insert into auth_header (authid,datecreated,authtypecode,marc,marcxml) values (?,now(),?,?,?)");
702 $sth->execute($authid,$authtypecode,$record->as_usmarc,$record->as_xml_record($format));
704 logaction( "AUTHORITIES", "ADD", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
706 ModZebra($authid,'specialUpdate',"authorityserver",$oldRecord,$record);
713 $authid= &DelAuthority($authid)
721 my $dbh=C4::Context->dbh;
723 logaction( "AUTHORITIES", "DELETE", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
724 ModZebra($authid,"recordDelete","authorityserver",GetAuthority($authid),undef);
725 my $sth = $dbh->prepare("DELETE FROM auth_header WHERE authid=?");
726 $sth->execute($authid);
731 $authid= &ModAuthority($authid,$record,$authtypecode)
733 Modifies authority record, optionally updates attached biblios.
738 my ($authid,$record,$authtypecode)=@_; # deprecated $merge parameter removed
740 my $dbh=C4::Context->dbh;
741 #Now rewrite the $record to table with an add
742 my $oldrecord=GetAuthority($authid);
743 $authid=AddAuthority($record,$authid,$authtypecode);
745 # If a library thinks that updating all biblios is a long process and wishes
746 # to leave that to a cron job, use misc/migration_tools/merge_authority.pl.
747 # In that case set system preference "dontmerge" to 1. Otherwise biblios will
749 unless(C4::Context->preference('dontmerge') eq '1'){
750 &merge($authid,$oldrecord,$authid,$record);
752 # save a record in need_merge_authorities table
753 my $sqlinsert="INSERT INTO need_merge_authorities (authid, done) ".
755 $dbh->do($sqlinsert,undef,($authid,0));
757 logaction( "AUTHORITIES", "MODIFY", $authid, "authority BEFORE=>" . $oldrecord->as_formatted ) if C4::Context->preference("AuthoritiesLog");
761 =head2 GetAuthorityXML
763 $marcxml= &GetAuthorityXML( $authid)
765 returns xml form of record $authid
769 sub GetAuthorityXML {
770 # Returns MARC::XML of the authority passed in parameter.
772 if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') {
773 my $dbh=C4::Context->dbh;
774 my $sth = $dbh->prepare("select marcxml from auth_header where authid=? " );
775 $sth->execute($authid);
776 my ($marcxml)=$sth->fetchrow;
780 # for MARC21, call GetAuthority instead of
781 # getting the XML directly since we may
782 # need to fix up the location of the authority
783 # code -- note that this is reasonably safe
784 # because GetAuthorityXML is used only by the
785 # indexing processes like zebraqueue_start.pl
786 my $record = GetAuthority($authid);
787 return $record->as_xml_record('MARC21');
793 $record= &GetAuthority( $authid)
795 Returns MARC::Record of the authority passed in parameter.
801 my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
802 return unless $authority;
803 return ($authority->record);
808 $result = &GetAuthType($authtypecode)
810 If the authority type specified by C<$authtypecode> exists,
811 returns a hashref of the type's fields. If the type
812 does not exist, returns undef.
817 my ($authtypecode) = @_;
818 my $dbh=C4::Context->dbh;
820 if (defined $authtypecode){ # NOTE - in MARC21 framework, '' is a valid authority
821 # type (FIXME but why?)
822 $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
823 $sth->execute($authtypecode);
824 if (my $res = $sth->fetchrow_hashref) {
832 =head2 FindDuplicateAuthority
834 $record= &FindDuplicateAuthority( $record, $authtypecode)
836 return $authid,Summary if duplicate is found.
838 Comments : an improvement would be to return All the records that match.
842 sub FindDuplicateAuthority {
844 my ($record,$authtypecode)=@_;
845 # warn "IN for ".$record->as_formatted;
846 my $dbh = C4::Context->dbh;
847 # warn "".$record->as_formatted;
848 my $auth_tag_to_report = Koha::Authority::Types->find($authtypecode)->auth_tag_to_report;
849 # warn "record :".$record->as_formatted." auth_tag_to_report :$auth_tag_to_report";
850 # build a request for SearchAuthorities
852 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
859 my $query='at:'.$authtypecode.' ';
860 my $filtervalues=qr([\001-\040\Q!'"`#$%&*+,-./:;<=>?@(){[}_|~\E\]]);
861 if ($record->field($auth_tag_to_report)) {
862 foreach ($record->field($auth_tag_to_report)->subfields()) {
863 $_->[1]=~s/$filtervalues/ /g; $query.= " $op he:\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/);
866 my ($error, $results, $total_hits) = C4::Search::SimpleSearch( $query, 0, 1, [ "authorityserver" ] );
867 # there is at least 1 result => return the 1st one
868 if (!defined $error && @{$results} ) {
869 my $marcrecord = C4::Search::new_record_from_zebra(
873 return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode);
875 # no result, returns nothing
881 $summary= &BuildSummary( $record, $authid, $authtypecode)
883 Returns a hashref with a summary of the specified record.
885 Comment : authtypecode can be inferred from both record and authid.
886 Moreover, authid can also be inferred from $record.
887 Would it be interesting to delete those things.
892 ## give this a Marc record to return summary
893 my ($record,$authid,$authtypecode)=@_;
894 my $dbh=C4::Context->dbh;
896 my $summary_template;
897 # handle $authtypecode is NULL or eq ""
899 my $authref = GetAuthType($authtypecode);
900 $summary{authtypecode} = $authref->{authtypecode};
901 $summary{type} = $authref->{authtypetext};
902 $summary_template = $authref->{summary};
903 # for MARC21, the authority type summary displays a label meant for
905 if (C4::Context->preference('marcflavour') ne 'UNIMARC') {
906 $summary{label} = $authref->{summary};
908 $summary{summary} = $authref->{summary};
911 my $marc21subfields = 'abcdfghjklmnopqrstuvxyz68';
912 my %marc21controlrefs = ( 'a' => 'earlier',
918 'n' => 'notapplicable',
922 my %unimarc_relation_from_code = (
928 $thesaurus{'1'}="Peuples";
929 $thesaurus{'2'}="Anthroponymes";
930 $thesaurus{'3'}="Oeuvres";
931 $thesaurus{'4'}="Chronologie";
932 $thesaurus{'5'}="Lieux";
933 $thesaurus{'6'}="Sujets";
936 # if the library has a summary defined, use it. Otherwise, build a standard one
937 # FIXME - it appears that the summary field in the authority frameworks
938 # can work as a display template. However, this doesn't
939 # suit the MARC21 version, so for now the "templating"
940 # feature will be enabled only for UNIMARC for backwards
942 if ($summary{summary} and C4::Context->preference('marcflavour') eq 'UNIMARC') {
943 my @matches = ($summary{summary} =~ m/\[(.*?)(\d{3})([\*a-z0-9])(.*?)\]/g);
944 my (@textbefore, @tag, @subtag, @textafter);
945 for(my $i = 0; $i < scalar @matches; $i++){
946 push @textbefore, $matches[$i] if($i%4 == 0);
947 push @tag, $matches[$i] if($i%4 == 1);
948 push @subtag, $matches[$i] if($i%4 == 2);
949 push @textafter, $matches[$i] if($i%4 == 3);
951 for(my $i = scalar @tag; $i >= 0; $i--){
952 my $textbefore = $textbefore[$i] || '';
953 my $tag = $tag[$i] || '';
954 my $subtag = $subtag[$i] || '';
955 my $textafter = $textafter[$i] || '';
957 my $field = $record->field($tag);
961 $value = $textbefore . $field->data() . $textafter;
964 my @subfields = $field->subfield($subtag);
966 $value = $textbefore . join (" - ", @subfields) . $textafter;
970 $summary{summary} =~ s/\[\Q$textbefore$tag$subtag$textafter\E\]/$value/;
972 $summary{summary} =~ s/\\n/<br \/>/g;
979 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
980 # construct UNIMARC summary, that is quite different from MARC21 one
982 foreach my $field ($record->field('2..')) {
984 heading => $field->as_string('abcdefghijlmnopqrstuvwxyz'),
985 hemain => ( $field->subfield('a') // undef ),
986 field => $field->tag(),
990 foreach my $field ($record->field('3..')) {
991 push @notes, { note => $field->subfield('a'), field => $field->tag() };
993 foreach my $field ($record->field('4..')) {
994 my $thesaurus = $field->subfield('2') ? "thes. : ".$thesaurus{"$field->subfield('2')"}." : " : '';
996 heading => $thesaurus . $field->as_string('abcdefghijlmnopqrstuvwxyz'),
997 hemain => ( $field->subfield('a') // undef ),
999 field => $field->tag(),
1005 my $type = $unimarc_relation_from_code{$_->subfield('5') || 'a'};
1006 my $heading = $_->as_string('abcdefgjxyz');
1010 heading => $heading,
1011 hemain => ( $_->subfield('a') // undef ),
1013 authid => ( $_->subfield('9') // undef ),
1015 } $record->field('5..');
1018 @otherscript = map { {
1019 lang => length ($_->subfield('8')) == 6 ? substr ($_->subfield('8'), 3, 3) : $_->subfield('8') || '',
1020 term => $_->subfield('a') . ($_->subfield('b') ? ', ' . $_->subfield('b') : ''),
1023 } } $record->field('7..');
1026 # construct MARC21 summary
1027 # FIXME - looping over 1XX is questionable
1028 # since MARC21 authority should have only one 1XX
1029 my $subfields_to_report;
1030 foreach my $field ($record->field('1..')) {
1031 my $tag = $field->tag();
1032 next if "152" eq $tag;
1033 # FIXME - 152 is not a good tag to use
1034 # in MARC21 -- purely local tags really ought to be
1036 if ($tag eq '100') {
1037 $subfields_to_report = 'abcdefghjklmnopqrstvxyz';
1038 } elsif ($tag eq '110') {
1039 $subfields_to_report = 'abcdefghklmnoprstvxyz';
1040 } elsif ($tag eq '111') {
1041 $subfields_to_report = 'acdefghklnpqstvxyz';
1042 } elsif ($tag eq '130') {
1043 $subfields_to_report = 'adfghklmnoprstvxyz';
1044 } elsif ($tag eq '148') {
1045 $subfields_to_report = 'abvxyz';
1046 } elsif ($tag eq '150') {
1047 $subfields_to_report = 'abvxyz';
1048 } elsif ($tag eq '151') {
1049 $subfields_to_report = 'avxyz';
1050 } elsif ($tag eq '155') {
1051 $subfields_to_report = 'abvxyz';
1052 } elsif ($tag eq '180') {
1053 $subfields_to_report = 'vxyz';
1054 } elsif ($tag eq '181') {
1055 $subfields_to_report = 'vxyz';
1056 } elsif ($tag eq '182') {
1057 $subfields_to_report = 'vxyz';
1058 } elsif ($tag eq '185') {
1059 $subfields_to_report = 'vxyz';
1061 if ($subfields_to_report) {
1063 heading => $field->as_string($subfields_to_report),
1064 hemain => ( $field->subfield( substr($subfields_to_report, 0, 1) ) // undef ),
1069 heading => $field->as_string(),
1070 hemain => ( $field->subfield( 'a' ) // undef ),
1075 foreach my $field ($record->field('4..')) { #See From
1076 my $type = 'seefrom';
1077 $type = ($marc21controlrefs{substr $field->subfield('w'), 0, 1} || '') if ($field->subfield('w'));
1078 if ($type eq 'notapplicable') {
1079 $type = substr $field->subfield('w'), 2, 1;
1080 $type = 'earlier' if $type && $type ne 'n';
1082 if ($type eq 'subfi') {
1084 heading => $field->as_string($marc21subfields),
1085 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1086 type => ($field->subfield('i') || ''),
1087 field => $field->tag(),
1091 heading => $field->as_string($marc21subfields),
1092 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1094 field => $field->tag(),
1098 foreach my $field ($record->field('5..')) { #See Also
1099 my $type = 'seealso';
1100 $type = ($marc21controlrefs{substr $field->subfield('w'), 0, 1} || '') if ($field->subfield('w'));
1101 if ($type eq 'notapplicable') {
1102 $type = substr $field->subfield('w'), 2, 1;
1103 $type = 'earlier' if $type && $type ne 'n';
1105 if ($type eq 'subfi') {
1107 heading => $field->as_string($marc21subfields),
1108 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1109 type => $field->subfield('i'),
1110 field => $field->tag(),
1111 search => $field->as_string($marc21subfields) || '',
1112 authid => $field->subfield('9') || ''
1116 heading => $field->as_string($marc21subfields),
1117 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1119 field => $field->tag(),
1120 search => $field->as_string($marc21subfields) || '',
1121 authid => $field->subfield('9') || ''
1125 foreach my $field ($record->field('6..')) {
1126 push @notes, { note => $field->as_string(), field => $field->tag() };
1128 foreach my $field ($record->field('880')) {
1129 my $linkage = $field->subfield('6');
1130 my $category = substr $linkage, 0, 1;
1131 if ($category eq '1') {
1132 $category = 'preferred';
1133 } elsif ($category eq '4') {
1134 $category = 'seefrom';
1135 } elsif ($category eq '5') {
1136 $category = 'seealso';
1139 if ($field->subfield('w')) {
1140 $type = $marc21controlrefs{substr $field->subfield('w'), '0'};
1144 my $direction = $linkage =~ m#/r$# ? 'rtl' : 'ltr';
1145 push @otherscript, { term => $field->as_string($subfields_to_report), category => $category, type => $type, direction => $direction, linkage => $linkage };
1148 $summary{mainentry} = $authorized[0]->{heading};
1149 $summary{mainmainentry} = $authorized[0]->{hemain};
1150 $summary{authorized} = \@authorized;
1151 $summary{notes} = \@notes;
1152 $summary{seefrom} = \@seefrom;
1153 $summary{seealso} = \@seealso;
1154 $summary{otherscript} = \@otherscript;
1158 =head2 GetAuthorizedHeading
1160 $heading = &GetAuthorizedHeading({ record => $record, authid => $authid })
1162 Takes a MARC::Record object describing an authority record or an authid, and
1163 returns a string representation of the first authorized heading. This routine
1164 should be considered a temporary shim to ease the future migration of authority
1165 data from C4::AuthoritiesMarc to the object-oriented Koha::*::Authority.
1169 sub GetAuthorizedHeading {
1172 unless ($record = $args->{record}) {
1173 return unless $args->{authid};
1174 $record = GetAuthority($args->{authid});
1176 return unless (ref $record eq 'MARC::Record');
1177 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
1178 # construct UNIMARC summary, that is quite different from MARC21 one
1180 foreach my $field ($record->field('2..')) {
1181 return $field->as_string('abcdefghijlmnopqrstuvwxyz');
1184 foreach my $field ($record->field('1..')) {
1185 my $tag = $field->tag();
1186 next if "152" eq $tag;
1187 # FIXME - 152 is not a good tag to use
1188 # in MARC21 -- purely local tags really ought to be
1190 if ($tag eq '100') {
1191 return $field->as_string('abcdefghjklmnopqrstvxyz68');
1192 } elsif ($tag eq '110') {
1193 return $field->as_string('abcdefghklmnoprstvxyz68');
1194 } elsif ($tag eq '111') {
1195 return $field->as_string('acdefghklnpqstvxyz68');
1196 } elsif ($tag eq '130') {
1197 return $field->as_string('adfghklmnoprstvxyz68');
1198 } elsif ($tag eq '148') {
1199 return $field->as_string('abvxyz68');
1200 } elsif ($tag eq '150') {
1201 return $field->as_string('abvxyz68');
1202 } elsif ($tag eq '151') {
1203 return $field->as_string('avxyz68');
1204 } elsif ($tag eq '155') {
1205 return $field->as_string('abvxyz68');
1206 } elsif ($tag eq '180') {
1207 return $field->as_string('vxyz68');
1208 } elsif ($tag eq '181') {
1209 return $field->as_string('vxyz68');
1210 } elsif ($tag eq '182') {
1211 return $field->as_string('vxyz68');
1212 } elsif ($tag eq '185') {
1213 return $field->as_string('vxyz68');
1215 return $field->as_string();
1222 =head2 BuildAuthHierarchies
1224 $text= &BuildAuthHierarchies( $authid, $force)
1226 return text containing trees for hierarchies
1227 for them to be stored in auth_header
1230 122,1314,2452;1324,2342,3,2452
1234 sub BuildAuthHierarchies{
1235 my $authid = shift @_;
1236 # warn "authid : $authid";
1237 my $force = shift @_ || (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 0 : 1);
1239 my $dbh=C4::Context->dbh;
1241 my $data = GetHeaderAuthority($authid);
1242 if ($data->{'authtrees'} and not $force){
1243 return $data->{'authtrees'};
1244 # } elsif ($data->{'authtrees'}){
1245 # $hierarchies=$data->{'authtrees'};
1247 my $record = GetAuthority($authid);
1249 return unless $record;
1250 foreach my $field ($record->field('5..')){
1253 (C4::Context->preference('marcflavour') eq 'UNIMARC' && $field->subfield('5') && $field->subfield('5') eq 'g') ||
1254 (C4::Context->preference('marcflavour') ne 'UNIMARC' && $field->subfield('w') && substr($field->subfield('w'), 0, 1) eq 'g'));
1256 my $subfauthid=_get_authid_subfield($field) || '';
1257 next if ($subfauthid eq $authid);
1258 my $parentrecord = GetAuthority($subfauthid);
1259 next unless $parentrecord;
1260 my $localresult=$hierarchies;
1262 $trees = BuildAuthHierarchies($subfauthid);
1265 @trees = split(/;/,$trees);
1267 push @trees, $trees;
1272 @globalresult = (@globalresult,@trees);
1275 $hierarchies=join(";",@globalresult);
1277 #Unless there is no ancestor, I am alone.
1278 $hierarchies="$authid" unless ($hierarchies);
1280 AddAuthorityTrees($authid,$hierarchies);
1281 return $hierarchies;
1284 =head2 BuildAuthHierarchy
1286 $ref= &BuildAuthHierarchy( $record, $class,$authid)
1288 return a hashref in order to display hierarchy for record and final Authid $authid
1299 sub BuildAuthHierarchy{
1300 my $record = shift @_;
1301 my $class = shift @_;
1302 my $authid_constructed = shift @_;
1303 return unless ($record && $record->field('001'));
1304 my $authid=$record->field('001')->data();
1306 my $parents=""; my $children="";
1307 my (@loopparents,@loopchildren);
1308 my $marcflavour = C4::Context->preference('marcflavour');
1309 my $relationshipsf = $marcflavour eq 'UNIMARC' ? '5' : 'w';
1310 foreach my $field ($record->field('5..')){
1311 my $subfauthid=_get_authid_subfield($field);
1312 if ($subfauthid && $field->subfield($relationshipsf) && $field->subfield('a')){
1313 my $relationship = substr($field->subfield($relationshipsf), 0, 1);
1314 if ($relationship eq 'h'){
1315 push @loopchildren, { "authid"=>$subfauthid,"value"=>$field->subfield('a')};
1317 elsif ($relationship eq 'g'){
1318 push @loopparents, { "authid"=>$subfauthid,"value"=>$field->subfield('a')};
1320 # brothers could get in there with an else
1323 $cell{"parents"}=\@loopparents;
1324 $cell{"children"}=\@loopchildren;
1325 $cell{"class"}=$class;
1326 $cell{"authid"}=$authid;
1327 $cell{"current_value"} =1 if ($authid eq $authid_constructed);
1328 $cell{"value"}=C4::Context->preference('marcflavour') eq 'UNIMARC' ? $record->subfield('2..',"a") : $record->subfield('1..', 'a');
1332 =head2 BuildAuthHierarchyBranch
1334 $branch = &BuildAuthHierarchyBranch( $tree, $authid[, $cnt])
1336 Return a data structure representing an authority hierarchy
1337 given a list of authorities representing a single branch in
1338 an authority hierarchy tree. $authid is the current node in
1339 the tree (which may or may not be somewhere in the middle).
1340 $cnt represents the level of the upper-most item, and is only
1341 used when BuildAuthHierarchyBranch is called recursively (i.e.,
1342 don't ever pass in anything but zero to it).
1346 sub BuildAuthHierarchyBranch {
1347 my ($tree, $authid, $cnt) = @_;
1349 my $elementdata = GetAuthority(shift @$tree);
1350 my $branch = BuildAuthHierarchy($elementdata,"child".$cnt, $authid);
1351 if (scalar @$tree > 0) {
1352 my $nextBranch = BuildAuthHierarchyBranch($tree, $authid, ++$cnt);
1353 my $nextAuthid = $nextBranch->{authid};
1355 # If we already have the next branch listed as a child, let's
1356 # replace the old listing with the new one. If not, we will add
1357 # the branch at the end.
1358 foreach my $cell (@{$branch->{children}}) {
1359 if ($cell->{authid} eq $nextAuthid) {
1360 $cell = $nextBranch;
1365 push @{$branch->{children}}, $nextBranch unless $found;
1370 =head2 GenerateHierarchy
1372 $hierarchy = &GenerateHierarchy($authid);
1374 Return an arrayref holding one or more "trees" representing
1375 authority hierarchies.
1379 sub GenerateHierarchy {
1381 my $trees = BuildAuthHierarchies($authid);
1382 my @trees = split /;/,$trees ;
1383 push @trees,$trees unless (@trees);
1384 my @loophierarchies;
1385 foreach my $tree (@trees){
1386 my @tree=split /,/,$tree;
1387 push @tree, $tree unless (@tree);
1388 my $branch = BuildAuthHierarchyBranch(\@tree, $authid);
1389 push @loophierarchies, [ $branch ];
1391 return \@loophierarchies;
1394 sub _get_authid_subfield{
1396 return $field->subfield('9')||$field->subfield('3');
1398 =head2 GetHeaderAuthority
1400 $ref= &GetHeaderAuthority( $authid)
1402 return a hashref in order auth_header table data
1406 sub GetHeaderAuthority{
1407 my $authid = shift @_;
1408 my $sql= "SELECT * from auth_header WHERE authid = ?";
1409 my $dbh=C4::Context->dbh;
1410 my $rq= $dbh->prepare($sql);
1411 $rq->execute($authid);
1412 my $data= $rq->fetchrow_hashref;
1416 =head2 AddAuthorityTrees
1418 $ref= &AddAuthorityTrees( $authid, $trees)
1420 return success or failure
1424 sub AddAuthorityTrees{
1425 my $authid = shift @_;
1426 my $trees = shift @_;
1427 my $sql= "UPDATE IGNORE auth_header set authtrees=? WHERE authid = ?";
1428 my $dbh=C4::Context->dbh;
1429 my $rq= $dbh->prepare($sql);
1430 return $rq->execute($trees,$authid);
1435 $ref= &merge(mergefrom,$MARCfrom,$mergeto,$MARCto)
1437 Could add some feature : Migrating from a typecode to an other for instance.
1438 Then we should add some new parameter : bibliotargettag, authtargettag
1443 my ($mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
1444 my ($counteditedbiblio,$countunmodifiedbiblio,$counterrors)=(0,0,0);
1445 my $dbh=C4::Context->dbh;
1446 my $authtypecodefrom = GetAuthTypeCode($mergefrom);
1447 my $authtypecodeto = GetAuthTypeCode($mergeto);
1448 # warn "mergefrom : $authtypecodefrom $mergefrom mergeto : $authtypecodeto $mergeto ";
1449 # return if authority does not exist
1450 return "error MARCFROM not a marcrecord ".Data::Dumper::Dumper($MARCfrom) if scalar($MARCfrom->fields()) == 0;
1451 return "error MARCTO not a marcrecord".Data::Dumper::Dumper($MARCto) if scalar($MARCto->fields()) == 0;
1452 # search the tag to report
1453 my $auth_tag_to_report_from = Koha::Authority::Types->find($authtypecodefrom)->auth_tag_to_report;
1454 my $auth_tag_to_report_to = Koha::Authority::Types->find($authtypecodeto)->auth_tag_to_report;
1457 @record_to = $MARCto->field($auth_tag_to_report_to)->subfields() if $MARCto->field($auth_tag_to_report_to);
1459 @record_from = $MARCfrom->field($auth_tag_to_report_from)->subfields() if $MARCfrom->field($auth_tag_to_report_from);
1462 # search all biblio tags using this authority.
1463 #Getting marcbiblios impacted by the change.
1465 my $oConnection=C4::Context->Zconn("biblioserver",0);
1466 # We used to use XML syntax here, but that no longer works.
1467 # Thankfully, we don't need it.
1469 $query= "an=".$mergefrom;
1470 my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1473 $count=$oResult->size();
1476 while ( $z<$count ) {
1477 my $marcrecordzebra = C4::Search::new_record_from_zebra(
1479 $oResult->record($z)->raw()
1481 my ( $biblionumbertagfield, $biblionumbertagsubfield ) = &GetMarcFromKohaField( "biblio.biblionumber", '' );
1482 my $i = ($biblionumbertagfield < 10)
1483 ? $marcrecordzebra->field( $biblionumbertagfield )->data
1484 : $marcrecordzebra->subfield( $biblionumbertagfield, $biblionumbertagsubfield );
1485 my $marcrecorddb = GetMarcBiblio($i);
1486 push @reccache, $marcrecorddb;
1489 $oResult->destroy();
1490 #warn scalar(@reccache)." biblios to update";
1491 # Get All candidate Tags for the change
1492 # (This will reduce the search scope in marc records).
1493 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1494 $sth->execute($authtypecodefrom);
1495 my @tags_using_authtype;
1496 while (my ($tagfield) = $sth->fetchrow) {
1497 push @tags_using_authtype,$tagfield ;
1500 if ($authtypecodeto ne $authtypecodefrom){
1501 # If many tags, take the first
1502 $sth->execute($authtypecodeto);
1503 $tag_to=$sth->fetchrow;
1506 # BulkEdit marc records
1507 # May be used as a template for a bulkedit field
1508 foreach my $marcrecord(@reccache){
1510 foreach my $tagfield (@tags_using_authtype){
1511 # warn "tagfield : $tagfield ";
1512 foreach my $field ($marcrecord->field($tagfield)){
1513 # biblio is linked to authority with $9 subfield containing authid
1514 my $auth_number=$field->subfield("9");
1515 my $tag=$field->tag();
1516 if ($auth_number==$mergefrom) {
1517 my $field_to=MARC::Field->new(($tag_to?$tag_to:$tag),$field->indicator(1),$field->indicator(2),"9"=>$mergeto);
1519 foreach my $subfield (grep {$_->[0] ne '9'} @record_to) {
1520 $field_to->add_subfields($subfield->[0] =>$subfield->[1]);
1521 $exclude.= $subfield->[0];
1523 $exclude='['.$exclude.']';
1524 # add subfields in $field not included in @record_to
1525 my @restore= grep {$_->[0]!~/$exclude/} $field->subfields();
1526 foreach my $subfield (@restore) {
1527 $field_to->add_subfields($subfield->[0] =>$subfield->[1]);
1529 $marcrecord->delete_field($field);
1530 $marcrecord->insert_grouped_field($field_to);
1535 my ($bibliotag,$bibliosubf) = GetMarcFromKohaField("biblio.biblionumber","") ;
1538 $biblionumber=$marcrecord->field($bibliotag)->data;
1541 $biblionumber=$marcrecord->subfield($bibliotag,$bibliosubf);
1543 unless ($biblionumber){
1544 warn "pas de numéro de notice bibliographique dans : ".$marcrecord->as_formatted;
1548 &ModBiblio($marcrecord,$biblionumber,GetFrameworkCode($biblionumber)) ;
1549 $counteditedbiblio++;
1550 warn $counteditedbiblio if (($counteditedbiblio % 10) and $ENV{DEBUG});
1553 return $counteditedbiblio;
1554 # now, find every other authority linked with this authority
1555 # now, find every other authority linked with this authority
1556 # my $oConnection=C4::Context->Zconn("authorityserver");
1558 # # att 9210 Auth-Internal-authtype
1559 # # att 9220 Auth-Internal-LN
1560 # # ccl.properties to add for authorities
1561 # $query= "= ".$mergefrom;
1562 # my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1563 # my $count=$oResult->size() if ($oResult);
1566 # while ( $z<$count ) {
1568 # $rec=$oResult->record($z);
1569 # my $marcdata = $rec->raw();
1570 # push @reccache, $marcdata;
1573 # $oResult->destroy();
1574 # foreach my $marc(@reccache){
1577 # $marcrecord = MARC::File::USMARC::decode($marc);
1578 # foreach my $tagfield (@tags_using_authtype){
1579 # $tagfield=substr($tagfield,0,3);
1580 # my @tags = $marcrecord->field($tagfield);
1581 # foreach my $tag (@tags){
1582 # my $tagsubs=$tag->subfield("9");
1583 # #warn "$tagfield:$tagsubs:$mergefrom";
1584 # if ($tagsubs== $mergefrom) {
1585 # $tag->update("9" =>$mergeto);
1586 # foreach my $subfield (@record_to) {
1587 # # warn "$subfield,$subfield->[0],$subfield->[1]";
1588 # $tag->update($subfield->[0] =>$subfield->[1]);
1591 # $marcrecord->delete_field($tag);
1592 # $marcrecord->add_fields($tag);
1595 # }#foreach tagfield
1596 # my $authoritynumber = TransformMarcToKoha($dbh,$marcrecord,"") ;
1598 # &ModAuthority($marcrecord,$authoritynumber,GetAuthTypeCode($authoritynumber)) ;
1604 =head2 get_auth_type_location
1606 my ($tag, $subfield) = get_auth_type_location($auth_type_code);
1608 Get the tag and subfield used to store the heading type
1609 for indexing purposes. The C<$auth_type> parameter is
1610 optional; if it is not supplied, assume ''.
1612 This routine searches the MARC authority framework
1613 for the tag and subfield whose kohafield is
1614 C<auth_header.authtypecode>; if no such field is
1615 defined in the framework, default to the hardcoded value
1616 specific to the MARC format.
1620 sub get_auth_type_location {
1621 my $auth_type_code = @_ ? shift : '';
1623 my ($tag, $subfield) = GetAuthMARCFromKohaField('auth_header.authtypecode', $auth_type_code);
1624 if (defined $tag and defined $subfield and $tag != 0 and $subfield ne '' and $subfield ne ' ') {
1625 return ($tag, $subfield);
1627 if (C4::Context->preference('marcflavour') eq "MARC21") {
1628 return C4::AuthoritiesMarc::MARC21::default_auth_type_location();
1630 return C4::AuthoritiesMarc::UNIMARC::default_auth_type_location();
1635 END { } # module clean-up code here (global destructor)
1642 Koha Development Team <http://koha-community.org/>
1644 Paul POULAIN paul.poulain@free.fr