refactoring how limits are built, first working version
[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 (defined $authtypecode){ # NOTE - in MARC21 framework, '' is a valid authority 
657                                 # type
658       $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
659       $sth->execute($authtypecode);
660     } else {
661       $sth=$dbh->prepare("select * from auth_types");
662       $sth->execute;
663     }
664     my $res=$sth->fetchall_arrayref({});
665     if (scalar(@$res)==1){
666       return $res->[0];
667     } else {
668       return $res;
669     }
670 }
671
672
673 sub AUTHhtml2marc {
674     my ($rtags,$rsubfields,$rvalues,%indicators) = @_;
675     my $dbh=C4::Context->dbh;
676     my $prevtag = -1;
677     my $record = MARC::Record->new();
678 #---- TODO : the leader is missing
679
680 #     my %subfieldlist=();
681     my $prevvalue; # if tag <10
682     my $field; # if tag >=10
683     for (my $i=0; $i< @$rtags; $i++) {
684         # rebuild MARC::Record
685         if (@$rtags[$i] ne $prevtag) {
686             if ($prevtag < 10) {
687                 if ($prevvalue) {
688                     $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
689                 }
690             } else {
691                 if ($field) {
692                     $record->add_fields($field);
693                 }
694             }
695             $indicators{@$rtags[$i]}.='  ';
696             if (@$rtags[$i] <10) {
697                 $prevvalue= @$rvalues[$i];
698                 undef $field;
699             } else {
700                 undef $prevvalue;
701                 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
702             }
703             $prevtag = @$rtags[$i];
704         } else {
705             if (@$rtags[$i] <10) {
706                 $prevvalue=@$rvalues[$i];
707             } else {
708                 if (length(@$rvalues[$i])>0) {
709                     $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
710                 }
711             }
712             $prevtag= @$rtags[$i];
713         }
714     }
715     # the last has not been included inside the loop... do it now !
716     $record->add_fields($field) if $field;
717     return $record;
718 }
719
720 =head2 FindDuplicateAuthority
721
722 =over 4
723
724 $record= &FindDuplicateAuthority( $record, $authtypecode)
725 return $authid,Summary if duplicate is found.
726
727 Comments : an improvement would be to return All the records that match.
728
729 =back
730
731 =cut
732
733 sub FindDuplicateAuthority {
734
735     my ($record,$authtypecode)=@_;
736 #    warn "IN for ".$record->as_formatted;
737     my $dbh = C4::Context->dbh;
738 #    warn "".$record->as_formatted;
739     my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
740     $sth->execute($authtypecode);
741     my ($auth_tag_to_report) = $sth->fetchrow;
742     $sth->finish;
743 #     warn "record :".$record->as_formatted."  auth_tag_to_report :$auth_tag_to_report";
744     # build a request for SearchAuthorities
745     my $query='at='.$authtypecode.' ';
746     map {$query.= " and he=\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/)}  $record->field($auth_tag_to_report)->subfields() if $record->field($auth_tag_to_report);
747     my ($error,$results)=SimpleSearch($query,"authorityserver");
748     # there is at least 1 result => return the 1st one
749     if (@$results>0) {
750       my $marcrecord = MARC::File::USMARC::decode($results->[0]);
751       return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode);
752     }
753     # no result, returns nothing
754     return;
755 }
756
757 =head2 BuildSummary
758
759 =over 4
760
761 $text= &BuildSummary( $record, $authid, $authtypecode)
762 return HTML encoded Summary
763
764 Comment : authtypecode can be infered from both record and authid.
765 Moreover, authid can also be inferred from $record.
766 Would it be interesting to delete those things.
767
768 =back
769
770 =cut
771
772 sub BuildSummary{
773 ## give this a Marc record to return summary
774   my ($record,$authid,$authtypecode)=@_;
775   my $dbh=C4::Context->dbh;
776   my $authref = GetAuthType($authtypecode);
777   my $summary = $authref->{summary};
778   my %language;
779   $language{'fre'}="Français";
780   $language{'eng'}="Anglais";
781   $language{'ger'}="Allemand";
782   $language{'ita'}="Italien";
783   $language{'spa'}="Espagnol";
784   my %thesaurus;
785   $thesaurus{'1'}="Peuples";
786   $thesaurus{'2'}="Anthroponymes";
787   $thesaurus{'3'}="Oeuvres";
788   $thesaurus{'4'}="Chronologie";
789   $thesaurus{'5'}="Lieux";
790   $thesaurus{'6'}="Sujets";
791   #thesaurus a remplir
792   my @fields = $record->fields();
793   my $reported_tag;
794   # if the library has a summary defined, use it. Otherwise, build a standard one
795   # FIXME - it appears that the summary field in the authority frameworks
796   #         can work as a display template.  However, this doesn't
797   #         suit the MARC21 version, so for now the "templating"
798   #         feature will be enabled only for UNIMARC for backwards
799   #         compatibility.
800   if ($summary and C4::Context->preference('marcflavour') eq 'UNIMARC') {
801     my @fields = $record->fields();
802     #             $reported_tag = '$9'.$result[$counter];
803     foreach my $field (@fields) {
804       my $tag = $field->tag();
805       my $tagvalue = $field->as_string();
806       $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
807       if ($tag<10) {
808         if ($tag eq '001') {
809           $reported_tag.='$3'.$field->data();
810         }
811       } else {
812         my @subf = $field->subfields;
813         for my $i (0..$#subf) {
814           my $subfieldcode = $subf[$i][0];
815           my $subfieldvalue = $subf[$i][1];
816           my $tagsubf = $tag.$subfieldcode;
817           $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
818         }
819       }
820     }
821     $summary =~ s/\[(.*?)]//g;
822     $summary =~ s/\n/<br>/g;
823   } else {
824     my $heading; 
825     my $authid; 
826     my $altheading;
827     my $seealso;
828     my $broaderterms;
829     my $narrowerterms;
830     my $see;
831     my $seeheading;
832         my $notes;
833     my @fields = $record->fields();
834     if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
835     # construct UNIMARC summary, that is quite different from MARC21 one
836       # accepted form
837       foreach my $field ($record->field('2..')) {
838         $heading.= $field->subfield('a');
839                 $authid=$field->subfield('3');
840       }
841       # rejected form(s)
842       foreach my $field ($record->field('3..')) {
843         $notes.= '<span class="note">'.$field->subfield('a')."</span>\n";
844       }
845       foreach my $field ($record->field('4..')) {
846         my $thesaurus = "thes. : ".$thesaurus{"$field->subfield('2')"}." : " if ($field->subfield('2'));
847         $see.= '<span class="UF">'.$thesaurus.$field->subfield('a')."</span> -- \n";
848       }
849       # see :
850       foreach my $field ($record->field('5..')) {
851             
852         if (($field->subfield('5')) && ($field->subfield('a')) && ($field->subfield('5') eq 'g')) {
853           $broaderterms.= '<span class="BT"> <a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
854         } elsif (($field->subfield('5')) && ($field->subfield('a')) && ($field->subfield('5') eq 'h')){
855           $narrowerterms.= '<span class="NT"><a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
856         } elsif ($field->subfield('a')) {
857           $seealso.= '<span class="RT"><a href="detail.pl?authid='.$field->subfield('3').'">'.$field->subfield('a')."</a></span> -- \n";
858         }
859       }
860       # // form
861       foreach my $field ($record->field('7..')) {
862         my $lang = substr($field->subfield('8'),3,3);
863         $seeheading.= '<span class="langue"> En '.$language{$lang}.' : </span><span class="OT"> '.$field->subfield('a')."</span><br />\n";  
864       }
865             $broaderterms =~s/-- \n$//;
866             $narrowerterms =~s/-- \n$//;
867             $seealso =~s/-- \n$//;
868             $see =~s/-- \n$//;
869       $summary = "<b><a href=\"detail.pl?authid=$authid\">".$heading."</a></b><br />".($notes?"$notes <br />":"");
870       $summary.= '<p><div class="label">TG : '.$broaderterms.'</div></p>' if ($broaderterms);
871       $summary.= '<p><div class="label">TS : '.$narrowerterms.'</div></p>' if ($narrowerterms);
872       $summary.= '<p><div class="label">TA : '.$seealso.'</div></p>' if ($seealso);
873       $summary.= '<p><div class="label">EP : '.$see.'</div></p>' if ($see);
874       $summary.= '<p><div class="label">'.$seeheading.'</div></p>' if ($seeheading);
875       } else {
876       # construct MARC21 summary
877           # FIXME - looping over 1XX is questionable
878           # since MARC21 authority should have only one 1XX
879           foreach my $field ($record->field('1..')) {
880               next if "152" eq $field->tag(); # FIXME - 152 is not a good tag to use
881                                               # in MARC21 -- purely local tags really ought to be
882                                               # 9XX
883               if ($record->field('100')) {
884                   $heading.= $field->as_string('abcdefghjklmnopqrstvxyz68');
885               } elsif ($record->field('110')) {
886                                       $heading.= $field->as_string('abcdefghklmnoprstvxyz68');
887               } elsif ($record->field('111')) {
888                                       $heading.= $field->as_string('acdefghklnpqstvxyz68');
889               } elsif ($record->field('130')) {
890                                       $heading.= $field->as_string('adfghklmnoprstvxyz68');
891               } elsif ($record->field('148')) {
892                                       $heading.= $field->as_string('abvxyz68');
893               } elsif ($record->field('150')) {
894           #    $heading.= $field->as_string('abvxyz68');
895           $heading.= $field->as_formatted();
896               my $tag=$field->tag();
897               $heading=~s /^$tag//g;
898               $heading =~s /\_/\$/g;
899               } elsif ($record->field('151')) {
900                                       $heading.= $field->as_string('avxyz68');
901               } elsif ($record->field('155')) {
902                                       $heading.= $field->as_string('abvxyz68');
903               } elsif ($record->field('180')) {
904                                       $heading.= $field->as_string('vxyz68');
905               } elsif ($record->field('181')) {
906                                       $heading.= $field->as_string('vxyz68');
907               } elsif ($record->field('182')) {
908                                       $heading.= $field->as_string('vxyz68');
909               } elsif ($record->field('185')) {
910                                       $heading.= $field->as_string('vxyz68');
911               } else {
912                   $heading.= $field->as_string();
913               }
914           } #See From
915           foreach my $field ($record->field('4..')) {
916               $seeheading.= "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>used for/see from:</i> ".$field->as_string();
917           } #See Also
918           foreach my $field ($record->field('5..')) {
919               $altheading.= "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>see also:</i> ".$field->as_string();
920           }
921           $summary .= ": " if $summary;
922           $summary.=$heading.$seeheading.$altheading;
923       }
924   }
925   return $summary;
926 }
927
928 =head2 BuildUnimarcHierarchies
929
930 =over 4
931
932 $text= &BuildUnimarcHierarchies( $authid, $force)
933 return text containing trees for hierarchies
934 for them to be stored in auth_header
935
936 Example of text:
937 122,1314,2452;1324,2342,3,2452
938
939 =back
940
941 =cut
942 sub BuildUnimarcHierarchies{
943   my $authid = shift @_;
944 #   warn "authid : $authid";
945   my $force = shift @_;
946   my @globalresult;
947   my $dbh=C4::Context->dbh;
948   my $hierarchies;
949   my $data = GetHeaderAuthority($authid);
950   if ($data->{'authtrees'} and not $force){
951     return $data->{'authtrees'};
952   } elsif ($data->{'authtrees'}){
953     $hierarchies=$data->{'authtrees'};
954   } else {
955     my $record = GetAuthority($authid);
956     my $found;
957     foreach my $field ($record->field('550')){
958       if ($field->subfield('5') && $field->subfield('5') eq 'g'){
959         my $parentrecord = GetAuthority($field->subfield('3'));
960         my $localresult=$hierarchies;
961         my $trees;
962         $trees = BuildUnimarcHierarchies($field->subfield('3'));
963         my @trees;
964         if ($trees=~/;/){
965            @trees = split(/;/,$trees);
966         } else {
967            push @trees, $trees;
968         }
969         foreach (@trees){
970           $_.= ",$authid";
971         }
972         @globalresult = (@globalresult,@trees);
973         $found=1;
974       }
975       $hierarchies=join(";",@globalresult);
976     }
977     #Unless there is no ancestor, I am alone.
978     $hierarchies="$authid" unless ($hierarchies);
979   }
980   AddAuthorityTrees($authid,$hierarchies);
981   return $hierarchies;
982 }
983
984 =head2 BuildUnimarcHierarchy
985
986 =over 4
987
988 $ref= &BuildUnimarcHierarchy( $record, $class,$authid)
989 return a hashref in order to display hierarchy for record and final Authid $authid
990
991 "loopparents"
992 "loopchildren"
993 "class"
994 "loopauthid"
995 "current_value"
996 "value"
997
998 "ifparents"  
999 "ifchildren" 
1000 Those two latest ones should disappear soon.
1001
1002 =back
1003
1004 =cut
1005 sub BuildUnimarcHierarchy{
1006   my $record = shift @_;
1007   my $class = shift @_;
1008   my $authid_constructed = shift @_;
1009   my $authid=$record->subfield('250','3');
1010   my %cell;
1011   my $parents=""; my $children="";
1012   my (@loopparents,@loopchildren);
1013   foreach my $field ($record->field('550')){
1014     if ($field->subfield('5') && $field->subfield('a')){
1015       if ($field->subfield('5') eq 'h'){
1016         push @loopchildren, { "childauthid"=>$field->subfield('3'),"childvalue"=>$field->subfield('a')};
1017       }elsif ($field->subfield('5') eq 'g'){
1018         push @loopparents, { "parentauthid"=>$field->subfield('3'),"parentvalue"=>$field->subfield('a')};
1019       }
1020           # brothers could get in there with an else
1021     }
1022   }
1023   $cell{"ifparents"}=1 if (scalar(@loopparents)>0);
1024   $cell{"ifchildren"}=1 if (scalar(@loopchildren)>0);
1025   $cell{"loopparents"}=\@loopparents if (scalar(@loopparents)>0);
1026   $cell{"loopchildren"}=\@loopchildren if (scalar(@loopchildren)>0);
1027   $cell{"class"}=$class;
1028   $cell{"loopauthid"}=$authid;
1029   $cell{"current_value"} =1 if $authid eq $authid_constructed;
1030   $cell{"value"}=$record->subfield('250',"a");
1031   return \%cell;
1032 }
1033
1034 =head2 GetHeaderAuthority
1035
1036 =over 4
1037
1038 $ref= &GetHeaderAuthority( $authid)
1039 return a hashref in order auth_header table data
1040
1041 =back
1042
1043 =cut
1044 sub GetHeaderAuthority{
1045   my $authid = shift @_;
1046   my $sql= "SELECT * from auth_header WHERE authid = ?";
1047   my $dbh=C4::Context->dbh;
1048   my $rq= $dbh->prepare($sql);
1049   $rq->execute($authid);
1050   my $data= $rq->fetchrow_hashref;
1051   return $data;
1052 }
1053
1054 =head2 AddAuthorityTrees
1055
1056 =over 4
1057
1058 $ref= &AddAuthorityTrees( $authid, $trees)
1059 return success or failure
1060
1061 =back
1062
1063 =cut
1064
1065 sub AddAuthorityTrees{
1066   my $authid = shift @_;
1067   my $trees = shift @_;
1068   my $sql= "UPDATE IGNORE auth_header set authtrees=? WHERE authid = ?";
1069   my $dbh=C4::Context->dbh;
1070   my $rq= $dbh->prepare($sql);
1071   return $rq->execute($trees,$authid);
1072 }
1073
1074 =head2 merge
1075
1076 =over 4
1077
1078 $ref= &merge(mergefrom,$MARCfrom,$mergeto,$MARCto)
1079
1080
1081 Could add some feature : Migrating from a typecode to an other for instance.
1082 Then we should add some new parameter : bibliotargettag, authtargettag
1083
1084 =back
1085
1086 =cut
1087 sub merge {
1088     my ($mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
1089     my $dbh=C4::Context->dbh;
1090     my $authtypecodefrom = GetAuthTypeCode($mergefrom);
1091     my $authtypecodeto = GetAuthTypeCode($mergeto);
1092     # return if authority does not exist
1093     my @X = $MARCfrom->fields();
1094     return if $#X == -1;
1095     @X = $MARCto->fields();
1096     return if $#X == -1;
1097     # search the tag to report
1098     my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
1099     $sth->execute($authtypecodefrom);
1100     my ($auth_tag_to_report) = $sth->fetchrow;
1101     
1102     my @record_to;
1103     @record_to = $MARCto->field($auth_tag_to_report)->subfields() if $MARCto->field($auth_tag_to_report);
1104     my @record_from;
1105     @record_from = $MARCfrom->field($auth_tag_to_report)->subfields() if $MARCfrom->field($auth_tag_to_report);
1106     
1107     # search all biblio tags using this authority.
1108     $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1109     $sth->execute($authtypecodefrom);
1110     my @tags_using_authtype;
1111     while (my ($tagfield) = $sth->fetchrow) {
1112         push @tags_using_authtype,$tagfield."9" ;
1113     }
1114
1115     if (C4::Context->preference('NoZebra')) {
1116         warn "MERGE TO DO";
1117     } else {
1118         # now, find every biblio using this authority
1119         my $oConnection=C4::Context->Zconn("biblioserver");
1120         my $query;
1121         $query= "an= ".$mergefrom;
1122         my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1123         my $count=$oResult->size() if  ($oResult);
1124         my @reccache;
1125         my $z=0;
1126         while ( $z<$count ) {
1127         my $rec;
1128                 $rec=$oResult->record($z);
1129             my $marcdata = $rec->raw();
1130         push @reccache, $marcdata;
1131         $z++;
1132         }
1133         $oResult->destroy();
1134         foreach my $marc(@reccache){
1135             my $update;
1136             my $marcrecord;
1137             $marcrecord = MARC::File::USMARC::decode($marc);
1138             foreach my $tagfield (@tags_using_authtype){
1139             $tagfield=substr($tagfield,0,3);
1140             my @tags = $marcrecord->field($tagfield);
1141             foreach my $tag (@tags){
1142                 my $tagsubs=$tag->subfield("9");
1143             #warn "$tagfield:$tagsubs:$mergefrom";
1144                 if ($tagsubs== $mergefrom) {
1145                 $tag->update("9" =>$mergeto);
1146                 foreach my $subfield (@record_to) {
1147             #        warn "$subfield,$subfield->[0],$subfield->[1]";
1148                     $tag->update($subfield->[0] =>$subfield->[1]);
1149                 }#for $subfield
1150                 }
1151                 $marcrecord->delete_field($tag);
1152                 $marcrecord->add_fields($tag);
1153                 $update=1;
1154             }#for each tag
1155             }#foreach tagfield
1156             my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,"") ;
1157             if ($update==1){
1158             &ModBiblio($marcrecord,$oldbiblio->{'biblionumber'},GetFrameworkCode($oldbiblio->{'biblionumber'})) ;
1159             }
1160             
1161         }#foreach $marc
1162     }
1163   # now, find every other authority linked with this authority
1164 #   my $oConnection=C4::Context->Zconn("authorityserver");
1165 #   my $query;
1166 # # att 9210               Auth-Internal-authtype
1167 # # att 9220               Auth-Internal-LN
1168 # # ccl.properties to add for authorities
1169 #   $query= "= ".$mergefrom;
1170 #   my $oResult = $oConnection->search(new ZOOM::Query::CCL2RPN( $query, $oConnection ));
1171 #   my $count=$oResult->size() if  ($oResult);
1172 #   my @reccache;
1173 #   my $z=0;
1174 #   while ( $z<$count ) {
1175 #   my $rec;
1176 #           $rec=$oResult->record($z);
1177 #       my $marcdata = $rec->raw();
1178 #   push @reccache, $marcdata;
1179 #   $z++;
1180 #   }
1181 #   $oResult->destroy();
1182 #   foreach my $marc(@reccache){
1183 #     my $update;
1184 #     my $marcrecord;
1185 #     $marcrecord = MARC::File::USMARC::decode($marc);
1186 #     foreach my $tagfield (@tags_using_authtype){
1187 #       $tagfield=substr($tagfield,0,3);
1188 #       my @tags = $marcrecord->field($tagfield);
1189 #       foreach my $tag (@tags){
1190 #         my $tagsubs=$tag->subfield("9");
1191 #     #warn "$tagfield:$tagsubs:$mergefrom";
1192 #         if ($tagsubs== $mergefrom) {
1193 #           $tag->update("9" =>$mergeto);
1194 #           foreach my $subfield (@record_to) {
1195 #     #        warn "$subfield,$subfield->[0],$subfield->[1]";
1196 #             $tag->update($subfield->[0] =>$subfield->[1]);
1197 #           }#for $subfield
1198 #         }
1199 #         $marcrecord->delete_field($tag);
1200 #         $marcrecord->add_fields($tag);
1201 #         $update=1;
1202 #       }#for each tag
1203 #     }#foreach tagfield
1204 #     my $authoritynumber = TransformMarcToKoha($dbh,$marcrecord,"") ;
1205 #     if ($update==1){
1206 #       &ModAuthority($marcrecord,$authoritynumber,GetAuthTypeCode($authoritynumber)) ;
1207 #     }
1208
1209 #   }#foreach $marc
1210 }#sub
1211 END { }       # module clean-up code here (global destructor)
1212
1213 =back
1214
1215 =head1 AUTHOR
1216
1217 Koha Developement team <info@koha.org>
1218
1219 Paul POULAIN paul.poulain@free.fr
1220
1221 =cut
1222
1223 # Revision 1.50  2007/07/26 15:14:05  toins
1224 # removing warn compilation.
1225 #
1226 # Revision 1.49  2007/07/16 15:45:28  hdl
1227 # Adding Summary for UNIMARC authorities
1228 #
1229 # Revision 1.48  2007/06/25 15:01:45  tipaul
1230 # bugfixes on unimarc 100 handling (the field used for encoding)
1231 #
1232 # Revision 1.47  2007/06/06 13:08:35  tipaul
1233 # 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-
1234 #
1235 # Revision 1.46  2007/05/10 14:45:15  tipaul
1236 # Koha NoZebra :
1237 # - support for authorities
1238 # - some bugfixes in ordering and "CCL" parsing
1239 # - support for authorities <=> biblios walking
1240 #
1241 # Seems I can do what I want now, so I consider its done, except for bugfixes that will be needed i m sure !
1242 #
1243 # Revision 1.45  2007/04/06 14:48:45  hdl
1244 # Code Cleaning : AuthoritiesMARC.
1245 #
1246 # Revision 1.44  2007/04/05 12:17:55  btoumi
1247 # add "sort by" with heading-entity in authorities search
1248 #
1249 # Revision 1.43  2007/03/30 11:59:16  tipaul
1250 # some cleaning (minor, the main one will come later) : removing some unused subs
1251 #
1252 # Revision 1.42  2007/03/29 16:45:53  tipaul
1253 # Code cleaning of Biblio.pm (continued)
1254 #
1255 # All subs have be cleaned :
1256 # - removed useless
1257 # - merged some
1258 # - reordering Biblio.pm completly
1259 # - using only naming conventions
1260 #
1261 # Seems to have broken nothing, but it still has to be heavily tested.
1262 # Note that Biblio.pm is now much more efficient than previously & probably more reliable as well.
1263 #
1264 # Revision 1.41  2007/03/29 13:30:31  tipaul
1265 # Code cleaning :
1266 # == Biblio.pm cleaning (useless) ==
1267 # * some sub declaration dropped
1268 # * removed modbiblio sub
1269 # * removed moditem sub
1270 # * removed newitems. It was used only in finishrecieve. Replaced by a TransformKohaToMarc+AddItem, that is better.
1271 # * removed MARCkoha2marcItem
1272 # * removed MARCdelsubfield declaration
1273 # * removed MARCkoha2marcBiblio
1274 #
1275 # == Biblio.pm cleaning (naming conventions) ==
1276 # * MARCgettagslib renamed to GetMarcStructure
1277 # * MARCgetitems renamed to GetMarcItem
1278 # * MARCfind_frameworkcode renamed to GetFrameworkCode
1279 # * MARCmarc2koha renamed to TransformMarcToKoha
1280 # * MARChtml2marc renamed to TransformHtmlToMarc
1281 # * MARChtml2xml renamed to TranformeHtmlToXml
1282 # * zebraop renamed to ModZebra
1283 #
1284 # == MARC=OFF ==
1285 # * removing MARC=OFF related scripts (in cataloguing directory)
1286 # * removed checkitems (function related to MARC=off feature, that is completly broken in head. If someone want to reintroduce it, hard work coming...)
1287 # * removed getitemsbybiblioitem (used only by MARC=OFF scripts, that is removed as well)
1288 #
1289 # Revision 1.40  2007/03/28 10:39:16  hdl
1290 # removing $dbh as a parameter in AuthoritiesMarc functions
1291 # And reporting all differences into the scripts taht relies on those functions.
1292 #
1293 # Revision 1.39  2007/03/16 01:25:08  kados
1294 # Using my precrash CVS copy I did the following:
1295 #
1296 # cvs -z3 -d:ext:kados@cvs.savannah.nongnu.org:/sources/koha co -P koha
1297 # find koha.precrash -type d -name "CVS" -exec rm -v {} \;
1298 # cp -r koha.precrash/* koha/
1299 # cd koha/
1300 # cvs commit
1301 #
1302 # This should in theory put us right back where we were before the crash
1303 #
1304 # Revision 1.39  2007/03/12 22:16:31  kados
1305 # chcking for field before calling subfields
1306 #
1307 # Revision 1.38  2007/03/09 14:31:47  tipaul
1308 # rel_3_0 moved to HEAD
1309 #
1310 # Revision 1.28.2.17  2007/02/05 13:16:08  hdl
1311 # Removing Link from AuthoritiesMARC summary (caused a problem owed to the API differences between opac and intranet)
1312 # + removing $dbh in SearchAuthorities
1313 # + adding links in templates on summaries to go to full view.
1314 # (no more links in popup authorities. or should we add it ?)
1315 #
1316 # Revision 1.28.2.16  2007/02/02 18:07:42  hdl
1317 # Sorting and searching for exact term now works.
1318 #
1319 # Revision 1.28.2.15  2007/01/24 10:17:47  hdl
1320 # FindDuplicate Now works.
1321 # Be AWARE that it needs a change ccl.properties.
1322 #
1323 # Revision 1.28.2.14  2007/01/10 14:40:11  hdl
1324 # Adding Authorities tree.
1325 #
1326 # Revision 1.28.2.13  2007/01/09 15:18:09  hdl
1327 # Adding an to ccl.properties to allow ccl search for authority-numbers.
1328 # Fixing Some problems with the previous modification to allow pqf search to work for more than one page.
1329 # Using search for an= for an authority-Number.
1330 #
1331 # Revision 1.28.2.12  2007/01/09 13:51:31  hdl
1332 # Bug Fixing : CountUsage used *synchronous* connection where biblio used ****asynchronous**** one.
1333 # First try to get it work.
1334 #
1335 # Revision 1.28.2.11  2007/01/05 14:37:26  btoumi
1336 # bug fix : remove wrong field in sql syntaxe from auth_subfield_structure table
1337 #
1338 # Revision 1.28.2.10  2007/01/04 13:11:08  tipaul
1339 # commenting 2 zconn destroy
1340 #
1341 # Revision 1.28.2.9  2006/12/22 15:09:53  toins
1342 # removing C4::Database;
1343 #
1344 # Revision 1.28.2.8  2006/12/20 17:13:19  hdl
1345 # modifying use of GILS into use of @attr 1=Koha-Auth-Number
1346 #
1347 # Revision 1.28.2.7  2006/12/18 16:45:38  tipaul
1348 # FIXME upcased
1349 #
1350 # Revision 1.28.2.6  2006/12/07 16:45:43  toins
1351 # removing warn compilation. (perl -wc)
1352 #
1353 # Revision 1.28.2.5  2006/12/06 14:19:59  hdl
1354 # ABugFixing : Authority count  Management.
1355 #
1356 # Revision 1.28.2.4  2006/11/17 13:18:58  tipaul
1357 # code cleaning : removing use of "bib", and replacing with "biblionumber"
1358 #
1359 # WARNING : I tried to do carefully, but there are probably some mistakes.
1360 # So if you encounter a problem you didn't have before, look for this change !!!
1361 # anyway, I urge everybody to use only "biblionumber", instead of "bib", "bi", "biblio" or anything else. will be easier to maintain !!!
1362 #
1363 # Revision 1.28.2.3  2006/11/17 11:17:30  tipaul
1364 # code cleaning : removing use of "bib", and replacing with "biblionumber"
1365 #
1366 # WARNING : I tried to do carefully, but there are probably some mistakes.
1367 # So if you encounter a problem you didn't have before, look for this change !!!
1368 # anyway, I urge everybody to use only "biblionumber", instead of "bib", "bi", "biblio" or anything else. will be easier to maintain !!!
1369 #
1370 # Revision 1.28.2.2  2006/10/12 22:04:47  hdl
1371 # Authorities working with zebra.
1372 # zebra Configuration files are comitted next.