Changing 100$a coded field Only when marcflavour eq UNIMARC.
[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 MARC::Record;
24 use C4::Biblio;
25 use C4::Search;
26
27 use vars qw($VERSION @ISA @EXPORT);
28
29 # set the version for version checking
30 $VERSION = 3.00;
31
32 @ISA = qw(Exporter);
33 @EXPORT = qw(
34     &GetTagsLabels
35     &GetAuthType
36     &GetAuthTypeCode
37     &GetAuthMARCFromKohaField 
38     &AUTHhtml2marc
39
40     &AddAuthority
41     &ModAuthority
42     &DelAuthority
43     &GetAuthority
44     &GetAuthorityXML
45     
46     &CountUsage
47     &CountUsageChildren
48     &SearchAuthorities
49     
50     &BuildSummary
51     &BuildUnimarcHierarchies
52     &BuildUnimarcHierarchy
53     
54     &merge
55     &FindDuplicateAuthority
56  );
57
58 =head2 GetAuthMARCFromKohaField 
59
60 =over 4
61
62 ( $tag, $subfield ) = &GetAuthMARCFromKohaField ($kohafield,$authtypecode);
63 returns tag and subfield linked to kohafield
64
65 Comment :
66 Suppose Kohafield is only linked to ONE subfield
67 =back
68
69 =cut
70 sub GetAuthMARCFromKohaField {
71 #AUTHfind_marc_from_kohafield
72   my ( $kohafield,$authtypecode ) = @_;
73   my $dbh=C4::Context->dbh;
74   return 0, 0 unless $kohafield;
75   $authtypecode="" unless $authtypecode;
76   my $marcfromkohafield;
77   my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
78   $sth->execute($kohafield,$authtypecode);
79   my ($tagfield,$tagsubfield) = $sth->fetchrow;
80     
81   return  ($tagfield,$tagsubfield);
82 }
83
84 =head2 SearchAuthorities 
85
86 =over 4
87
88 (\@finalresult, $nbresults)= &SearchAuthorities($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby)
89 returns ref to array result and count of results returned
90
91 =back
92
93 =cut
94 sub SearchAuthorities {
95     my ($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby) = @_;
96 #     warn "CALL : $tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby";
97     my $dbh=C4::Context->dbh;
98     if (C4::Context->preference('NoZebra')) {
99     
100         #
101         # build the query
102         #
103         my $query;
104         my @auths=split / /,$authtypecode ;
105         foreach my  $auth (@auths){
106             $query .="AND auth_type= $auth ";
107         }
108         $query =~ s/^AND //;
109         my $dosearch;
110         for(my $i = 0 ; $i <= $#{$value} ; $i++)
111         {
112             if (@$value[$i]){
113                 if (@$tags[$i] eq "mainmainentry") {
114                     $query .=" AND mainmainentry";
115                 }elsif (@$tags[$i] eq "mainentry") {
116                     $query .=" AND mainentry";
117                 } else {
118                     $query .=" AND ";
119                 }
120                 if (@$operator[$i] eq 'is') {
121                     $query.=(@$tags[$i]?"=":""). '"'.@$value[$i].'"';
122                 }elsif (@$operator[$i] eq "="){
123                     $query.=(@$tags[$i]?"=":""). '"'.@$value[$i].'"';
124                 }elsif (@$operator[$i] eq "start"){
125                     $query.=(@$tags[$i]?"=":"").'"'.@$value[$i].'%"';
126                 } else {
127                     $query.=(@$tags[$i]?"=":"").'"'.@$value[$i].'%"';
128                 }
129                 $dosearch=1;
130             }#if value
131         }
132         #
133         # do the query (if we had some search term
134         #
135         if ($dosearch) {
136 #             warn "QUERY : $query";
137             my $result = C4::Search::NZanalyse($query,'authorityserver');
138 #             warn "result : $result";
139             my %result;
140             foreach (split /;/,$result) {
141                 my ($authid,$title) = split /,/,$_;
142                 # hint : the result is sorted by title.biblionumber because we can have X biblios with the same title
143                 # and we don't want to get only 1 result for each of them !!!
144                 # hint & speed improvement : we can order without reading the record
145                 # so order, and read records only for the requested page !
146                 $result{$title.$authid}=$authid;
147             }
148             # sort the hash and return the same structure as GetRecords (Zebra querying)
149             my @finalresult = ();
150             my $numbers=0;
151             if ($sortby eq 'HeadingDsc') { # sort by mainmainentry desc
152                 foreach my $key (sort {$b cmp $a} (keys %result)) {
153                     push @finalresult, $result{$key};
154 #                     warn "push..."$#finalresult;
155                     $numbers++;
156                 }
157             } else { # sort by mainmainentry ASC
158                 foreach my $key (sort (keys %result)) {
159                     push @finalresult, $result{$key};
160 #                     warn "push..."$#finalresult;
161                     $numbers++;
162                 }
163             }
164             # limit the $results_per_page to result size if it's more
165             $length = $numbers-1 if $numbers < $length;
166             # for the requested page, replace authid by the complete record
167             # speed improvement : avoid reading too much things
168             for (my $counter=$offset;$counter<=$offset+$length;$counter++) {
169 #                 $finalresult[$counter] = GetAuthority($finalresult[$counter])->as_usmarc;
170                 my $separator=C4::Context->preference('authoritysep');
171                 my $authrecord = MARC::File::USMARC::decode(GetAuthority($finalresult[$counter])->as_usmarc);
172                 my $authid=$authrecord->field('001')->data(); 
173                 my $summary=BuildSummary($authrecord,$authid,$authtypecode);
174                 my $query_auth_tag = "SELECT auth_tag_to_report FROM auth_types WHERE authtypecode=?";
175                 my $sth = $dbh->prepare($query_auth_tag);
176                 $sth->execute($authtypecode);
177                 my $auth_tag_to_report = $sth->fetchrow;
178                 my %newline;
179                 $newline{used}=CountUsage($authid);
180                 $newline{summary} = $summary;
181                 $newline{authid} = $authid;
182                 $newline{even} = $counter % 2;
183                 $finalresult[$counter]= \%newline;
184             }
185             return (\@finalresult, $numbers);
186         } else {
187             return;
188         }
189     } else {
190         my $query;
191         my $attr;
192             # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
193             # the authtypecode. Then, search on $a of this tag_to_report
194             # also store main entry MARC tag, to extract it at end of search
195         my $mainentrytag;
196         ##first set the authtype search and may be multiple authorities
197         my $n=0;
198         my @authtypecode;
199         my @auths=split / /,$authtypecode ;
200         foreach my  $auth (@auths){
201             $query .=" \@attr 1=Authority/format-id \@attr 5=100 ".$auth; ##No truncation on authtype
202             push @authtypecode ,$auth;
203             $n++;
204         }
205         if ($n>1){
206             while ($n>1){$query= "\@or ".$query;$n--;}
207         }
208         
209         my $dosearch;
210         my $and;
211         my $q2;
212         for(my $i = 0 ; $i <= $#{$value} ; $i++)
213         {
214             if (@$value[$i]){
215             ##If mainentry search $a tag
216                 if (@$tags[$i] eq "mainmainentry") {
217                 $attr =" \@attr 1=Heading ";
218                 }elsif (@$tags[$i] eq "mainentry") {
219                 $attr =" \@attr 1=Heading-Entity ";
220                 }else{
221                 $attr =" \@attr 1=Any ";
222                 }
223                 if (@$operator[$i] eq 'is') {
224                     $attr.=" \@attr 4=1  \@attr 5=100 ";##Phrase, No truncation,all of subfield field must match
225                 }elsif (@$operator[$i] eq "="){
226                     $attr.=" \@attr 4=107 ";           #Number Exact match
227                 }elsif (@$operator[$i] eq "start"){
228                     $attr.=" \@attr 4=1 \@attr 5=1 ";#Phrase, Right truncated
229                 } else {
230                     $attr .=" \@attr 5=1 \@attr 4=6 ";## Word list, right truncated, anywhere
231                 }
232                 $and .=" \@and " ;
233                 $attr =$attr."\"".@$value[$i]."\"";
234                 $q2 .=$attr;
235             $dosearch=1;
236             }#if value
237         }
238         ##Add how many queries generated
239         if ($query=~/\S+/){    
240           $query= $and.$query.$q2 
241         } else {
242           $query=$q2;    
243         }         
244         ## Adding order
245         $query=' @or  @attr 7=1 @attr 1=Heading 0 @or  @attr 7=1 @attr 1=Heading-Entity 1'.$query if ($sortby eq "HeadingAsc");
246         $query=' @or  @attr 7=2 @attr 1=Heading 0 @or  @attr 7=1 @attr 1=Heading-Entity 1'.$query if ($sortby eq "HeadingDsc");
247         
248         $offset=0 unless $offset;
249         my $counter = $offset;
250         $length=10 unless $length;
251         my @oAuth;
252         my $i;
253         $oAuth[0]=C4::Context->Zconn("authorityserver" , 1);
254         my $Anewq= new ZOOM::Query::PQF($query,$oAuth[0]);
255         my $oAResult;
256         $oAResult= $oAuth[0]->search($Anewq) ; 
257         while (($i = ZOOM::event(\@oAuth)) != 0) {
258             my $ev = $oAuth[$i-1]->last_event();
259             last if $ev == ZOOM::Event::ZEND;
260         }
261         my($error, $errmsg, $addinfo, $diagset) = $oAuth[0]->error_x();
262         if ($error) {
263             warn  "oAuth error: $errmsg ($error) $addinfo $diagset\n";
264             goto NOLUCK;
265         }
266         
267         my $nbresults;
268         $nbresults=$oAResult->size();
269         my $nremains=$nbresults;    
270         my @result = ();
271         my @finalresult = ();
272         
273         if ($nbresults>0){
274         
275         ##Find authid and linkid fields
276         ##we may be searching multiple authoritytypes.
277         ## FIXME this assumes that all authid and linkid fields are the same for all authority types
278         # my ($authidfield,$authidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.authid",$authtypecode[0]);
279         # my ($linkidfield,$linkidsubfield)=GetAuthMARCFromKohaField($dbh,"auth_header.linkid",$authtypecode[0]);
280             while (($counter < $nbresults) && ($counter < ($offset + $length))) {
281             
282             ##Here we have to extract MARC record and $authid from ZEBRA AUTHORITIES
283             my $rec=$oAResult->record($counter);
284             my $marcdata=$rec->raw();
285             my $authrecord;
286             my $separator=C4::Context->preference('authoritysep');
287             $authrecord = MARC::File::USMARC::decode($marcdata);
288             my $authid=$authrecord->field('001')->data(); 
289             my $summary=BuildSummary($authrecord,$authid,$authtypecode);
290             my $query_auth_tag = "SELECT auth_tag_to_report FROM auth_types WHERE authtypecode=?";
291             my $sth = $dbh->prepare($query_auth_tag);
292             $sth->execute($authtypecode);
293             my $auth_tag_to_report = $sth->fetchrow;
294             my %newline;
295             $newline{summary} = $summary;
296             $newline{authid} = $authid;
297             $newline{even} = $counter % 2;
298             $counter++;
299             push @finalresult, \%newline;
300             }## while counter
301         ###
302         for (my $z=0; $z<@finalresult; $z++){
303                 my  $count=CountUsage($finalresult[$z]{authid});
304                 $finalresult[$z]{used}=$count;
305         }# all $z's
306         
307         }## if nbresult
308         NOLUCK:
309         # $oAResult->destroy();
310         # $oAuth[0]->destroy();
311         
312         return (\@finalresult, $nbresults);
313     }
314 }
315
316 =head2 CountUsage 
317
318 =over 4
319
320 $count= &CountUsage($authid)
321 counts Usage of Authid in bibliorecords. 
322
323 =back
324
325 =cut
326
327 sub CountUsage {
328     my ($authid) = @_;
329     if (C4::Context->preference('NoZebra')) {
330         # Read the index Koha-Auth-Number for this authid and count the lines
331         my $result = C4::Search::NZanalyse("an=$authid");
332         my @tab = split /;/,$result;
333         return scalar @tab;
334     } else {
335         ### ZOOM search here
336         my $oConnection=C4::Context->Zconn("biblioserver",1);
337         my $query;
338         $query= "an=".$authid;
339         my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
340         my $result;
341         while ((my $i = ZOOM::event([ $oConnection ])) != 0) {
342             my $ev = $oConnection->last_event();
343             if ($ev == ZOOM::Event::ZEND) {
344                 $result = $oResult->size();
345             }
346         }
347         return ($result);
348     }
349 }
350
351 =head2 CountUsageChildren 
352
353 =over 4
354
355 $count= &CountUsageChildren($authid)
356 counts Usage of narrower terms of Authid in bibliorecords.
357
358 =back
359
360 =cut
361 sub CountUsageChildren {
362   my ($authid) = @_;
363 }
364
365 =head2 GetAuthTypeCode
366
367 =over 4
368
369 $authtypecode= &GetAuthTypeCode($authid)
370 returns authtypecode of an authid
371
372 =back
373
374 =cut
375 sub GetAuthTypeCode {
376 #AUTHfind_authtypecode
377   my ($authid) = @_;
378   my $dbh=C4::Context->dbh;
379   my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
380   $sth->execute($authid);
381   my ($authtypecode) = $sth->fetchrow;
382   return $authtypecode;
383 }
384  
385 =head2 GetTagsLabels
386
387 =over 4
388
389 $tagslabel= &GetTagsLabels($forlibrarian,$authtypecode)
390 returns a ref to hashref of authorities tag and subfield structure.
391
392 tagslabel usage : 
393 $tagslabel->{$tag}->{$subfield}->{'attribute'}
394 where attribute takes values in :
395   lib
396   tab
397   mandatory
398   repeatable
399   authorised_value
400   authtypecode
401   value_builder
402   kohafield
403   seealso
404   hidden
405   isurl
406   link
407
408 =back
409
410 =cut
411 sub GetTagsLabels {
412   my ($forlibrarian,$authtypecode)= @_;
413   my $dbh=C4::Context->dbh;
414   $authtypecode="" unless $authtypecode;
415   my $sth;
416   my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
417
418
419   # check that authority exists
420   $sth=$dbh->prepare("SELECT count(*) FROM auth_tag_structure WHERE authtypecode=?");
421   $sth->execute($authtypecode);
422   my ($total) = $sth->fetchrow;
423   $authtypecode="" unless ($total >0);
424   $sth= $dbh->prepare(
425 "SELECT auth_tag_structure.tagfield,auth_tag_structure.liblibrarian,auth_tag_structure.libopac,auth_tag_structure.mandatory,auth_tag_structure.repeatable 
426  FROM auth_tag_structure 
427  WHERE authtypecode=? 
428  ORDER BY tagfield"
429     );
430
431   $sth->execute($authtypecode);
432   my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
433
434   while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
435         $res->{$tag}->{lib}        = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
436         $res->{$tag}->{tab}        = " ";            # XXX
437         $res->{$tag}->{mandatory}  = $mandatory;
438         $res->{$tag}->{repeatable} = $repeatable;
439   }
440   $sth=      $dbh->prepare(
441 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,frameworkcode as authtypecode,value_builder,kohafield,seealso,hidden,isurl 
442 FROM auth_subfield_structure 
443 WHERE authtypecode=? 
444 ORDER BY tagfield,tagsubfield"
445     );
446     $sth->execute($authtypecode);
447
448     my $subfield;
449     my $authorised_value;
450     my $value_builder;
451     my $kohafield;
452     my $seealso;
453     my $hidden;
454     my $isurl;
455     my $link;
456
457     while (
458         ( $tag,         $subfield,   $liblibrarian,   , $libopac,      $tab,
459         $mandatory,     $repeatable, $authorised_value, $authtypecode,
460         $value_builder, $kohafield,  $seealso,          $hidden,
461         $isurl,            $link )
462         = $sth->fetchrow
463       )
464     {
465         $res->{$tag}->{$subfield}->{lib}              = ($forlibrarian or !$libopac)?$liblibrarian:$libopac;
466         $res->{$tag}->{$subfield}->{tab}              = $tab;
467         $res->{$tag}->{$subfield}->{mandatory}        = $mandatory;
468         $res->{$tag}->{$subfield}->{repeatable}       = $repeatable;
469         $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
470         $res->{$tag}->{$subfield}->{authtypecode}     = $authtypecode;
471         $res->{$tag}->{$subfield}->{value_builder}    = $value_builder;
472         $res->{$tag}->{$subfield}->{kohafield}        = $kohafield;
473         $res->{$tag}->{$subfield}->{seealso}          = $seealso;
474         $res->{$tag}->{$subfield}->{hidden}           = $hidden;
475         $res->{$tag}->{$subfield}->{isurl}            = $isurl;
476         $res->{$tag}->{$subfield}->{link}            = $link;
477     }
478     return $res;
479 }
480
481 =head2 AddAuthority
482
483 =over 4
484
485 $authid= &AddAuthority($record, $authid,$authtypecode)
486 returns authid of the newly created authority
487
488 Either Create Or Modify existing authority.
489
490 =back
491
492 =cut
493 sub AddAuthority {
494 # pass the MARC::Record to this function, and it will create the records in the authority table
495   my ($record,$authid,$authtypecode) = @_;
496   my $dbh=C4::Context->dbh;
497   my $leader='         a              ';##Fixme correct leader as this one just adds utf8 to MARC21
498
499 # if authid empty => true add, find a new authid number
500   my $format= 'UNIMARCAUTH' if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC');
501   $format= 'MARC21' if (uc(C4::Context->preference('marcflavour')) ne 'UNIMARC');
502   if (!$authid) {
503     my $sth=$dbh->prepare("select max(authid) from auth_header");
504     $sth->execute;
505     ($authid)=$sth->fetchrow;
506     $authid=$authid+1;
507   ##Insert the recordID in MARC record 
508     unless ($record->field('001') && $record->field('001')->data() eq $authid){
509         $record->delete_field($record->field('001'));
510         $record->insert_fields_ordered(MARC::Field->new('001',$authid));
511     }
512     $record->add_fields('152','','','b'=>$authtypecode) unless $record->field('152');
513 #     warn $record->as_formatted;
514     $dbh->do("lock tables auth_header WRITE");
515     $sth=$dbh->prepare("insert into auth_header (authid,datecreated,authtypecode,marc,marcxml) values (?,now(),?,?,?)");
516     $sth->execute($authid,$authtypecode,$record->as_usmarc,$record->as_xml_record($format));
517     $sth->finish;
518   }else{
519       $record->add_fields('001',$authid) unless ($record->field('001'));
520       if (($format eq "UNIMARCAUTH") && (!$record->subfield('100','a'))){
521             $record->leader("     nx  j22             ");
522             my $date=POSIX::strftime("%Y%m%d",localtime);    
523             if ($record->field('100')){
524               $record->field('100')->update('a'=>$date."afrey50      ba0");
525             } else {      
526               $record->append_fields(
527                 MARC::Field->new('100',' ',' '
528                   ,'a'=>$date."afrey50      ba0")
529               );
530             }      
531       }    
532       $record->add_fields('152','','','b'=>$authtypecode) unless ($record->field('152'));
533       $dbh->do("lock tables auth_header WRITE");
534       my $sth=$dbh->prepare("update auth_header set marc=?,marcxml=? where authid=?");
535       $sth->execute($record->as_usmarc,$record->as_xml_record($format),$authid);
536       $sth->finish;
537   }
538   $dbh->do("unlock tables");
539   ModZebra($authid,'specialUpdate',"authorityserver",$record);
540   return ($authid);
541 }
542
543
544 =head2 DelAuthority
545
546 =over 4
547
548 $authid= &DelAuthority($authid)
549 Deletes $authid
550
551 =back
552
553 =cut
554
555
556 sub DelAuthority {
557     my ($authid) = @_;
558     my $dbh=C4::Context->dbh;
559
560     ModZebra($authid,"recordDelete","authorityserver",GetAuthority($authid));
561     $dbh->do("delete from auth_header where authid=$authid") ;
562
563 }
564
565 sub ModAuthority {
566   my ($authid,$record,$authtypecode,$merge)=@_;
567   my $dbh=C4::Context->dbh;
568 #   my ($oldrecord)=&GetAuthority($authid);
569 #   if ($oldrecord eq $record) {
570 #       return;
571 #   }
572 #   my $sth=$dbh->prepare("update auth_header set marc=?,marcxml=? where authid=?");
573   #Now rewrite the $record to table with an add
574   $authid=AddAuthority($record,$authid,$authtypecode);
575
576 ### 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.p
577 ### they should have a system preference "dontmerge=1" otherwise by default biblios will be updated
578 ### the $merge flag is now depreceated and will be removed at code cleaning
579   if (C4::Context->preference('dontmerge') ){
580   # save the file in tmp/modified_authorities
581       my $cgidir = C4::Context->intranetdir ."/cgi-bin";
582       unless (opendir(DIR,"$cgidir")) {
583               $cgidir = C4::Context->intranetdir."/";
584       }
585   
586       my $filename = $cgidir."/tmp/modified_authorities/$authid.authid";
587       open AUTH, "> $filename";
588       print AUTH $authid;
589       close AUTH;
590   } else {
591 #        &merge($authid,$record,$authid,$record);
592   }
593   return $authid;
594 }
595
596 =head2 GetAuthorityXML 
597
598 =over 4
599
600 $marcxml= &GetAuthorityXML( $authid)
601 returns xml form of record $authid
602
603 =back
604
605 =cut
606 sub GetAuthorityXML {
607   # Returns MARC::XML of the authority passed in parameter.
608   my ( $authid ) = @_;
609   my $dbh=C4::Context->dbh;
610   my $sth =
611       $dbh->prepare("select marcxml from auth_header where authid=? "  );
612   $sth->execute($authid);
613   my ($marcxml)=$sth->fetchrow;
614   return $marcxml;
615
616 }
617
618 =head2 GetAuthority 
619
620 =over 4
621
622 $record= &GetAuthority( $authid)
623 Returns MARC::Record of the authority passed in parameter.
624
625 =back
626
627 =cut
628 sub GetAuthority {
629     my ($authid)=@_;
630     my $dbh=C4::Context->dbh;
631     my $sth=$dbh->prepare("select marcxml from auth_header where authid=?");
632     $sth->execute($authid);
633     my ($marcxml) = $sth->fetchrow;
634     my $record=MARC::Record->new_from_xml($marcxml,'UTF-8',(C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")));
635     $record->encoding('UTF-8');
636     return ($record);
637 }
638
639 =head2 GetAuthType 
640
641 =over 4
642
643 $result= &GetAuthType( $authtypecode)
644 If $authtypecode is not "" then 
645   Returns hashref to authtypecode information
646 else 
647   returns ref to array of hashref information of all Authtypes
648
649 =back
650
651 =cut
652 sub GetAuthType {
653     my ($authtypecode) = @_;
654     my $dbh=C4::Context->dbh;
655     my $sth;
656     if ($authtypecode){
657       $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
658       $sth->execute($authtypecode);
659     } else {
660       $sth=$dbh->prepare("select * from auth_types");
661       $sth->execute;
662     }
663     my $res=$sth->fetchall_arrayref({});
664     if (scalar(@$res)==1){
665       return $res->[0];
666     } else {
667       return $res;
668     }
669 }
670
671
672 sub AUTHhtml2marc {
673     my ($rtags,$rsubfields,$rvalues,%indicators) = @_;
674     my $dbh=C4::Context->dbh;
675     my $prevtag = -1;
676     my $record = MARC::Record->new();
677 #---- TODO : the leader is missing
678
679 #     my %subfieldlist=();
680     my $prevvalue; # if tag <10
681     my $field; # if tag >=10
682     for (my $i=0; $i< @$rtags; $i++) {
683         # rebuild MARC::Record
684         if (@$rtags[$i] ne $prevtag) {
685             if ($prevtag < 10) {
686                 if ($prevvalue) {
687                     $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
688                 }
689             } else {
690                 if ($field) {
691                     $record->add_fields($field);
692                 }
693             }
694             $indicators{@$rtags[$i]}.='  ';
695             if (@$rtags[$i] <10) {
696                 $prevvalue= @$rvalues[$i];
697                 undef $field;
698             } else {
699                 undef $prevvalue;
700                 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
701             }
702             $prevtag = @$rtags[$i];
703         } else {
704             if (@$rtags[$i] <10) {
705                 $prevvalue=@$rvalues[$i];
706             } else {
707                 if (length(@$rvalues[$i])>0) {
708                     $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
709                 }
710             }
711             $prevtag= @$rtags[$i];
712         }
713     }
714     # the last has not been included inside the loop... do it now !
715     $record->add_fields($field) if $field;
716     return $record;
717 }
718
719 =head2 FindDuplicateAuthority
720
721 =over 4
722
723 $record= &FindDuplicateAuthority( $record, $authtypecode)
724 return $authid,Summary if duplicate is found.
725
726 Comments : an improvement would be to return All the records that match.
727
728 =back
729
730 =cut
731
732 sub FindDuplicateAuthority {
733
734     my ($record,$authtypecode)=@_;
735 #    warn "IN for ".$record->as_formatted;
736     my $dbh = C4::Context->dbh;
737 #    warn "".$record->as_formatted;
738     my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
739     $sth->execute($authtypecode);
740     my ($auth_tag_to_report) = $sth->fetchrow;
741     $sth->finish;
742 #     warn "record :".$record->as_formatted."  auth_tag_to_report :$auth_tag_to_report";
743     # build a request for SearchAuthorities
744     my $query='at='.$authtypecode.' ';
745     map {$query.= " and he=\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/)}  $record->field($auth_tag_to_report)->subfields() if $record->field($auth_tag_to_report);
746     my ($error,$results)=SimpleSearch($query,"authorityserver");
747     # there is at least 1 result => return the 1st one
748     if (@$results>0) {
749       my $marcrecord = MARC::File::USMARC::decode($results->[0]);
750       return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode);
751     }
752     # no result, returns nothing
753     return;
754 }
755
756 =head2 BuildSummary
757
758 =over 4
759
760 $text= &BuildSummary( $record, $authid, $authtypecode)
761 return HTML encoded Summary
762
763 Comment : authtypecode can be infered from both record and authid.
764 Moreover, authid can also be inferred from $record.
765 Would it be interesting to delete those things.
766
767 =back
768
769 =cut
770
771 sub BuildSummary{
772 ## give this a Marc record to return summary
773   my ($record,$authid,$authtypecode)=@_;
774   my $dbh=C4::Context->dbh;
775   my $authref = GetAuthType($authtypecode);
776   my $summary = $authref->{summary};
777   my %language;
778   $language{'fre'}="Français";
779   $language{'eng'}="Anglais";
780   $language{'ger'}="Allemand";
781   $language{'ita'}="Italien";
782   $language{'spa'}="Espagnol";
783   my %thesaurus;
784   $thesaurus{'1'}="Peuples";
785   $thesaurus{'2'}="Anthroponymes";
786   $thesaurus{'3'}="Oeuvres";
787   $thesaurus{'4'}="Chronologie";
788   $thesaurus{'5'}="Lieux";
789   $thesaurus{'6'}="Sujets";
790   #thesaurus a remplir
791   my @fields = $record->fields();
792   my $reported_tag;
793   # if the library has a summary defined, use it. Otherwise, build a standard one
794   if ($summary) {
795     my @fields = $record->fields();
796     #             $reported_tag = '$9'.$result[$counter];
797     foreach my $field (@fields) {
798       my $tag = $field->tag();
799       my $tagvalue = $field->as_string();
800       $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
801       if ($tag<10) {
802         if ($tag eq '001') {
803           $reported_tag.='$3'.$field->data();
804         }
805       } else {
806         my @subf = $field->subfields;
807         for my $i (0..$#subf) {
808           my $subfieldcode = $subf[$i][0];
809           my $subfieldvalue = $subf[$i][1];
810           my $tagsubf = $tag.$subfieldcode;
811           $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
812         }
813       }
814     }
815     $summary =~ s/\[(.*?)]//g;
816     $summary =~ s/\n/<br>/g;
817   } else {
818     my $heading; 
819     my $authid; 
820     my $altheading;
821     my $seealso;
822     my $broaderterms;
823     my $narrowerterms;
824     my $see;
825     my $seeheading;
826         my $notes;
827     my @fields = $record->fields();
828     if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
829     # construct UNIMARC summary, that is quite different from MARC21 one
830       # accepted form
831       foreach my $field ($record->field('2..')) {
832         $heading.= $field->subfield('a');
833                 $authid=$field->subfield('3');
834       }
835       # rejected form(s)
836       foreach my $field ($record->field('3..')) {
837         $notes.= '<span class="note">'.$field->subfield('a')."</span>\n";
838       }
839       foreach my $field ($record->field('4..')) {
840         my $thesaurus = "thes. : ".$thesaurus{"$field->subfield('2')"}." : " if ($field->subfield('2'));
841         $see.= '<span class="UF">'.$thesaurus.$field->subfield('a')."</span> -- \n";
842       }
843       # see :
844       foreach my $field ($record->field('5..')) {
845             
846         if (($field->subfield('5')) && ($field->subfield('a')) && ($field->subfield('5') eq 'g')) {
847           $broaderterms.= '<span class="BT"> <a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
848         } elsif (($field->subfield('5')) && ($field->subfield('a')) && ($field->subfield('5') eq 'h')){
849           $narrowerterms.= '<span class="NT"><a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
850         } elsif ($field->subfield('a')) {
851           $seealso.= '<span class="RT"><a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
852         }
853       }
854       # // form
855       foreach my $field ($record->field('7..')) {
856         my $lang = substr($field->subfield('8'),3,3);
857         $seeheading.= '<span class="langue"> En '.$language{$lang}.' : </span><span class="OT"> '.$field->subfield('a')."</span><br />\n";  
858       }
859             $broaderterms =~s/-- \n$//;
860             $narrowerterms =~s/-- \n$//;
861             $seealso =~s/-- \n$//;
862             $see =~s/-- \n$//;
863       $summary = "<b><a href=\"detail.pl?authid=$authid\">".$heading."</a></b><br />".($notes?"$notes <br />":"");
864       $summary.= '<p><div class="label">TG : '.$broaderterms.'</div></p>' if ($broaderterms);
865       $summary.= '<p><div class="label">TS : '.$narrowerterms.'</div></p>' if ($narrowerterms);
866       $summary.= '<p><div class="label">TA : '.$seealso.'</div></p>' if ($seealso);
867       $summary.= '<p><div class="label">EP : '.$see.'</div></p>' if ($see);
868       $summary.= '<p><div class="label">'.$seeheading.'</div></p>' if ($seeheading);
869       } else {
870       # construct MARC21 summary
871           foreach my $field ($record->field('1..')) {
872               if ($record->field('100')) {
873                   $heading.= $field->as_string('abcdefghjklmnopqrstvxyz68');
874               } elsif ($record->field('110')) {
875                                       $heading.= $field->as_string('abcdefghklmnoprstvxyz68');
876               } elsif ($record->field('111')) {
877                                       $heading.= $field->as_string('acdefghklnpqstvxyz68');
878               } elsif ($record->field('130')) {
879                                       $heading.= $field->as_string('adfghklmnoprstvxyz68');
880               } elsif ($record->field('148')) {
881                                       $heading.= $field->as_string('abvxyz68');
882               } elsif ($record->field('150')) {
883           #    $heading.= $field->as_string('abvxyz68');
884           $heading.= $field->as_formatted();
885               my $tag=$field->tag();
886               $heading=~s /^$tag//g;
887               $heading =~s /\_/\$/g;
888               } elsif ($record->field('151')) {
889                                       $heading.= $field->as_string('avxyz68');
890               } elsif ($record->field('155')) {
891                                       $heading.= $field->as_string('abvxyz68');
892               } elsif ($record->field('180')) {
893                                       $heading.= $field->as_string('vxyz68');
894               } elsif ($record->field('181')) {
895                                       $heading.= $field->as_string('vxyz68');
896               } elsif ($record->field('182')) {
897                                       $heading.= $field->as_string('vxyz68');
898               } elsif ($record->field('185')) {
899                                       $heading.= $field->as_string('vxyz68');
900               } else {
901                   $heading.= $field->as_string();
902               }
903           } #See From
904           foreach my $field ($record->field('4..')) {
905               $seeheading.= "&nbsp;&nbsp;&nbsp;".$field->as_string()."<br />";
906               $seeheading.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see:</i> ".$seeheading."<br />";
907           } #See Also
908           foreach my $field ($record->field('5..')) {
909               $altheading.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see also:</i> ".$field->as_string()."<br />";
910               $altheading.= "&nbsp;&nbsp;&nbsp;".$field->as_string()."<br />";
911               $altheading.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see also:</i> ".$altheading."<br />";
912           }
913           $summary.=$heading.$seeheading.$altheading;
914       }
915   }
916   return $summary;
917 }
918
919 =head2 BuildUnimarcHierarchies
920
921 =over 4
922
923 $text= &BuildUnimarcHierarchies( $authid, $force)
924 return text containing trees for hierarchies
925 for them to be stored in auth_header
926
927 Example of text:
928 122,1314,2452;1324,2342,3,2452
929
930 =back
931
932 =cut
933 sub BuildUnimarcHierarchies{
934   my $authid = shift @_;
935 #   warn "authid : $authid";
936   my $force = shift @_;
937   my @globalresult;
938   my $dbh=C4::Context->dbh;
939   my $hierarchies;
940   my $data = GetHeaderAuthority($authid);
941   if ($data->{'authtrees'} and not $force){
942     return $data->{'authtrees'};
943   } elsif ($data->{'authtrees'}){
944     $hierarchies=$data->{'authtrees'};
945   } else {
946     my $record = GetAuthority($authid);
947     my $found;
948     foreach my $field ($record->field('550')){
949       if ($field->subfield('5') && $field->subfield('5') eq 'g'){
950         my $parentrecord = GetAuthority($field->subfield('3'));
951         my $localresult=$hierarchies;
952         my $trees;
953         $trees = BuildUnimarcHierarchies($field->subfield('3'));
954         my @trees;
955         if ($trees=~/;/){
956            @trees = split(/;/,$trees);
957         } else {
958            push @trees, $trees;
959         }
960         foreach (@trees){
961           $_.= ",$authid";
962         }
963         @globalresult = (@globalresult,@trees);
964         $found=1;
965       }
966       $hierarchies=join(";",@globalresult);
967     }
968     #Unless there is no ancestor, I am alone.
969     $hierarchies="$authid" unless ($hierarchies);
970   }
971   AddAuthorityTrees($authid,$hierarchies);
972   return $hierarchies;
973 }
974
975 =head2 BuildUnimarcHierarchy
976
977 =over 4
978
979 $ref= &BuildUnimarcHierarchy( $record, $class,$authid)
980 return a hashref in order to display hierarchy for record and final Authid $authid
981
982 "loopparents"
983 "loopchildren"
984 "class"
985 "loopauthid"
986 "current_value"
987 "value"
988
989 "ifparents"  
990 "ifchildren" 
991 Those two latest ones should disappear soon.
992
993 =back
994
995 =cut
996 sub BuildUnimarcHierarchy{
997   my $record = shift @_;
998   my $class = shift @_;
999   my $authid_constructed = shift @_;
1000   my $authid=$record->subfield('250','3');
1001   my %cell;
1002   my $parents=""; my $children="";
1003   my (@loopparents,@loopchildren);
1004   foreach my $field ($record->field('550')){
1005     if ($field->subfield('5') && $field->subfield('a')){
1006       if ($field->subfield('5') eq 'h'){
1007         push @loopchildren, { "childauthid"=>$field->subfield('3'),"childvalue"=>$field->subfield('a')};
1008       }elsif ($field->subfield('5') eq 'g'){
1009         push @loopparents, { "parentauthid"=>$field->subfield('3'),"parentvalue"=>$field->subfield('a')};
1010       }
1011           # brothers could get in there with an else
1012     }
1013   }
1014   $cell{"ifparents"}=1 if (scalar(@loopparents)>0);
1015   $cell{"ifchildren"}=1 if (scalar(@loopchildren)>0);
1016   $cell{"loopparents"}=\@loopparents if (scalar(@loopparents)>0);
1017   $cell{"loopchildren"}=\@loopchildren if (scalar(@loopchildren)>0);
1018   $cell{"class"}=$class;
1019   $cell{"loopauthid"}=$authid;
1020   $cell{"current_value"} =1 if $authid eq $authid_constructed;
1021   $cell{"value"}=$record->subfield('250',"a");
1022   return \%cell;
1023 }
1024
1025 =head2 GetHeaderAuthority
1026
1027 =over 4
1028
1029 $ref= &GetHeaderAuthority( $authid)
1030 return a hashref in order auth_header table data
1031
1032 =back
1033
1034 =cut
1035 sub GetHeaderAuthority{
1036   my $authid = shift @_;
1037   my $sql= "SELECT * from auth_header WHERE authid = ?";
1038   my $dbh=C4::Context->dbh;
1039   my $rq= $dbh->prepare($sql);
1040   $rq->execute($authid);
1041   my $data= $rq->fetchrow_hashref;
1042   return $data;
1043 }
1044
1045 =head2 AddAuthorityTrees
1046
1047 =over 4
1048
1049 $ref= &AddAuthorityTrees( $authid, $trees)
1050 return success or failure
1051
1052 =back
1053
1054 =cut
1055
1056 sub AddAuthorityTrees{
1057   my $authid = shift @_;
1058   my $trees = shift @_;
1059   my $sql= "UPDATE IGNORE auth_header set authtrees=? WHERE authid = ?";
1060   my $dbh=C4::Context->dbh;
1061   my $rq= $dbh->prepare($sql);
1062   return $rq->execute($trees,$authid);
1063 }
1064
1065 =head2 merge
1066
1067 =over 4
1068
1069 $ref= &merge(mergefrom,$MARCfrom,$mergeto,$MARCto)
1070
1071
1072 Could add some feature : Migrating from a typecode to an other for instance.
1073 Then we should add some new parameter : bibliotargettag, authtargettag
1074
1075 =back
1076
1077 =cut
1078 sub merge {
1079     my ($mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
1080     my $dbh=C4::Context->dbh;
1081     my $authtypecodefrom = GetAuthTypeCode($mergefrom);
1082     my $authtypecodeto = GetAuthTypeCode($mergeto);
1083     # return if authority does not exist
1084     my @X = $MARCfrom->fields();
1085     return if $#X == -1;
1086     @X = $MARCto->fields();
1087     return if $#X == -1;
1088     # search the tag to report
1089     my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
1090     $sth->execute($authtypecodefrom);
1091     my ($auth_tag_to_report) = $sth->fetchrow;
1092     
1093     my @record_to;
1094     @record_to = $MARCto->field($auth_tag_to_report)->subfields() if $MARCto->field($auth_tag_to_report);
1095     my @record_from;
1096     @record_from = $MARCfrom->field($auth_tag_to_report)->subfields() if $MARCfrom->field($auth_tag_to_report);
1097     
1098     # search all biblio tags using this authority.
1099     $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1100     $sth->execute($authtypecodefrom);
1101     my @tags_using_authtype;
1102     while (my ($tagfield) = $sth->fetchrow) {
1103         push @tags_using_authtype,$tagfield."9" ;
1104     }
1105
1106     if (C4::Context->preference('NoZebra')) {
1107         warn "MERGE TO DO";
1108     } else {
1109         # now, find every biblio using this authority
1110         my $oConnection=C4::Context->Zconn("biblioserver");
1111         my $query;
1112         $query= "an= ".$mergefrom;
1113         my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1114         my $count=$oResult->size() if  ($oResult);
1115         my @reccache;
1116         my $z=0;
1117         while ( $z<$count ) {
1118         my $rec;
1119                 $rec=$oResult->record($z);
1120             my $marcdata = $rec->raw();
1121         push @reccache, $marcdata;
1122         $z++;
1123         }
1124         $oResult->destroy();
1125         foreach my $marc(@reccache){
1126             my $update;
1127             my $marcrecord;
1128             $marcrecord = MARC::File::USMARC::decode($marc);
1129             foreach my $tagfield (@tags_using_authtype){
1130             $tagfield=substr($tagfield,0,3);
1131             my @tags = $marcrecord->field($tagfield);
1132             foreach my $tag (@tags){
1133                 my $tagsubs=$tag->subfield("9");
1134             #warn "$tagfield:$tagsubs:$mergefrom";
1135                 if ($tagsubs== $mergefrom) {
1136                 $tag->update("9" =>$mergeto);
1137                 foreach my $subfield (@record_to) {
1138             #        warn "$subfield,$subfield->[0],$subfield->[1]";
1139                     $tag->update($subfield->[0] =>$subfield->[1]);
1140                 }#for $subfield
1141                 }
1142                 $marcrecord->delete_field($tag);
1143                 $marcrecord->add_fields($tag);
1144                 $update=1;
1145             }#for each tag
1146             }#foreach tagfield
1147             my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,"") ;
1148             if ($update==1){
1149             &ModBiblio($marcrecord,$oldbiblio->{'biblionumber'},GetFrameworkCode($oldbiblio->{'biblionumber'})) ;
1150             }
1151             
1152         }#foreach $marc
1153     }
1154   # now, find every other authority linked with this authority
1155 #   my $oConnection=C4::Context->Zconn("authorityserver");
1156 #   my $query;
1157 # # att 9210               Auth-Internal-authtype
1158 # # att 9220               Auth-Internal-LN
1159 # # ccl.properties to add for authorities
1160 #   $query= "= ".$mergefrom;
1161 #   my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1162 #   my $count=$oResult->size() if  ($oResult);
1163 #   my @reccache;
1164 #   my $z=0;
1165 #   while ( $z<$count ) {
1166 #   my $rec;
1167 #           $rec=$oResult->record($z);
1168 #       my $marcdata = $rec->raw();
1169 #   push @reccache, $marcdata;
1170 #   $z++;
1171 #   }
1172 #   $oResult->destroy();
1173 #   foreach my $marc(@reccache){
1174 #     my $update;
1175 #     my $marcrecord;
1176 #     $marcrecord = MARC::File::USMARC::decode($marc);
1177 #     foreach my $tagfield (@tags_using_authtype){
1178 #       $tagfield=substr($tagfield,0,3);
1179 #       my @tags = $marcrecord->field($tagfield);
1180 #       foreach my $tag (@tags){
1181 #         my $tagsubs=$tag->subfield("9");
1182 #     #warn "$tagfield:$tagsubs:$mergefrom";
1183 #         if ($tagsubs== $mergefrom) {
1184 #           $tag->update("9" =>$mergeto);
1185 #           foreach my $subfield (@record_to) {
1186 #     #        warn "$subfield,$subfield->[0],$subfield->[1]";
1187 #             $tag->update($subfield->[0] =>$subfield->[1]);
1188 #           }#for $subfield
1189 #         }
1190 #         $marcrecord->delete_field($tag);
1191 #         $marcrecord->add_fields($tag);
1192 #         $update=1;
1193 #       }#for each tag
1194 #     }#foreach tagfield
1195 #     my $authoritynumber = TransformMarcToKoha($dbh,$marcrecord,"") ;
1196 #     if ($update==1){
1197 #       &ModAuthority($marcrecord,$authoritynumber,GetAuthTypeCode($authoritynumber)) ;
1198 #     }
1199
1200 #   }#foreach $marc
1201 }#sub
1202 END { }       # module clean-up code here (global destructor)
1203
1204 =back
1205
1206 =head1 AUTHOR
1207
1208 Koha Developement team <info@koha.org>
1209
1210 Paul POULAIN paul.poulain@free.fr
1211
1212 =cut
1213
1214 # Revision 1.50  2007/07/26 15:14:05  toins
1215 # removing warn compilation.
1216 #
1217 # Revision 1.49  2007/07/16 15:45:28  hdl
1218 # Adding Summary for UNIMARC authorities
1219 #
1220 # Revision 1.48  2007/06/25 15:01:45  tipaul
1221 # bugfixes on unimarc 100 handling (the field used for encoding)
1222 #
1223 # Revision 1.47  2007/06/06 13:08:35  tipaul
1224 # bugfixes (various), handling utf-8 without guessencoding (as suggested by joshua, fixing some zebra config files -for french but should be interesting for other languages-
1225 #
1226 # Revision 1.46  2007/05/10 14:45:15  tipaul
1227 # Koha NoZebra :
1228 # - support for authorities
1229 # - some bugfixes in ordering and "CCL" parsing
1230 # - support for authorities <=> biblios walking
1231 #
1232 # Seems I can do what I want now, so I consider its done, except for bugfixes that will be needed i m sure !
1233 #
1234 # Revision 1.45  2007/04/06 14:48:45  hdl
1235 # Code Cleaning : AuthoritiesMARC.
1236 #
1237 # Revision 1.44  2007/04/05 12:17:55  btoumi
1238 # add "sort by" with heading-entity in authorities search
1239 #
1240 # Revision 1.43  2007/03/30 11:59:16  tipaul
1241 # some cleaning (minor, the main one will come later) : removing some unused subs
1242 #
1243 # Revision 1.42  2007/03/29 16:45:53  tipaul
1244 # Code cleaning of Biblio.pm (continued)
1245 #
1246 # All subs have be cleaned :
1247 # - removed useless
1248 # - merged some
1249 # - reordering Biblio.pm completly
1250 # - using only naming conventions
1251 #
1252 # Seems to have broken nothing, but it still has to be heavily tested.
1253 # Note that Biblio.pm is now much more efficient than previously & probably more reliable as well.
1254 #
1255 # Revision 1.41  2007/03/29 13:30:31  tipaul
1256 # Code cleaning :
1257 # == Biblio.pm cleaning (useless) ==
1258 # * some sub declaration dropped
1259 # * removed modbiblio sub
1260 # * removed moditem sub
1261 # * removed newitems. It was used only in finishrecieve. Replaced by a TransformKohaToMarc+AddItem, that is better.
1262 # * removed MARCkoha2marcItem
1263 # * removed MARCdelsubfield declaration
1264 # * removed MARCkoha2marcBiblio
1265 #
1266 # == Biblio.pm cleaning (naming conventions) ==
1267 # * MARCgettagslib renamed to GetMarcStructure
1268 # * MARCgetitems renamed to GetMarcItem
1269 # * MARCfind_frameworkcode renamed to GetFrameworkCode
1270 # * MARCmarc2koha renamed to TransformMarcToKoha
1271 # * MARChtml2marc renamed to TransformHtmlToMarc
1272 # * MARChtml2xml renamed to TranformeHtmlToXml
1273 # * zebraop renamed to ModZebra
1274 #
1275 # == MARC=OFF ==
1276 # * removing MARC=OFF related scripts (in cataloguing directory)
1277 # * removed checkitems (function related to MARC=off feature, that is completly broken in head. If someone want to reintroduce it, hard work coming...)
1278 # * removed getitemsbybiblioitem (used only by MARC=OFF scripts, that is removed as well)
1279 #
1280 # Revision 1.40  2007/03/28 10:39:16  hdl
1281 # removing $dbh as a parameter in AuthoritiesMarc functions
1282 # And reporting all differences into the scripts taht relies on those functions.
1283 #
1284 # Revision 1.39  2007/03/16 01:25:08  kados
1285 # Using my precrash CVS copy I did the following:
1286 #
1287 # cvs -z3 -d:ext:kados@cvs.savannah.nongnu.org:/sources/koha co -P koha
1288 # find koha.precrash -type d -name "CVS" -exec rm -v {} \;
1289 # cp -r koha.precrash/* koha/
1290 # cd koha/
1291 # cvs commit
1292 #
1293 # This should in theory put us right back where we were before the crash
1294 #
1295 # Revision 1.39  2007/03/12 22:16:31  kados
1296 # chcking for field before calling subfields
1297 #
1298 # Revision 1.38  2007/03/09 14:31:47  tipaul
1299 # rel_3_0 moved to HEAD
1300 #
1301 # Revision 1.28.2.17  2007/02/05 13:16:08  hdl
1302 # Removing Link from AuthoritiesMARC summary (caused a problem owed to the API differences between opac and intranet)
1303 # + removing $dbh in SearchAuthorities
1304 # + adding links in templates on summaries to go to full view.
1305 # (no more links in popup authorities. or should we add it ?)
1306 #
1307 # Revision 1.28.2.16  2007/02/02 18:07:42  hdl
1308 # Sorting and searching for exact term now works.
1309 #
1310 # Revision 1.28.2.15  2007/01/24 10:17:47  hdl
1311 # FindDuplicate Now works.
1312 # Be AWARE that it needs a change ccl.properties.
1313 #
1314 # Revision 1.28.2.14  2007/01/10 14:40:11  hdl
1315 # Adding Authorities tree.
1316 #
1317 # Revision 1.28.2.13  2007/01/09 15:18:09  hdl
1318 # Adding an to ccl.properties to allow ccl search for authority-numbers.
1319 # Fixing Some problems with the previous modification to allow pqf search to work for more than one page.
1320 # Using search for an= for an authority-Number.
1321 #
1322 # Revision 1.28.2.12  2007/01/09 13:51:31  hdl
1323 # Bug Fixing : CountUsage used *synchronous* connection where biblio used ****asynchronous**** one.
1324 # First try to get it work.
1325 #
1326 # Revision 1.28.2.11  2007/01/05 14:37:26  btoumi
1327 # bug fix : remove wrong field in sql syntaxe from auth_subfield_structure table
1328 #
1329 # Revision 1.28.2.10  2007/01/04 13:11:08  tipaul
1330 # commenting 2 zconn destroy
1331 #
1332 # Revision 1.28.2.9  2006/12/22 15:09:53  toins
1333 # removing C4::Database;
1334 #
1335 # Revision 1.28.2.8  2006/12/20 17:13:19  hdl
1336 # modifying use of GILS into use of @attr 1=Koha-Auth-Number
1337 #
1338 # Revision 1.28.2.7  2006/12/18 16:45:38  tipaul
1339 # FIXME upcased
1340 #
1341 # Revision 1.28.2.6  2006/12/07 16:45:43  toins
1342 # removing warn compilation. (perl -wc)
1343 #
1344 # Revision 1.28.2.5  2006/12/06 14:19:59  hdl
1345 # ABugFixing : Authority count  Management.
1346 #
1347 # Revision 1.28.2.4  2006/11/17 13:18:58  tipaul
1348 # code cleaning : removing use of "bib", and replacing with "biblionumber"
1349 #
1350 # WARNING : I tried to do carefully, but there are probably some mistakes.
1351 # So if you encounter a problem you didn't have before, look for this change !!!
1352 # anyway, I urge everybody to use only "biblionumber", instead of "bib", "bi", "biblio" or anything else. will be easier to maintain !!!
1353 #
1354 # Revision 1.28.2.3  2006/11/17 11:17:30  tipaul
1355 # code cleaning : removing use of "bib", and replacing with "biblionumber"
1356 #
1357 # WARNING : I tried to do carefully, but there are probably some mistakes.
1358 # So if you encounter a problem you didn't have before, look for this change !!!
1359 # anyway, I urge everybody to use only "biblionumber", instead of "bib", "bi", "biblio" or anything else. will be easier to maintain !!!
1360 #
1361 # Revision 1.28.2.2  2006/10/12 22:04:47  hdl
1362 # Authorities working with zebra.
1363 # zebra Configuration files are comitted next.