1 package C4::AuthoritiesMarc;
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2018 The National Library of Finland, University of Helsinki
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
26 use C4::Biblio qw( GetFrameworkCode GetMarcBiblio ModBiblio );
27 use C4::Search qw( FindDuplicate new_record_from_zebra );
28 use C4::AuthoritiesMarc::MARC21;
29 use C4::AuthoritiesMarc::UNIMARC;
30 use C4::Charset qw( SetUTF8Flag );
31 use C4::Log qw( logaction );
32 use Koha::MetadataRecord::Authority;
33 use Koha::Authorities;
34 use Koha::Authority::MergeRequests;
35 use Koha::Authority::Types;
38 use Koha::SearchEngine;
39 use Koha::SearchEngine::Indexer;
40 use Koha::SearchEngine::Search;
42 our (@ISA, @EXPORT_OK);
49 GetAuthMARCFromKohaField
65 CompareFieldWithAuthority
68 FindDuplicateAuthority
81 =head2 GetAuthMARCFromKohaField
83 ( $tag, $subfield ) = &GetAuthMARCFromKohaField ($kohafield,$authtypecode);
85 returns tag and subfield linked to kohafield
88 Suppose Kohafield is only linked to ONE subfield
92 sub GetAuthMARCFromKohaField {
93 #AUTHfind_marc_from_kohafield
94 my ( $kohafield,$authtypecode ) = @_;
95 my $dbh=C4::Context->dbh;
96 return 0, 0 unless $kohafield;
97 $authtypecode="" unless $authtypecode;
98 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
99 $sth->execute($kohafield,$authtypecode);
100 my ($tagfield,$tagsubfield) = $sth->fetchrow;
102 return ($tagfield,$tagsubfield);
105 =head2 SearchAuthorities
107 (\@finalresult, $nbresults)= &SearchAuthorities($tags, $and_or,
108 $excluding, $operator, $value, $offset,$length,$authtypecode,
109 $sortby[, $skipmetadata])
111 returns ref to array result and count of results returned
115 sub SearchAuthorities {
116 my ($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby,$skipmetadata) = @_;
117 # warn Dumper($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby);
118 my $dbh=C4::Context->dbh;
119 $sortby="" unless $sortby;
123 # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
124 # the authtypecode. Then, search on $a of this tag_to_report
125 # also store main entry MARC tag, to extract it at end of search
126 ##first set the authtype search and may be multiple authorities
130 my @auths=split / /,$authtypecode ;
131 foreach my $auth (@auths){
132 $query .=" \@attr 1=authtype \@attr 5=100 ".$auth; ##No truncation on authtype
133 push @authtypecode ,$auth;
137 while ($n>1){$query= "\@or ".$query;$n--;}
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 elsif ( @$tags[$i] eq "all" ) {
167 $attr = " \@attr 1=Any ";
169 else { # Use the index passed in params
170 $attr = " \@attr 1=" . @$tags[$i] . " ";
173 else { # Assume any if no index was specified
174 $attr = " \@attr 1=Any ";
177 my $operator = @$operator[$i];
178 if ( $operator and $operator eq 'is' ) {
179 $attr .= " \@attr 4=1 \@attr 5=100 "
180 ; ##Phrase, No truncation,all of subfield field must match
182 elsif ( $operator and $operator eq "=" ) {
183 $attr .= " \@attr 4=107 "; #Number Exact match
185 elsif ( $operator and $operator eq "start" ) {
186 $attr .= " \@attr 3=2 \@attr 4=1 \@attr 5=1 "
187 ; #Firstinfield Phrase, Right truncated
189 elsif ( $operator and $operator eq "exact" ) {
190 $attr .= " \@attr 4=1 \@attr 5=100 \@attr 6=3 "
191 ; ##Phrase, No truncation,all of subfield field must match
194 $attr .= " \@attr 5=1 \@attr 4=6 "
195 ; ## Word list, right truncated, anywhere
196 if ( $sortby eq 'Relevance' ) {
197 $attr .= "\@attr 2=102 ";
201 s/"/\\"/g; # Escape the double-quotes in the search value
202 $attr = $attr . "\"" . @$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';
226 $query=($query?$query:"\@attr 1=_ALLRECORDS \@attr 2=103 ''");
227 $query="\@or $orderstring $query" if $orderstring;
229 $offset = 0 if not defined $offset or $offset < 0;
230 my $counter = $offset;
231 $length=10 unless $length;
234 $oAuth[0]=C4::Context->Zconn("authorityserver" , 1);
235 my $Anewq= ZOOM::Query::PQF->new($query,$oAuth[0]);
237 $oAResult= $oAuth[0]->search($Anewq) ;
238 while (($i = ZOOM::event(\@oAuth)) != 0) {
239 my $ev = $oAuth[$i-1]->last_event();
240 last if $ev == ZOOM::Event::ZEND;
242 my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
244 warn "oAuth error: $errmsg ($error) $addinfo $diagset\n";
249 $nbresults=$oAResult->size();
250 my $nremains=$nbresults;
252 my @finalresult = ();
256 ##Find authid and linkid fields
257 ##we may be searching multiple authoritytypes.
258 ## FIXME this assumes that all authid and linkid fields are the same for all authority types
259 # my ($authidfield,$authidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.authid",$authtypecode[0]);
260 # my ($linkidfield,$linkidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.linkid",$authtypecode[0]);
261 while (($counter < $nbresults) && ($counter < ($offset + $length))) {
263 ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
264 my $rec=$oAResult->record($counter);
265 my $separator=C4::Context->preference('AuthoritySeparator');
266 my $authrecord = C4::Search::new_record_from_zebra(
271 if ( !defined $authrecord or !defined $authrecord->field('001') ) {
276 SetUTF8Flag( $authrecord );
278 my $authid=$authrecord->field('001')->data();
280 $newline{authid} = $authid;
281 if ( !$skipmetadata ) {
282 my $auth_tag_to_report;
283 $auth_tag_to_report = Koha::Authority::Types->find($authtypecode)->auth_tag_to_report
286 my $mainentry = $authrecord->field($auth_tag_to_report);
288 foreach ( $mainentry->subfields() ) {
289 $reported_tag .= '$' . $_->[0] . $_->[1];
293 my ( $thisauthtype, $thisauthtypecode );
294 if ( my $authority = Koha::Authorities->find($authid) ) {
295 $thisauthtypecode = $authority->authtypecode;
296 $thisauthtype = Koha::Authority::Types->find($thisauthtypecode);
298 unless (defined $thisauthtype) {
299 $thisauthtypecode = $authtypecode;
300 $thisauthtype = Koha::Authority::Types->find($thisauthtypecode);
302 my $summary = BuildSummary( $authrecord, $authid, $thisauthtypecode );
304 if ( C4::Context->preference('ShowHeadingUse') ) {
305 # checking valid heading use
306 my $f008 = $authrecord->field('008');
307 my $pos14to16 = substr( $f008->data, 14, 3 );
308 my $main = substr( $pos14to16, 0, 1 );
309 $newline{main} = 1 if $main eq 'a';
310 my $subject = substr( $pos14to16, 1, 1);
311 $newline{subject} = 1 if $subject eq 'a';
312 my $series = substr( $pos14to16, 2, 1 );
313 $newline{series} = 1 if $series eq 'a';
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 = Koha::Authorities->get_usage_count({ authid => $finalresult[$z]{authid} });
329 $finalresult[$z]{used}=$count;
335 $oAResult->destroy();
336 # $oAuth[0]->destroy();
338 return (\@finalresult, $nbresults);
341 =head2 GuessAuthTypeCode
343 my $authtypecode = GuessAuthTypeCode($record);
345 Get the record and tries to guess the adequate authtypecode from its content.
349 sub GuessAuthTypeCode {
350 my ($record, $heading_fields) = @_;
351 return unless defined $record;
352 $heading_fields //= {
354 '100'=>{authtypecode=>'PERSO_NAME'},
355 '110'=>{authtypecode=>'CORPO_NAME'},
356 '111'=>{authtypecode=>'MEETI_NAME'},
357 '130'=>{authtypecode=>'UNIF_TITLE'},
358 '147'=>{authtypecode=>'NAME_EVENT'},
359 '148'=>{authtypecode=>'CHRON_TERM'},
360 '150'=>{authtypecode=>'TOPIC_TERM'},
361 '151'=>{authtypecode=>'GEOGR_NAME'},
362 '155'=>{authtypecode=>'GENRE/FORM'},
363 '162'=>{authtypecode=>'MED_PERFRM'},
364 '180'=>{authtypecode=>'GEN_SUBDIV'},
365 '181'=>{authtypecode=>'GEO_SUBDIV'},
366 '182'=>{authtypecode=>'CHRON_SUBD'},
367 '185'=>{authtypecode=>'FORM_SUBD'},
369 #200 Personal name 700, 701, 702 4-- with embedded 700, 701, 702 600
370 # 604 with embedded 700, 701, 702
371 #210 Corporate or meeting name 710, 711, 712 4-- with embedded 710, 711, 712 601 604 with embedded 710, 711, 712
372 #215 Territorial or geographic name 710, 711, 712 4-- with embedded 710, 711, 712 601, 607 604 with embedded 710, 711, 712
373 #216 Trademark 716 [Reserved for future use]
374 #220 Family name 720, 721, 722 4-- with embedded 720, 721, 722 602 604 with embedded 720, 721, 722
375 #230 Title 500 4-- with embedded 500 605
376 #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
377 #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
378 #250 Topical subject 606
379 #260 Place access 620
380 #280 Form, genre or physical characteristics 608
383 # Could also be represented with :
385 #a = personal name entry
386 #b = corporate name entry
387 #c = territorial or geographical name
391 #g = collective uniform title
393 #i = name/collective uniform title
396 #l = form, genre or physical characteristics
398 '200'=>{authtypecode=>'NP'},
399 '210'=>{authtypecode=>'CO'},
400 '215'=>{authtypecode=>'SNG'},
401 '216'=>{authtypecode=>'TM'},
402 '220'=>{authtypecode=>'FAM'},
403 '230'=>{authtypecode=>'TU'},
404 '235'=>{authtypecode=>'CO_UNI_TI'},
405 '240'=>{authtypecode=>'SAUTTIT'},
406 '245'=>{authtypecode=>'NAME_COL'},
407 '250'=>{authtypecode=>'SNC'},
408 '260'=>{authtypecode=>'PA'},
409 '280'=>{authtypecode=>'GENRE/FORM'},
412 foreach my $field (keys %{$heading_fields->{uc(C4::Context->preference('marcflavour'))} }) {
413 return $heading_fields->{uc(C4::Context->preference('marcflavour'))}->{$field}->{'authtypecode'} if (defined $record->field($field));
420 my $authtid = GuessAuthId($record);
422 Get the record and tries to guess the adequate authtypecode from its content.
428 return unless ($record && $record->field('001'));
429 # my $authtypecode=GuessAuthTypeCode($record);
430 # my ($tag,$subfield)=GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
431 # if ($tag > 010) {return $record->subfield($tag,$subfield)}
432 # else {return $record->field($tag)->data}
433 return $record->field('001')->data;
438 $tagslabel= &GetTagsLabels($forlibrarian,$authtypecode)
440 returns a ref to hashref of authorities tag and subfield structure.
444 $tagslabel->{$tag}->{$subfield}->{'attribute'}
446 where attribute takes values in :
464 my ($forlibrarian,$authtypecode)= @_;
465 my $dbh=C4::Context->dbh;
466 $authtypecode="" unless $authtypecode;
468 my $libfield = ($forlibrarian) ? 'liblibrarian' : 'libopac';
471 # check that authority exists
472 $sth=$dbh->prepare("SELECT count(*) FROM auth_tag_structure WHERE authtypecode=?");
473 $sth->execute($authtypecode);
474 my ($total) = $sth->fetchrow;
475 $authtypecode="" unless ($total >0);
477 "SELECT auth_tag_structure.tagfield,auth_tag_structure.liblibrarian,auth_tag_structure.libopac,auth_tag_structure.mandatory,auth_tag_structure.repeatable
478 FROM auth_tag_structure
483 $sth->execute($authtypecode);
484 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
486 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
487 $res->{$tag}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
488 $res->{$tag}->{tab} = " "; # XXX
489 $res->{$tag}->{mandatory} = $mandatory;
490 $res->{$tag}->{repeatable} = $repeatable;
493 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,frameworkcode as authtypecode,value_builder,kohafield,seealso,hidden,isurl,defaultvalue, display_order
494 FROM auth_subfield_structure
496 ORDER BY tagfield, display_order, tagsubfield"
498 $sth->execute($authtypecode);
501 my $authorised_value;
511 ( $tag, $subfield, $liblibrarian, , $libopac, $tab,
512 $mandatory, $repeatable, $authorised_value, $authtypecode,
513 $value_builder, $kohafield, $seealso, $hidden,
514 $isurl, $defaultvalue, $display_order )
518 $res->{$tag}->{$subfield}->{subfield} = $subfield;
519 $res->{$tag}->{$subfield}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
520 $res->{$tag}->{$subfield}->{tab} = $tab;
521 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
522 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
523 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
524 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
525 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
526 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
527 $res->{$tag}->{$subfield}->{seealso} = $seealso;
528 $res->{$tag}->{$subfield}->{hidden} = $hidden;
529 $res->{$tag}->{$subfield}->{isurl} = $isurl;
530 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
531 $res->{$tag}->{$subfield}->{display_order} = $display_order;
539 $authid= &AddAuthority($record, $authid,$authtypecode)
541 Either Create Or Modify existing authority.
542 returns authid of the newly created authority
547 # pass the MARC::Record to this function, and it will create the records in the authority table
548 my ($record,$authid,$authtypecode) = @_;
549 my $dbh=C4::Context->dbh;
550 my $leader=' nz a22 o 4500';#Leader for incomplete MARC21 record
552 # if authid empty => true add, find a new authid number
554 if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') {
555 $format= 'UNIMARCAUTH';
561 #update date/time to 005 for marc and unimarc
562 my $time=POSIX::strftime("%Y%m%d%H%M%S",localtime);
563 my $f5=$record->field('005');
565 $record->insert_fields_ordered( MARC::Field->new('005',$time.".0") );
568 $f5->update($time.".0");
571 SetUTF8Flag($record);
572 if ($format eq "MARC21") {
573 my $userenv = C4::Context->userenv;
575 my $marcorgcode = C4::Context->preference('MARCOrgCode');
576 if ( $userenv && $userenv->{'branch'} ) {
577 $library = Koha::Libraries->find( $userenv->{'branch'} );
578 # userenv's library could not exist because of a trick in misc/commit_file.pl (see FIXME and set_userenv statement)
579 $marcorgcode = $library ? $library->get_effective_marcorgcode : $marcorgcode;
581 if (!$record->leader) {
582 $record->leader($leader);
584 if (!$record->field('003')) {
585 $record->insert_fields_ordered(
586 MARC::Field->new('003', $marcorgcode),
589 my $date=POSIX::strftime("%y%m%d",localtime);
590 if (!$record->field('008')) {
591 # Get a valid default value for field 008
592 my $default_008 = C4::Context->preference('MARCAuthorityControlField008');
593 if(!$default_008 or length($default_008)<34) {
594 $default_008 = '|| aca||aabn | a|a d';
597 $default_008 = substr($default_008,0,34);
600 $record->insert_fields_ordered( MARC::Field->new('008',$date.$default_008) );
602 if (!$record->field('040')) {
603 $record->insert_fields_ordered(
604 MARC::Field->new('040','','',
612 if ($format eq "UNIMARCAUTH") {
613 $record->leader(" nx j22 ") unless ($record->leader());
614 my $date=POSIX::strftime("%Y%m%d",localtime);
615 my $defaultfield100 = C4::Context->preference('UNIMARCAuthorityField100');
616 if (my $string=$record->subfield('100',"a")){
617 $string=~s/fre50/frey50/;
618 $record->field('100')->update('a'=>$string);
620 elsif ($record->field('100')){
621 $record->field('100')->update('a'=>$date.$defaultfield100);
623 $record->append_fields(
624 MARC::Field->new('100',' ',' '
625 ,'a'=>$date.$defaultfield100)
629 my ($auth_type_tag, $auth_type_subfield) = get_auth_type_location($authtypecode);
630 if (!$authid and $format eq "MARC21") {
631 # only need to do this fix when modifying an existing authority
632 C4::AuthoritiesMarc::MARC21::fix_marc21_auth_type_location($record, $auth_type_tag, $auth_type_subfield);
634 if (my $field=$record->field($auth_type_tag)){
635 $field->update($auth_type_subfield=>$authtypecode);
638 $record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode);
641 # Save record into auth_header, update 001
645 # Save a blank record, get authid
646 $dbh->do( "INSERT INTO auth_header (datecreated,marcxml) values (NOW(),?)", undef, '' );
647 $authid = $dbh->last_insert_id( undef, undef, 'auth_header', 'authid' );
648 logaction( "AUTHORITIES", "ADD", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
652 # Insert/update the recordID in MARC record
653 $record->delete_field( $record->field('001') );
654 $record->insert_fields_ordered( MARC::Field->new( '001', $authid ) );
656 $dbh->do( "UPDATE auth_header SET authtypecode=?, marc=?, marcxml=? WHERE authid=?", undef, $authtypecode, $record->as_usmarc, $record->as_xml_record($format), $authid ) or die $DBI::errstr;
657 my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
658 $indexer->index_records( $authid, "specialUpdate", "authorityserver", $record );
660 _after_authority_action_hooks({ action => $action, authority_id => $authid });
666 DelAuthority({ authid => $authid, [ skip_merge => 1 ] });
668 Deletes $authid and calls merge to cleanup linked biblio records.
669 Parameter skip_merge is used in authorities/merge.pl. You should normally not
676 my $authid = $params->{authid} || return;
677 my $skip_merge = $params->{skip_merge};
678 my $dbh = C4::Context->dbh;
680 # Remove older pending merge requests for $authid to itself. (See bug 22437)
681 my $condition = { authid => $authid, authid_new => [undef, 0, $authid], done => 0 };
682 Koha::Authority::MergeRequests->search($condition)->delete;
684 merge({ mergefrom => $authid }) if !$skip_merge;
685 $dbh->do( "DELETE FROM auth_header WHERE authid=?", undef, $authid );
686 logaction( "AUTHORITIES", "DELETE", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
687 my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
688 $indexer->index_records( $authid, "recordDelete", "authorityserver", undef );
690 _after_authority_action_hooks({ action => 'delete', authority_id => $authid });
695 $authid= &ModAuthority($authid,$record,$authtypecode, [ { skip_merge => 1 ] )
697 Modifies authority record, optionally updates attached biblios.
698 The parameter skip_merge is optional and should be used with care.
703 my ( $authid, $record, $authtypecode, $params ) = @_;
704 my $oldrecord = GetAuthority($authid);
705 #Now rewrite the $record to table with an add
706 $authid = AddAuthority($record, $authid, $authtypecode);
707 merge({ mergefrom => $authid, MARCfrom => $oldrecord, mergeto => $authid, MARCto => $record }) if !$params->{skip_merge};
708 logaction( "AUTHORITIES", "MODIFY", $authid, "authority BEFORE=>" . $oldrecord->as_formatted ) if C4::Context->preference("AuthoritiesLog");
712 =head2 GetAuthorityXML
714 $marcxml= &GetAuthorityXML( $authid)
716 returns xml form of record $authid
720 sub GetAuthorityXML {
721 # Returns MARC::XML of the authority passed in parameter.
723 if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') {
724 my $dbh=C4::Context->dbh;
725 my $sth = $dbh->prepare("select marcxml from auth_header where authid=? " );
726 $sth->execute($authid);
727 my ($marcxml)=$sth->fetchrow;
731 # for MARC21, call GetAuthority instead of
732 # getting the XML directly since we may
733 # need to fix up the location of the authority
734 # code -- note that this is reasonably safe
735 # because GetAuthorityXML is used only by the
736 # indexing processes like zebraqueue_start.pl
737 my $record = GetAuthority($authid);
738 return $record->as_xml_record('MARC21');
744 $record= &GetAuthority( $authid)
746 Returns MARC::Record of the authority passed in parameter.
752 my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
753 return unless $authority;
754 return ($authority->record);
757 =head2 FindDuplicateAuthority
759 $record= &FindDuplicateAuthority( $record, $authtypecode)
761 return $authid,Summary if duplicate is found.
763 Comments : an improvement would be to return All the records that match.
767 sub FindDuplicateAuthority {
769 my ($record,$authtypecode)=@_;
770 # warn "IN for ".$record->as_formatted;
771 my $dbh = C4::Context->dbh;
772 # warn "".$record->as_formatted;
773 my $auth_tag_to_report = Koha::Authority::Types->find($authtypecode)->auth_tag_to_report;
774 # warn "record :".$record->as_formatted." auth_tag_to_report :$auth_tag_to_report";
775 # build a request for SearchAuthorities
777 $authtypecode =~ s#/#\\/#; # GENRE/FORM contains forward slash which is a reserved character
778 my $query='at:'.$authtypecode.' ';
779 my $filtervalues=qr([\001-\040\Q!'"`#$%&*+,-./:;<=>?@(){[}_|~\E\]]);
780 if ($record->field($auth_tag_to_report)) {
781 foreach ($record->field($auth_tag_to_report)->subfields()) {
782 $_->[1]=~s/$filtervalues/ /g; $query.= " $op he:\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/);
785 my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::AUTHORITIES_INDEX});
786 my ($error, $results, $total_hits) = $searcher->simple_search_compat( $query, 0, 1, [ 'authorityserver' ] );
787 # there is at least 1 result => return the 1st one
788 if (!defined $error && @{$results} ) {
789 my $marcrecord = C4::Search::new_record_from_zebra(
793 return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode);
795 # no result, returns nothing
801 $summary= &BuildSummary( $record, $authid, $authtypecode)
803 Returns a hashref with a summary of the specified record.
805 Comment : authtypecode can be inferred from both record and authid.
806 Moreover, authid can also be inferred from $record.
807 Would it be interesting to delete those things.
812 ## give this a Marc record to return summary
813 my ($record,$authid,$authtypecode)=@_;
814 my $dbh=C4::Context->dbh;
816 my $summary_template;
817 # handle $authtypecode is NULL or eq ""
819 my $authref = Koha::Authority::Types->find($authtypecode);
821 $summary{authtypecode} = $authref->authtypecode;
822 $summary{type} = $authref->authtypetext;
823 $summary_template = $authref->summary;
824 # for MARC21, the authority type summary displays a label meant for
826 if (C4::Context->preference('marcflavour') ne 'UNIMARC') {
827 $summary{label} = $authref->summary;
829 $summary{summary} = $authref->summary;
833 my $marc21subfields = 'abcdfghjklmnopqrstuvxyz68';
834 my %marc21controlrefs = ( 'a' => 'earlier',
840 'n' => 'notapplicable',
844 my %unimarc_relation_from_code = (
850 $thesaurus{'1'}="Peuples";
851 $thesaurus{'2'}="Anthroponymes";
852 $thesaurus{'3'}="Oeuvres";
853 $thesaurus{'4'}="Chronologie";
854 $thesaurus{'5'}="Lieux";
855 $thesaurus{'6'}="Sujets";
858 # if the library has a summary defined, use it. Otherwise, build a standard one
859 # FIXME - it appears that the summary field in the authority frameworks
860 # can work as a display template. However, this doesn't
861 # suit the MARC21 version, so for now the "templating"
862 # feature will be enabled only for UNIMARC for backwards
864 if ($summary{summary} and C4::Context->preference('marcflavour') eq 'UNIMARC') {
865 my @matches = ($summary{summary} =~ m/\[(.*?)(\d{3})([\*a-z0-9])(.*?)\]/g);
866 my (@textbefore, @tag, @subtag, @textafter);
867 for(my $i = 0; $i < scalar @matches; $i++){
868 push @textbefore, $matches[$i] if($i%4 == 0);
869 push @tag, $matches[$i] if($i%4 == 1);
870 push @subtag, $matches[$i] if($i%4 == 2);
871 push @textafter, $matches[$i] if($i%4 == 3);
873 for(my $i = scalar @tag; $i >= 0; $i--){
874 my $textbefore = $textbefore[$i] || '';
875 my $tag = $tag[$i] || '';
876 my $subtag = $subtag[$i] || '';
877 my $textafter = $textafter[$i] || '';
879 my $field = $record->field($tag);
883 $value = $textbefore . $field->data() . $textafter;
886 my @subfields = $field->subfield($subtag);
888 $value = $textbefore . join (" - ", @subfields) . $textafter;
892 $summary{summary} =~ s/\[\Q$textbefore$tag$subtag$textafter\E\]/$value/;
894 $summary{summary} =~ s/\\n/<br \/>/g;
901 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
902 # construct UNIMARC summary, that is quite different from MARC21 one
904 foreach my $field ($record->field('2..')) {
906 heading => $field->as_string('abcdefghijlmnopqrstuvwxyz'),
907 hemain => ( $field->subfield('a') // undef ),
908 field => $field->tag(),
912 foreach my $field ($record->field('3..')) {
913 push @notes, { note => $field->subfield('a'), field => $field->tag() };
915 foreach my $field ($record->field('4..')) {
916 my $thesaurus = $field->subfield('2') ? "thes. : ".$thesaurus{"$field->subfield('2')"}." : " : '';
918 heading => $thesaurus . $field->as_string('abcdefghijlmnopqrstuvwxyz'),
919 hemain => ( $field->subfield('a') // undef ),
921 field => $field->tag(),
927 my $type = $unimarc_relation_from_code{$_->subfield('5') || 'a'};
928 my $heading = $_->as_string('abcdefgjxyz');
933 hemain => ( $_->subfield('a') // undef ),
935 authid => ( $_->subfield('9') // undef ),
937 } $record->field('5..');
940 @otherscript = map { {
941 lang => length ($_->subfield('8')) == 6 ? substr ($_->subfield('8'), 3, 3) : $_->subfield('8') || '',
942 term => $_->subfield('a') . ($_->subfield('b') ? ', ' . $_->subfield('b') : ''),
945 } } $record->field('7..');
948 # construct MARC21 summary
949 # FIXME - looping over 1XX is questionable
950 # since MARC21 authority should have only one 1XX
951 use C4::Heading::MARC21;
952 my $handler = C4::Heading::MARC21->new();
953 my $subfields_to_report;
954 foreach my $field ($record->field('1..')) {
955 my $tag = $field->tag();
956 next if "152" eq $tag;
957 # FIXME - 152 is not a good tag to use
958 # in MARC21 -- purely local tags really ought to be
961 $subfields_to_report = $handler->get_auth_heading_subfields_to_report($tag);
963 if ($subfields_to_report) {
965 heading => $field->as_string($subfields_to_report),
966 hemain => ( $field->subfield( substr($subfields_to_report, 0, 1) ) // undef ),
971 heading => $field->as_string(),
972 hemain => ( $field->subfield( 'a' ) // undef ),
977 foreach my $field ($record->field('4..')) { #See From
978 my $type = 'seefrom';
979 $type = ($marc21controlrefs{substr $field->subfield('w'), 0, 1} || '') if ($field->subfield('w'));
980 if ($type eq 'notapplicable') {
981 $type = substr $field->subfield('w'), 2, 1;
982 $type = 'earlier' if $type && $type ne 'n';
984 if ($type eq 'subfi') {
986 heading => $field->as_string($marc21subfields),
987 hemain => scalar $field->subfield( substr($marc21subfields, 0, 1) ),
988 type => ($field->subfield('i') || ''),
989 field => $field->tag(),
993 heading => $field->as_string($marc21subfields),
994 hemain => scalar $field->subfield( substr($marc21subfields, 0, 1) ),
996 field => $field->tag(),
1000 foreach my $field ($record->field('5..')) { #See Also
1001 my $type = 'seealso';
1002 $type = ($marc21controlrefs{substr $field->subfield('w'), 0, 1} || '') if ($field->subfield('w'));
1003 if ($type eq 'notapplicable') {
1004 $type = substr $field->subfield('w'), 2, 1;
1005 $type = 'earlier' if $type && $type ne 'n';
1007 if ($type eq 'subfi') {
1009 heading => $field->as_string($marc21subfields),
1010 hemain => scalar $field->subfield( substr($marc21subfields, 0, 1) ),
1011 type => scalar $field->subfield('i'),
1012 field => $field->tag(),
1013 search => $field->as_string($marc21subfields) || '',
1014 authid => $field->subfield('9') || ''
1018 heading => $field->as_string($marc21subfields),
1019 hemain => scalar $field->subfield( substr($marc21subfields, 0, 1) ),
1021 field => $field->tag(),
1022 search => $field->as_string($marc21subfields) || '',
1023 authid => $field->subfield('9') || ''
1027 foreach my $field ($record->field('6..')) {
1028 push @notes, { note => $field->as_string(), field => $field->tag() };
1030 foreach my $field ($record->field('880')) {
1031 my $linkage = $field->subfield('6');
1032 my $category = substr $linkage, 0, 1;
1033 if ($category eq '1') {
1034 $category = 'preferred';
1035 } elsif ($category eq '4') {
1036 $category = 'seefrom';
1037 } elsif ($category eq '5') {
1038 $category = 'seealso';
1041 if ($field->subfield('w')) {
1042 $type = $marc21controlrefs{substr $field->subfield('w'), '0'};
1046 my $direction = $linkage =~ m#/r$# ? 'rtl' : 'ltr';
1047 push @otherscript, { term => $field->as_string($subfields_to_report), category => $category, type => $type, direction => $direction, linkage => $linkage };
1050 $summary{mainentry} = $authorized[0]->{heading};
1051 $summary{mainmainentry} = $authorized[0]->{hemain};
1052 $summary{authorized} = \@authorized;
1053 $summary{notes} = \@notes;
1054 $summary{seefrom} = \@seefrom;
1055 $summary{seealso} = \@seealso;
1056 $summary{otherscript} = \@otherscript;
1060 =head2 GetAuthorizedHeading
1062 $heading = &GetAuthorizedHeading({ record => $record, authid => $authid })
1064 Takes a MARC::Record object describing an authority record or an authid, and
1065 returns a string representation of the first authorized heading. This routine
1066 should be considered a temporary shim to ease the future migration of authority
1067 data from C4::AuthoritiesMarc to the object-oriented Koha::*::Authority.
1071 sub GetAuthorizedHeading {
1074 unless ($record = $args->{record}) {
1075 return unless $args->{authid};
1076 $record = GetAuthority($args->{authid});
1078 return unless (ref $record eq 'MARC::Record');
1079 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
1080 # construct UNIMARC summary, that is quite different from MARC21 one
1082 foreach my $field ($record->field('2..')) {
1083 return $field->as_string('abcdefghijlmnopqrstuvwxyz');
1086 use C4::Heading::MARC21;
1087 my $handler = C4::Heading::MARC21->new();
1089 foreach my $field ($record->field('1..')) {
1090 my $subfields = $handler->get_valid_bib_heading_subfields($field->tag());
1091 return $field->as_string($subfields) if ($subfields);
1097 =head2 CompareFieldWithAuthority
1099 $match = &CompareFieldWithAuthority({ field => $field, authid => $authid })
1101 Takes a MARC::Field from a bibliographic record and an authid, and returns true if they match.
1105 sub CompareFieldWithAuthority {
1108 my $record = GetAuthority($args->{authid});
1109 return unless (ref $record eq 'MARC::Record');
1110 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
1111 # UNIMARC has same subfields for bibs and authorities
1112 foreach my $field ($record->field('2..')) {
1113 return compare_fields($field, $args->{field}, 'abcdefghijlmnopqrstuvwxyz');
1116 use C4::Heading::MARC21;
1117 my $handler = C4::Heading::MARC21->new();
1119 foreach my $field ($record->field('1..')) {
1120 my $subfields = $handler->get_valid_bib_heading_subfields($field->tag());
1121 return compare_fields($field, $args->{field}, $subfields) if ($subfields);
1127 =head2 BuildAuthHierarchies
1129 $text= &BuildAuthHierarchies( $authid, $force)
1131 return text containing trees for hierarchies
1132 for them to be stored in auth_header
1135 122,1314,2452;1324,2342,3,2452
1139 sub BuildAuthHierarchies{
1140 my $authid = shift @_;
1141 # warn "authid : $authid";
1142 my $force = shift @_ || (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 0 : 1);
1144 my $dbh=C4::Context->dbh;
1146 my $data = GetHeaderAuthority($authid);
1147 if ($data->{'authtrees'} and not $force){
1148 return $data->{'authtrees'};
1149 # } elsif ($data->{'authtrees'}){
1150 # $hierarchies=$data->{'authtrees'};
1152 my $record = GetAuthority($authid);
1154 return unless $record;
1155 foreach my $field ($record->field('5..')){
1158 (C4::Context->preference('marcflavour') eq 'UNIMARC' && $field->subfield('5') && $field->subfield('5') eq 'g') ||
1159 (C4::Context->preference('marcflavour') ne 'UNIMARC' && $field->subfield('w') && substr($field->subfield('w'), 0, 1) eq 'g'));
1161 my $subfauthid=_get_authid_subfield($field) || '';
1162 next if ($subfauthid eq $authid);
1163 my $parentrecord = GetAuthority($subfauthid);
1164 next unless $parentrecord;
1165 my $localresult=$hierarchies;
1167 $trees = BuildAuthHierarchies($subfauthid);
1170 @trees = split(/;/,$trees);
1172 push @trees, $trees;
1177 @globalresult = (@globalresult,@trees);
1180 $hierarchies=join(";",@globalresult);
1182 #Unless there is no ancestor, I am alone.
1183 $hierarchies="$authid" unless ($hierarchies);
1185 AddAuthorityTrees($authid,$hierarchies);
1186 return $hierarchies;
1189 =head2 BuildAuthHierarchy
1191 $ref= &BuildAuthHierarchy( $record, $class,$authid)
1193 return a hashref in order to display hierarchy for record and final Authid $authid
1204 sub BuildAuthHierarchy{
1205 my $record = shift @_;
1206 my $class = shift @_;
1207 my $authid_constructed = shift @_;
1208 return unless ($record && $record->field('001'));
1209 my $authid=$record->field('001')->data();
1211 my $parents=""; my $children="";
1212 my (@loopparents,@loopchildren);
1213 my $marcflavour = C4::Context->preference('marcflavour');
1214 my $relationshipsf = $marcflavour eq 'UNIMARC' ? '5' : 'w';
1215 foreach my $field ($record->field('5..')){
1216 my $subfauthid=_get_authid_subfield($field);
1217 if ($subfauthid && $field->subfield($relationshipsf) && $field->subfield('a')){
1218 my $relationship = substr($field->subfield($relationshipsf), 0, 1);
1219 if ($relationship eq 'h'){
1220 push @loopchildren, { "authid"=>$subfauthid,"value"=>$field->subfield('a')};
1222 elsif ($relationship eq 'g'){
1223 push @loopparents, { "authid"=>$subfauthid,"value"=>$field->subfield('a')};
1225 # brothers could get in there with an else
1228 $cell{"parents"}=\@loopparents;
1229 $cell{"children"}=\@loopchildren;
1230 $cell{"class"}=$class;
1231 $cell{"authid"}=$authid;
1232 $cell{"current_value"} =1 if ($authid eq $authid_constructed);
1233 $cell{"value"}=C4::Context->preference('marcflavour') eq 'UNIMARC' ? $record->subfield('2..',"a") : $record->subfield('1..', 'a');
1237 =head2 BuildAuthHierarchyBranch
1239 $branch = &BuildAuthHierarchyBranch( $tree, $authid[, $cnt])
1241 Return a data structure representing an authority hierarchy
1242 given a list of authorities representing a single branch in
1243 an authority hierarchy tree. $authid is the current node in
1244 the tree (which may or may not be somewhere in the middle).
1245 $cnt represents the level of the upper-most item, and is only
1246 used when BuildAuthHierarchyBranch is called recursively (i.e.,
1247 don't ever pass in anything but zero to it).
1251 sub BuildAuthHierarchyBranch {
1252 my ($tree, $authid, $cnt) = @_;
1254 my $elementdata = GetAuthority(shift @$tree);
1255 my $branch = BuildAuthHierarchy($elementdata,"child".$cnt, $authid);
1256 if (scalar @$tree > 0) {
1257 my $nextBranch = BuildAuthHierarchyBranch($tree, $authid, ++$cnt);
1258 my $nextAuthid = $nextBranch->{authid};
1260 # If we already have the next branch listed as a child, let's
1261 # replace the old listing with the new one. If not, we will add
1262 # the branch at the end.
1263 foreach my $cell (@{$branch->{children}}) {
1264 if ($cell->{authid} eq $nextAuthid) {
1265 $cell = $nextBranch;
1270 push @{$branch->{children}}, $nextBranch unless $found;
1275 =head2 GenerateHierarchy
1277 $hierarchy = &GenerateHierarchy($authid);
1279 Return an arrayref holding one or more "trees" representing
1280 authority hierarchies.
1284 sub GenerateHierarchy {
1286 my $trees = BuildAuthHierarchies($authid);
1287 my @trees = split /;/,$trees ;
1288 push @trees,$trees unless (@trees);
1289 my @loophierarchies;
1290 foreach my $tree (@trees){
1291 my @tree=split /,/,$tree;
1292 push @tree, $tree unless (@tree);
1293 my $branch = BuildAuthHierarchyBranch(\@tree, $authid);
1294 push @loophierarchies, [ $branch ];
1296 return \@loophierarchies;
1299 sub _get_authid_subfield{
1301 return $field->subfield('9')||$field->subfield('3');
1304 =head2 GetHeaderAuthority
1306 $ref= &GetHeaderAuthority( $authid)
1308 return a hashref in order auth_header table data
1312 sub GetHeaderAuthority{
1313 my $authid = shift @_;
1314 my $sql= "SELECT * from auth_header WHERE authid = ?";
1315 my $dbh=C4::Context->dbh;
1316 my $rq= $dbh->prepare($sql);
1317 $rq->execute($authid);
1318 my $data= $rq->fetchrow_hashref;
1322 =head2 AddAuthorityTrees
1324 $ref= &AddAuthorityTrees( $authid, $trees)
1326 return success or failure
1330 sub AddAuthorityTrees{
1331 my $authid = shift @_;
1332 my $trees = shift @_;
1333 my $sql= "UPDATE IGNORE auth_header set authtrees=? WHERE authid = ?";
1334 my $dbh=C4::Context->dbh;
1335 my $rq= $dbh->prepare($sql);
1336 return $rq->execute($trees,$authid);
1342 mergefrom => $mergefrom,
1343 [ MARCfrom => $MARCfrom, ]
1344 [ mergeto => $mergeto, ]
1345 [ MARCto => $MARCto, ]
1346 [ biblionumbers => [ $a, $b, $c ], ]
1347 [ override_limit => 1, ]
1350 Merge biblios linked to authority $mergefrom (mandatory parameter).
1351 If $mergeto equals mergefrom, the linked biblio field is updated.
1352 If $mergeto is different, the biblio field will be linked to $mergeto.
1353 If $mergeto is missing, the biblio field is deleted.
1355 MARCfrom is used to determine if a cleared subfield in the authority record
1356 should be removed from a biblio. MARCto is used to populate the biblio
1357 record with the updated values; if you do not pass it, the biblio field
1358 will be deleted (same as missing mergeto).
1360 Normally all biblio records linked to $mergefrom, will be considered. But
1361 you can pass specific numbers via the biblionumbers parameter.
1363 The parameter override_limit is used by the cron job to force larger
1366 Note: Although $mergefrom and $mergeto will normally be of the same
1367 authority type, merge also supports moving to another authority type.
1372 my ( $params ) = @_;
1373 my $mergefrom = $params->{mergefrom} || return;
1374 my $MARCfrom = $params->{MARCfrom};
1375 my $mergeto = $params->{mergeto};
1376 my $MARCto = $params->{MARCto};
1377 my $override_limit = $params->{override_limit};
1379 # If we do not have biblionumbers, we get all linked biblios if the
1380 # number of linked records does not exceed the limit UNLESS we override.
1382 if( $params->{biblionumbers} ) {
1383 @biblionumbers = @{ $params->{biblionumbers} };
1384 } elsif( $override_limit ) {
1385 @biblionumbers = Koha::Authorities->linked_biblionumbers({ authid => $mergefrom });
1386 } else { # now first check number of linked records
1387 my $max = C4::Context->preference('AuthorityMergeLimit') // 0;
1388 my $hits = Koha::Authorities->get_usage_count({ authid => $mergefrom });
1389 if( $hits > 0 && $hits <= $max ) {
1390 @biblionumbers = Koha::Authorities->linked_biblionumbers({ authid => $mergefrom });
1391 } elsif( $hits > $max ) { #postpone this merge to the cron job
1392 Koha::Authority::MergeRequest->new({
1393 authid => $mergefrom,
1394 oldrecord => $MARCfrom,
1395 authid_new => $mergeto,
1399 return 0 if !@biblionumbers;
1401 # Search authtypes and reporting tags
1402 my $authfrom = Koha::Authorities->find($mergefrom);
1403 my $authto = Koha::Authorities->find($mergeto);
1404 my $authtypefrom = $authfrom ? Koha::Authority::Types->find($authfrom->authtypecode) : undef;
1405 my $authtypeto = $authto ? Koha::Authority::Types->find($authto->authtypecode) : undef;
1406 my $auth_tag_to_report_from = $authtypefrom ? $authtypefrom->auth_tag_to_report : '';
1407 my $auth_tag_to_report_to = $authtypeto ? $authtypeto->auth_tag_to_report : '';
1410 @record_to = $MARCto->field($auth_tag_to_report_to)->subfields() if $auth_tag_to_report_to && $MARCto && $MARCto->field($auth_tag_to_report_to);
1411 # Exceptional: If MARCto and authtypeto exist but $auth_tag_to_report_to
1412 # is empty, make sure that $9 and $a remain (instead of clearing the
1413 # reference) in order to allow for data recovery.
1414 # Note: We need $a too, since a single $9 does not pass ModBiblio.
1415 if( $MARCto && $authtypeto && !@record_to ) {
1416 push @record_to, [ 'a', ' ' ]; # do not remove the space
1420 if( !$authfrom && $MARCfrom && $MARCfrom->field('1..','2..') ) {
1421 # postponed merge, authfrom was deleted and MARCfrom only contains the old reporting tag (and possibly a 100 for UNIMARC)
1422 # 2XX is for UNIMARC; we use -1 in order to skip 100 in UNIMARC; this will not impact MARC21, since there is only one tag
1423 @record_from = ( $MARCfrom->field('1..','2..') )[-1]->subfields;
1424 } elsif( $auth_tag_to_report_from && $MARCfrom && $MARCfrom->field($auth_tag_to_report_from) ) {
1425 @record_from = $MARCfrom->field($auth_tag_to_report_from)->subfields;
1428 # Get all candidate tags for the change
1429 # (This will reduce the search scope in marc records).
1430 # For a deleted authority record, we scan all auth controlled fields
1431 my $dbh = C4::Context->dbh;
1432 my $sql = "SELECT DISTINCT tagfield FROM marc_subfield_structure WHERE authtypecode=?";
1433 my $tags_using_authtype = $authtypefrom && $authtypefrom->authtypecode ? $dbh->selectcol_arrayref( $sql, undef, ( $authtypefrom->authtypecode )) : $dbh->selectcol_arrayref( "SELECT DISTINCT tagfield FROM marc_subfield_structure WHERE authtypecode IS NOT NULL AND authtypecode<>''" );
1435 if( $authtypeto && ( !$authtypefrom || $authtypeto->authtypecode ne $authtypefrom->authtypecode )) {
1436 $tags_new = $dbh->selectcol_arrayref( $sql, undef, ( $authtypeto->authtypecode ));
1439 my $overwrite = C4::Context->preference( 'AuthorityMergeMode' ) eq 'strict';
1440 my $skip_subfields = $overwrite
1441 # This hash contains all subfields from the authority report fields
1442 # Including $MARCfrom as well as $MARCto
1443 # We only need it in loose merge mode; replaces the former $exclude
1445 : { map { ( $_->[0], 1 ); } ( @record_from, @record_to ) };
1447 my $counteditedbiblio = 0;
1448 foreach my $biblionumber ( @biblionumbers ) {
1449 my $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber });
1450 next if !$marcrecord;
1452 foreach my $tagfield (@$tags_using_authtype) {
1453 my $countfrom = 0; # used in strict mode to remove duplicates
1454 foreach my $field ( $marcrecord->field($tagfield) ) {
1455 my $auth_number = $field->subfield("9"); # link to authority
1456 my $tag = $field->tag();
1457 next if !defined($auth_number) || $auth_number ne $mergefrom;
1459 if ( !$mergeto || !@record_to ||
1460 ( $overwrite && $countfrom > 1 ) ) {
1461 # !mergeto or !record_to indicates a delete
1462 # Other condition: remove this duplicate in strict mode
1463 $marcrecord->delete_field($field);
1467 my $newtag = $tags_new && @$tags_new
1468 ? _merge_newtag( $tag, $tags_new )
1470 my $controlled_ind = $authto->controlled_indicators({ record => $MARCto, biblio_tag => $newtag });
1471 my $field_to = MARC::Field->new(
1473 $controlled_ind->{ind1} // $field->indicator(1),
1474 $controlled_ind->{ind2} // $field->indicator(2),
1475 9 => $mergeto, # Needed to create field, will be moved
1477 my ( @prefix, @postfix );
1478 if ( !$overwrite ) {
1479 # add subfields back in loose mode, check skip_subfields
1480 # The first extra subfields will be in front of the
1481 # controlled block, the rest at the end.
1482 my $prefix_flag = 1;
1483 foreach my $subfield ( $field->subfields ) {
1484 next if $subfield->[0] eq '9'; # skip but leave flag
1485 if ( $skip_subfields->{ $subfield->[0] } ) {
1486 # This marks the beginning of the controlled block
1491 push @prefix, [ $subfield->[0], $subfield->[1] ];
1493 push @postfix, [ $subfield->[0], $subfield->[1] ];
1497 foreach my $subfield ( @prefix, @record_to, @postfix ) {
1498 $field_to->add_subfields($subfield->[0] => $subfield->[1]);
1500 if( exists $controlled_ind->{sub2} ) { # thesaurus info
1501 if( defined $controlled_ind->{sub2} ) {
1503 $field_to->update( 2 => $controlled_ind->{sub2} );
1505 # Key alerts us here to remove $2
1506 $field_to->delete_subfield( code => '2' );
1509 # Move $9 to the end
1510 $field_to->delete_subfield( code => '9' );
1511 $field_to->add_subfields( 9 => $mergeto );
1513 if ($tags_new && @$tags_new) {
1514 $marcrecord->delete_field($field);
1515 append_fields_ordered( $marcrecord, $field_to );
1517 $field->replace_with($field_to);
1523 ModBiblio($marcrecord, $biblionumber, GetFrameworkCode($biblionumber));
1524 $counteditedbiblio++;
1526 return $counteditedbiblio;
1530 # Routine is only called for an (exceptional) authtypecode change
1531 # Fixes old behavior of returning the first tag found
1532 my ( $oldtag, $new_tags ) = @_;
1534 # If we e.g. have 650 and 151,651,751 try 651 and check presence
1535 my $prefix = substr( $oldtag, 0, 1 );
1536 my $guess = $prefix . substr( $new_tags->[0], -2 );
1537 if( grep { $_ eq $guess } @$new_tags ) {
1540 # Otherwise return one from the same block e.g. 6XX for 650
1541 # If not there too, fall back to first new tag (old behavior!)
1542 my @same_block = grep { /^$prefix/ } @$new_tags;
1543 return @same_block ? $same_block[0] : $new_tags->[0];
1546 sub append_fields_ordered {
1547 # while we lack this function in MARC::Record
1548 # we do not want insert_fields_ordered since it inserts before
1549 my ( $record, $field ) = @_;
1550 if( my @flds = $record->field( $field->tag ) ) {
1551 $record->insert_fields_after( pop @flds, $field );
1552 } else { # now fallback to insert_fields_ordered
1553 $record->insert_fields_ordered( $field );
1557 =head2 get_auth_type_location
1559 my ($tag, $subfield) = get_auth_type_location($auth_type_code);
1561 Get the tag and subfield used to store the heading type
1562 for indexing purposes. The C<$auth_type> parameter is
1563 optional; if it is not supplied, assume ''.
1565 This routine searches the MARC authority framework
1566 for the tag and subfield whose kohafield is
1567 C<auth_header.authtypecode>; if no such field is
1568 defined in the framework, default to the hardcoded value
1569 specific to the MARC format.
1573 sub get_auth_type_location {
1574 my $auth_type_code = @_ ? shift : '';
1576 my ($tag, $subfield) = GetAuthMARCFromKohaField('auth_header.authtypecode', $auth_type_code);
1577 if (defined $tag and defined $subfield and $tag != 0 and $subfield ne '' and $subfield ne ' ') {
1578 return ($tag, $subfield);
1580 if (C4::Context->preference('marcflavour') eq "MARC21") {
1581 return C4::AuthoritiesMarc::MARC21::default_auth_type_location();
1583 return C4::AuthoritiesMarc::UNIMARC::default_auth_type_location();
1588 =head2 compare_fields
1590 my match = compare_fields($field1, $field2, 'abcde');
1592 Compares the listed subfields of both fields and return true if they all match
1596 sub compare_fields {
1597 my ($field1, $field2, $subfields) = @_;
1599 foreach my $subfield (split(//, $subfields)) {
1600 my $subfield1 = $field1->subfield($subfield) // '';
1601 my $subfield2 = $field2->subfield($subfield) // '';
1602 return 0 unless $subfield1 eq $subfield2;
1608 =head2 _after_authority_action_hooks
1610 Helper method that takes care of calling all plugin hooks
1614 sub _after_authority_action_hooks {
1615 my ( $args ) = @_; # hash keys: action, authority_id
1616 return Koha::Plugins->call( 'after_authority_action', $args );
1619 END { } # module clean-up code here (global destructor)
1626 Koha Development Team <http://koha-community.org/>
1628 Paul POULAIN paul.poulain@free.fr
1629 Ere Maijala ere.maijala@helsinki.fi