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::Authorities;
31 use Koha::Authority::Types;
33 use Koha::SearchEngine;
34 use Koha::SearchEngine::Search;
36 use vars qw(@ISA @EXPORT);
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 $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;
112 $sortby="" unless $sortby;
116 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
118 # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
119 # the authtypecode. Then, search on $a of this tag_to_report
120 # also store main entry MARC tag, to extract it at end of search
122 ##first set the authtype search and may be multiple authorities
126 my @auths=split / /,$authtypecode ;
127 foreach my $auth (@auths){
128 $query .=" \@attr 1=authtype \@attr 5=100 ".$auth; ##No truncation on authtype
129 push @authtypecode ,$auth;
133 while ($n>1){$query= "\@or ".$query;$n--;}
136 $qpquery .= '(authtype:' . join('|| authtype:', @auths) . ')';
144 for ( my $i = 0 ; $i <= $#{$value} ; $i++ ) {
147 if ( @$tags[$i] eq "mainmainentry" ) {
148 $attr = " \@attr 1=Heading-Main ";
150 elsif ( @$tags[$i] eq "mainentry" ) {
151 $attr = " \@attr 1=Heading ";
153 elsif ( @$tags[$i] eq "match" ) {
154 $attr = " \@attr 1=Match ";
156 elsif ( @$tags[$i] eq "match-heading" ) {
157 $attr = " \@attr 1=Match-heading ";
159 elsif ( @$tags[$i] eq "see-from" ) {
160 $attr = " \@attr 1=Match-heading-see-from ";
162 elsif ( @$tags[$i] eq "thesaurus" ) {
163 $attr = " \@attr 1=Subject-heading-thesaurus ";
165 else { # Assume any if no index was specified
166 $attr = " \@attr 1=Any ";
169 else { # Assume any if no index was specified
170 $attr = " \@attr 1=Any ";
173 my $operator = @$operator[$i];
174 if ( $operator and $operator eq 'is' ) {
175 $attr .= " \@attr 4=1 \@attr 5=100 "
176 ; ##Phrase, No truncation,all of subfield field must match
178 elsif ( $operator and $operator eq "=" ) {
179 $attr .= " \@attr 4=107 "; #Number Exact match
181 elsif ( $operator and $operator eq "start" ) {
182 $attr .= " \@attr 3=2 \@attr 4=1 \@attr 5=1 "
183 ; #Firstinfield Phrase, Right truncated
185 elsif ( $operator and $operator eq "exact" ) {
186 $attr .= " \@attr 4=1 \@attr 5=100 \@attr 6=3 "
187 ; ##Phrase, No truncation,all of subfield field must match
190 $attr .= " \@attr 5=1 \@attr 4=6 "
191 ; ## Word list, right truncated, anywhere
192 if ( $sortby eq 'Relevance' ) {
193 $attr .= "\@attr 2=102 ";
197 s/"/\\"/g; # Escape the double-quotes in the search value
198 $attr = $attr . "\"" . @$value[$i] . "\"";
203 $qpquery .= " $tags->[$i]:\"$value->[$i]\"";
207 ##Add how many queries generated
208 if (defined $query && $query=~/\S+/){
209 $query= $and x $attr_cnt . $query . (defined $q2 ? $q2 : '');
214 #$query=' @or @attr 7=2 @attr 1=Heading 0 @or @attr 7=1 @attr 1=Heading 1'.$query if ($sortby eq "HeadingDsc");
216 if ($sortby eq 'HeadingAsc') {
217 $orderstring = '@attr 7=1 @attr 1=Heading 0';
218 } elsif ($sortby eq 'HeadingDsc') {
219 $orderstring = '@attr 7=2 @attr 1=Heading 0';
220 } elsif ($sortby eq 'AuthidAsc') {
221 $orderstring = '@attr 7=1 @attr 4=109 @attr 1=Local-Number 0';
222 } elsif ($sortby eq 'AuthidDsc') {
223 $orderstring = '@attr 7=2 @attr 4=109 @attr 1=Local-Number 0';
226 $qpquery .= ' all:all' unless $value->[0];
228 if ( $value->[0] =~ m/^qp=(.*)$/ ) {
232 $qpquery .= " #$sortby" unless $sortby eq '';
234 $QParser->parse( $qpquery );
235 $query = $QParser->target_syntax('authorityserver');
237 $query=($query?$query:"\@attr 1=_ALLRECORDS \@attr 2=103 ''");
238 $query="\@or $orderstring $query" if $orderstring;
241 $offset=0 unless $offset;
242 my $counter = $offset;
243 $length=10 unless $length;
246 $oAuth[0]=C4::Context->Zconn("authorityserver" , 1);
247 my $Anewq= new ZOOM::Query::PQF($query,$oAuth[0]);
249 $oAResult= $oAuth[0]->search($Anewq) ;
250 while (($i = ZOOM::event(\@oAuth)) != 0) {
251 my $ev = $oAuth[$i-1]->last_event();
252 last if $ev == ZOOM::Event::ZEND;
254 my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
256 warn "oAuth error: $errmsg ($error) $addinfo $diagset\n";
261 $nbresults=$oAResult->size();
262 my $nremains=$nbresults;
264 my @finalresult = ();
268 ##Find authid and linkid fields
269 ##we may be searching multiple authoritytypes.
270 ## FIXME this assumes that all authid and linkid fields are the same for all authority types
271 # my ($authidfield,$authidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.authid",$authtypecode[0]);
272 # my ($linkidfield,$linkidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.linkid",$authtypecode[0]);
273 while (($counter < $nbresults) && ($counter < ($offset + $length))) {
275 ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
276 my $rec=$oAResult->record($counter);
277 my $separator=C4::Context->preference('AuthoritySeparator');
278 my $authrecord = C4::Search::new_record_from_zebra(
283 if ( !defined $authrecord or !defined $authrecord->field('001') ) {
288 SetUTF8Flag( $authrecord );
290 my $authid=$authrecord->field('001')->data();
292 $newline{authid} = $authid;
293 if ( !$skipmetadata ) {
294 my $auth_tag_to_report;
295 $auth_tag_to_report = Koha::Authority::Types->find($authtypecode)->auth_tag_to_report
298 my $mainentry = $authrecord->field($auth_tag_to_report);
300 foreach ( $mainentry->subfields() ) {
301 $reported_tag .= '$' . $_->[0] . $_->[1];
305 my ( $thisauthtype, $thisauthtypecode );
306 if ( my $authority = Koha::Authorities->find($authid) ) {
307 $thisauthtypecode = $authority->authtypecode;
308 $thisauthtype = Koha::Authority::Types->find($thisauthtypecode);
310 unless (defined $thisauthtype) {
311 $thisauthtypecode = $authtypecode;
312 $thisauthtype = Koha::Authority::Types->find($thisauthtypecode);
314 my $summary = BuildSummary( $authrecord, $authid, $thisauthtypecode );
316 $newline{authtype} = defined($thisauthtype) ?
317 $thisauthtype->authtypetext : '';
318 $newline{summary} = $summary;
319 $newline{even} = $counter % 2;
320 $newline{reported_tag} = $reported_tag;
323 push @finalresult, \%newline;
326 if (! $skipmetadata) {
327 for (my $z=0; $z<@finalresult; $z++){
328 my $count=CountUsage($finalresult[$z]{authid});
329 $finalresult[$z]{used}=$count;
335 $oAResult->destroy();
336 # $oAuth[0]->destroy();
338 return (\@finalresult, $nbresults);
343 $count= &CountUsage($authid)
345 counts Usage of Authid in bibliorecords.
353 $query= "an:".$authid;
354 # Should really be replaced with a real count call, this is a
356 my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
357 my ($err,$res,$result) = $searcher->simple_search_compat($query,0,1);
359 warn "Error: $err from search $query";
366 =head2 CountUsageChildren
368 $count= &CountUsageChildren($authid)
370 counts Usage of narrower terms of Authid in bibliorecords.
374 sub CountUsageChildren {
378 =head2 GuessAuthTypeCode
380 my $authtypecode = GuessAuthTypeCode($record);
382 Get the record and tries to guess the adequate authtypecode from its content.
386 sub GuessAuthTypeCode {
387 my ($record, $heading_fields) = @_;
388 return unless defined $record;
389 $heading_fields //= {
391 '100'=>{authtypecode=>'PERSO_NAME'},
392 '110'=>{authtypecode=>'CORPO_NAME'},
393 '111'=>{authtypecode=>'MEETI_NAME'},
394 '130'=>{authtypecode=>'UNIF_TITLE'},
395 '148'=>{authtypecode=>'CHRON_TERM'},
396 '150'=>{authtypecode=>'TOPIC_TERM'},
397 '151'=>{authtypecode=>'GEOGR_NAME'},
398 '155'=>{authtypecode=>'GENRE/FORM'},
399 '180'=>{authtypecode=>'GEN_SUBDIV'},
400 '181'=>{authtypecode=>'GEO_SUBDIV'},
401 '182'=>{authtypecode=>'CHRON_SUBD'},
402 '185'=>{authtypecode=>'FORM_SUBD'},
404 #200 Personal name 700, 701, 702 4-- with embedded 700, 701, 702 600
405 # 604 with embedded 700, 701, 702
406 #210 Corporate or meeting name 710, 711, 712 4-- with embedded 710, 711, 712 601 604 with embedded 710, 711, 712
407 #215 Territorial or geographic name 710, 711, 712 4-- with embedded 710, 711, 712 601, 607 604 with embedded 710, 711, 712
408 #216 Trademark 716 [Reserved for future use]
409 #220 Family name 720, 721, 722 4-- with embedded 720, 721, 722 602 604 with embedded 720, 721, 722
410 #230 Title 500 4-- with embedded 500 605
411 #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
412 #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
413 #250 Topical subject 606
414 #260 Place access 620
415 #280 Form, genre or physical characteristics 608
418 # Could also be represented with :
420 #a = personal name entry
421 #b = corporate name entry
422 #c = territorial or geographical name
426 #g = collective uniform title
428 #i = name/collective uniform title
431 #l = form, genre or physical characteristics
433 '200'=>{authtypecode=>'NP'},
434 '210'=>{authtypecode=>'CO'},
435 '215'=>{authtypecode=>'SNG'},
436 '216'=>{authtypecode=>'TM'},
437 '220'=>{authtypecode=>'FAM'},
438 '230'=>{authtypecode=>'TU'},
439 '235'=>{authtypecode=>'CO_UNI_TI'},
440 '240'=>{authtypecode=>'SAUTTIT'},
441 '245'=>{authtypecode=>'NAME_COL'},
442 '250'=>{authtypecode=>'SNC'},
443 '260'=>{authtypecode=>'PA'},
444 '280'=>{authtypecode=>'GENRE/FORM'},
447 foreach my $field (keys %{$heading_fields->{uc(C4::Context->preference('marcflavour'))} }) {
448 return $heading_fields->{uc(C4::Context->preference('marcflavour'))}->{$field}->{'authtypecode'} if (defined $record->field($field));
455 my $authtid = GuessAuthId($record);
457 Get the record and tries to guess the adequate authtypecode from its content.
463 return unless ($record && $record->field('001'));
464 # my $authtypecode=GuessAuthTypeCode($record);
465 # my ($tag,$subfield)=GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
466 # if ($tag > 010) {return $record->subfield($tag,$subfield)}
467 # else {return $record->field($tag)->data}
468 return $record->field('001')->data;
473 $tagslabel= &GetTagsLabels($forlibrarian,$authtypecode)
475 returns a ref to hashref of authorities tag and subfield structure.
479 $tagslabel->{$tag}->{$subfield}->{'attribute'}
481 where attribute takes values in :
499 my ($forlibrarian,$authtypecode)= @_;
500 my $dbh=C4::Context->dbh;
501 $authtypecode="" unless $authtypecode;
503 my $libfield = ($forlibrarian == 1)? 'liblibrarian' : 'libopac';
506 # check that authority exists
507 $sth=$dbh->prepare("SELECT count(*) FROM auth_tag_structure WHERE authtypecode=?");
508 $sth->execute($authtypecode);
509 my ($total) = $sth->fetchrow;
510 $authtypecode="" unless ($total >0);
512 "SELECT auth_tag_structure.tagfield,auth_tag_structure.liblibrarian,auth_tag_structure.libopac,auth_tag_structure.mandatory,auth_tag_structure.repeatable
513 FROM auth_tag_structure
518 $sth->execute($authtypecode);
519 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
521 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
522 $res->{$tag}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
523 $res->{$tag}->{tab} = " "; # XXX
524 $res->{$tag}->{mandatory} = $mandatory;
525 $res->{$tag}->{repeatable} = $repeatable;
528 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,frameworkcode as authtypecode,value_builder,kohafield,seealso,hidden,isurl,defaultvalue
529 FROM auth_subfield_structure
531 ORDER BY tagfield,tagsubfield"
533 $sth->execute($authtypecode);
536 my $authorised_value;
546 ( $tag, $subfield, $liblibrarian, , $libopac, $tab,
547 $mandatory, $repeatable, $authorised_value, $authtypecode,
548 $value_builder, $kohafield, $seealso, $hidden,
549 $isurl, $defaultvalue, $link )
553 $res->{$tag}->{$subfield}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
554 $res->{$tag}->{$subfield}->{tab} = $tab;
555 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
556 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
557 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
558 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
559 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
560 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
561 $res->{$tag}->{$subfield}->{seealso} = $seealso;
562 $res->{$tag}->{$subfield}->{hidden} = $hidden;
563 $res->{$tag}->{$subfield}->{isurl} = $isurl;
564 $res->{$tag}->{$subfield}->{link} = $link;
565 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
572 $authid= &AddAuthority($record, $authid,$authtypecode)
574 Either Create Or Modify existing authority.
575 returns authid of the newly created authority
580 # pass the MARC::Record to this function, and it will create the records in the authority table
581 my ($record,$authid,$authtypecode) = @_;
582 my $dbh=C4::Context->dbh;
583 my $leader=' nz a22 o 4500';#Leader for incomplete MARC21 record
585 # if authid empty => true add, find a new authid number
587 if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') {
588 $format= 'UNIMARCAUTH';
594 #update date/time to 005 for marc and unimarc
595 my $time=POSIX::strftime("%Y%m%d%H%M%S",localtime);
596 my $f5=$record->field('005');
598 $record->insert_fields_ordered( MARC::Field->new('005',$time.".0") );
601 $f5->update($time.".0");
604 SetUTF8Flag($record);
605 if ($format eq "MARC21") {
606 if (!$record->leader) {
607 $record->leader($leader);
609 if (!$record->field('003')) {
610 $record->insert_fields_ordered(
611 MARC::Field->new('003',C4::Context->preference('MARCOrgCode'))
614 my $date=POSIX::strftime("%y%m%d",localtime);
615 if (!$record->field('008')) {
616 # Get a valid default value for field 008
617 my $default_008 = C4::Context->preference('MARCAuthorityControlField008');
618 if(!$default_008 or length($default_008)<34) {
619 $default_008 = '|| aca||aabn | a|a d';
622 $default_008 = substr($default_008,0,34);
625 $record->insert_fields_ordered( MARC::Field->new('008',$date.$default_008) );
627 if (!$record->field('040')) {
628 $record->insert_fields_ordered(
629 MARC::Field->new('040','','',
630 'a' => C4::Context->preference('MARCOrgCode'),
631 'c' => C4::Context->preference('MARCOrgCode')
637 if ($format eq "UNIMARCAUTH") {
638 $record->leader(" nx j22 ") unless ($record->leader());
639 my $date=POSIX::strftime("%Y%m%d",localtime);
640 my $defaultfield100 = C4::Context->preference('UNIMARCAuthorityField100');
641 if (my $string=$record->subfield('100',"a")){
642 $string=~s/fre50/frey50/;
643 $record->field('100')->update('a'=>$string);
645 elsif ($record->field('100')){
646 $record->field('100')->update('a'=>$date.$defaultfield100);
648 $record->append_fields(
649 MARC::Field->new('100',' ',' '
650 ,'a'=>$date.$defaultfield100)
654 my ($auth_type_tag, $auth_type_subfield) = get_auth_type_location($authtypecode);
655 if (!$authid and $format eq "MARC21") {
656 # only need to do this fix when modifying an existing authority
657 C4::AuthoritiesMarc::MARC21::fix_marc21_auth_type_location($record, $auth_type_tag, $auth_type_subfield);
659 if (my $field=$record->field($auth_type_tag)){
660 $field->update($auth_type_subfield=>$authtypecode);
663 $record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode);
669 my $sth=$dbh->prepare("select max(authid) from auth_header");
671 ($authid)=$sth->fetchrow;
673 ##Insert the recordID in MARC record
674 unless ($record->field('001') && $record->field('001')->data() eq $authid){
675 $record->delete_field($record->field('001'));
676 $record->insert_fields_ordered(MARC::Field->new('001',$authid));
679 $auth_exists=$dbh->do(qq(select authid from auth_header where authid=?),undef,$authid);
680 # warn "auth_exists = $auth_exists";
683 $oldRecord=GetAuthority($authid);
684 $record->add_fields('001',$authid) unless ($record->field('001'));
685 # warn "\n\n\n enregistrement".$record->as_formatted;
686 my $sth=$dbh->prepare("update auth_header set authtypecode=?,marc=?,marcxml=? where authid=?");
687 $sth->execute($authtypecode,$record->as_usmarc,$record->as_xml_record($format),$authid) or die $sth->errstr;
691 my $sth=$dbh->prepare("insert into auth_header (authid,datecreated,authtypecode,marc,marcxml) values (?,now(),?,?,?)");
692 $sth->execute($authid,$authtypecode,$record->as_usmarc,$record->as_xml_record($format));
694 logaction( "AUTHORITIES", "ADD", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
696 ModZebra($authid,'specialUpdate',"authorityserver",$oldRecord,$record);
703 $authid= &DelAuthority($authid)
711 my $dbh=C4::Context->dbh;
713 logaction( "AUTHORITIES", "DELETE", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
714 ModZebra($authid,"recordDelete","authorityserver",GetAuthority($authid),undef);
715 my $sth = $dbh->prepare("DELETE FROM auth_header WHERE authid=?");
716 $sth->execute($authid);
721 $authid= &ModAuthority($authid,$record,$authtypecode)
723 Modifies authority record, optionally updates attached biblios.
728 my ($authid,$record,$authtypecode)=@_; # deprecated $merge parameter removed
730 my $dbh=C4::Context->dbh;
731 #Now rewrite the $record to table with an add
732 my $oldrecord=GetAuthority($authid);
733 $authid=AddAuthority($record,$authid,$authtypecode);
735 # If a library thinks that updating all biblios is a long process and wishes
736 # to leave that to a cron job, use misc/migration_tools/merge_authority.pl.
737 # In that case set system preference "dontmerge" to 1. Otherwise biblios will
739 unless(C4::Context->preference('dontmerge') eq '1'){
740 &merge($authid,$oldrecord,$authid,$record);
742 # save a record in need_merge_authorities table
743 my $sqlinsert="INSERT INTO need_merge_authorities (authid, done) ".
745 $dbh->do($sqlinsert,undef,($authid,0));
747 logaction( "AUTHORITIES", "MODIFY", $authid, "authority BEFORE=>" . $oldrecord->as_formatted ) if C4::Context->preference("AuthoritiesLog");
751 =head2 GetAuthorityXML
753 $marcxml= &GetAuthorityXML( $authid)
755 returns xml form of record $authid
759 sub GetAuthorityXML {
760 # Returns MARC::XML of the authority passed in parameter.
762 if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') {
763 my $dbh=C4::Context->dbh;
764 my $sth = $dbh->prepare("select marcxml from auth_header where authid=? " );
765 $sth->execute($authid);
766 my ($marcxml)=$sth->fetchrow;
770 # for MARC21, call GetAuthority instead of
771 # getting the XML directly since we may
772 # need to fix up the location of the authority
773 # code -- note that this is reasonably safe
774 # because GetAuthorityXML is used only by the
775 # indexing processes like zebraqueue_start.pl
776 my $record = GetAuthority($authid);
777 return $record->as_xml_record('MARC21');
783 $record= &GetAuthority( $authid)
785 Returns MARC::Record of the authority passed in parameter.
791 my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
792 return unless $authority;
793 return ($authority->record);
796 =head2 FindDuplicateAuthority
798 $record= &FindDuplicateAuthority( $record, $authtypecode)
800 return $authid,Summary if duplicate is found.
802 Comments : an improvement would be to return All the records that match.
806 sub FindDuplicateAuthority {
808 my ($record,$authtypecode)=@_;
809 # warn "IN for ".$record->as_formatted;
810 my $dbh = C4::Context->dbh;
811 # warn "".$record->as_formatted;
812 my $auth_tag_to_report = Koha::Authority::Types->find($authtypecode)->auth_tag_to_report;
813 # warn "record :".$record->as_formatted." auth_tag_to_report :$auth_tag_to_report";
814 # build a request for SearchAuthorities
816 $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
823 my $query='at:'.$authtypecode.' ';
824 my $filtervalues=qr([\001-\040\Q!'"`#$%&*+,-./:;<=>?@(){[}_|~\E\]]);
825 if ($record->field($auth_tag_to_report)) {
826 foreach ($record->field($auth_tag_to_report)->subfields()) {
827 $_->[1]=~s/$filtervalues/ /g; $query.= " $op he:\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/);
830 my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::AUTHORITIES_INDEX});
831 my ($error, $results, $total_hits) = $searcher->simple_search_compat( $query, 0, 1 );
832 # there is at least 1 result => return the 1st one
833 if (!defined $error && @{$results} ) {
834 my $marcrecord = C4::Search::new_record_from_zebra(
838 return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode);
840 # no result, returns nothing
846 $summary= &BuildSummary( $record, $authid, $authtypecode)
848 Returns a hashref with a summary of the specified record.
850 Comment : authtypecode can be inferred from both record and authid.
851 Moreover, authid can also be inferred from $record.
852 Would it be interesting to delete those things.
857 ## give this a Marc record to return summary
858 my ($record,$authid,$authtypecode)=@_;
859 my $dbh=C4::Context->dbh;
861 my $summary_template;
862 # handle $authtypecode is NULL or eq ""
864 my $authref = Koha::Authority::Types->find($authtypecode);
866 $summary{authtypecode} = $authref->authtypecode;
867 $summary{type} = $authref->authtypetext;
868 $summary_template = $authref->summary;
869 # for MARC21, the authority type summary displays a label meant for
871 if (C4::Context->preference('marcflavour') ne 'UNIMARC') {
872 $summary{label} = $authref->summary;
874 $summary{summary} = $authref->summary;
878 my $marc21subfields = 'abcdfghjklmnopqrstuvxyz68';
879 my %marc21controlrefs = ( 'a' => 'earlier',
885 'n' => 'notapplicable',
889 my %unimarc_relation_from_code = (
895 $thesaurus{'1'}="Peuples";
896 $thesaurus{'2'}="Anthroponymes";
897 $thesaurus{'3'}="Oeuvres";
898 $thesaurus{'4'}="Chronologie";
899 $thesaurus{'5'}="Lieux";
900 $thesaurus{'6'}="Sujets";
903 # if the library has a summary defined, use it. Otherwise, build a standard one
904 # FIXME - it appears that the summary field in the authority frameworks
905 # can work as a display template. However, this doesn't
906 # suit the MARC21 version, so for now the "templating"
907 # feature will be enabled only for UNIMARC for backwards
909 if ($summary{summary} and C4::Context->preference('marcflavour') eq 'UNIMARC') {
910 my @matches = ($summary{summary} =~ m/\[(.*?)(\d{3})([\*a-z0-9])(.*?)\]/g);
911 my (@textbefore, @tag, @subtag, @textafter);
912 for(my $i = 0; $i < scalar @matches; $i++){
913 push @textbefore, $matches[$i] if($i%4 == 0);
914 push @tag, $matches[$i] if($i%4 == 1);
915 push @subtag, $matches[$i] if($i%4 == 2);
916 push @textafter, $matches[$i] if($i%4 == 3);
918 for(my $i = scalar @tag; $i >= 0; $i--){
919 my $textbefore = $textbefore[$i] || '';
920 my $tag = $tag[$i] || '';
921 my $subtag = $subtag[$i] || '';
922 my $textafter = $textafter[$i] || '';
924 my $field = $record->field($tag);
928 $value = $textbefore . $field->data() . $textafter;
931 my @subfields = $field->subfield($subtag);
933 $value = $textbefore . join (" - ", @subfields) . $textafter;
937 $summary{summary} =~ s/\[\Q$textbefore$tag$subtag$textafter\E\]/$value/;
939 $summary{summary} =~ s/\\n/<br \/>/g;
946 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
947 # construct UNIMARC summary, that is quite different from MARC21 one
949 foreach my $field ($record->field('2..')) {
951 heading => $field->as_string('abcdefghijlmnopqrstuvwxyz'),
952 hemain => ( $field->subfield('a') // undef ),
953 field => $field->tag(),
957 foreach my $field ($record->field('3..')) {
958 push @notes, { note => $field->subfield('a'), field => $field->tag() };
960 foreach my $field ($record->field('4..')) {
961 my $thesaurus = $field->subfield('2') ? "thes. : ".$thesaurus{"$field->subfield('2')"}." : " : '';
963 heading => $thesaurus . $field->as_string('abcdefghijlmnopqrstuvwxyz'),
964 hemain => ( $field->subfield('a') // undef ),
966 field => $field->tag(),
972 my $type = $unimarc_relation_from_code{$_->subfield('5') || 'a'};
973 my $heading = $_->as_string('abcdefgjxyz');
978 hemain => ( $_->subfield('a') // undef ),
980 authid => ( $_->subfield('9') // undef ),
982 } $record->field('5..');
985 @otherscript = map { {
986 lang => length ($_->subfield('8')) == 6 ? substr ($_->subfield('8'), 3, 3) : $_->subfield('8') || '',
987 term => $_->subfield('a') . ($_->subfield('b') ? ', ' . $_->subfield('b') : ''),
990 } } $record->field('7..');
993 # construct MARC21 summary
994 # FIXME - looping over 1XX is questionable
995 # since MARC21 authority should have only one 1XX
996 my $subfields_to_report;
997 foreach my $field ($record->field('1..')) {
998 my $tag = $field->tag();
999 next if "152" eq $tag;
1000 # FIXME - 152 is not a good tag to use
1001 # in MARC21 -- purely local tags really ought to be
1003 if ($tag eq '100') {
1004 $subfields_to_report = 'abcdefghjklmnopqrstvxyz';
1005 } elsif ($tag eq '110') {
1006 $subfields_to_report = 'abcdefghklmnoprstvxyz';
1007 } elsif ($tag eq '111') {
1008 $subfields_to_report = 'acdefghklnpqstvxyz';
1009 } elsif ($tag eq '130') {
1010 $subfields_to_report = 'adfghklmnoprstvxyz';
1011 } elsif ($tag eq '148') {
1012 $subfields_to_report = 'abvxyz';
1013 } elsif ($tag eq '150') {
1014 $subfields_to_report = 'abvxyz';
1015 } elsif ($tag eq '151') {
1016 $subfields_to_report = 'avxyz';
1017 } elsif ($tag eq '155') {
1018 $subfields_to_report = 'abvxyz';
1019 } elsif ($tag eq '180') {
1020 $subfields_to_report = 'vxyz';
1021 } elsif ($tag eq '181') {
1022 $subfields_to_report = 'vxyz';
1023 } elsif ($tag eq '182') {
1024 $subfields_to_report = 'vxyz';
1025 } elsif ($tag eq '185') {
1026 $subfields_to_report = 'vxyz';
1028 if ($subfields_to_report) {
1030 heading => $field->as_string($subfields_to_report),
1031 hemain => ( $field->subfield( substr($subfields_to_report, 0, 1) ) // undef ),
1036 heading => $field->as_string(),
1037 hemain => ( $field->subfield( 'a' ) // undef ),
1042 foreach my $field ($record->field('4..')) { #See From
1043 my $type = 'seefrom';
1044 $type = ($marc21controlrefs{substr $field->subfield('w'), 0, 1} || '') if ($field->subfield('w'));
1045 if ($type eq 'notapplicable') {
1046 $type = substr $field->subfield('w'), 2, 1;
1047 $type = 'earlier' if $type && $type ne 'n';
1049 if ($type eq 'subfi') {
1051 heading => $field->as_string($marc21subfields),
1052 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1053 type => ($field->subfield('i') || ''),
1054 field => $field->tag(),
1058 heading => $field->as_string($marc21subfields),
1059 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1061 field => $field->tag(),
1065 foreach my $field ($record->field('5..')) { #See Also
1066 my $type = 'seealso';
1067 $type = ($marc21controlrefs{substr $field->subfield('w'), 0, 1} || '') if ($field->subfield('w'));
1068 if ($type eq 'notapplicable') {
1069 $type = substr $field->subfield('w'), 2, 1;
1070 $type = 'earlier' if $type && $type ne 'n';
1072 if ($type eq 'subfi') {
1074 heading => $field->as_string($marc21subfields),
1075 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1076 type => $field->subfield('i'),
1077 field => $field->tag(),
1078 search => $field->as_string($marc21subfields) || '',
1079 authid => $field->subfield('9') || ''
1083 heading => $field->as_string($marc21subfields),
1084 hemain => $field->subfield( substr($marc21subfields, 0, 1) ),
1086 field => $field->tag(),
1087 search => $field->as_string($marc21subfields) || '',
1088 authid => $field->subfield('9') || ''
1092 foreach my $field ($record->field('6..')) {
1093 push @notes, { note => $field->as_string(), field => $field->tag() };
1095 foreach my $field ($record->field('880')) {
1096 my $linkage = $field->subfield('6');
1097 my $category = substr $linkage, 0, 1;
1098 if ($category eq '1') {
1099 $category = 'preferred';
1100 } elsif ($category eq '4') {
1101 $category = 'seefrom';
1102 } elsif ($category eq '5') {
1103 $category = 'seealso';
1106 if ($field->subfield('w')) {
1107 $type = $marc21controlrefs{substr $field->subfield('w'), '0'};
1111 my $direction = $linkage =~ m#/r$# ? 'rtl' : 'ltr';
1112 push @otherscript, { term => $field->as_string($subfields_to_report), category => $category, type => $type, direction => $direction, linkage => $linkage };
1115 $summary{mainentry} = $authorized[0]->{heading};
1116 $summary{mainmainentry} = $authorized[0]->{hemain};
1117 $summary{authorized} = \@authorized;
1118 $summary{notes} = \@notes;
1119 $summary{seefrom} = \@seefrom;
1120 $summary{seealso} = \@seealso;
1121 $summary{otherscript} = \@otherscript;
1125 =head2 GetAuthorizedHeading
1127 $heading = &GetAuthorizedHeading({ record => $record, authid => $authid })
1129 Takes a MARC::Record object describing an authority record or an authid, and
1130 returns a string representation of the first authorized heading. This routine
1131 should be considered a temporary shim to ease the future migration of authority
1132 data from C4::AuthoritiesMarc to the object-oriented Koha::*::Authority.
1136 sub GetAuthorizedHeading {
1139 unless ($record = $args->{record}) {
1140 return unless $args->{authid};
1141 $record = GetAuthority($args->{authid});
1143 return unless (ref $record eq 'MARC::Record');
1144 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
1145 # construct UNIMARC summary, that is quite different from MARC21 one
1147 foreach my $field ($record->field('2..')) {
1148 return $field->as_string('abcdefghijlmnopqrstuvwxyz');
1151 foreach my $field ($record->field('1..')) {
1152 my $tag = $field->tag();
1153 next if "152" eq $tag;
1154 # FIXME - 152 is not a good tag to use
1155 # in MARC21 -- purely local tags really ought to be
1157 if ($tag eq '100') {
1158 return $field->as_string('abcdefghjklmnopqrstvxyz68');
1159 } elsif ($tag eq '110') {
1160 return $field->as_string('abcdefghklmnoprstvxyz68');
1161 } elsif ($tag eq '111') {
1162 return $field->as_string('acdefghklnpqstvxyz68');
1163 } elsif ($tag eq '130') {
1164 return $field->as_string('adfghklmnoprstvxyz68');
1165 } elsif ($tag eq '148') {
1166 return $field->as_string('abvxyz68');
1167 } elsif ($tag eq '150') {
1168 return $field->as_string('abvxyz68');
1169 } elsif ($tag eq '151') {
1170 return $field->as_string('avxyz68');
1171 } elsif ($tag eq '155') {
1172 return $field->as_string('abvxyz68');
1173 } elsif ($tag eq '180') {
1174 return $field->as_string('vxyz68');
1175 } elsif ($tag eq '181') {
1176 return $field->as_string('vxyz68');
1177 } elsif ($tag eq '182') {
1178 return $field->as_string('vxyz68');
1179 } elsif ($tag eq '185') {
1180 return $field->as_string('vxyz68');
1182 return $field->as_string();
1189 =head2 BuildAuthHierarchies
1191 $text= &BuildAuthHierarchies( $authid, $force)
1193 return text containing trees for hierarchies
1194 for them to be stored in auth_header
1197 122,1314,2452;1324,2342,3,2452
1201 sub BuildAuthHierarchies{
1202 my $authid = shift @_;
1203 # warn "authid : $authid";
1204 my $force = shift @_ || (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 0 : 1);
1206 my $dbh=C4::Context->dbh;
1208 my $data = GetHeaderAuthority($authid);
1209 if ($data->{'authtrees'} and not $force){
1210 return $data->{'authtrees'};
1211 # } elsif ($data->{'authtrees'}){
1212 # $hierarchies=$data->{'authtrees'};
1214 my $record = GetAuthority($authid);
1216 return unless $record;
1217 foreach my $field ($record->field('5..')){
1220 (C4::Context->preference('marcflavour') eq 'UNIMARC' && $field->subfield('5') && $field->subfield('5') eq 'g') ||
1221 (C4::Context->preference('marcflavour') ne 'UNIMARC' && $field->subfield('w') && substr($field->subfield('w'), 0, 1) eq 'g'));
1223 my $subfauthid=_get_authid_subfield($field) || '';
1224 next if ($subfauthid eq $authid);
1225 my $parentrecord = GetAuthority($subfauthid);
1226 next unless $parentrecord;
1227 my $localresult=$hierarchies;
1229 $trees = BuildAuthHierarchies($subfauthid);
1232 @trees = split(/;/,$trees);
1234 push @trees, $trees;
1239 @globalresult = (@globalresult,@trees);
1242 $hierarchies=join(";",@globalresult);
1244 #Unless there is no ancestor, I am alone.
1245 $hierarchies="$authid" unless ($hierarchies);
1247 AddAuthorityTrees($authid,$hierarchies);
1248 return $hierarchies;
1251 =head2 BuildAuthHierarchy
1253 $ref= &BuildAuthHierarchy( $record, $class,$authid)
1255 return a hashref in order to display hierarchy for record and final Authid $authid
1266 sub BuildAuthHierarchy{
1267 my $record = shift @_;
1268 my $class = shift @_;
1269 my $authid_constructed = shift @_;
1270 return unless ($record && $record->field('001'));
1271 my $authid=$record->field('001')->data();
1273 my $parents=""; my $children="";
1274 my (@loopparents,@loopchildren);
1275 my $marcflavour = C4::Context->preference('marcflavour');
1276 my $relationshipsf = $marcflavour eq 'UNIMARC' ? '5' : 'w';
1277 foreach my $field ($record->field('5..')){
1278 my $subfauthid=_get_authid_subfield($field);
1279 if ($subfauthid && $field->subfield($relationshipsf) && $field->subfield('a')){
1280 my $relationship = substr($field->subfield($relationshipsf), 0, 1);
1281 if ($relationship eq 'h'){
1282 push @loopchildren, { "authid"=>$subfauthid,"value"=>$field->subfield('a')};
1284 elsif ($relationship eq 'g'){
1285 push @loopparents, { "authid"=>$subfauthid,"value"=>$field->subfield('a')};
1287 # brothers could get in there with an else
1290 $cell{"parents"}=\@loopparents;
1291 $cell{"children"}=\@loopchildren;
1292 $cell{"class"}=$class;
1293 $cell{"authid"}=$authid;
1294 $cell{"current_value"} =1 if ($authid eq $authid_constructed);
1295 $cell{"value"}=C4::Context->preference('marcflavour') eq 'UNIMARC' ? $record->subfield('2..',"a") : $record->subfield('1..', 'a');
1299 =head2 BuildAuthHierarchyBranch
1301 $branch = &BuildAuthHierarchyBranch( $tree, $authid[, $cnt])
1303 Return a data structure representing an authority hierarchy
1304 given a list of authorities representing a single branch in
1305 an authority hierarchy tree. $authid is the current node in
1306 the tree (which may or may not be somewhere in the middle).
1307 $cnt represents the level of the upper-most item, and is only
1308 used when BuildAuthHierarchyBranch is called recursively (i.e.,
1309 don't ever pass in anything but zero to it).
1313 sub BuildAuthHierarchyBranch {
1314 my ($tree, $authid, $cnt) = @_;
1316 my $elementdata = GetAuthority(shift @$tree);
1317 my $branch = BuildAuthHierarchy($elementdata,"child".$cnt, $authid);
1318 if (scalar @$tree > 0) {
1319 my $nextBranch = BuildAuthHierarchyBranch($tree, $authid, ++$cnt);
1320 my $nextAuthid = $nextBranch->{authid};
1322 # If we already have the next branch listed as a child, let's
1323 # replace the old listing with the new one. If not, we will add
1324 # the branch at the end.
1325 foreach my $cell (@{$branch->{children}}) {
1326 if ($cell->{authid} eq $nextAuthid) {
1327 $cell = $nextBranch;
1332 push @{$branch->{children}}, $nextBranch unless $found;
1337 =head2 GenerateHierarchy
1339 $hierarchy = &GenerateHierarchy($authid);
1341 Return an arrayref holding one or more "trees" representing
1342 authority hierarchies.
1346 sub GenerateHierarchy {
1348 my $trees = BuildAuthHierarchies($authid);
1349 my @trees = split /;/,$trees ;
1350 push @trees,$trees unless (@trees);
1351 my @loophierarchies;
1352 foreach my $tree (@trees){
1353 my @tree=split /,/,$tree;
1354 push @tree, $tree unless (@tree);
1355 my $branch = BuildAuthHierarchyBranch(\@tree, $authid);
1356 push @loophierarchies, [ $branch ];
1358 return \@loophierarchies;
1361 sub _get_authid_subfield{
1363 return $field->subfield('9')||$field->subfield('3');
1365 =head2 GetHeaderAuthority
1367 $ref= &GetHeaderAuthority( $authid)
1369 return a hashref in order auth_header table data
1373 sub GetHeaderAuthority{
1374 my $authid = shift @_;
1375 my $sql= "SELECT * from auth_header WHERE authid = ?";
1376 my $dbh=C4::Context->dbh;
1377 my $rq= $dbh->prepare($sql);
1378 $rq->execute($authid);
1379 my $data= $rq->fetchrow_hashref;
1383 =head2 AddAuthorityTrees
1385 $ref= &AddAuthorityTrees( $authid, $trees)
1387 return success or failure
1391 sub AddAuthorityTrees{
1392 my $authid = shift @_;
1393 my $trees = shift @_;
1394 my $sql= "UPDATE IGNORE auth_header set authtrees=? WHERE authid = ?";
1395 my $dbh=C4::Context->dbh;
1396 my $rq= $dbh->prepare($sql);
1397 return $rq->execute($trees,$authid);
1402 $ref= &merge(mergefrom,$MARCfrom,$mergeto,$MARCto)
1404 Could add some feature : Migrating from a typecode to an other for instance.
1405 Then we should add some new parameter : bibliotargettag, authtargettag
1410 my ($mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
1411 my ($counteditedbiblio,$countunmodifiedbiblio,$counterrors)=(0,0,0);
1412 my $dbh=C4::Context->dbh;
1413 my $authfrom = Koha::Authorities->find($mergefrom);
1414 my $authto = Koha::Authorities->find($mergeto);
1415 my $authtypefrom = Koha::Authority::Types->find($authfrom->authtypecode);
1416 my $authtypeto = Koha::Authority::Types->find($authto->authtypecode);
1418 return "error MARCFROM not a marcrecord ".Data::Dumper::Dumper($MARCfrom) if scalar($MARCfrom->fields()) == 0;
1419 return "error MARCTO not a marcrecord".Data::Dumper::Dumper($MARCto) if scalar($MARCto->fields()) == 0;
1420 # search the tag to report
1421 my $auth_tag_to_report_from = $authtypefrom->auth_tag_to_report;
1422 my $auth_tag_to_report_to = $authtypeto->auth_tag_to_report;
1425 @record_to = $MARCto->field($auth_tag_to_report_to)->subfields() if $MARCto->field($auth_tag_to_report_to);
1427 @record_from = $MARCfrom->field($auth_tag_to_report_from)->subfields() if $MARCfrom->field($auth_tag_to_report_from);
1430 # search all biblio tags using this authority.
1431 #Getting marcbiblios impacted by the change.
1433 my $oConnection=C4::Context->Zconn("biblioserver",0);
1434 # We used to use XML syntax here, but that no longer works.
1435 # Thankfully, we don't need it.
1437 $query= "an=".$mergefrom;
1438 my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1441 $count=$oResult->size();
1444 while ( $z<$count ) {
1445 my $marcrecordzebra = C4::Search::new_record_from_zebra(
1447 $oResult->record($z)->raw()
1449 my ( $biblionumbertagfield, $biblionumbertagsubfield ) = &GetMarcFromKohaField( "biblio.biblionumber", '' );
1450 my $i = ($biblionumbertagfield < 10)
1451 ? $marcrecordzebra->field( $biblionumbertagfield )->data
1452 : $marcrecordzebra->subfield( $biblionumbertagfield, $biblionumbertagsubfield );
1453 my $marcrecorddb = GetMarcBiblio($i);
1454 push @reccache, $marcrecorddb;
1457 $oResult->destroy();
1458 # Get All candidate Tags for the change
1459 # (This will reduce the search scope in marc records).
1460 my $sql = "SELECT DISTINCT tagfield FROM marc_subfield_structure WHERE authtypecode=?";
1461 my $tags_using_authtype = $dbh->selectcol_arrayref( $sql, undef, ( $authtypefrom->authtypecode ));
1463 if ($authtypeto->authtypecode ne $authtypefrom->authtypecode){
1464 $tags_new = $dbh->selectcol_arrayref( $sql, undef, ( $authtypeto->authtypecode ));
1467 my $overwrite = C4::Context->preference( 'AuthorityMergeMode' ) eq 'strict';
1468 my $skip_subfields = $overwrite
1469 # This hash contains all subfields from the authority report fields
1470 # Including $MARCfrom as well as $MARCto
1471 # We only need it in loose merge mode; replaces the former $exclude
1473 : { map { ( $_->[0], 1 ); } ( @record_from, @record_to ) };
1474 # And we need to add $9 in order not to duplicate
1475 $skip_subfields->{9} = 1 if !$overwrite;
1477 foreach my $marcrecord(@reccache){
1479 foreach my $tagfield (@$tags_using_authtype) {
1480 my $countfrom = 0; # used in strict mode to remove duplicates
1481 foreach my $field ( $marcrecord->field($tagfield) ) {
1482 my $auth_number = $field->subfield("9"); # link to authority
1483 my $tag = $field->tag();
1484 next if !defined($auth_number) || $auth_number ne $mergefrom;
1486 if ( $overwrite && $countfrom > 1 ) {
1487 # remove this duplicate in strict mode
1488 $marcrecord->delete_field($field);
1492 my $newtag = $tags_new
1493 ? _merge_newtag( $tag, $tags_new )
1495 my $field_to = MARC::Field->new(
1497 $field->indicator(1),
1498 $field->indicator(2),
1501 foreach my $subfield ( grep { $_->[0] ne '9' } @record_to ) {
1502 $field_to->add_subfields( $subfield->[0] => $subfield->[1] );
1504 if ( !$overwrite ) {
1505 # add subfields back in loose mode, check skip_subfields
1506 foreach my $subfield ( $field->subfields ) {
1507 next if $skip_subfields->{ $subfield->[0] };
1508 $field_to->add_subfields( $subfield->[0], $subfield->[1] );
1512 $marcrecord->delete_field($field);
1513 append_fields_ordered( $marcrecord, $field_to );
1515 $field->replace_with($field_to);
1520 my ($bibliotag,$bibliosubf) = GetMarcFromKohaField("biblio.biblionumber","") ;
1523 $biblionumber=$marcrecord->field($bibliotag)->data;
1526 $biblionumber=$marcrecord->subfield($bibliotag,$bibliosubf);
1528 unless ($biblionumber){
1529 warn "pas de numéro de notice bibliographique dans : ".$marcrecord->as_formatted;
1533 &ModBiblio($marcrecord,$biblionumber,GetFrameworkCode($biblionumber)) ;
1534 $counteditedbiblio++;
1535 warn $counteditedbiblio if (($counteditedbiblio % 10) and $ENV{DEBUG});
1538 return $counteditedbiblio;
1542 # Routine is only called for an (exceptional) authtypecode change
1543 # Fixes old behavior of returning the first tag found
1544 my ( $oldtag, $new_tags ) = @_;
1546 # If we e.g. have 650 and 151,651,751 try 651 and check presence
1547 my $prefix = substr( $oldtag, 0, 1 );
1548 my $guess = $prefix . substr( $new_tags->[0], -2 );
1549 if( grep { $_ eq $guess } @$new_tags ) {
1552 # Otherwise return one from the same block e.g. 6XX for 650
1553 # If not there too, fall back to first new tag (old behavior!)
1554 my @same_block = grep { /^$prefix/ } @$new_tags;
1555 return @same_block ? $same_block[0] : $new_tags->[0];
1558 sub append_fields_ordered {
1559 # while we lack this function in MARC::Record
1560 # we do not want insert_fields_ordered since it inserts before
1561 my ( $record, $field ) = @_;
1562 if( my @flds = $record->field( $field->tag ) ) {
1563 $record->insert_fields_after( pop @flds, $field );
1564 } else { # now fallback to insert_fields_ordered
1565 $record->insert_fields_ordered( $field );
1569 =head2 get_auth_type_location
1571 my ($tag, $subfield) = get_auth_type_location($auth_type_code);
1573 Get the tag and subfield used to store the heading type
1574 for indexing purposes. The C<$auth_type> parameter is
1575 optional; if it is not supplied, assume ''.
1577 This routine searches the MARC authority framework
1578 for the tag and subfield whose kohafield is
1579 C<auth_header.authtypecode>; if no such field is
1580 defined in the framework, default to the hardcoded value
1581 specific to the MARC format.
1585 sub get_auth_type_location {
1586 my $auth_type_code = @_ ? shift : '';
1588 my ($tag, $subfield) = GetAuthMARCFromKohaField('auth_header.authtypecode', $auth_type_code);
1589 if (defined $tag and defined $subfield and $tag != 0 and $subfield ne '' and $subfield ne ' ') {
1590 return ($tag, $subfield);
1592 if (C4::Context->preference('marcflavour') eq "MARC21") {
1593 return C4::AuthoritiesMarc::MARC21::default_auth_type_location();
1595 return C4::AuthoritiesMarc::UNIMARC::default_auth_type_location();
1600 END { } # module clean-up code here (global destructor)
1607 Koha Development Team <http://koha-community.org/>
1609 Paul POULAIN paul.poulain@free.fr