1 package C4::AuthoritiesMarc;
2 # Copyright 2000-2002 Katipo Communications
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA 02111-1307 USA
26 use vars qw($VERSION @ISA @EXPORT);
28 # set the version for version checking
35 &AUTHfind_authtypecode
41 &AUTHfind_marc_from_kohafield
48 &XML_readline_withtags
54 sub AUTHfind_marc_from_kohafield {
55 my ( $dbh, $kohafield,$authtypecode ) = @_;
56 return 0, 0 unless $kohafield;
57 $authtypecode="" unless $authtypecode;
58 my $marcfromkohafield;
59 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
60 $sth->execute($kohafield,$authtypecode);
61 my ($tagfield,$tagsubfield) = $sth->fetchrow;
62 return ($tagfield,$tagsubfield);
65 ## This routine requires rewrite--TG
66 my ($dbh, $tags, $operator, $value, $offset,$length,$authtypecode,$dictionary) = @_;
67 ###Dictionary flag used to set what to show in summary;
72 ##first set the authtype search and may be multiple authorities( linked authorities)
75 my @auths=split / /,$authtypecode ;
76 my ($attrfield)=MARCfind_attr_from_kohafield("authtypecode");
77 foreach my $auth (@auths){
78 $query .=$attrfield." ".$auth." "; ##No truncation on authtype
79 push @authtypecode ,$auth;
83 $query= "\@or ".$query;
89 for(my $i = 0 ; $i <= $#{$value} ; $i++)
93 ##If mainentry search $a tag
94 if (@$tags[$i] eq "mainentry") {
95 ($attr)=MARCfind_attr_from_kohafield("mainentry")." ";
97 ($attr) =MARCfind_attr_from_kohafield("allentry")." ";
99 if (@$operator[$i] eq 'phrase') {
100 $attr.=" \@attr 4=1 \@attr 5=100 \@attr 6=3 ";##Phrase, No truncation,all of subfield field must match
104 $attr .=" \@attr 4=6 \@attr 5=1 ";## Word list, right truncated, anywhere
109 $attr =$attr."\"".@$value[$i]."\"";
115 ##Add how many queries generated
116 $query= $and.$query.$q2;
119 $offset=0 unless $offset;
120 my $counter = $offset;
121 $length=10 unless $length;
124 $oAuth[0]=C4::Context->Zconnauth("authorityserver");
125 my ($mainentry)=MARCfind_attr_from_kohafield("mainentry");
126 my ($allentry)=MARCfind_attr_from_kohafield("allentry");
128 $query="\@attr 2=102 \@or \@or ".$query." \@attr 7=1 ".$mainentry." 0 \@attr 7=1 ".$allentry." 1"; ## sort on mainfield and subfields
132 $oAResult= $oAuth[0]->search_pqf($query) ;
133 while (($i = ZOOM::event(\@oAuth)) != 0) {
134 my $ev = $oAuth[$i-1]->last_event();
135 # warn("Authority ", $i-1, ": event $ev (", ZOOM::event_str($ev), ")\n");
136 last if $ev == ZOOM::Event::ZEND;
138 my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
140 warn "oAuth error: $errmsg ($error) $addinfo $diagset\n";
146 $nbresults=$oAResult->size();
147 my $nremains=$nbresults;
149 my @finalresult = ();
153 ##Find authid and linkid fields
156 while (($counter < $nbresults) && ($counter < ($offset + $length))) {
157 ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
158 my $rec=$oAResult->record($counter);
159 my $marcdata=$rec->raw();
160 my $authrecord=Encode::decode("utf8",$marcdata);
161 $authrecord=XML_xml2hash_onerecord($authrecord);
163 my $separator=C4::Context->preference('authoritysep');
164 my $linksummary=" ".$separator;
165 my $authid=XML_readline_onerecord($authrecord,"authid","authorities");
166 my @linkid=XML_readline_asarray($authrecord,"linkid","authorities");##May have many linked records
168 foreach my $linkid (@linkid){
169 my $linktype=AUTHfind_authtypecode($dbh,$linkid);
170 my $linkrecord=XMLgetauthorityhash($dbh,$linkid);
171 $linksummary.="<br> <a href='detail.pl?authid=$linkid'>".getsummary($dbh,$linkrecord,$linkid,$linktype).".</a>".$separator;
175 unless ($dictionary){
176 $summary=getsummary($dbh,$authrecord,$authid,$authtypecode);
177 $summary="<a href='detail.pl?authid=$authid'>".$summary.".</a>";
178 if ( $linksummary ne " ".$separator){
179 $summary="<b>".$summary."</b>".$linksummary;
182 $summary=getdictsummary($dbh,$authrecord,$authid,$authtypecode);
191 $newline{'toggle'}=$toggle;
192 $newline{summary} = $summary;
193 $newline{authid} = $authid;
194 $newline{linkid} = $linkid[0];
195 $newline{even} = $counter % 2;
197 push @finalresult, \%newline;
201 for (my $z=0; $z<$length; $z++){
202 $finalresult[$z]{used}=AUTHcount_usage($finalresult[$z]{authid});
209 $oAResult->destroy();
210 $oAuth[0]->destroy();
212 return (\@finalresult, $nbresults);
217 sub AUTHcount_usage {
219 ### try ZOOM search here
221 $oConnection[0]=C4::Context->Zconn("biblioserver");
223 my ($attrfield)=MARCfind_attr_from_kohafield("authid");
224 $query= $attrfield." ".$authid;
226 my $oResult = $oConnection[0]->search_pqf($query);
229 while (($i = ZOOM::event(\@oConnection)) != 0) {
230 $event = $oConnection[$i-1]->last_event();
231 last if $event == ZOOM::Event::ZEND;
233 my $result=$oResult->size() ;
239 sub AUTHfind_authtypecode {
240 my ($dbh,$authid) = @_;
241 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
242 $sth->execute($authid);
243 my ($authtypecode) = $sth->fetchrow;
244 return $authtypecode;
249 my ($dbh,$forlibrarian,$authtypecode)= @_;
250 $authtypecode="" unless $authtypecode;
252 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
255 # check that authority exists
256 $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
257 $sth->execute($authtypecode);
258 my ($total) = $sth->fetchrow;
259 $authtypecode="" unless ($total >0);
261 "select tagfield,liblibrarian,libopac,mandatory,repeatable from auth_tag_structure where authtypecode=? order by tagfield"
264 $sth->execute($authtypecode);
265 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
267 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
268 $res->{$tag}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
269 $res->{$tab}->{tab} = ""; # XXX
270 $res->{$tag}->{mandatory} = $mandatory;
271 $res->{$tag}->{repeatable} = $repeatable;
273 $sth= $dbh->prepare("select tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,authtypecode,value_builder,seealso,hidden,isurl,link from auth_subfield_structure where authtypecode=? order by tagfield,tagsubfield"
275 $sth->execute($authtypecode);
278 my $authorised_value;
288 ( $tag, $subfield, $liblibrarian, , $libopac, $tab,
289 $mandatory, $repeatable, $authorised_value, $authtypecode,
290 $value_builder, $seealso, $hidden,
295 $res->{$tag}->{$subfield}->{lib} = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
296 $res->{$tag}->{$subfield}->{tab} = $tab;
297 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
298 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
299 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
300 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
301 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
302 $res->{$tag}->{$subfield}->{seealso} = $seealso;
303 $res->{$tag}->{$subfield}->{hidden} = $hidden;
304 $res->{$tag}->{$subfield}->{isurl} = $isurl;
305 $res->{$tag}->{$subfield}->{link} = $link;
310 sub AUTHaddauthority {
311 # pass the XML hash to this function, and it will create the records in the authority table
312 my ($dbh,$record,$authid,$authtypecode) = @_;
313 # if authid empty => true add, find a new authid number
315 my $sth=$dbh->prepare("select max(authid) from auth_header");
317 ($authid)=$sth->fetchrow;
319 XML_writeline($record,"authid",$authid,"authorities");
320 XML_writeline($record,"authtypecode",$authtypecode,"authorities");
321 my $xml=XML_hash2xml($record);
322 $dbh->do("lock tables auth_header WRITE");
323 $sth=$dbh->prepare("insert into auth_header (authid,datecreated,authtypecode,marcxml) values (?,now(),?,?)");
324 $sth->execute($authid,$authtypecode,$xml);
328 ##Modified record may also come here use UPDATE -- bulk import comes here
329 XML_writeline($record,"authid",$authid,"authorities");
330 XML_writeline($record,"authtypecode",$authtypecode,"authorities");
331 my $xml=XML_hash2xml($record);
332 my $sth=$dbh->prepare("UPDATE auth_header set marcxml=?,authtypecode=? where authid=?");
333 $sth->execute($xml,$authtypecode,$authid);
336 ZEBRAop($dbh,$authid,'specialUpdate',"authorityserver");
337 ## If the record is linked to another update the linked authorities with new authid
338 my @linkids=XML_readline_asarray($record,"linkid","authorities");
339 foreach my $linkid (@linkids){
340 ##Modify the record of linked
341 AUTHaddlink($dbh,$linkid,$authid);
347 my ($dbh,$linkid,$authid)=@_;
348 my $record=XMLgetauthorityhash($dbh,$linkid);
349 my $authtypecode=AUTHfind_authtypecode($dbh,$linkid);
350 #warn "adding l:$linkid,a:$authid,auth:$authtypecode";
351 XML_writeline($record,"linkid",$authid,"authorities");
352 my $xml=XML_hash2xml($record);
353 $dbh->do("lock tables header WRITE");
354 my $sth=$dbh->prepare("update auth_header set marcxml=? where authid=?");
355 $sth->execute($xml,$linkid);
357 $dbh->do("unlock tables");
358 ZEBRAop($dbh,$linkid,'specialUpdate',"authorityserver");
363 sub XMLgetauthority {
364 # Returns MARC::XML of the authority passed in parameter.
365 my ( $dbh, $authid ) = @_;
366 my $sth = $dbh->prepare("select marcxml from auth_header where authid=? " );
367 $sth->execute($authid);
368 my ($marcxml)=$sth->fetchrow;
369 $marcxml=Encode::decode('utf8',$marcxml);
373 sub XMLgetauthorityhash {
374 ## Utility to return hashed MARCXML
375 my ($dbh,$authid)=@_;
376 my $xml=XMLgetauthority($dbh,$authid);
377 my $xmlhash=XML_xml2hash_onerecord($xml);
384 sub AUTHgetauth_type {
385 my ($authtypecode) = @_;
386 my $dbh=C4::Context->dbh;
387 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
388 $sth->execute($authtypecode);
389 return $sth->fetchrow_hashref;
393 sub AUTHmodauthority {
394 ## $record is expected to be an xmlhash
395 my ($dbh,$authid,$record,$authtypecode)=@_;
396 my ($oldrecord)=&XMLgetauthorityhash($dbh,$authid);
397 ### This equality is very dodgy ,It porobaby wont work
398 if ($oldrecord eq $record) {
402 my $sth=$dbh->prepare("update auth_header set marcxml=? where authid=?");
403 # find if linked records exist and delete the link in them
404 my @linkids=XML_readline_asarray($oldrecord,"linkid","authorities");
406 foreach my $linkid (@linkids){
407 ##Modify the record of linked
408 my $linkrecord=XMLgetauthorityhash($dbh,$linkid);
409 my $linktypecode=AUTHfind_authtypecode($dbh,$linkid);
410 my @linkfields=XML_readline_asarray($linkrecord,"linkid","authorities");
412 foreach my $linkfield (@linkfields){
413 if ($linkfield eq $authid){
414 XML_writeline_id($linkrecord,"linkid",$linkfield,"","authorities");
418 my $linkedxml=XML_hash2xml($linkrecord);
420 $sth->execute($linkedxml,$linkid);
421 ZEBRAop($dbh,$linkid,'specialUpdate',"authorityserver");
426 #Now rewrite the $record to table with an add
427 $authid=AUTHaddauthority($dbh,$record,$authid,$authtypecode);
430 ### If a library thinks that updating all biblios is a long process and wishes to leave that to a cron job to use merge_authotities.pl
431 ### they should have a system preference "dontmerge=1" otherwise by default biblios will be updated
433 if (C4::Context->preference('dontmerge') ){
434 # save the file in localfile/modified_authorities
435 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
436 unless (opendir(DIR, "$cgidir")) {
437 $cgidir = C4::Context->intranetdir."/";
440 my $filename = $cgidir."/localfile/modified_authorities/$authid.authid";
441 open AUTH, "> $filename";
445 &merge($dbh,$authid,$record,$authid,$record);
450 sub AUTHdelauthority {
451 my ($dbh,$authid,$keep_biblio) = @_;
453 # if the keep_biblio is set to 1, then authority entries in biblio are preserved.
454 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag) is not implemented
455 ZEBRAop($dbh,$authid,"recordDelete","authorityserver");
458 sub ZEBRAdelauthority {
459 my ($dbh,$authid)=@_;
460 $dbh->do("delete from auth_header where authid=$authid") ;
463 sub AUTHfind_authtypecode {
464 my ($dbh,$authid) = @_;
465 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
466 $sth->execute($authid);
467 my ($authtypecode) = $sth->fetchrow;
468 return $authtypecode;
472 sub FindDuplicateauth {
473 ### Should receive an xmlhash
474 my ($record,$authtypecode)=@_;
475 my $dbh = C4::Context->dbh;
476 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
477 $sth->execute($authtypecode);
478 my ($auth_tag_to_report) = $sth->fetchrow;
480 # build a request for authoritysearch
481 my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
483 # if ($record->field($auth_tag_to_report)) {
484 push @tags, $auth_tag_to_report;
485 push @operator, "all";
486 @value, XML_readline_asarray($record,"","",$auth_tag_to_report);
489 my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
490 # there is at least 1 result => return the 1st one
492 return @$finalresult[0]->{authid},@$finalresult[0]->{summary};
494 # no result, returns nothing
499 ## give this an XMLhash record to return summary
500 my ($dbh,$record,$authid,$authtypecode)=@_;
501 my $authref = getauthtype($authtypecode);
502 my $summary = $authref->{summary};
503 # if the library has a summary defined, use it. Otherwise, build a standard one
505 my $fields = $record->{'datafield'};
506 foreach my $field (@$fields) {
507 my $tag = $field->{'tag'};
509 my $tagvalue = XML_readline_onerecord($record,"","",$field->{tag});
510 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
512 my @subf = XML_readline_withtags($record,"","",$tag);
513 for my $i (0..$#subf) {
514 my $subfieldcode = $subf[$i][0];
515 my $subfieldvalue = $subf[$i][1];
516 my $tagsubf = $tag.$subfieldcode;
517 $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
521 $summary =~ s/\[(.*?)]//g;
522 $summary =~ s/\n/<br>/g;
524 ## $summary did not exist create a standard summary
525 my $heading; # = $authref->{summary};
529 my $fields = $record->{datafield};
530 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
531 # construct UNIMARC summary, that is quite different from MARC21 one
532 foreach my $field (@$fields) {
534 if ($field->{tag} = ~/'2..'/) {
535 foreach my $subfield ("a".."z"){
536 ## Fixme-- if UNICODE uses numeric subfields as well add them
537 $heading.=XML_readline_onerecord($record,"","",$field->{tag},$subfield);
541 if ($field->{tag} = ~/'4..'/) {
543 foreach my $subfield ("a".."z"){
544 ## Fixme-- if UNICODE uses numeric subfields as well add them
545 $value.=XML_readline_onerecord($record,"","",$field->{tag},$subfield);
547 $summary.= " <i>".$value."</i><br/>";
548 $summary.= " <i>see:</i> ".$heading."<br/>";
551 if ($field->{tag} = ~/'5..'/) {
553 foreach my $subfield ("a".."z"){
554 ## Fixme-- if UNICODE uses numeric subfields as well add them
555 $value.=XML_readline_onerecord($record,"","",$field->{tag},$subfield);
557 $summary.= " <i>".$value."</i><br/>";
558 $summary.= " <i>see:</i> ".$heading."<br/>";
561 if ($field->{tag} = ~/'7..'/) {
563 foreach my $subfield ("a".."z"){
564 ## Fixme-- if UNICODE uses numeric subfields as well add them
565 $value.=XML_readline_onerecord($record,"","",$field->{tag},$subfield);
567 $seeheading.= " <i>see also:</i> ".$value."<br />";
568 $altheading.= " ".$value."<br />";
569 $altheading.= " <i>see also:</i> ".$heading."<br />";
572 $summary = "<b>".$heading."</b><br />".$seeheading.$altheading.$summary;
574 # construct MARC21 summary
575 foreach my $field (@$fields) {
577 if($field->{tag} =~ /^$tag/) {
578 if ($field->{tag} eq '150') {
580 foreach my $subfield ("a".."z"){
581 $value=XML_readline_onerecord($record,"","","150",$subfield);
582 $heading.="\$".$subfield.$value if $value;
585 foreach my $subfield ("a".."z"){
586 $heading.=XML_readline_onerecord($record,"","",$field->{tag},$subfield);
591 if($field->{tag} =~ /^$tag/) {
592 foreach my $subfield ("a".."z"){
593 $seeheading.=XML_readline_onerecord($record,"","",$field->{tag},$subfield);
595 $seeheading.= " ".$seeheading."<br />";
596 $seeheading.= " <i>see:</i> ".$seeheading."<br />";
599 if($field->{tag} =~ /^$tag/) {
601 foreach my $subfield ("a".."z"){
602 $value.=XML_readline_onerecord($record,"","",$field->{tag},$subfield);
604 $seeheading.= " <i>see also:</i> ".$value."<br />";
605 $altheading.= " ".$value."<br />";
606 $altheading.= " <i>see also:</i> ".$altheading."<br />";
610 $summary.=$heading.$seeheading.$altheading;
612 }###Summary exists or not
616 ## give this a XML record to return a brief summary
617 my ($dbh,$record,$authid,$authtypecode)=@_;
618 my $authref = getauthtype($authtypecode);
619 my $summary = $authref->{summary};
620 my $fields = $record->{'datafield'};
621 # if the library has a summary defined, use it. Otherwise, build a standard one
623 foreach my $field (@$fields) {
624 my $tag = $field->{'tag'};
626 my $tagvalue = XML_readline_onerecord($record,"","",$field->{tag});
627 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
629 my @subf = XML_readline_withtags($record,"","",$tag);
630 for my $i (0..$#subf) {
631 my $subfieldcode = $subf[$i][0];
632 my $subfieldvalue = $subf[$i][1];
633 my $tagsubf = $tag.$subfieldcode;
634 $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
638 $summary =~ s/\[(.*?)]//g;
639 $summary =~ s/\n/<br>/g;
641 my $heading; # = $authref->{summary};
645 my @fields = $record->{datafields};
646 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
647 # construct UNIMARC summary, that is quite different from MARC21 one
648 foreach my $field (@$fields) {
650 if ($field->{tag} = ~/'2..'/) {
651 foreach my $subfield ("a".."z"){
652 ## Fixme-- if UNICODE uses numeric subfields as well add them
653 $heading.=XML_readline_onerecord($record,"","",$field->{tag},$subfield);
659 # construct MARC21 summary
660 foreach my $field (@fields) {
661 if ($field->{tag}=~/'1..'/){
662 $heading.= XML_readline_onerecord($record,"","",$field->{tag},"a");
674 ##mergefrom is authid MARCfrom is marcxml hash of authority
676 my ($dbh,$mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
677 return unless (defined $MARCfrom);
678 return unless (defined $MARCto);
679 my $authtypecodefrom = AUTHfind_authtypecode($dbh,$mergefrom);
680 my $authtypecodeto = AUTHfind_authtypecode($dbh,$mergeto);
681 # return if authority does not exist
683 # search the tag to report
684 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
685 $sth->execute($authtypecodefrom);
686 my ($auth_tag_to_report) = $sth->fetchrow;
688 # search all biblio tags using this authority.
689 $sth = $dbh->prepare("select distinct tagfield from biblios_subfield_structure where authtypecode=? ");
690 $sth->execute($authtypecodefrom);
691 my @tags_using_authtype;
692 while (my ($tagfield) = $sth->fetchrow) {
693 push @tags_using_authtype,$tagfield ;
695 ## The subfield for linking authorities is stored in koha_attr named auth_biblio_link_subf
696 ## This way we may use whichever subfield we want without harcoding 9 in
697 my ($dummyfield,$tagsubfield)=MARCfind_marc_from_kohafield("auth_biblio_link_subf","biblios");
698 # now, find every biblio using this authority
699 ### try ZOOM search here
701 $oConnection[0]=C4::Context->Zconn("biblioserver");
702 ##$oConnection[0]->option(elementSetName=>"biblios"); ## Needs a fix
704 my ($attr2)=MARCfind_attr_from_kohafield("authid");
705 my $attrfield.=$attr2;
706 $query= $attrfield." ".$mergefrom;
708 my $oResult = $oConnection[0]->search_pqf($query);
709 while (($i = ZOOM::event(\@oConnection)) != 0) {
710 $event = $oConnection[$i-1]->last_event();
711 last if $event == ZOOM::Event::ZEND;
713 my $count=$oResult->size();
716 while ( $z<$count ) {
718 $rec=$oResult->record($z);
719 my $marcdata = $rec->raw();
720 my $koharecord=Encode::decode("utf8",$marcdata);
721 $koharecord=XML_xml2hash($koharecord);
722 my ( $xmlrecord, @itemsrecord) = XML_separate($koharecord);
724 push @reccache, $xmlrecord;
728 $oConnection[0]->destroy();
729 foreach my $xmlhash (@reccache){
731 foreach my $tagfield (@tags_using_authtype){
733 ###Change the authid in biblio
734 $xmlhash=XML_writeline_id($xmlhash,$mergefrom,$mergeto,$tagfield,$tagsubfield);
735 ### delete all subfields of bibliorecord
736 $xmlhash=XML_delete_withid($xmlhash,$mergeto,$tagfield,$tagsubfield);
737 ####Read all the data in from authrecord
738 my @record_to=XML_readline_withtags($MARCto,"","",$auth_tag_to_report);
739 ##Write the data to biblio
740 foreach my $subfield (@record_to) {
741 ## Replace the data in MARCXML with the new matching authid
742 XML_writeline_withid($xmlhash,$tagsubfield,$mergeto,$subfield->[1],$tagfield,$subfield->[0]);
747 my $biblionumber=XML_readline_onerecord($xmlhash,"biblionumber","biblios");
748 my $frameworkcode=MARCfind_frameworkcode($dbh,$biblionumber);
749 NEWmodbiblio($dbh,$biblionumber,$xmlhash,$frameworkcode) ;
755 sub XML_writeline_withid{
756 ## Only used in authorities to update biblios with matching authids
757 my ($xml,$idsubf,$id,$newvalue,$tag,$subf)=@_;
758 my $biblio=$xml->{'datafield'};
761 foreach my $data (@$biblio){
762 if ($data->{'tag'} eq $tag){
763 my @subfields=$data->{'subfield'};
764 foreach my $subfield ( @subfields){
765 foreach my $code ( @$subfield){
766 if ($code->{'code'} eq $idsubf && $code->{'content'} eq $id){
767 ###This is the correct tag -- Now reiterate and update
769 foreach my $code ( @$subfield){
770 if ($code->{'code'} eq $subf ){
771 $code->{'content'}=$newvalue;
774 push @newsubs, $code;
775 }## each code updated
777 ##Create the subfield if it did not exist
778 push @newsubs,{code=>$subf,content=>$newvalue};
779 $data->{subfield}= \@newsubs;
782 }### correct tag with id
790 sub XML_delete_withid{
791 ## Currently only usedin authorities
792 ### deletes all the subfields of a matching authid
793 my ($xml,$id,$tag,$idsubf)=@_;
794 my $biblio=$xml->{'datafield'};
796 foreach my $data (@$biblio){
797 if ($data->{'tag'} eq $tag){
798 my @subfields=$data->{'subfield'};
799 foreach my $subfield ( @subfields){
800 foreach my $code ( @$subfield){
801 if ($code->{'code'} eq $idsubf && $code->{'content'} eq $id){
802 ###This is the correct tag -- Now reiterate and delete all but id subfield
803 foreach my $code ( @$subfield){
804 if ($code->{'code'} ne $idsubf ){
805 $code->{'content'}="";
807 }## each code deleted
808 }### correct tag with id
817 sub XML_readline_withtags {
818 my ($xml,$kohafield,$recordtype,$tag,$subf)=@_;
819 #$xml represents one record of MARCXML as perlhashed
820 ## returns an array of read fields--useful for reading repeated fields
821 ### $recordtype is needed for mapping the correct field if supplied
822 ### If only $tag is give reads the whole tag
823 ###Returns subfieldcodes as well
825 ($tag,$subf)=MARCfind_marc_from_kohafield($kohafield,$recordtype) if $kohafield;
827 ### Only datafields are read
828 my $biblio=$xml->{'datafield'};
830 foreach my $data (@$biblio){
831 if ($data->{'tag'} eq $tag){
832 foreach my $subfield ( $data->{'subfield'}){
833 foreach my $code ( @$subfield){
834 if ($code->{'code'} eq $subf || !$subf){
835 push @value,[$code->{'code'},$code->{'content'}];
846 END { } # module clean-up code here (global destructor)
852 Koha Developement team <info@koha.org>
854 Paul POULAIN paul.poulain@free.fr
860 # Revision 1.30 2006/09/06 16:21:03 tgarip1957
861 # Clean up before final commits
863 # Revision 1.9.2.6 2005/06/07 10:02:00 tipaul
864 # porting dictionnary search from head to 2.2. there is now a ... facing titles, author & subject, to search in biblio & authorities existing values.
866 # Revision 1.9.2.5 2005/05/31 14:50:46 tipaul
867 # fix for authority merging. There was a bug on official installs
869 # Revision 1.9.2.4 2005/05/30 11:24:15 tipaul
870 # fixing a bug : when a field was repeated, the last field was also repeated. (Was due to the "empty" field in html between fields : to separate fields, in html, an empty field is automatically added. in AUTHhtml2marc, this empty field was not discarded correctly)
872 # Revision 1.9.2.3 2005/04/28 08:45:33 tipaul
873 # porting FindDuplicate feature for authorities from HEAD to rel_2_2, works correctly now.
875 # Revision 1.9.2.2 2005/02/28 14:03:13 tipaul
876 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
877 # * adding a select box to requet "contain" or "begin with" search.
878 # * fixing some bug in authority search (related to "main entry" search)
880 # Revision 1.9.2.1 2005/02/24 13:12:13 tipaul
881 # saving authority modif in a text file. This will be used soon with another script (in crontab). The script in crontab will retrieve every authorityid in the directory localfile/authorities and modify every biblio using this authority. Those modifs may be long. So they can't be done through http, because we may encounter a webserver timeout, and kill the process before end of the job.
882 # So, it will be done through a cron job.
883 # (/me agree we need some doc for command line scripts)
885 # Revision 1.9 2004/12/23 09:48:11 tipaul
886 # Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
888 # Revision 1.8 2004/11/05 10:11:39 tipaul
889 # export auth_count_usage (bugfix)
891 # Revision 1.7 2004/09/23 16:13:00 tipaul
892 # Bugfix in modification
894 # Revision 1.6 2004/08/18 16:00:24 tipaul
895 # fixes for authorities management
897 # Revision 1.5 2004/07/05 13:37:22 doxulting
898 # First step for working authorities
900 # Revision 1.4 2004/06/22 11:35:37 tipaul
901 # removing % at the beginning of a string to avoid loooonnnngggg searchs
903 # Revision 1.3 2004/06/17 08:02:13 tipaul
904 # merging tag & subfield in auth_word for better perfs
906 # Revision 1.2 2004/06/10 08:29:01 tipaul
907 # MARC authority management (continued)
909 # Revision 1.1 2004/06/07 07:35:01 tipaul
910 # MARC authority management package