Clean up before final commits
[koha.git] / C4 / AuthoritiesMarc.pm
1 package C4::AuthoritiesMarc;
2 # Copyright 2000-2002 Katipo Communications
3 #
4 # This file is part of Koha.
5 #
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
9 # version.
10 #
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.
14 #
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
18
19 use strict;
20 require Exporter;
21 use C4::Context;
22 use C4::Koha;
23 use Encode;
24 use C4::Biblio;
25
26 use vars qw($VERSION @ISA @EXPORT);
27
28 # set the version for version checking
29 $VERSION = 0.01;
30
31 @ISA = qw(Exporter);
32 @EXPORT = qw(
33         &AUTHgettagslib
34         &AUTHfindsubfield
35         &AUTHfind_authtypecode
36         &AUTHaddauthority
37         &AUTHmodauthority
38         &AUTHdelauthority
39         &AUTHaddsubfield
40
41         &AUTHfind_marc_from_kohafield
42         &AUTHgetauth_type
43         &AUTHcount_usage
44         &getsummary
45         &authoritysearch
46         &XMLgetauthority
47         &XMLgetauthorityhash
48         &XML_readline_withtags
49         &merge
50         &FindDuplicateauth
51         &ZEBRAdelauthority
52  );
53
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);
63 }
64 sub authoritysearch {
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;
68         my $query;
69         my $attr;
70         my $server;
71         my $mainentrytag;
72         ##first set the authtype search and may be multiple authorities( linked authorities)
73         my $n=0;
74         my @authtypecode;
75                                 my @auths=split / /,$authtypecode ;
76                                 my ($attrfield)=MARCfind_attr_from_kohafield("auth_authtypecode");
77                                 foreach my  $auth (@auths){
78                                 $query .=$attrfield." ".$auth." "; ##No truncation on authtype
79                                 push @authtypecode ,$auth;
80                                 $n++;
81                                 }
82                         if ($n>1){
83                          $query= "\@or ".$query;
84                         }
85         
86         my $dosearch;
87         my $and;
88         my $q2;
89         for(my $i = 0 ; $i <= $#{$value} ; $i++)
90         {
91
92         if (@$value[$i]){
93         ##If mainentry search $a tag
94                 if (@$tags[$i] eq "mainentry") {
95                  ($attr)=MARCfind_attr_from_kohafield("auth_mainentry")." ";            
96                 }else{
97                 ($attr) =MARCfind_attr_from_kohafield("auth_allentry")." ";
98                 }
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
101                 
102                 } else {
103                 
104                          $attr .=" \@attr 4=6  \@attr 5=1  ";## Word list, right truncated, anywhere
105                 }                
106         
107                 
108                 $and .=" \@and " ;
109                 $attr =$attr."\"".@$value[$i]."\"";
110                 $q2 .=$attr;
111         $dosearch=1;            
112         }#if value              
113                 
114         }## value loop
115 ##Add how many queries generated
116 $query= $and.$query.$q2;
117 #warn $query;
118
119 $offset=0 unless $offset;
120 my $counter = $offset;
121 $length=10 unless $length;
122 my @oAuth;
123 my $i;
124  $oAuth[0]=C4::Context->Zconnauth("authorityserver");
125 my ($mainentry)=MARCfind_attr_from_kohafield("auth_mainentry");
126 my ($allentry)=MARCfind_attr_from_kohafield("auth_allentry");
127
128 $query="\@attr 2=102 \@or \@or ".$query." \@attr 7=1 ".$mainentry." 0 \@attr 7=1 ".$allentry." 1"; ## sort on mainfield and subfields
129
130
131 my $oAResult;
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;
137 }
138  my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
139     if ($error) {
140         warn  "oAuth error: $errmsg ($error) $addinfo $diagset\n";
141         goto NOLUCK;
142     }
143
144
145 my $nbresults;
146  $nbresults=$oAResult->size();
147 my $nremains=$nbresults;        
148         my @result = ();
149         my @finalresult = ();
150
151 if ($nbresults>0){
152
153 ##Find authid and linkid fields
154
155
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);                
162 my @linkids;    
163 my $separator=C4::Context->preference('authoritysep');
164 my $linksummary=" ".$separator; 
165 my $authid=XML_readline_onerecord($authrecord,"auth_authid","authorities");     
166 my @linkid=XML_readline_asarray($authrecord,"auth_linkid","authorities");##May have many linked records 
167         
168         foreach my $linkid (@linkid){
169                 my $linktype=AUTHfind_authtypecode($dbh,$linkid);
170 #               my $linkrecord=XMLgetauthorityhash($dbh,$linkid);
171 #               $linksummary.="<br>&nbsp;&nbsp;&nbsp;&nbsp;<a href='detail.pl?authid=$linkid'>".getsummary($dbh,$linkrecord,$linkid,$linktype).".</a>".$separator;
172                 
173         }
174 my  $summary;
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;
180         }
181 }else{
182  $summary=getdictsummary($dbh,$authrecord,$authid,$authtypecode);
183 }
184 my $toggle;
185         if ($counter % 2) {
186                 $toggle="#ffffcc";
187         } else {
188                 $toggle="white";
189         }
190 my %newline;
191         $newline{'toggle'}=$toggle;     
192         $newline{summary} = $summary;
193         $newline{authid} = $authid;
194         $newline{linkid} = $linkid[0];
195         $newline{even} = $counter % 2;
196         $counter++;
197         push @finalresult, \%newline;
198         }## while counter
199
200
201 for (my $z=0; $z<$length; $z++){
202                 $finalresult[$z]{used}=AUTHcount_usage($finalresult[$z]{authid});
203         
204  }# all $z's
205
206
207 }## if nbresult
208 NOLUCK:
209 $oAResult->destroy();
210 $oAuth[0]->destroy();
211
212         return (\@finalresult, $nbresults);
213 }
214
215
216
217 sub AUTHcount_usage {
218         my ($authid) = @_;
219 ### try ZOOM search here
220 my @oConnection;
221 $oConnection[0]=C4::Context->Zconn("biblioserver");
222 my $query;
223 my ($attrfield)=MARCfind_attr_from_kohafield("auth_authid");
224 $query= $attrfield." ".$authid;
225
226 my $oResult = $oConnection[0]->search_pqf($query);
227 my $event;
228 my $i;
229    while (($i = ZOOM::event(\@oConnection)) != 0) {
230         $event = $oConnection[$i-1]->last_event();
231         last if $event == ZOOM::Event::ZEND;
232    }# while
233 my $result=$oResult->size() ;
234         return ($result);
235 }
236
237
238
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;
245 }
246  
247
248 sub AUTHgettagslib {
249         my ($dbh,$forlibrarian,$authtypecode)= @_;
250         $authtypecode="" unless $authtypecode;
251         my $sth;
252         my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
253
254
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);
260         $sth= $dbh->prepare(
261 "select tagfield,liblibrarian,libopac,mandatory,repeatable from auth_tag_structure where authtypecode=? order by tagfield"
262     );
263
264 $sth->execute($authtypecode);
265          my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
266
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;
272     }
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"
274     );
275         $sth->execute($authtypecode);
276
277          my $subfield;
278     my $authorised_value;
279     my $authtypecode;
280     my $value_builder;
281     my $kohafield;
282     my $seealso;
283     my $hidden;
284     my $isurl;
285         my $link;
286
287     while (
288         ( $tag,         $subfield,   $liblibrarian,   , $libopac,      $tab,
289         $mandatory,     $repeatable, $authorised_value, $authtypecode,
290         $value_builder,   $seealso,          $hidden,
291         $isurl,                 $link )
292         = $sth->fetchrow
293       )
294     {
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;
306     }
307     return $res;
308 }
309
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
314         if (!$authid) {
315         my      $sth=$dbh->prepare("select max(authid) from auth_header");
316                 $sth->execute;
317                 ($authid)=$sth->fetchrow;
318                 $authid=$authid+1;
319         }       
320
321 ##Modified record may also come here use REPLACE -- bulk import comes here
322 XML_writeline($record,"auth_authid",$authid,"authorities");
323 XML_writeline($record,"auth_authtypecode",$authtypecode,"authorities");
324 my $xml=XML_hash2xml($record);
325         my $sth=$dbh->prepare("REPLACE auth_header set marcxml=?  authid=?,authtypecode=?,datecreated=now()");
326         $sth->execute($xml,$authid,$authtypecode);
327         $sth->finish;
328         
329         
330         ZEBRAop($dbh,$authid,'specialUpdate',"authorityserver");
331 ## If the record is linked to another update the linked authorities with new authid
332 my @linkids=XML_readline_asarray($record,"auth_linkid","authorities");
333         foreach my $linkid (@linkids){
334         ##Modify the record of linked 
335         AUTHaddlink($dbh,$linkid,$authid);
336         }
337 return ($authid);
338 }
339
340 sub AUTHaddlink{
341 my ($dbh,$linkid,$authid)=@_;
342 my $record=XMLgetauthorityhash($dbh,$linkid);
343 my $authtypecode=AUTHfind_authtypecode($dbh,$linkid);
344 #warn "adding l:$linkid,a:$authid,auth:$authtypecode";
345 XML_writeline($record,"auth_linkid",$authid,"authorities");
346 my $xml=XML_hash2xml($record);
347 $dbh->do("lock tables auth_header WRITE");
348         my $sth=$dbh->prepare("update auth_header set marcxml=? where authid=?");
349         $sth->execute($xml,$linkid);
350         $sth->finish;   
351         $dbh->do("unlock tables");
352         ZEBRAop($dbh,$linkid,'specialUpdate',"authorityserver");
353 }
354
355
356
357 sub XMLgetauthority {
358     # Returns MARC::XML of the authority passed in parameter.
359     my ( $dbh, $authid ) = @_;
360     my $sth =  $dbh->prepare("select marcxml from auth_header where authid=? "  );
361     $sth->execute($authid);
362  my ($marcxml)=$sth->fetchrow;
363         $marcxml=Encode::decode('utf8',$marcxml);
364  return ($marcxml);
365 }
366
367 sub XMLgetauthorityhash {
368 ## Utility to return  hashed MARCXML
369 my ($dbh,$authid)=@_;
370 my $xml=XMLgetauthority($dbh,$authid);
371 my $xmlhash=XML_xml2hash_onerecord($xml);
372 return $xmlhash;
373 }
374
375
376
377
378 sub AUTHgetauth_type {
379         my ($authtypecode) = @_;
380         my $dbh=C4::Context->dbh;
381         my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
382         $sth->execute($authtypecode);
383         return $sth->fetchrow_hashref;
384 }
385
386
387 sub AUTHmodauthority {
388 ## $record is expected to be an xmlhash
389         my ($dbh,$authid,$record,$authtypecode)=@_;
390         my ($oldrecord)=&AUTHgetauthorityhash($dbh,$authid);
391 ### This equality is very dodgy ,It porobaby wont work
392         if ($oldrecord eq $record) {
393                 return;
394         }
395 ##
396 my $sth=$dbh->prepare("update auth_header set marcxml=? where authid=?");
397 # find if linked records exist and delete the link in them
398 my @linkids=XML_readline_asarray($oldrecord,"auth_linkid","authorities");
399
400         foreach my $linkid (@linkids){
401                 ##Modify the record of linked 
402                 my $linkrecord=AUTHgetauthorityhash($dbh,$linkid);
403                 my $linktypecode=AUTHfind_authtypecode($dbh,$linkid);
404                 my @linkfields=XML_readline_asarray($linkrecord,"auth_linkid","authorities");
405                 my $updated;
406                        foreach my $linkfield (@linkfields){
407                         if ($linkfield eq $authid){
408                                 XML_writeline_id($linkrecord,"auth_linkid",$linkfield,"","authorities");
409                                 $updated=1;
410                         }
411                        }#foreach linkfield
412                         my $linkedxml=XML_hash2xml($linkrecord);
413                         if ($updated==1){
414                         $sth->execute($linkedxml,$linkid);
415                         ZEBRAop($dbh,$linkid,'specialUpdate',"authorityserver");
416                         }
417         
418         }#foreach linkid
419
420 #Now rewrite the $record to table with an add
421 $authid=AUTHaddauthority($dbh,$record,$authid,$authtypecode);
422
423
424 ### 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
425 ### they should have a system preference "dontmerge=1" otherwise by default biblios will be updated
426
427 if (C4::Context->preference('dontmerge') ){
428 # save the file in localfile/modified_authorities
429         my $cgidir = C4::Context->intranetdir ."/cgi-bin";
430         unless (opendir(DIR, "$cgidir")) {
431                         $cgidir = C4::Context->intranetdir."/";
432         } 
433
434         my $filename = $cgidir."/localfile/modified_authorities/$authid.authid";
435         open AUTH, "> $filename";
436         print AUTH $authid;
437         close AUTH;
438 }else{
439         &merge($dbh,$authid,$record,$authid,$record);
440 }
441 return $authid;
442 }
443
444 sub AUTHdelauthority {
445         my ($dbh,$authid,$keep_biblio) = @_;
446
447 # if the keep_biblio is set to 1, then authority entries in biblio are preserved.
448 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag) is not implemented
449 ZEBRAop($dbh,$authid,"recordDelete","authorityserver");
450 }
451
452 sub ZEBRAdelauthority {
453 my ($dbh,$authid)=@_;
454         $dbh->do("delete from auth_header where authid=$authid") ;
455 }
456
457 sub AUTHfind_authtypecode {
458         my ($dbh,$authid) = @_;
459         my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
460         $sth->execute($authid);
461         my ($authtypecode) = $sth->fetchrow;
462         return $authtypecode;
463 }
464
465
466 sub FindDuplicateauth {
467 ### Should receive an xmlhash
468         my ($record,$authtypecode)=@_;
469         my $dbh = C4::Context->dbh;
470         my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
471         $sth->execute($authtypecode);
472         my ($auth_tag_to_report) = $sth->fetchrow;
473         $sth->finish;
474         # build a request for authoritysearch
475         my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
476         
477 #       if ($record->field($auth_tag_to_report)) {
478                                 push @tags, $auth_tag_to_report;
479                                 push @operator, "all";
480                                  @value, XML_readline_asarray($record,"","",$auth_tag_to_report);
481 #                       }
482  
483         my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
484         # there is at least 1 result => return the 1st one
485         if ($nbresult>0) {
486                 return @$finalresult[0]->{authid},@$finalresult[0]->{summary};
487         }
488         # no result, returns nothing
489         return;
490 }
491
492 sub getsummary{
493 ## give this an XMLhash record to return summary
494 my ($dbh,$record,$authid,$authtypecode)=@_;
495  my $authref = getauthtype($authtypecode);
496                 my $summary = $authref->{summary};
497                 # if the library has a summary defined, use it. Otherwise, build a standard one
498         if ($summary) {
499                         my $fields = $record->{'datafield'};
500                         foreach my $field (@$fields) {
501                                 my $tag = $field->{'tag'};                              
502                                 if ($tag<10) {
503                                 my $tagvalue = XML_readline_onerecord($record,"","",$field->{tag});
504                                 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
505                                 } else {
506                                         my @subf = XML_readline_withtags($record,"","",$tag);
507                                         for my $i (0..$#subf) {
508                                                 my $subfieldcode = $subf[$i][0];
509                                                 my $subfieldvalue = $subf[$i][1];
510                                                 my $tagsubf = $tag.$subfieldcode;
511                                                 $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
512                                         }## each subf
513                                 }#tag >10
514                         }##each field
515                         $summary =~ s/\[(.*?)]//g;
516                         $summary =~ s/\n/<br>/g;
517         } else {
518 ## $summary did not exist create a standard summary
519                         my $heading; # = $authref->{summary};
520                         my $altheading;
521                         my $seeheading;
522                         my $see;
523                         my $fields = $record->{datafield};
524                         if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
525                         # construct UNIMARC summary, that is quite different from MARC21 one
526                         foreach my $field (@$fields) {
527                                 # accepted form
528                                 if ($field->{tag} = ~/'2..'/) {
529                                         foreach my $subfield ("a".."z"){
530                                         ## Fixme-- if UNICODE uses numeric subfields as well add them
531                                         $heading.=XML_readline_onerecord($record,"","",$field->{tag},$subfield); 
532                                         }
533                                 }##tag 2..
534                                 # rejected form(s)
535                                 if ($field->{tag} = ~/'4..'/) {
536                                         my $value;
537                                         foreach my $subfield ("a".."z"){
538                                         ## Fixme-- if UNICODE uses numeric subfields as well add them
539                                         $value.=XML_readline_onerecord($record,"","",$field->{tag},$subfield);
540                                         }
541                                         $summary.= "&nbsp;&nbsp;&nbsp;<i>".$value."</i><br/>";
542                                         $summary.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see:</i> ".$heading."<br/>";
543                                 }##tag 4..
544                                 # see :
545                                 if ($field->{tag} = ~/'5..'/) {
546                                         my $value;
547                                         foreach my $subfield ("a".."z"){
548                                         ## Fixme-- if UNICODE uses numeric subfields as well add them
549                                         $value.=XML_readline_onerecord($record,"","",$field->{tag},$subfield);
550                                         }
551                                         $summary.= "&nbsp;&nbsp;&nbsp;<i>".$value."</i><br/>";
552                                         $summary.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see:</i> ".$heading."<br/>";
553                                 }# tag 5..
554                                 # // form
555                                 if ($field->{tag} = ~/'7..'/) {
556                                         my $value;
557                                         foreach my $subfield ("a".."z"){
558                                         ## Fixme-- if UNICODE uses numeric subfields as well add them
559                                         $value.=XML_readline_onerecord($record,"","",$field->{tag},$subfield);
560                                         }
561                                         $seeheading.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see also:</i> ".$value."<br />";  
562                                         $altheading.= "&nbsp;&nbsp;&nbsp;".$value."<br />";
563                                         $altheading.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see also:</i> ".$heading."<br />";
564                                 }# tag 7..
565                         }## Foreach fields
566                                 $summary = "<b>".$heading."</b><br />".$seeheading.$altheading.$summary;        
567                      } else {
568                         # construct MARC21 summary
569                         foreach my $field (@$fields) {
570                                 my $tag="1..";
571                                   if($field->{tag}  =~ /^$tag/) {
572                                               if ($field->{tag} eq '150') {
573                                                 my $value;
574                                                 foreach my $subfield ("a".."z"){
575                                                  $value=XML_readline_onerecord($record,"","","150",$subfield); 
576                                                 $heading.="\$".$subfield.$value if $value;
577                                                         }
578                                               }else{                            
579                                                 foreach my $subfield ("a".."z"){
580                                                 $heading.=XML_readline_onerecord($record,"","",$field->{tag},$subfield); 
581                                                         }
582                                              }### tag 150 or else
583                                    }##tag 1..
584                                 my $tag="4..";
585                                  if($field->{tag}  =~ /^$tag/) {
586                                         foreach my $subfield ("a".."z"){
587                                                 $seeheading.=XML_readline_onerecord($record,"","",$field->{tag},$subfield); 
588                                                 }
589                                         $seeheading.= "&nbsp;&nbsp;&nbsp;".$seeheading."<br />";
590                                         $seeheading.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see:</i> ".$seeheading."<br />";  
591                                 } #tag 4..
592                                 my $tag="5..";
593                                  if($field->{tag}  =~ /^$tag/) {
594                                         my $value;
595                                         foreach my $subfield ("a".."z"){
596                                                 $value.=XML_readline_onerecord($record,"","",$field->{tag},$subfield); 
597                                                 }
598                                         $seeheading.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see also:</i> ".$value."<br />";  
599                                         $altheading.= "&nbsp;&nbsp;&nbsp;".$value."<br />";
600                                         $altheading.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see also:</i> ".$altheading."<br />";
601                                 }#tag 5..
602                                         
603                         }##for each field
604                     $summary.=$heading.$seeheading.$altheading;         
605                 }##USMARC vs UNIMARC
606         }###Summary exists or not
607 return $summary;
608 }
609 sub getdictsummary{
610 ## give this a Marc record to return summary
611 my ($dbh,$record,$authid,$authtypecode)=@_;
612  my $authref = getauthtype($authtypecode);
613                 my $summary = $authref->{summary};
614                 my @fields = $record->fields();
615 #               chop $tags_using_authtype;
616                 # if the library has a summary defined, use it. Otherwise, build a standard one
617                 if ($summary) {
618                         my @fields = $record->fields();
619                         foreach my $field (@fields) {
620                                 my $tag = $field->tag();
621                                 my $tagvalue = $field->as_string();
622                                 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
623                                 if ($tag<10) {
624                                 } else {
625                                         my @subf = $field->subfields;
626                                         for my $i (0..$#subf) {
627                                                 my $subfieldcode = $subf[$i][0];
628                                                 my $subfieldvalue = $subf[$i][1];
629                                                 my $tagsubf = $tag.$subfieldcode;
630                                                 $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
631                                         }#for $i
632                                 }#tag >10
633                         }## each field
634                         $summary =~ s/\[(.*?)]//g;
635                         $summary =~ s/\n/<br>/g;
636                 } else {
637                         my $heading; # = $authref->{summary};
638                         my $altheading;
639                         my $seeheading;
640                         my $see;
641                         my @fields = $record->{datafields};
642                         if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
643                         # construct UNIMARC summary, that is quite different from MARC21 one
644                                 # accepted form
645                                 foreach my $field ($record->field('2..')) {
646                                         $heading.= $field->as_string();
647                                 }
648                                 # rejected form(s)
649                                 foreach my $field ($record->field('4..')) {
650                                         $summary.= "&nbsp;&nbsp;&nbsp;<i>".$field->as_string()."</i><br/>";
651                                         $summary.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see:</i> ".$heading."<br/>";
652                                 }
653                                 # see :
654                                 foreach my $field ($record->field('5..')) {
655                                         $summary.= "&nbsp;&nbsp;&nbsp;<i>".$field->as_string()."</i><br/>";
656                                         $summary.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see:</i> ".$heading."<br/>";
657                                 }
658                                 # // form
659                                 foreach my $field ($record->field('7..')) {
660                                         $seeheading.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see also:</i> ".$field->as_string()."<br />";     
661                                         $altheading.= "&nbsp;&nbsp;&nbsp;".$field->as_string()."<br />";
662                                         $altheading.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see also:</i> ".$heading."<br />";
663                                 }
664                                 $summary = "<b>".$heading."</b><br />".$seeheading.$altheading.$summary;        
665                         } else {
666                         # construct MARC21 summary
667                                 foreach my $field (@fields) {   
668                                         if ($field->{tag}=~/'1..'/){                    
669                                                 $heading.= XML_readline_onerecord($record,"","",$field->{tag},"a");
670                                         }
671                                 } #each fieldd
672                                 
673                                 $summary=$heading;
674                         }# USMARC vs UNIMARC
675                 }### Summary exists
676 return $summary;
677 }
678
679
680 sub merge {
681 ##mergefrom is authid MARCfrom is marcxml hash of authority
682 ### mergeto ditto
683         my ($dbh,$mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
684         return unless (defined $MARCfrom);
685         return unless (defined $MARCto);
686         my $authtypecodefrom = AUTHfind_authtypecode($dbh,$mergefrom);
687         my $authtypecodeto = AUTHfind_authtypecode($dbh,$mergeto);
688         # return if authority does not exist
689         
690         # search the tag to report
691         my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
692         $sth->execute($authtypecodefrom);
693         my ($auth_tag_to_report) = $sth->fetchrow;
694         my @record_to;
695         # search all biblio tags using this authority.
696         $sth = $dbh->prepare("select distinct tagfield from biblios_subfield_structure where authtypecode=? ");
697         $sth->execute($authtypecodefrom);
698 my @tags_using_authtype;
699         while (my ($tagfield) = $sth->fetchrow) {
700                 push @tags_using_authtype,$tagfield ;
701         }
702 ## The subfield for linking authorities is stored in koha_attr named auth_biblio_link_subf
703 ## This way we may use whichever subfield we want without harcoding 9 in
704 my ($dummyfield,$tagsubfield)=MARCfind_marc_from_kohafield("auth_biblio_link_subf","biblios");
705         # now, find every biblio using this authority
706 ### try ZOOM search here
707 my @oConnection;
708  $oConnection[0]=C4::Context->Zconn("biblioserver");
709 ##$oConnection[0]->option(elementSetName=>"biblios"); ##  Needs a fix
710 my $query;
711 my ($attr2)=MARCfind_attr_from_kohafield("auth_authid");
712 my $attrfield.=$attr2;
713 $query= $attrfield." ".$mergefrom;
714 my ($event,$i);
715 my $oResult = $oConnection[0]->search_pqf($query);
716   while (($i = ZOOM::event(\@oConnection)) != 0) {
717         $event = $oConnection[$i-1]->last_event();
718         last if $event == ZOOM::Event::ZEND;
719    }# while event
720 my $count=$oResult->size();
721 my @reccache;
722 my $z=0;
723 while ( $z<$count ) {
724 my $rec;
725         $rec=$oResult->record($z);
726         my $marcdata = $rec->raw();
727 my $koharecord=Encode::decode("utf8",$marcdata);
728 $koharecord=XML_xml2hash($koharecord);
729  my ( $xmlrecord, @itemsrecord) = XML_separate($koharecord);
730
731 push @reccache, $xmlrecord;
732 $z++;
733 }
734 $oResult->destroy();
735 $oConnection[0]->destroy();
736       foreach my $xmlhash (@reccache){
737         my $update;
738         foreach my $tagfield (@tags_using_authtype){
739
740         ###Change the authid in biblio
741         $xmlhash=XML_writeline_id($xmlhash,$mergefrom,$mergeto,$tagfield,$tagsubfield);
742         ### delete all subfields of bibliorecord
743         $xmlhash=XML_delete_withid($xmlhash,$mergeto,$tagfield,$tagsubfield);
744         ####Read all the data in from authrecord
745         my @record_to=XML_readline_withtags($MARCto,"","",$auth_tag_to_report);
746         ##Write the data to biblio
747                 foreach my $subfield (@record_to) {
748                 ## Replace the data in MARCXML with the new matching authid
749                 XML_writeline_withid($xmlhash,$tagsubfield,$mergeto,$subfield->[1],$tagfield,$subfield->[0]);
750                 $update=1;
751                 }#foreach  $subfield            
752         }#foreach tagfield
753                 if ($update==1){
754                 my $biblionumber=XML_readline_onerecord($xmlhash,"biblionumber","biblios");
755                 my $frameworkcode=MARCfind_frameworkcode($dbh,$biblionumber);
756                 NEWmodbiblio($dbh,$biblionumber,$xmlhash,$frameworkcode) ;
757                 }
758                 
759      }#foreach $xmlhash
760 }#sub
761
762 sub XML_writeline_withid{
763 ## Only used in authorities to update biblios with matching authids
764 my ($xml,$idsubf,$id,$newvalue,$tag,$subf)=@_;
765 my $biblio=$xml->{'datafield'};
766 my $updated=0;
767     if ($tag>9){
768         foreach my $data (@$biblio){
769                         if ($data->{'tag'} eq $tag){
770                         my @subfields=$data->{'subfield'};
771                         foreach my $subfield ( @subfields){
772                               foreach my $code ( @$subfield){
773                                 if ($code->{'code'} eq $idsubf && $code->{'content'} eq $id){
774                                 ###This is the correct tag -- Now reiterate and update
775                                         my @newsubs;
776                                           foreach my $code ( @$subfield){               
777                                                 if ($code->{'code'} eq $subf ){
778                                                 $code->{'content'}=$newvalue;
779                                                 $updated=1;
780                                                 }
781                                            push @newsubs, $code;
782                                         }## each code updated
783                                         if (!$updated){
784                                         ##Create the subfield if it did not exist       
785                                         push @newsubs,{code=>$subf,content=>$newvalue};
786                                         $data->{subfield}= \@newsubs;
787                                         $updated=1;
788                                          }### created   
789                                 }### correct tag with id
790                               }#each code
791                         }##each subfield        
792                 }# tag match
793          }## each datafield
794     }### tag >9
795 return $xml;
796 }
797 sub XML_delete_withid{
798 ## Currently  only usedin authorities
799 ### deletes all the subfields of a matching authid
800 my ($xml,$id,$tag,$idsubf)=@_;
801 my $biblio=$xml->{'datafield'};
802     if ($tag>9){
803         foreach my $data (@$biblio){
804                         if ($data->{'tag'} eq $tag){
805                         my @subfields=$data->{'subfield'};
806                         foreach my $subfield ( @subfields){
807                               foreach my $code ( @$subfield){
808                                 if ($code->{'code'} eq $idsubf && $code->{'content'} eq $id){
809                                 ###This is the correct tag -- Now reiterate and delete all but id subfield
810                                           foreach my $code ( @$subfield){               
811                                                 if ($code->{'code'} ne $idsubf ){
812                                                 $code->{'content'}="";                                  
813                                                 }                                          
814                                           }## each code deleted 
815                                 }### correct tag with id
816                               }#each code
817                         }## each subfield       
818                 }## tag matches
819          }## each datafield
820     }# tag >9
821 return $xml;
822 }
823
824 sub XML_readline_withtags {
825 my ($xml,$kohafield,$recordtype,$tag,$subf)=@_;
826 #$xml represents one record of MARCXML as perlhashed 
827 ## returns an array of read fields--useful for reading repeated fields
828 ### $recordtype is needed for mapping the correct field if supplied
829 ### If only $tag is give reads the whole tag
830 ###Returns subfieldcodes as well
831 my @value;
832  ($tag,$subf)=MARCfind_marc_from_kohafield($kohafield,$recordtype) if $kohafield;
833 if ($tag){
834 ### Only datafields are read
835 my $biblio=$xml->{'datafield'};
836  if ($tag>9){
837         foreach my $data (@$biblio){
838             if ($data->{'tag'} eq $tag){
839                 foreach my $subfield ( $data->{'subfield'}){
840                     foreach my $code ( @$subfield){
841                         if ($code->{'code'} eq $subf || !$subf){
842                         push @value,[$code->{'code'},$code->{'content'}];
843                         }
844                    }# each code
845                 }# each subfield
846            }### tag found
847         }## each tag
848    }##tag >9
849 }## if tag 
850 return @value;
851 }
852
853 END { }       # module clean-up code here (global destructor)
854
855 =back
856
857 =head1 AUTHOR
858
859 Koha Developement team <info@koha.org>
860
861 Paul POULAIN paul.poulain@free.fr
862
863 =cut
864
865 # $Id$
866 # $Log$
867 # Revision 1.30  2006/09/06 16:21:03  tgarip1957
868 # Clean up before final commits
869 #
870 # Revision 1.9.2.6  2005/06/07 10:02:00  tipaul
871 # porting dictionnary search from head to 2.2. there is now a ... facing titles, author & subject, to search in biblio & authorities existing values.
872 #
873 # Revision 1.9.2.5  2005/05/31 14:50:46  tipaul
874 # fix for authority merging. There was a bug on official installs
875 #
876 # Revision 1.9.2.4  2005/05/30 11:24:15  tipaul
877 # 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)
878 #
879 # Revision 1.9.2.3  2005/04/28 08:45:33  tipaul
880 # porting FindDuplicate feature for authorities from HEAD to rel_2_2, works correctly now.
881 #
882 # Revision 1.9.2.2  2005/02/28 14:03:13  tipaul
883 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
884 # * adding a select box to requet "contain" or "begin with" search.
885 # * fixing some bug in authority search (related to "main entry" search)
886 #
887 # Revision 1.9.2.1  2005/02/24 13:12:13  tipaul
888 # 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.
889 # So, it will be done through a cron job.
890 # (/me agree we need some doc for command line scripts)
891 #
892 # Revision 1.9  2004/12/23 09:48:11  tipaul
893 # Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
894 #
895 # Revision 1.8  2004/11/05 10:11:39  tipaul
896 # export auth_count_usage (bugfix)
897 #
898 # Revision 1.7  2004/09/23 16:13:00  tipaul
899 # Bugfix in modification
900 #
901 # Revision 1.6  2004/08/18 16:00:24  tipaul
902 # fixes for authorities management
903 #
904 # Revision 1.5  2004/07/05 13:37:22  doxulting
905 # First step for working authorities
906 #
907 # Revision 1.4  2004/06/22 11:35:37  tipaul
908 # removing % at the beginning of a string to avoid loooonnnngggg searchs
909 #
910 # Revision 1.3  2004/06/17 08:02:13  tipaul
911 # merging tag & subfield in auth_word for better perfs
912 #
913 # Revision 1.2  2004/06/10 08:29:01  tipaul
914 # MARC authority management (continued)
915 #
916 # Revision 1.1  2004/06/07 07:35:01  tipaul
917 # MARC authority management package
918 #