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( 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::RecordProcessor;
39 use Koha::SearchEngine;
40 use Koha::SearchEngine::Indexer;
41 use Koha::SearchEngine::Search;
43 our (@ISA, @EXPORT_OK);
50 GetAuthMARCFromKohaField
67 CompareFieldWithAuthority
70 FindDuplicateAuthority
83 =head2 GetAuthMARCFromKohaField
85 ( $tag, $subfield ) = &GetAuthMARCFromKohaField ($kohafield,$authtypecode);
87 returns tag and subfield linked to kohafield
90 Suppose Kohafield is only linked to ONE subfield
94 sub GetAuthMARCFromKohaField {
95 #AUTHfind_marc_from_kohafield
96 my ( $kohafield,$authtypecode ) = @_;
97 my $dbh=C4::Context->dbh;
98 return 0, 0 unless $kohafield;
99 $authtypecode="" unless $authtypecode;
100 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
101 $sth->execute($kohafield,$authtypecode);
102 my ($tagfield,$tagsubfield) = $sth->fetchrow;
104 return ($tagfield,$tagsubfield);
107 =head2 SearchAuthorities
109 (\@finalresult, $nbresults)= &SearchAuthorities($tags, $and_or,
110 $excluding, $operator, $value, $offset,$length,$authtypecode,
111 $sortby[, $skipmetadata])
113 returns ref to array result and count of results returned
117 sub SearchAuthorities {
118 my ($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby,$skipmetadata) = @_;
119 # warn Dumper($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby);
120 my $dbh=C4::Context->dbh;
121 $sortby="" unless $sortby;
125 # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
126 # the authtypecode. Then, search on $a of this tag_to_report
127 # also store main entry MARC tag, to extract it at end of search
128 ##first set the authtype search and may be multiple authorities
132 my @auths=split / /,$authtypecode ;
133 foreach my $auth (@auths){
134 $query .=" \@attr 1=authtype \@attr 5=100 ".$auth; ##No truncation on authtype
135 push @authtypecode ,$auth;
139 while ($n>1){$query= "\@or ".$query;$n--;}
147 for ( my $i = 0 ; $i <= $#{$value} ; $i++ ) {
150 if ( @$tags[$i] eq "mainmainentry" ) {
151 $attr = " \@attr 1=Heading-Main ";
153 elsif ( @$tags[$i] eq "mainentry" ) {
154 $attr = " \@attr 1=Heading ";
156 elsif ( @$tags[$i] eq "match" ) {
157 $attr = " \@attr 1=Match ";
159 elsif ( @$tags[$i] eq "match-heading" ) {
160 $attr = " \@attr 1=Match-heading ";
162 elsif ( @$tags[$i] eq "see-from" ) {
163 $attr = " \@attr 1=Match-heading-see-from ";
165 elsif ( @$tags[$i] eq "thesaurus" ) {
166 $attr = " \@attr 1=Subject-heading-thesaurus ";
168 elsif ( @$tags[$i] eq "all" ) {
169 $attr = " \@attr 1=Any ";
171 else { # Use the index passed in params
172 $attr = " \@attr 1=" . @$tags[$i] . " ";
175 else { # Assume any if no index was specified
176 $attr = " \@attr 1=Any ";
179 my $operator = @$operator[$i];
180 if ( $operator and $operator eq 'is' ) {
181 $attr .= " \@attr 4=1 \@attr 5=100 "
182 ; ##Phrase, No truncation,all of subfield field must match
184 elsif ( $operator and $operator eq "=" ) {
185 $attr .= " \@attr 4=107 "; #Number Exact match
187 elsif ( $operator and $operator eq "start" ) {
188 $attr .= " \@attr 3=2 \@attr 4=1 \@attr 5=1 \@attr 6=3 "
189 ; #Firstinfield Phrase, Right truncated, Complete field
191 elsif ( $operator and $operator eq "exact" ) {
192 $attr .= " \@attr 4=1 \@attr 5=100 \@attr 6=3 "
193 ; ##Phrase, No truncation,all of subfield field must match
196 $attr .= " \@attr 5=1 \@attr 4=6 "
197 ; ## Word list, right truncated, anywhere
198 if ( $sortby eq 'Relevance' ) {
199 $attr .= "\@attr 2=102 ";
203 s/"/\\"/g; # Escape the double-quotes in the search value
204 $attr = $attr . "\"" . @$value[$i] . "\"";
210 ##Add how many queries generated
211 if ( defined $query && $query =~ /\S+/ ) {
212 #NOTE: This code path is used by authority search in cataloguing plugins...
213 #FIXME: This does not quite work the way the author probably intended.
214 #It creates a ($query prefix) AND (query 1) AND (query 2) structure instead of
215 #($query prefix) AND (query 1 AND query 2)
216 $query = $and x $attr_cnt . $query . ( defined $q2 ? $q2 : '' );
218 #NOTE: This code path is used by authority search in authority home and record matching rules...
220 #NOTE: Without the following code, multiple queries will never be joined together
221 #with a Boolean operator.
222 if ( $attr_cnt > 1 ) {
223 #NOTE: We always need 1 less operator than we have operands,
224 #so long as there is more than 1 operand
225 my $or_cnt = $attr_cnt - 1;
226 #NOTE: We hard-code OR here because that's what Elasticsearch does
227 $op_prefix = ' @or ' x $or_cnt;
228 #NOTE: This evaluates to a logical structure like (query 1) OR (query 2) OR (query 3)
230 $query = $op_prefix . $q2;
233 #$query=' @or @attr 7=2 @attr 1=Heading 0 @or @attr 7=1 @attr 1=Heading 1'.$query if ($sortby eq "HeadingDsc");
235 if ($sortby eq 'HeadingAsc') {
236 $orderstring = '@attr 7=1 @attr 1=Heading 0';
237 } elsif ($sortby eq 'HeadingDsc') {
238 $orderstring = '@attr 7=2 @attr 1=Heading 0';
239 } elsif ($sortby eq 'AuthidAsc') {
240 $orderstring = '@attr 7=1 @attr 4=109 @attr 1=Local-Number 0';
241 } elsif ($sortby eq 'AuthidDsc') {
242 $orderstring = '@attr 7=2 @attr 4=109 @attr 1=Local-Number 0';
244 $query=($query?$query:"\@attr 1=_ALLRECORDS \@attr 2=103 ''");
245 $query="\@or $orderstring $query" if $orderstring;
247 $offset = 0 if not defined $offset or $offset < 0;
248 my $counter = $offset;
249 $length=10 unless $length;
252 $oAuth[0]=C4::Context->Zconn("authorityserver" , 1);
253 my $Anewq= ZOOM::Query::PQF->new($query,$oAuth[0]);
255 $oAResult= $oAuth[0]->search($Anewq) ;
256 while (($i = ZOOM::event(\@oAuth)) != 0) {
257 my $ev = $oAuth[$i-1]->last_event();
258 last if $ev == ZOOM::Event::ZEND;
260 my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
262 warn "oAuth error: $errmsg ($error) $addinfo $diagset\n";
267 $nbresults=$oAResult->size();
268 my $nremains=$nbresults;
270 my @finalresult = ();
274 ##Find authid and linkid fields
275 ##we may be searching multiple authoritytypes.
276 ## FIXME this assumes that all authid and linkid fields are the same for all authority types
277 # my ($authidfield,$authidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.authid",$authtypecode[0]);
278 # my ($linkidfield,$linkidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.linkid",$authtypecode[0]);
279 while (($counter < $nbresults) && ($counter < ($offset + $length))) {
281 ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
282 my $rec=$oAResult->record($counter);
283 my $separator=C4::Context->preference('AuthoritySeparator');
284 my $authrecord = C4::Search::new_record_from_zebra(
289 if ( !defined $authrecord or !defined $authrecord->field('001') ) {
294 SetUTF8Flag( $authrecord );
296 my $authid=$authrecord->field('001')->data();
298 $newline{authid} = $authid;
299 if ( !$skipmetadata ) {
300 my ( $auth_tag_to_report, $reported_tag, $mainentry );
301 if ( $authtypecode ) {
302 $auth_tag_to_report = Koha::Authority::Types->find($authtypecode)->auth_tag_to_report;
303 $mainentry = $authrecord->field($auth_tag_to_report) if $auth_tag_to_report;
307 foreach ( $mainentry->subfields() ) {
308 $reported_tag .= '$' . $_->[0] . $_->[1];
312 my ( $thisauthtype, $thisauthtypecode );
313 if ( my $authority = Koha::Authorities->find($authid) ) {
314 $thisauthtypecode = $authority->authtypecode;
315 $thisauthtype = Koha::Authority::Types->find($thisauthtypecode);
317 unless (defined $thisauthtype) {
318 $thisauthtypecode = $authtypecode;
319 $thisauthtype = Koha::Authority::Types->find($thisauthtypecode);
321 my $summary = BuildSummary( $authrecord, $authid, $thisauthtypecode );
323 if ( C4::Context->preference('ShowHeadingUse') ) {
324 # checking valid heading use
325 my $f008 = $authrecord->field('008');
326 my $pos14to16 = substr( $f008->data, 14, 3 );
327 my $main = substr( $pos14to16, 0, 1 );
328 $newline{main} = 1 if $main eq 'a';
329 my $subject = substr( $pos14to16, 1, 1);
330 $newline{subject} = 1 if $subject eq 'a';
331 my $series = substr( $pos14to16, 2, 1 );
332 $newline{series} = 1 if $series eq 'a';
335 $newline{authtype} = defined($thisauthtype) ?
336 $thisauthtype->authtypetext : '';
337 $newline{summary} = $summary;
338 $newline{even} = $counter % 2;
339 $newline{reported_tag} = $reported_tag;
342 push @finalresult, \%newline;
345 if (! $skipmetadata) {
346 for (my $z=0; $z<@finalresult; $z++){
347 my $count = Koha::Authorities->get_usage_count({ authid => $finalresult[$z]{authid} });
348 $finalresult[$z]{used}=$count;
354 $oAResult->destroy();
355 # $oAuth[0]->destroy();
357 return (\@finalresult, $nbresults);
360 =head2 GuessAuthTypeCode
362 my $authtypecode = GuessAuthTypeCode($record);
364 Get the record and tries to guess the adequate authtypecode from its content.
368 sub GuessAuthTypeCode {
369 my ($record, $heading_fields) = @_;
370 return unless defined $record;
371 $heading_fields //= {
373 '100'=>{authtypecode=>'PERSO_NAME'},
374 '110'=>{authtypecode=>'CORPO_NAME'},
375 '111'=>{authtypecode=>'MEETI_NAME'},
376 '130'=>{authtypecode=>'UNIF_TITLE'},
377 '147'=>{authtypecode=>'NAME_EVENT'},
378 '148'=>{authtypecode=>'CHRON_TERM'},
379 '150'=>{authtypecode=>'TOPIC_TERM'},
380 '151'=>{authtypecode=>'GEOGR_NAME'},
381 '155'=>{authtypecode=>'GENRE/FORM'},
382 '162'=>{authtypecode=>'MED_PERFRM'},
383 '180'=>{authtypecode=>'GEN_SUBDIV'},
384 '181'=>{authtypecode=>'GEO_SUBDIV'},
385 '182'=>{authtypecode=>'CHRON_SUBD'},
386 '185'=>{authtypecode=>'FORM_SUBD'},
388 #200 Personal name 700, 701, 702 4-- with embedded 700, 701, 702 600
389 # 604 with embedded 700, 701, 702
390 #210 Corporate or meeting name 710, 711, 712 4-- with embedded 710, 711, 712 601 604 with embedded 710, 711, 712
391 #215 Territorial or geographic name 710, 711, 712 4-- with embedded 710, 711, 712 601, 607 604 with embedded 710, 711, 712
392 #216 Trademark 716 [Reserved for future use]
393 #220 Family name 720, 721, 722 4-- with embedded 720, 721, 722 602 604 with embedded 720, 721, 722
394 #230 Title 500 4-- with embedded 500 605
395 #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
396 #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
397 #250 Topical subject 606
398 #260 Place access 620
399 #280 Form, genre or physical characteristics 608
402 # Could also be represented with :
404 #a = personal name entry
405 #b = corporate name entry
406 #c = territorial or geographical name
410 #g = collective uniform title
412 #i = name/collective uniform title
415 #l = form, genre or physical characteristics
417 '200'=>{authtypecode=>'NP'},
418 '210'=>{authtypecode=>'CO'},
419 '215'=>{authtypecode=>'SNG'},
420 '216'=>{authtypecode=>'TM'},
421 '220'=>{authtypecode=>'FAM'},
422 '230'=>{authtypecode=>'TU'},
423 '235'=>{authtypecode=>'CO_UNI_TI'},
424 '240'=>{authtypecode=>'SAUTTIT'},
425 '245'=>{authtypecode=>'NAME_COL'},
426 '250'=>{authtypecode=>'SNC'},
427 '260'=>{authtypecode=>'PA'},
428 '280'=>{authtypecode=>'GENRE/FORM'},
431 foreach my $field (keys %{$heading_fields->{uc(C4::Context->preference('marcflavour'))} }) {
432 return $heading_fields->{uc(C4::Context->preference('marcflavour'))}->{$field}->{'authtypecode'} if (defined $record->field($field));
439 my $authtid = GuessAuthId($record);
441 Get the record and tries to guess the adequate authtypecode from its content.
447 return unless ($record && $record->field('001'));
448 # my $authtypecode=GuessAuthTypeCode($record);
449 # my ($tag,$subfield)=GetAuthMARCFromKohaField("auth_header.authid",$authtypecode);
450 # if ($tag > 010) {return $record->subfield($tag,$subfield)}
451 # else {return $record->field($tag)->data}
452 return $record->field('001')->data;
457 $tagslabel= &GetTagsLabels($forlibrarian,$authtypecode)
459 returns a ref to hashref of authorities tag and subfield structure.
463 $tagslabel->{$tag}->{$subfield}->{'attribute'}
465 where attribute takes values in :
483 my ($forlibrarian,$authtypecode)= @_;
484 my $dbh=C4::Context->dbh;
485 $authtypecode="" unless $authtypecode;
487 my $libfield = ($forlibrarian) ? 'liblibrarian' : 'libopac';
490 # check that authority exists
491 $sth=$dbh->prepare("SELECT count(*) FROM auth_tag_structure WHERE authtypecode=?");
492 $sth->execute($authtypecode);
493 my ($total) = $sth->fetchrow;
494 $authtypecode="" unless ($total >0);
496 "SELECT auth_tag_structure.tagfield,auth_tag_structure.liblibrarian,auth_tag_structure.libopac,auth_tag_structure.mandatory,auth_tag_structure.repeatable
497 FROM auth_tag_structure
502 $sth->execute($authtypecode);
503 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
505 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
506 $res->{$tag}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
507 $res->{$tag}->{tab} = " "; # XXX
508 $res->{$tag}->{mandatory} = $mandatory;
509 $res->{$tag}->{repeatable} = $repeatable;
512 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,frameworkcode as authtypecode,value_builder,kohafield,seealso,hidden,isurl,defaultvalue, display_order
513 FROM auth_subfield_structure
515 ORDER BY tagfield, display_order, tagsubfield"
517 $sth->execute($authtypecode);
520 my $authorised_value;
530 ( $tag, $subfield, $liblibrarian, , $libopac, $tab,
531 $mandatory, $repeatable, $authorised_value, $authtypecode,
532 $value_builder, $kohafield, $seealso, $hidden,
533 $isurl, $defaultvalue, $display_order )
537 $res->{$tag}->{$subfield}->{subfield} = $subfield;
538 $res->{$tag}->{$subfield}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
539 $res->{$tag}->{$subfield}->{tab} = $tab;
540 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
541 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
542 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
543 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
544 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
545 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
546 $res->{$tag}->{$subfield}->{seealso} = $seealso;
547 $res->{$tag}->{$subfield}->{hidden} = $hidden;
548 $res->{$tag}->{$subfield}->{isurl} = $isurl;
549 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
550 $res->{$tag}->{$subfield}->{display_order} = $display_order;
558 $authid= &AddAuthority($record, $authid,$authtypecode)
560 Either Create Or Modify existing authority.
561 returns authid of the newly created authority
567 # pass the MARC::Record to this function, and it will create the records in the authority table
568 my ( $record, $authid, $authtypecode, $params ) = @_;
570 my $skip_record_index = $params->{skip_record_index} || 0;
572 my $dbh = C4::Context->dbh;
573 my $leader = ' nz a22 o 4500'; #Leader for incomplete MARC21 record
575 # if authid empty => true add, find a new authid number
577 if ( uc( C4::Context->preference('marcflavour') ) eq 'UNIMARC' ) {
578 $format = 'UNIMARCAUTH';
583 #update date/time to 005 for marc and unimarc
584 my $time = POSIX::strftime( "%Y%m%d%H%M%S", localtime );
585 my $f5 = $record->field('005');
587 $record->insert_fields_ordered( MARC::Field->new( '005', $time . ".0" ) );
589 $f5->update( $time . ".0" );
592 SetUTF8Flag($record);
593 if ( $format eq "MARC21" ) {
594 my $userenv = C4::Context->userenv;
596 my $marcorgcode = C4::Context->preference('MARCOrgCode');
597 if ( $userenv && $userenv->{'branch'} ) {
598 $library = Koha::Libraries->find( $userenv->{'branch'} );
600 # userenv's library could not exist because of a trick in misc/commit_file.pl (see FIXME and set_userenv statement)
601 $marcorgcode = $library ? $library->get_effective_marcorgcode : $marcorgcode;
603 if ( !$record->leader ) {
604 $record->leader($leader);
606 if ( !$record->field('003') ) {
607 $record->insert_fields_ordered(
608 MARC::Field->new( '003', $marcorgcode ),
611 my $date = POSIX::strftime( "%y%m%d", localtime );
612 if ( !$record->field('008') ) {
614 # Get a valid default value for field 008
615 my $default_008 = C4::Context->preference('MARCAuthorityControlField008');
616 if ( !$default_008 or length($default_008) < 34 ) {
617 $default_008 = '|| aca||aabn | a|a d';
619 $default_008 = substr( $default_008, 0, 34 );
622 $record->insert_fields_ordered( MARC::Field->new( '008', $date . $default_008 ) );
624 if ( !$record->field('040') ) {
625 $record->insert_fields_ordered(
635 if ( $format eq "UNIMARCAUTH" ) {
636 $record->leader(" nx j22 ") unless ( $record->leader() );
637 my $date = POSIX::strftime( "%Y%m%d", localtime );
638 my $defaultfield100 = C4::Context->preference('UNIMARCAuthorityField100');
639 if ( my $string = $record->subfield( '100', "a" ) ) {
640 $string =~ s/fre50/frey50/;
641 $record->field('100')->update( 'a' => $string );
642 } elsif ( $record->field('100') ) {
643 $record->field('100')->update( 'a' => $date . $defaultfield100 );
645 $record->append_fields(
648 , 'a' => $date . $defaultfield100
653 my ( $auth_type_tag, $auth_type_subfield ) = get_auth_type_location($authtypecode);
654 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 );
662 $record->add_fields( $auth_type_tag, '', '', $auth_type_subfield => $authtypecode );
665 if ( C4::Context->preference('StripWhitespaceChars') ) {
666 my $p = Koha::RecordProcessor->new( { filters => qw(TrimFields) } );
667 $p->process($record);
670 # Save record into auth_header, update 001
676 # Save a blank record, get authid
677 $authority = Koha::Authority->new(
679 datecreated => \'NOW()',
681 authtypecode => $authtypecode
684 $authority->discard_changes();
685 $authid = $authority->authid;
686 logaction( "AUTHORITIES", "ADD", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
689 $authority = Koha::Authorities->find($authid);
692 # Insert/update the recordID in MARC record
693 $record->delete_field( $record->field('001') );
694 $record->insert_fields_ordered( MARC::Field->new( '001', $authid ) );
696 my $heading = $authority->heading_object( { record => $record } );
701 authtypecode => $authtypecode,
702 marc => $record->as_usmarc,
703 marcxml => $record->as_xml_record($format),
704 heading => $heading->display_form,
708 unless ($skip_record_index) {
709 my $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
710 $indexer->index_records( $authid, "specialUpdate", "authorityserver", $record );
713 _after_authority_action_hooks( { action => $action, authority_id => $authid } );
719 DelAuthority({ authid => $authid, [ skip_merge => 1 ] });
721 Deletes $authid and calls merge to cleanup linked biblio records.
722 Parameter skip_merge is used in authorities/merge.pl. You should normally not
725 skip_record_index will skip the indexation step.
731 my $authid = $params->{authid} || return;
732 my $skip_merge = $params->{skip_merge};
733 my $skip_record_index = $params->{skip_record_index} || 0;
735 my $dbh = C4::Context->dbh;
737 # Remove older pending merge requests for $authid to itself. (See bug 22437)
738 my $condition = { authid => $authid, authid_new => [undef, 0, $authid], done => 0 };
739 Koha::Authority::MergeRequests->search($condition)->delete;
741 merge({ mergefrom => $authid }) if !$skip_merge;
742 $dbh->do( "DELETE FROM auth_header WHERE authid=?", undef, $authid );
743 logaction( "AUTHORITIES", "DELETE", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
744 unless ( $skip_record_index ) {
745 my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
746 $indexer->index_records( $authid, "recordDelete", "authorityserver", undef );
749 _after_authority_action_hooks({ action => 'delete', authority_id => $authid });
754 $authid= &ModAuthority($authid,$record,$authtypecode, [ { skip_merge => 1 ] )
756 Modifies authority record, optionally updates attached biblios.
757 The parameter skip_merge is optional and should be used with care.
759 skip_record_index will skip the indexation step.
764 my ( $authid, $record, $authtypecode, $params ) = @_;
766 my $skip_record_index = $params->{skip_record_index} || 0;
768 my $oldrecord = GetAuthority($authid);
769 #Now rewrite the $record to table with an add
770 $authid = AddAuthority($record, $authid, $authtypecode, { skip_record_index => $skip_record_index });
771 merge({ mergefrom => $authid, MARCfrom => $oldrecord, mergeto => $authid, MARCto => $record }) if !$params->{skip_merge};
772 logaction( "AUTHORITIES", "MODIFY", $authid, "authority BEFORE=>" . $oldrecord->as_formatted ) if C4::Context->preference("AuthoritiesLog");
776 =head2 GetAuthorityXML
778 $marcxml= &GetAuthorityXML( $authid)
780 returns xml form of record $authid
784 sub GetAuthorityXML {
785 # Returns MARC::XML of the authority passed in parameter.
787 if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') {
788 my $dbh=C4::Context->dbh;
789 my $sth = $dbh->prepare("select marcxml from auth_header where authid=? " );
790 $sth->execute($authid);
791 my ($marcxml)=$sth->fetchrow;
795 # for MARC21, call GetAuthority instead of
796 # getting the XML directly since we may
797 # need to fix up the location of the authority
798 # code -- note that this is reasonably safe
799 # because GetAuthorityXML is used only by the
800 # indexing processes like zebraqueue_start.pl
801 my $record = GetAuthority($authid);
802 return $record->as_xml_record('MARC21');
808 $record= &GetAuthority( $authid)
810 Returns MARC::Record of the authority passed in parameter.
816 my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
817 return unless $authority;
818 return ($authority->record);
821 =head2 FindDuplicateAuthority
823 $record= &FindDuplicateAuthority( $record, $authtypecode)
825 return $authid,Summary if duplicate is found.
827 Comments : an improvement would be to return All the records that match.
831 sub FindDuplicateAuthority {
833 my ($record,$authtypecode)=@_;
834 my $dbh = C4::Context->dbh;
835 my $auth_tag_to_report = Koha::Authority::Types->find($authtypecode)->auth_tag_to_report;
836 # build a request for SearchAuthorities
838 my $query='at:"'.$authtypecode.'" '; # Quote authtype code to avoid unescaping slash in GENRE/FORM later
839 my $filtervalues=qr([\001-\040\Q!'"`#$%&*+,-./:;<=>?@(){[}_|~\E\]]);
840 if ($record->field($auth_tag_to_report)) {
841 foreach ($record->field($auth_tag_to_report)->subfields()) {
842 $_->[1]=~s/$filtervalues/ /g; $query.= " $op he:\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/);
845 my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::AUTHORITIES_INDEX});
846 my ($error, $results, $total_hits) = $searcher->simple_search_compat( $query, 0, 1, [ 'authorityserver' ] );
847 # there is at least 1 result => return the 1st one
848 if (!defined $error && @{$results} ) {
849 my $marcrecord = C4::Search::new_record_from_zebra(
853 return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode);
855 # no result, returns nothing
861 $summary= &BuildSummary( $record, $authid, $authtypecode)
863 Returns a hashref with a summary of the specified record.
865 Comment : authtypecode can be inferred from both record and authid.
866 Moreover, authid can also be inferred from $record.
867 Would it be interesting to delete those things.
872 ## give this a Marc record to return summary
873 my ($record,$authid,$authtypecode)=@_;
874 my $dbh=C4::Context->dbh;
876 my $summary_template;
877 # handle $authtypecode is NULL or eq ""
879 my $authref = Koha::Authority::Types->find($authtypecode);
881 $summary{authtypecode} = $authref->authtypecode;
882 $summary{type} = $authref->authtypetext;
883 $summary_template = $authref->summary;
884 # for MARC21, the authority type summary displays a label meant for
886 if (C4::Context->preference('marcflavour') ne 'UNIMARC') {
887 $summary{label} = $authref->summary;
889 $summary{summary} = $authref->summary;
893 my $marc21subfields = 'abcdfghjklmnopqrstuvxyz68';
894 my %marc21controlrefs = ( 'a' => 'earlier',
900 'n' => 'notapplicable',
904 my %unimarc_relation_from_code = (
910 $thesaurus{'1'}="Peuples";
911 $thesaurus{'2'}="Anthroponymes";
912 $thesaurus{'3'}="Oeuvres";
913 $thesaurus{'4'}="Chronologie";
914 $thesaurus{'5'}="Lieux";
915 $thesaurus{'6'}="Sujets";
918 # if the library has a summary defined, use it. Otherwise, build a standard one
919 # FIXME - it appears that the summary field in the authority frameworks
920 # can work as a display template. However, this doesn't
921 # suit the MARC21 version, so for now the "templating"
922 # feature will be enabled only for UNIMARC for backwards
924 if ($summary{summary} and C4::Context->preference('marcflavour') eq 'UNIMARC') {
925 my @matches = ($summary{summary} =~ m/\[(.*?)(\d{3})([\*a-z0-9])(.*?)\]/g);
926 my (@textbefore, @tag, @subtag, @textafter);
927 for(my $i = 0; $i < scalar @matches; $i++){
928 push @textbefore, $matches[$i] if($i%4 == 0);
929 push @tag, $matches[$i] if($i%4 == 1);
930 push @subtag, $matches[$i] if($i%4 == 2);
931 push @textafter, $matches[$i] if($i%4 == 3);
933 for(my $i = scalar @tag; $i >= 0; $i--){
934 my $textbefore = $textbefore[$i] || '';
935 my $tag = $tag[$i] || '';
936 my $subtag = $subtag[$i] || '';
937 my $textafter = $textafter[$i] || '';
939 my $field = $record->field($tag);
943 $value = $textbefore . $field->data() . $textafter;
946 my @subfields = $field->subfield($subtag);
948 $value = $textbefore . join (" - ", @subfields) . $textafter;
952 $summary{summary} =~ s/\[\Q$textbefore$tag$subtag$textafter\E\]/$value/;
954 $summary{summary} =~ s/\\n/<br \/>/g;
963 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
964 # construct UNIMARC summary, that is quite different from MARC21 one
966 foreach my $field ($record->field('2..')) {
968 heading => $field->as_string('abcdefghijlmnopqrstuvwxyz'),
969 hemain => ( $field->subfield('a') // undef ),
970 field => $field->tag(),
974 foreach my $field ($record->field('3..')) {
975 push @notes, { note => $field->subfield('a'), field => $field->tag() };
977 foreach my $field ($record->field('4..')) {
978 my $thesaurus = $field->subfield('2') ? "thes. : ".$thesaurus{"$field->subfield('2')"}." : " : '';
980 heading => $thesaurus . $field->as_string('abcdefghijlmnopqrstuvwxyz'),
981 hemain => ( $field->subfield('a') // undef ),
983 field => $field->tag(),
989 my $type = $unimarc_relation_from_code{$_->subfield('5') || 'a'};
990 my $heading = $_->as_string('abcdefgjxyz');
995 hemain => ( $_->subfield('a') // undef ),
997 authid => ( $_->subfield('9') // undef ),
999 } $record->field('5..');
1002 @otherscript = map { {
1003 lang => length ($_->subfield('8')) == 6 ? substr ($_->subfield('8'), 3, 3) : $_->subfield('8') || '',
1004 term => $_->subfield('a') . ($_->subfield('b') ? ', ' . $_->subfield('b') : ''),
1007 } } $record->field('7..');
1010 # construct MARC21 summary
1011 # FIXME - looping over 1XX is questionable
1012 # since MARC21 authority should have only one 1XX
1013 use C4::Heading::MARC21;
1014 my $handler = C4::Heading::MARC21->new();
1015 my $subfields_to_report;
1016 my $subfields_to_subdivision = "";
1017 my $delimiter = C4::Context->preference('AuthoritySeparator');
1019 foreach my $field ($record->field('1..')) {
1020 my $tag = $field->tag();
1021 next if "152" eq $tag;
1022 # FIXME - 152 is not a good tag to use
1023 # in MARC21 -- purely local tags really ought to be
1026 $subfields_to_report = $handler->get_auth_heading_subfields_to_report($tag);
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 => scalar $field->subfield( substr($marc21subfields, 0, 1) ),
1053 type => ($field->subfield('i') || ''),
1054 field => $field->tag(),
1058 heading => $field->as_string($marc21subfields),
1059 hemain => scalar $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 => scalar $field->subfield( substr($marc21subfields, 0, 1) ),
1076 type => scalar $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 => scalar $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() };
1096 foreach my $field ( $record->field('7..') ) {
1097 my $tag = $field->tag();
1099 if ( $tag eq '700' ) {
1100 $subfields_to_report = 'abcdefghjklmnopqrst';
1101 $subfields_to_subdivision = 'vxyz';
1102 } elsif ( $tag eq '710' ) {
1103 $subfields_to_report = 'abcdefghklmnoprst';
1104 $subfields_to_subdivision = 'vxyz';
1105 } elsif ( $tag eq '711' ) {
1106 $subfields_to_report = 'acdefghklnpqst';
1107 $subfields_to_subdivision = 'vxyz';
1108 } elsif ( $tag eq '730' ) {
1109 $subfields_to_report = 'adfghklmnoprst';
1110 $subfields_to_subdivision = 'vxyz';
1111 } elsif ( $tag eq '748' ) {
1112 $subfields_to_report = 'ab';
1113 $subfields_to_subdivision = 'vxyz';
1114 } elsif ( $tag eq '750' ) {
1115 $subfields_to_report = 'ab';
1116 $subfields_to_subdivision = 'vxyz';
1117 } elsif ( $tag eq '751' ) {
1118 $subfields_to_report = 'a';
1119 $subfields_to_subdivision = 'vxyz';
1120 } elsif ( $tag eq '755' ) {
1121 $subfields_to_report = 'abvxyz';
1122 $subfields_to_subdivision = 'vxyz';
1123 } elsif ( $tag eq '780' ) {
1124 $subfields_to_report = 'vxyz';
1126 } elsif ( $tag eq '781' ) {
1127 $subfields_to_report = 'vxyz';
1129 } elsif ( $tag eq '782' ) {
1130 $subfields_to_report = 'vxyz';
1132 } elsif ( $tag eq '785' ) {
1133 $subfields_to_report = 'vxyz';
1137 my $heading = $field->as_string($subfields_to_report);
1139 my $subheading = $field->as_string( $subfields_to_subdivision, $delimiter );
1140 if ( length $subheading > 0 ) {
1141 $heading .= $delimiter . $subheading;
1144 if ($subfields_to_report) {
1146 heading => $heading,
1147 hemain => ( $field->subfield( substr( $subfields_to_report, 0, 1 ) ) // undef ),
1152 heading => $field->as_string(),
1153 hemain => ( $field->subfield('a') // undef ),
1159 foreach my $field ($record->field('880')) {
1160 my $linkage = $field->subfield('6');
1161 my $category = substr $linkage, 0, 1;
1162 if ($category eq '1') {
1163 $category = 'preferred';
1164 } elsif ($category eq '4') {
1165 $category = 'seefrom';
1166 } elsif ($category eq '5') {
1167 $category = 'seealso';
1170 if ($field->subfield('w')) {
1171 $type = $marc21controlrefs{substr $field->subfield('w'), '0'};
1175 my $direction = $linkage =~ m#/r$# ? 'rtl' : 'ltr';
1176 push @otherscript, { term => $field->as_string($subfields_to_report), category => $category, type => $type, direction => $direction, linkage => $linkage };
1179 $summary{mainentry} = $authorized[0]->{heading};
1180 $summary{mainmainentry} = $authorized[0]->{hemain};
1181 $summary{authorized} = \@authorized;
1182 $summary{notes} = \@notes;
1183 $summary{seefrom} = \@seefrom;
1184 $summary{seealso} = \@seealso;
1185 $summary{otherscript} = \@otherscript;
1186 $summary{equalterm} = \@equalterm;
1190 =head2 GetAuthorizedHeading
1192 $heading = &GetAuthorizedHeading({ record => $record, authid => $authid })
1194 Takes a MARC::Record object describing an authority record or an authid, and
1195 returns a string representation of the first authorized heading. This routine
1196 should be considered a temporary shim to ease the future migration of authority
1197 data from C4::AuthoritiesMarc to the object-oriented Koha::*::Authority.
1201 sub GetAuthorizedHeading {
1204 unless ($record = $args->{record}) {
1205 return unless $args->{authid};
1206 $record = GetAuthority($args->{authid});
1208 return unless (ref $record eq 'MARC::Record');
1209 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
1210 # construct UNIMARC summary, that is quite different from MARC21 one
1212 foreach my $field ($record->field('2..')) {
1213 return $field->as_string('abcdefghijlmnopqrstuvwxyz');
1216 use C4::Heading::MARC21;
1217 my $handler = C4::Heading::MARC21->new();
1219 foreach my $field ($record->field('1..')) {
1220 my $subfields = $handler->get_valid_bib_heading_subfields($field->tag());
1221 return $field->as_string($subfields) if ($subfields);
1227 =head2 CompareFieldWithAuthority
1229 $match = &CompareFieldWithAuthority({ field => $field, authid => $authid })
1231 Takes a MARC::Field from a bibliographic record and an authid, and returns true if they match.
1235 sub CompareFieldWithAuthority {
1238 my $record = GetAuthority($args->{authid});
1239 return unless (ref $record eq 'MARC::Record');
1240 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
1241 # UNIMARC has same subfields for bibs and authorities
1242 foreach my $field ($record->field('2..')) {
1243 return compare_fields($field, $args->{field}, 'abcdefghijlmnopqrstuvwxyz');
1246 use C4::Heading::MARC21;
1247 my $handler = C4::Heading::MARC21->new();
1249 foreach my $field ($record->field('1..')) {
1250 my $subfields = $handler->get_valid_bib_heading_subfields($field->tag());
1251 return compare_fields($field, $args->{field}, $subfields) if ($subfields);
1257 =head2 BuildAuthHierarchies
1259 $text= &BuildAuthHierarchies( $authid, $force)
1261 return text containing trees for hierarchies
1262 for them to be stored in auth_header
1265 122,1314,2452;1324,2342,3,2452
1269 sub BuildAuthHierarchies{
1270 my $authid = shift @_;
1271 # warn "authid : $authid";
1272 my $force = shift @_ || (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 0 : 1);
1274 my $dbh=C4::Context->dbh;
1276 my $data = GetHeaderAuthority($authid);
1277 if ($data->{'authtrees'} and not $force){
1278 return $data->{'authtrees'};
1279 # } elsif ($data->{'authtrees'}){
1280 # $hierarchies=$data->{'authtrees'};
1282 my $record = GetAuthority($authid);
1284 return unless $record;
1285 foreach my $field ($record->field('5..')){
1288 (C4::Context->preference('marcflavour') eq 'UNIMARC' && $field->subfield('5') && $field->subfield('5') eq 'g') ||
1289 (C4::Context->preference('marcflavour') ne 'UNIMARC' && $field->subfield('w') && substr($field->subfield('w'), 0, 1) eq 'g'));
1291 my $subfauthid=_get_authid_subfield($field) || '';
1292 next if ($subfauthid eq $authid);
1293 my $parentrecord = GetAuthority($subfauthid);
1294 next unless $parentrecord;
1295 my $localresult=$hierarchies;
1297 $trees = BuildAuthHierarchies($subfauthid);
1300 @trees = split(/;/,$trees);
1302 push @trees, $trees;
1307 @globalresult = (@globalresult,@trees);
1310 $hierarchies=join(";",@globalresult);
1312 #Unless there is no ancestor, I am alone.
1313 $hierarchies="$authid" unless ($hierarchies);
1315 AddAuthorityTrees($authid,$hierarchies);
1316 return $hierarchies;
1319 =head2 BuildAuthHierarchy
1321 $ref= &BuildAuthHierarchy( $record, $class,$authid)
1323 return a hashref in order to display hierarchy for record and final Authid $authid
1334 sub BuildAuthHierarchy{
1335 my $record = shift @_;
1336 my $class = shift @_;
1337 my $authid_constructed = shift @_;
1338 return unless ($record && $record->field('001'));
1339 my $authid=$record->field('001')->data();
1341 my $parents=""; my $children="";
1342 my (@loopparents,@loopchildren);
1343 my $marcflavour = C4::Context->preference('marcflavour');
1344 my $relationshipsf = $marcflavour eq 'UNIMARC' ? '5' : 'w';
1345 foreach my $field ($record->field('5..')){
1346 my $subfauthid=_get_authid_subfield($field);
1347 if ($subfauthid && $field->subfield($relationshipsf) && $field->subfield('a')){
1348 my $relationship = substr($field->subfield($relationshipsf), 0, 1);
1349 if ($relationship eq 'h'){
1350 push @loopchildren, { "authid"=>$subfauthid,"value"=>$field->subfield('a')};
1352 elsif ($relationship eq 'g'){
1353 push @loopparents, { "authid"=>$subfauthid,"value"=>$field->subfield('a')};
1355 # brothers could get in there with an else
1358 $cell{"parents"}=\@loopparents;
1359 $cell{"children"}=\@loopchildren;
1360 $cell{"class"}=$class;
1361 $cell{"authid"}=$authid;
1362 $cell{"current_value"} =1 if ($authid eq $authid_constructed);
1363 $cell{"value"}=C4::Context->preference('marcflavour') eq 'UNIMARC' ? $record->subfield('2..',"a") : $record->subfield('1..', 'a');
1367 =head2 BuildAuthHierarchyBranch
1369 $branch = &BuildAuthHierarchyBranch( $tree, $authid[, $cnt])
1371 Return a data structure representing an authority hierarchy
1372 given a list of authorities representing a single branch in
1373 an authority hierarchy tree. $authid is the current node in
1374 the tree (which may or may not be somewhere in the middle).
1375 $cnt represents the level of the upper-most item, and is only
1376 used when BuildAuthHierarchyBranch is called recursively (i.e.,
1377 don't ever pass in anything but zero to it).
1381 sub BuildAuthHierarchyBranch {
1382 my ($tree, $authid, $cnt) = @_;
1384 my $elementdata = GetAuthority(shift @$tree);
1385 my $branch = BuildAuthHierarchy($elementdata,"child".$cnt, $authid);
1386 if (scalar @$tree > 0) {
1387 my $nextBranch = BuildAuthHierarchyBranch($tree, $authid, ++$cnt);
1388 my $nextAuthid = $nextBranch->{authid};
1390 # If we already have the next branch listed as a child, let's
1391 # replace the old listing with the new one. If not, we will add
1392 # the branch at the end.
1393 foreach my $cell (@{$branch->{children}}) {
1394 if ($cell->{authid} eq $nextAuthid) {
1395 $cell = $nextBranch;
1400 push @{$branch->{children}}, $nextBranch unless $found;
1405 =head2 GenerateHierarchy
1407 $hierarchy = &GenerateHierarchy($authid);
1409 Return an arrayref holding one or more "trees" representing
1410 authority hierarchies.
1414 sub GenerateHierarchy {
1416 my $trees = BuildAuthHierarchies($authid);
1417 my @trees = split /;/,$trees ;
1418 push @trees,$trees unless (@trees);
1419 my @loophierarchies;
1420 foreach my $tree (@trees){
1421 my @tree=split /,/,$tree;
1422 push @tree, $tree unless (@tree);
1423 my $branch = BuildAuthHierarchyBranch(\@tree, $authid);
1424 push @loophierarchies, [ $branch ];
1426 return \@loophierarchies;
1429 sub _get_authid_subfield{
1431 return $field->subfield('9')||$field->subfield('3');
1434 =head2 GetHeaderAuthority
1436 $ref= &GetHeaderAuthority( $authid)
1438 return a hashref in order auth_header table data
1442 sub GetHeaderAuthority{
1443 my $authid = shift @_;
1444 my $sql= "SELECT * from auth_header WHERE authid = ?";
1445 my $dbh=C4::Context->dbh;
1446 my $rq= $dbh->prepare($sql);
1447 $rq->execute($authid);
1448 my $data= $rq->fetchrow_hashref;
1452 =head2 AddAuthorityTrees
1454 $ref= &AddAuthorityTrees( $authid, $trees)
1456 return success or failure
1460 sub AddAuthorityTrees{
1461 my $authid = shift @_;
1462 my $trees = shift @_;
1463 my $sql= "UPDATE IGNORE auth_header set authtrees=? WHERE authid = ?";
1464 my $dbh=C4::Context->dbh;
1465 my $rq= $dbh->prepare($sql);
1466 return $rq->execute($trees,$authid);
1472 mergefrom => $mergefrom,
1473 [ MARCfrom => $MARCfrom, ]
1474 [ mergeto => $mergeto, ]
1475 [ MARCto => $MARCto, ]
1476 [ biblionumbers => [ $a, $b, $c ], ]
1477 [ override_limit => 1, ]
1480 Merge biblios linked to authority $mergefrom (mandatory parameter).
1481 If $mergeto equals mergefrom, the linked biblio field is updated.
1482 If $mergeto is different, the biblio field will be linked to $mergeto.
1483 If $mergeto is missing, the biblio field is deleted.
1485 MARCfrom is used to determine if a cleared subfield in the authority record
1486 should be removed from a biblio. MARCto is used to populate the biblio
1487 record with the updated values; if you do not pass it, the biblio field
1488 will be deleted (same as missing mergeto).
1490 Normally all biblio records linked to $mergefrom, will be considered. But
1491 you can pass specific numbers via the biblionumbers parameter.
1493 The parameter override_limit is used by the cron job to force larger
1496 Note: Although $mergefrom and $mergeto will normally be of the same
1497 authority type, merge also supports moving to another authority type.
1502 my ( $params ) = @_;
1503 my $mergefrom = $params->{mergefrom} || return;
1504 my $MARCfrom = $params->{MARCfrom};
1505 my $mergeto = $params->{mergeto};
1506 my $MARCto = $params->{MARCto};
1507 my $override_limit = $params->{override_limit};
1509 # If we do not have biblionumbers, we get all linked biblios if the
1510 # number of linked records does not exceed the limit UNLESS we override.
1512 if( $params->{biblionumbers} ) {
1513 @biblionumbers = @{ $params->{biblionumbers} };
1514 } elsif( $override_limit ) {
1515 @biblionumbers = Koha::Authorities->linked_biblionumbers({ authid => $mergefrom });
1516 } else { # now first check number of linked records
1517 my $max = C4::Context->preference('AuthorityMergeLimit') // 0;
1518 my $hits = Koha::Authorities->get_usage_count({ authid => $mergefrom });
1519 if( $hits > 0 && $hits <= $max ) {
1520 @biblionumbers = Koha::Authorities->linked_biblionumbers({ authid => $mergefrom });
1521 } elsif( $hits > $max ) { #postpone this merge to the cron job
1522 Koha::Authority::MergeRequest->new({
1523 authid => $mergefrom,
1524 oldrecord => $MARCfrom,
1525 authid_new => $mergeto,
1529 return 0 if !@biblionumbers;
1531 # Search authtypes and reporting tags
1532 my $authfrom = Koha::Authorities->find($mergefrom);
1533 my $authto = Koha::Authorities->find($mergeto);
1535 my $authtypeto = $authto ? Koha::Authority::Types->find($authto->authtypecode) : undef;
1536 if( $mergeto && $mergefrom == $mergeto && $MARCfrom ) {
1537 # bulkmarcimport may have changed the authtype; see BZ 19693
1538 my $old_type = $MARCfrom->subfield( get_auth_type_location() ); # going via default
1539 if( $old_type && $authto && $old_type ne $authto->authtypecode ) {
1540 # Type change: handled by simulating a postponed merge where the auth record has been deleted already
1541 # This triggers a walk through all auth controlled tags
1545 $authtypefrom = Koha::Authority::Types->find($authfrom->authtypecode) if $authfrom;
1546 my $auth_tag_to_report_from = $authtypefrom ? $authtypefrom->auth_tag_to_report : '';
1547 my $auth_tag_to_report_to = $authtypeto ? $authtypeto->auth_tag_to_report : '';
1550 @record_to = $MARCto->field($auth_tag_to_report_to)->subfields() if $auth_tag_to_report_to && $MARCto && $MARCto->field($auth_tag_to_report_to);
1551 # Exceptional: If MARCto and authtypeto exist but $auth_tag_to_report_to
1552 # is empty, make sure that $9 and $a remain (instead of clearing the
1553 # reference) in order to allow for data recovery.
1554 # Note: We need $a too, since a single $9 does not pass ModBiblio.
1555 if( $MARCto && $authtypeto && !@record_to ) {
1556 push @record_to, [ 'a', ' ' ]; # do not remove the space
1560 if( !$authfrom && $MARCfrom && $MARCfrom->field('1..','2..') ) {
1561 # postponed merge, authfrom was deleted and MARCfrom only contains the old reporting tag (and possibly a 100 for UNIMARC)
1562 # 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
1563 @record_from = ( $MARCfrom->field('1..','2..') )[-1]->subfields;
1564 } elsif( $auth_tag_to_report_from && $MARCfrom && $MARCfrom->field($auth_tag_to_report_from) ) {
1565 @record_from = $MARCfrom->field($auth_tag_to_report_from)->subfields;
1568 # Get all candidate tags for the change
1569 # (This will reduce the search scope in marc records).
1570 # For a deleted authority record, we scan all auth controlled fields
1571 my $dbh = C4::Context->dbh;
1572 my $sql = "SELECT DISTINCT tagfield FROM marc_subfield_structure WHERE authtypecode=?";
1573 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<>''" );
1575 if( $authtypeto && ( !$authtypefrom || $authtypeto->authtypecode ne $authtypefrom->authtypecode )) {
1576 $tags_new = $dbh->selectcol_arrayref( $sql, undef, ( $authtypeto->authtypecode ));
1579 my $overwrite = C4::Context->preference( 'AuthorityMergeMode' ) eq 'strict';
1580 my $skip_subfields = $overwrite
1581 # This hash contains all subfields from the authority report fields
1582 # Including $MARCfrom as well as $MARCto
1583 # We only need it in loose merge mode; replaces the former $exclude
1585 : { map { ( $_->[0], 1 ); } ( @record_from, @record_to ) };
1587 my $counteditedbiblio = 0;
1589 my $biblios = Koha::Biblios->search({ biblionumber => { -in => \@biblionumbers } });
1591 while ( my $biblio = $biblios->next ) {
1592 my $marcrecord = $biblio->metadata->record;
1594 foreach my $tagfield (@$tags_using_authtype) {
1595 my $countfrom = 0; # used in strict mode to remove duplicates
1596 foreach my $field ( $marcrecord->field($tagfield) ) {
1597 my $auth_number = $field->subfield("9"); # link to authority
1598 my $tag = $field->tag();
1599 next if !defined($auth_number) || $auth_number ne $mergefrom;
1601 if ( !$mergeto || !@record_to ||
1602 ( $overwrite && $countfrom > 1 ) ) {
1603 # !mergeto or !record_to indicates a delete
1604 # Other condition: remove this duplicate in strict mode
1605 $marcrecord->delete_field($field);
1609 my $newtag = $tags_new && @$tags_new
1610 ? _merge_newtag( $tag, $tags_new )
1612 my $controlled_ind = $authto->controlled_indicators({ record => $MARCto, biblio_tag => $newtag });
1613 my $field_to = MARC::Field->new(
1615 $controlled_ind->{ind1} // $field->indicator(1),
1616 $controlled_ind->{ind2} // $field->indicator(2),
1617 9 => $mergeto, # Needed to create field, will be moved
1619 my ( @prefix, @postfix );
1620 if ( !$overwrite ) {
1621 # add subfields back in loose mode, check skip_subfields
1622 # The first extra subfields will be in front of the
1623 # controlled block, the rest at the end.
1624 my $prefix_flag = 1;
1625 foreach my $subfield ( $field->subfields ) {
1626 next if $subfield->[0] eq '9'; # skip but leave flag
1627 if ( $skip_subfields->{ $subfield->[0] } ) {
1628 # This marks the beginning of the controlled block
1633 push @prefix, [ $subfield->[0], $subfield->[1] ];
1635 push @postfix, [ $subfield->[0], $subfield->[1] ];
1639 foreach my $subfield ( @prefix, @record_to, @postfix ) {
1640 $field_to->add_subfields($subfield->[0] => $subfield->[1]);
1642 if( exists $controlled_ind->{sub2} ) { # thesaurus info
1643 if( defined $controlled_ind->{sub2} ) {
1645 $field_to->update( 2 => $controlled_ind->{sub2} );
1647 # Key alerts us here to remove $2
1648 $field_to->delete_subfield( code => '2' );
1651 # Move $9 to the end
1652 $field_to->delete_subfield( code => '9' );
1653 $field_to->add_subfields( 9 => $mergeto );
1655 if ($tags_new && @$tags_new) {
1656 $marcrecord->delete_field($field);
1657 append_fields_ordered( $marcrecord, $field_to );
1659 $field->replace_with($field_to);
1665 ModBiblio( $marcrecord, $biblio->biblionumber, $biblio->frameworkcode, { disable_autolink => 1 } );
1666 $counteditedbiblio++;
1668 return $counteditedbiblio;
1672 # Routine is only called for an (exceptional) authtypecode change
1673 # Fixes old behavior of returning the first tag found
1674 my ( $oldtag, $new_tags ) = @_;
1676 # If we e.g. have 650 and 151,651,751 try 651 and check presence
1677 my $prefix = substr( $oldtag, 0, 1 );
1678 my $guess = $prefix . substr( $new_tags->[0], -2 );
1679 if( grep { $_ eq $guess } @$new_tags ) {
1682 # Otherwise return one from the same block e.g. 6XX for 650
1683 # If not there too, fall back to first new tag (old behavior!)
1684 my @same_block = grep { /^$prefix/ } @$new_tags;
1685 return @same_block ? $same_block[0] : $new_tags->[0];
1688 sub append_fields_ordered {
1689 # while we lack this function in MARC::Record
1690 # we do not want insert_fields_ordered since it inserts before
1691 my ( $record, $field ) = @_;
1692 if( my @flds = $record->field( $field->tag ) ) {
1693 $record->insert_fields_after( pop @flds, $field );
1694 } else { # now fallback to insert_fields_ordered
1695 $record->insert_fields_ordered( $field );
1699 =head2 get_auth_type_location
1701 my ($tag, $subfield) = get_auth_type_location($auth_type_code);
1703 Get the tag and subfield used to store the heading type
1704 for indexing purposes. The C<$auth_type> parameter is
1705 optional; if it is not supplied, assume ''.
1707 This routine searches the MARC authority framework
1708 for the tag and subfield whose kohafield is
1709 C<auth_header.authtypecode>; if no such field is
1710 defined in the framework, default to the hardcoded value
1711 specific to the MARC format.
1715 sub get_auth_type_location {
1716 my $auth_type_code = @_ ? shift : '';
1718 my ($tag, $subfield) = GetAuthMARCFromKohaField('auth_header.authtypecode', $auth_type_code);
1719 if (defined $tag and defined $subfield and $tag != 0 and $subfield ne '' and $subfield ne ' ') {
1720 return ($tag, $subfield);
1722 if (C4::Context->preference('marcflavour') eq "MARC21") {
1723 return C4::AuthoritiesMarc::MARC21::default_auth_type_location();
1725 return C4::AuthoritiesMarc::UNIMARC::default_auth_type_location();
1730 =head2 compare_fields
1732 my match = compare_fields($field1, $field2, 'abcde');
1734 Compares the listed subfields of both fields and return true if they all match
1738 sub compare_fields {
1739 my ($field1, $field2, $subfields) = @_;
1741 foreach my $subfield (split(//, $subfields)) {
1742 my $subfield1 = $field1->subfield($subfield) // '';
1743 my $subfield2 = $field2->subfield($subfield) // '';
1744 return 0 unless $subfield1 eq $subfield2;
1750 =head2 _after_authority_action_hooks
1752 Helper method that takes care of calling all plugin hooks
1756 sub _after_authority_action_hooks {
1757 my ( $args ) = @_; # hash keys: action, authority_id
1758 return Koha::Plugins->call( 'after_authority_action', $args );
1761 END { } # module clean-up code here (global destructor)
1768 Koha Development Team <http://koha-community.org/>
1770 Paul POULAIN paul.poulain@free.fr
1771 Ere Maijala ere.maijala@helsinki.fi