synch'ing 2.2 and head
[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::Database;
23 use C4::Koha;
24 use MARC::Record;
25 use C4::Biblio;
26
27 use vars qw($VERSION @ISA @EXPORT);
28
29 # set the version for version checking
30 $VERSION = 0.01;
31
32 @ISA = qw(Exporter);
33 @EXPORT = qw(
34         &AUTHgettagslib
35         &AUTHfindsubfield
36         &AUTHfind_authtypecode
37
38         &AUTHaddauthority
39         &AUTHmodauthority
40         &AUTHdelauthority
41         &AUTHaddsubfield
42         &AUTHgetauthority
43         
44         &AUTHgetauth_type
45         &AUTHcount_usage
46         
47         &authoritysearch
48         
49         &MARCmodsubfield
50         &AUTHhtml2marc
51         &AUTHaddword
52         &MARCaddword &MARCdelword
53         &char_decode
54         &FindDuplicate
55  );
56
57 sub authoritysearch {
58         my ($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode) = @_;
59         # build the sql request. She will look like :
60         # select m1.bibid
61         #               from auth_subfield_table as m1, auth_subfield_table as m2
62         #               where m1.authid=m2.authid and
63         #               (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
64
65         # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
66         # the authtypecode. Then, search on $a of this tag_to_report
67         for (my $i=0;$i<$#{$tags};$i++) {
68                 if (@$tags[$i] eq "mainentry") {
69                         my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
70                         $sth->execute($authtypecode);
71                         my ($tag_to_report) = $sth->fetchrow;
72                         @$tags[$i] = $tag_to_report."a";
73                 }
74         }
75
76         # "Normal" statements
77         # quote marc fields/subfields
78         for (my $i=0;$i<=$#{$tags};$i++) {
79                 if (@$tags[$i]) {
80                         @$tags[$i] = $dbh->quote(@$tags[$i]);
81                 }
82         }
83         my @normal_tags = ();
84         my @normal_and_or = ();
85         my @normal_operator = ();
86         my @normal_value = ();
87         # Extracts the NOT statements from the list of statements
88         for(my $i = 0 ; $i <= $#{$value} ; $i++)
89         {
90                 if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
91                 {
92                         foreach my $word (split(/ /, @$value[$i]))
93                         {
94                                 unless (C4::Context->stopwords->{uc($word)}) {  #it's NOT a stopword => use it. Otherwise, ignore
95                                         my $tag = substr(@$tags[$i],0,3);
96                                         my $subf = substr(@$tags[$i],3,1);
97                                         push @normal_tags, @$tags[$i];
98                                         push @normal_and_or, "and";     # assumes "foo" and "bar" if "foo bar" is entered
99                                         push @normal_operator, @$operator[$i];
100                                         push @normal_value, $word;
101                                 }
102                         }
103                 }
104                 else
105                 {
106                         push @normal_tags, @$tags[$i];
107                         push @normal_and_or, @$and_or[$i];
108                         push @normal_operator, @$operator[$i];
109                         push @normal_value, @$value[$i];
110                 }
111         }
112
113         # Finds the basic results without the NOT requests
114         my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
115
116         my $sth;
117
118         if ($sql_where2) {
119                 $sth = $dbh->prepare("select distinct m1.authid from auth_header,$sql_tables where  m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where2 and ($sql_where1)");
120                 warn "Q2 : select distinct m1.authid from auth_header,$sql_tables where  m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where2 and ($sql_where1)";
121         } else {
122                 $sth = $dbh->prepare("select distinct m1.authid from auth_header,$sql_tables where  m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where1");
123                 warn "Q : select distinct m1.authid from auth_header,$sql_tables where  m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where1";
124         }
125         $sth->execute($authtypecode);
126         my @result = ();
127         while (my ($authid) = $sth->fetchrow) {
128                         push @result,$authid;
129                 }
130
131         # we have authid list. Now, loads summary from [offset] to [offset]+[length]
132         my $counter = $offset;
133         my @finalresult = ();
134         my $oldline;
135         while (($counter <= $#result) && ($counter <= ($offset + $length))) {
136 #               warn " HERE : $counter, $#result, $offset, $length";
137                 # get MARC::Record of the authority
138                 my $record = AUTHgetauthority($dbh,$result[$counter]);
139                 # then build the summary
140                 my $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
141                 my $authref = getauthtype($authtypecode);
142                 my $summary = $authref->{summary};
143                 my @fields = $record->fields();
144                 foreach my $field (@fields) {
145                         my $tag = $field->tag();
146                         if ($tag<10) {
147                         } else {
148                                 my @subf = $field->subfields;
149                                 for my $i (0..$#subf) {
150                                         my $subfieldcode = $subf[$i][0];
151                                         my $subfieldvalue = $subf[$i][1];
152                                         my $tagsubf = $tag.$subfieldcode;
153                                         $summary =~ s/\[(.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
154                                 }
155                         }
156                 }
157                 $summary =~ s/\[(.*?)]//g;
158                 $summary =~ s/\n/<br>/g;
159
160                 # find biblio MARC field using this authtypecode (to jump to biblio)
161                 my $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
162                 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
163                 $sth->execute($authtypecode);
164                 my $tags_using_authtype;
165                 while (my ($tagfield) = $sth->fetchrow) {
166 #                       warn "TAG : $tagfield";
167                         $tags_using_authtype.= $tagfield."9,";
168                 }
169                 chop $tags_using_authtype;
170                 
171                 # then add a line for the template loop
172                 my %newline;
173                 $newline{summary} = $summary;
174                 $newline{authid} = $result[$counter];
175                 $newline{used} = &AUTHcount_usage($result[$counter]);
176                 $newline{biblio_fields} = $tags_using_authtype;
177                 $counter++;
178                 push @finalresult, \%newline;
179         }
180         my $nbresults = $#result + 1;
181         return (\@finalresult, $nbresults);
182 }
183
184 # Creates the SQL Request
185
186 sub create_request {
187         my ($dbh,$tags, $and_or, $operator, $value) = @_;
188
189         my $sql_tables; # will contain marc_subfield_table as m1,...
190         my $sql_where1; # will contain the "true" where
191         my $sql_where2 = "("; # will contain m1.authid=m2.authid
192         my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
193         my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR  is provided.
194
195
196         for(my $i=0; $i<=@$value;$i++) {
197                 if (@$value[$i]) {
198                         $nb_active++;
199 #                       warn " @$tags[$i]";
200                         if ($nb_active==1) {
201                                 if (@$operator[$i] eq "start") {
202                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
203                                         $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]%");
204                                         if (@$tags[$i]) {
205                                                 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
206                                         }
207                                         $sql_where1.=")";
208                                 } elsif (@$operator[$i] eq "contains") {        
209                                 $sql_tables .= "auth_word as m$nb_table,";
210                                         $sql_where1 .= "(m1.word  like ".$dbh->quote("@$value[$i]%");
211                                         if (@$tags[$i]) {
212                                                  $sql_where1 .=" and m1.tagsubfield in (@$tags[$i])";
213                                         }
214                                         $sql_where1.=")";
215                                 } else {
216
217                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
218                                         $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
219                                         if (@$tags[$i]) {
220                                                  $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
221                                         }
222                                         $sql_where1.=")";
223                                 }
224                         } else {
225                                 if (@$operator[$i] eq "start") {
226                                         $nb_table++;
227                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
228                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]%");
229                                         if (@$tags[$i]) {
230                                                 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
231                                         }
232                                         $sql_where1.=")";
233                                         $sql_where2 .= "m1.authid=m$nb_table.authid and ";
234                                 } elsif (@$operator[$i] eq "contains") {
235                                         if (@$and_or[$i] eq 'and') {
236                                                 $nb_table++;
237                                                 $sql_tables .= "auth_word as m$nb_table,";
238                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
239                                                 if (@$tags[$i]) {
240                                                         $sql_where1 .=" and m$nb_table.tagsubfield in(@$tags[$i])";
241                                                 }
242                                                 $sql_where1.=")";
243                                                 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
244                                         } else {
245                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
246                                                 if (@$tags[$i]) {
247                                                         $sql_where1 .="  and m$nb_table.tag+m$nb_table.subfieldid in (@$tags[$i])";
248                                                 }
249                                                 $sql_where1.=")";
250                                                 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
251                                         }
252                                 } else {
253                                         $nb_table++;
254                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
255                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
256                                         if (@$tags[$i]) {
257                                                 $sql_where1 .="  and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
258                                         }
259                                         $sql_where2 .= "m1.authid=m$nb_table.authid and ";
260                                         $sql_where1.=")";
261                                 }
262                         }
263                 }
264         }
265
266         if($sql_where2 ne "(")  # some datas added to sql_where2, processing
267         {
268                 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
269                 $sql_where2 .= ")";
270         }
271         else    # no sql_where2 statement, deleting '('
272         {
273                 $sql_where2 = "";
274         }
275         chop $sql_tables;       # deletes the trailing ','
276         
277         return ($sql_tables, $sql_where1, $sql_where2);
278 }
279
280
281 sub AUTHcount_usage {
282         my ($authid) = @_;
283         my $dbh = C4::Context->dbh;
284         # find MARC fields using this authtype
285         my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
286         my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
287         $sth->execute($authtypecode);
288         my $tags_using_authtype;
289         while (my ($tagfield) = $sth->fetchrow) {
290 #               warn "TAG : $tagfield";
291                 $tags_using_authtype.= "'".$tagfield."9',";
292         }
293         chop $tags_using_authtype;
294         if ($tags_using_authtype) {
295                 $sth = $dbh->prepare("select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=?");
296         } else {
297                 $sth = $dbh->prepare("select count(*) from marc_subfield_table where subfieldvalue=?");
298         }
299 #       warn "Q : select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=$authid";
300         $sth->execute($authid);
301         my ($result) = $sth->fetchrow;
302 #       warn "Authority $authid TOTAL USED : $result";
303         return $result;
304 }
305
306 # merging 2 authority entries. After a merge, the "from" can be deleted.
307 # sub AUTHmerge {
308 #       my ($auth_merge_from,$auth_merge_to) = @_;
309 #       my $dbh = C4::Context->dbh;
310 #       # find MARC fields using this authtype
311 #       my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
312 #       # retrieve records
313 #       my $record_from = AUTHgetauthority($dbh,$auth_merge_from);
314 #       my $record_to = AUTHgetauthority($dbh,$auth_merge_to);
315 #       my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
316 #       $sth->execute($authtypecode);
317 #       my $tags_using_authtype;
318 #       while (my ($tagfield) = $sth->fetchrow) {
319 #               warn "TAG : $tagfield";
320 #               $tags_using_authtype.= "'".$tagfield."9',";
321 #       }
322 #       chop $tags_using_authtype;
323 #       # now, find every biblio using this authority
324 #       $sth = $dbh->prepare("select bibid,tag,tag_indicator,tagorder from marc_subfield_table where tag+subfieldid in ($tags_using_authtype) and subfieldvalue=?");
325 #       $sth->execute($authid);
326 #       # and delete entries before recreating them
327 #       while (my ($bibid,$tag,$tag_indicator,$tagorder) = $sth->fetchrow) {
328 #               &MARCdelsubfield($dbh,$bibid,$tag);
329 #               
330 #       }
331
332 # }
333
334 sub AUTHfind_authtypecode {
335         my ($dbh,$authid) = @_;
336         my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
337         $sth->execute($authid);
338         my ($authtypecode) = $sth->fetchrow;
339         return $authtypecode;
340 }
341  
342
343 sub AUTHgettagslib {
344         my ($dbh,$forlibrarian,$authtypecode)= @_;
345         $authtypecode="" unless $authtypecode;
346         my $sth;
347         my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
348         # check that framework exists
349         $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
350         $sth->execute($authtypecode);
351         my ($total) = $sth->fetchrow;
352         $authtypecode="" unless ($total >0);
353         $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory,repeatable from auth_tag_structure where authtypecode=? order by tagfield");
354         $sth->execute($authtypecode);
355         my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
356         while ( ($tag,$lib,$mandatory,$repeatable) = $sth->fetchrow) {
357                 $res->{$tag}->{lib}=$lib;
358                 $res->{$tab}->{tab}=""; # XXX
359                 $res->{$tag}->{mandatory}=$mandatory;
360                 $res->{$tag}->{repeatable}=$repeatable;
361         }
362
363         $sth=$dbh->prepare("select tagfield,tagsubfield,$libfield as lib,tab, mandatory, repeatable,authorised_value,value_builder,seealso from auth_subfield_structure where authtypecode=? order by tagfield,tagsubfield");
364         $sth->execute($authtypecode);
365
366         my $subfield;
367         my $authorised_value;
368         my $thesaurus_category;
369         my $value_builder;
370         my $kohafield;
371         my $seealso;
372         my $hidden;
373         my $isurl;
374         while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$value_builder,$seealso) = $sth->fetchrow) {
375                 $res->{$tag}->{$subfield}->{lib}=$lib;
376                 $res->{$tag}->{$subfield}->{tab}=$tab;
377                 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
378                 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
379                 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
380                 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
381                 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
382                 $res->{$tag}->{$subfield}->{seealso}=$seealso;
383                 $res->{$tag}->{$subfield}->{hidden}=$hidden;
384                 $res->{$tag}->{$subfield}->{isurl}=$isurl;
385         }
386         return $res;
387 }
388
389 sub AUTHaddauthority {
390 # pass the MARC::Record to this function, and it will create the records in the marc tables
391         my ($dbh,$record,$authid,$authtypecode) = @_;
392         my @fields=$record->fields();
393 #       warn "IN AUTHaddauthority $authid => ".$record->as_formatted;
394 # adding main table, and retrieving authid
395 # if authid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
396 # if authid empty => true add, find a new authid number
397         unless ($authid) {
398                 $dbh->do("lock tables auth_header WRITE,auth_subfield_table WRITE, auth_word WRITE, stopwords READ");
399                 my $sth=$dbh->prepare("insert into auth_header (datecreated,authtypecode) values (now(),?)");
400                 $sth->execute($authtypecode);
401                 $sth=$dbh->prepare("select max(authid) from auth_header");
402                 $sth->execute;
403                 ($authid)=$sth->fetchrow;
404                 $sth->finish;
405         }
406         my $fieldcount=0;
407         # now, add subfields...
408         foreach my $field (@fields) {
409                 $fieldcount++;
410                 if ($field->tag() <10) {
411                                 &AUTHaddsubfield($dbh,$authid,
412                                                 $field->tag(),
413                                                 '',
414                                                 $fieldcount,
415                                                 '',
416                                                 1,
417                                                 $field->data()
418                                                 );
419                 } else {
420                         my @subfields=$field->subfields();
421                         foreach my $subfieldcount (0..$#subfields) {
422                                 &AUTHaddsubfield($dbh,$authid,
423                                                 $field->tag(),
424                                                 $field->indicator(1).$field->indicator(2),
425                                                 $fieldcount,
426                                                 $subfields[$subfieldcount][0],
427                                                 $subfieldcount+1,
428                                                 $subfields[$subfieldcount][1]
429                                                 );
430                         }
431                 }
432         }
433         $dbh->do("unlock tables");
434         return $authid;
435 }
436
437
438 sub AUTHaddsubfield {
439 # Add a new subfield to a tag into the DB.
440         my ($dbh,$authid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
441         # if not value, end of job, we do nothing
442         if (length($subfieldvalues) ==0) {
443                 return;
444         }
445         if (not($subfieldcode)) {
446                 $subfieldcode=' ';
447         }
448         my @subfieldvalues = split /\|/,$subfieldvalues;
449         foreach my $subfieldvalue (@subfieldvalues) {
450                 my $sth=$dbh->prepare("insert into auth_subfield_table (authid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
451                 $sth->execute($authid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
452                 if ($sth->errstr) {
453                         warn "ERROR ==> insert into auth_subfield_table (authid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values ($authid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue)\n";
454                 }
455                 &AUTHaddword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
456         }
457 }
458
459 sub AUTHgetauthority {
460 # Returns MARC::Record of the biblio passed in parameter.
461     my ($dbh,$authid)=@_;
462     my $record = MARC::Record->new();
463 #---- TODO : the leader is missing
464         $record->leader('                        ');
465     my $sth=$dbh->prepare("select authid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue
466                                  from auth_subfield_table
467                                  where authid=? order by tag,tagorder,subfieldcode
468                          ");
469         $sth->execute($authid);
470         my $prevtagorder=1;
471         my $prevtag='XXX';
472         my $previndicator;
473         my $field; # for >=10 tags
474         my $prevvalue; # for <10 tags
475         while (my $row=$sth->fetchrow_hashref) {
476                 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
477                         $previndicator.="  ";
478                         if ($prevtag <10) {
479                         $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
480                         } else {
481                                 $record->add_fields($field) unless $prevtag eq "XXX";
482                         }
483                         undef $field;
484                         $prevtagorder=$row->{tagorder};
485                         $prevtag = $row->{tag};
486                         $previndicator=$row->{tag_indicator};
487                         if ($row->{tag}<10) {
488                                 $prevvalue = $row->{subfieldvalue};
489                         } else {
490                                 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.'  ',0,1), substr($row->{tag_indicator}.'  ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
491                         }
492                 } else {
493                         if ($row->{tag} <10) {
494                                 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
495                         } else {
496                                 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
497                         }
498                         $prevtag= $row->{tag};
499                         $previndicator=$row->{tag_indicator};
500                 }
501         }
502         # the last has not been included inside the loop... do it now !
503         if ($prevtag ne "XXX") { # check that we have found something. Otherwise, prevtag is still XXX and we
504                                                 # must return an empty record, not make MARC::Record fail because we try to
505                                                 # create a record with XXX as field :-(
506                 if ($prevtag <10) {
507                         $record->add_fields($prevtag,$prevvalue);
508                 } else {
509         #               my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
510                         $record->add_fields($field);
511                 }
512         }
513         return $record;
514 }
515
516 sub AUTHgetauth_type {
517         my ($authtypecode) = @_;
518         my $dbh=C4::Context->dbh;
519         my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
520         $sth->execute($authtypecode);
521         return $sth->fetchrow_hashref;
522 }
523 sub AUTHmodauthority {
524         my ($dbh,$authid,$record,$delete)=@_;
525         my $oldrecord=&AUTHgetauthority($dbh,$authid);
526         if ($oldrecord eq $record) {
527                 return;
528         }
529 # 1st delete the authority,
530 # 2nd recreate it
531         &AUTHdelauthority($dbh,$authid,1);
532         &AUTHaddauthority($dbh,$record,$authid,AUTHfind_authtypecode($dbh,$authid));
533         # save the file in localfile/modified_authorities
534         my $filename = C4::Context->config("intranetdir")."/localfile/modified_authorities/$authid.authid";
535         open AUTH, "> $filename";
536         print AUTH $authid;
537         close AUTH;
538 }
539
540 sub AUTHdelauthority {
541         my ($dbh,$authid,$keep_biblio) = @_;
542 # if the keep_biblio is set to 1, then authority entries in biblio are preserved.
543 # This flag is set when the delauthority is called by modauthority
544 # due to a too complex structure of MARC (repeatable fields and subfields),
545 # the best solution for a modif is to delete / recreate the record.
546
547         my $record = AUTHgetauthority($dbh,$authid);
548         $dbh->do("delete from auth_header where authid=$authid") unless $keep_biblio;
549         $dbh->do("delete from auth_subfield_table where authid=$authid");
550         $dbh->do("delete from auth_word where authid=$authid");
551 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag)
552 }
553
554 sub AUTHmodsubfield {
555 # Subroutine changes a subfield value given a subfieldid.
556         my ($dbh, $subfieldid, $subfieldvalue )=@_;
557         $dbh->do("lock tables auth_subfield_table WRITE");
558         my $sth=$dbh->prepare("update auth_subfield_table set subfieldvalue=? where subfieldid=?");
559         $sth->execute($subfieldvalue, $subfieldid);
560         $dbh->do("unlock tables");
561         $sth->finish;
562         $sth=$dbh->prepare("select authid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from auth_subfield_table where subfieldid=?");
563         $sth->execute($subfieldid);
564         my ($authid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
565         $subfieldid=$x;
566         &AUTHdelword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
567         &AUTHaddword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
568         return($subfieldid, $subfieldvalue);
569 }
570
571 sub AUTHfindsubfield {
572     my ($dbh,$authid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
573     my $resultcounter=0;
574     my $subfieldid;
575     my $lastsubfieldid;
576     my $query="select subfieldid from auth_subfield_table where authid=? and tag=? and subfieldcode=?";
577     my @bind_values = ($authid,$tag, $subfieldcode);
578     if ($subfieldvalue) {
579         $query .= " and subfieldvalue=?";
580         push(@bind_values,$subfieldvalue);
581     } else {
582         if ($subfieldorder<1) {
583             $subfieldorder=1;
584         }
585         $query .= " and subfieldorder=?";
586         push(@bind_values,$subfieldorder);
587     }
588     my $sti=$dbh->prepare($query);
589     $sti->execute(@bind_values);
590     while (($subfieldid) = $sti->fetchrow) {
591         $resultcounter++;
592         $lastsubfieldid=$subfieldid;
593     }
594     if ($resultcounter>1) {
595                 # Error condition.  Values given did not resolve into a unique record.  Don't know what to edit
596                 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
597                 return -1;
598     } else {
599                 return $lastsubfieldid;
600     }
601 }
602
603 sub AUTHfindsubfieldid {
604         my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
605         my $sth=$dbh->prepare("select subfieldid from auth_subfield_table
606                                 where authid=? and tag=? and tagorder=?
607                                         and subfieldcode=? and subfieldorder=?");
608         $sth->execute($authid,$tag,$tagorder,$subfield,$subfieldorder);
609         my ($res) = $sth->fetchrow;
610         unless ($res) {
611                 $sth=$dbh->prepare("select subfieldid from auth_subfield_table
612                                 where authid=? and tag=? and tagorder=?
613                                         and subfieldcode=?");
614                 $sth->execute($authid,$tag,$tagorder,$subfield);
615                 ($res) = $sth->fetchrow;
616         }
617     return $res;
618 }
619
620 sub AUTHfind_authtypecode {
621         my ($dbh,$authid) = @_;
622         my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
623         $sth->execute($authid);
624         my ($authtypecode) = $sth->fetchrow;
625         return $authtypecode;
626 }
627
628 sub AUTHdelsubfield {
629 # delete a subfield for $authid / tag / tagorder / subfield / subfieldorder
630     my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
631     $dbh->do("delete from auth_subfield_table where authid='$authid' and
632                         tag='$tag' and tagorder='$tagorder'
633                         and subfieldcode='$subfield' and subfieldorder='$subfieldorder'
634                         ");
635 }
636
637 sub AUTHhtml2marc {
638         my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
639         my $prevtag = -1;
640         my $record = MARC::Record->new();
641 #       my %subfieldlist=();
642         my $prevvalue; # if tag <10
643         my $field; # if tag >=10
644         for (my $i=0; $i< @$rtags; $i++) {
645                 # rebuild MARC::Record
646                 if (@$rtags[$i] ne $prevtag) {
647                         if ($prevtag < 10) {
648                                 if ($prevvalue) {
649                                         $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
650                                 }
651                         } else {
652                                 if ($field) {
653                                         $record->add_fields($field);
654                                 }
655                         }
656                         $indicators{@$rtags[$i]}.='  ';
657                         if (@$rtags[$i] <10) {
658                                 $prevvalue= @$rvalues[$i];
659                         } else {
660                                 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
661                         }
662                         $prevtag = @$rtags[$i];
663                 } else {
664                         if (@$rtags[$i] <10) {
665                                 $prevvalue=@$rvalues[$i];
666                         } else {
667                                 if (@$rvalues[$i]) {
668                                         $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
669                                 }
670                         }
671                         $prevtag= @$rtags[$i];
672                 }
673         }
674         # the last has not been included inside the loop... do it now !
675         $record->add_fields($field);
676 #       warn $record->as_formatted;
677         return $record;
678 }
679
680 sub AUTHaddword {
681 # split a subfield string and adds it into the word table.
682 # removes stopwords
683     my ($dbh,$authid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
684     $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
685     my @words = split / /,$sentence;
686     my $stopwords= C4::Context->stopwords;
687     my $sth=$dbh->prepare("insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word)
688                         values (?,concat(?,?),?,?,?,soundex(?))");
689     foreach my $word (@words) {
690 # we record only words longer than 2 car and not in stopwords hash
691         if (length($word)>2 and !($stopwords->{uc($word)})) {
692             $sth->execute($authid,$tag,$subfieldid,$tagorder,$subfieldorder,$word,$word);
693             if ($sth->err()) {
694                 warn "ERROR ==> insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word) values ($authid,concat($tag,$subfieldid),$tagorder,$subfieldorder,$word,soundex($word))\n";
695             }
696         }
697     }
698 }
699
700 sub AUTHdelword {
701 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
702     my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
703     my $sth=$dbh->prepare("delete from auth_word where authid=? and tagsubfield=concat(?,?) and tagorder=? and subfieldorder=?");
704     $sth->execute($authid,$tag,$subfield,$tagorder,$subfieldorder);
705 }
706
707 sub char_decode {
708         # converts ISO 5426 coded string to ISO 8859-1
709         # sloppy code : should be improved in next issue
710         my ($string,$encoding) = @_ ;
711         $_ = $string ;
712 #       $encoding = C4::Context->preference("marcflavour") unless $encoding;
713         if ($encoding eq "UNIMARC") {
714                 s/\xe1/Æ/gm ;
715                 s/\xe2/Ð/gm ;
716                 s/\xe9/Ø/gm ;
717                 s/\xec/þ/gm ;
718                 s/\xf1/æ/gm ;
719                 s/\xf3/ð/gm ;
720                 s/\xf9/ø/gm ;
721                 s/\xfb/ß/gm ;
722                 s/\xc1\x61/à/gm ;
723                 s/\xc1\x65/è/gm ;
724                 s/\xc1\x69/ì/gm ;
725                 s/\xc1\x6f/ò/gm ;
726                 s/\xc1\x75/ù/gm ;
727                 s/\xc1\x41/À/gm ;
728                 s/\xc1\x45/È/gm ;
729                 s/\xc1\x49/Ì/gm ;
730                 s/\xc1\x4f/Ò/gm ;
731                 s/\xc1\x55/Ù/gm ;
732                 s/\xc2\x41/Á/gm ;
733                 s/\xc2\x45/É/gm ;
734                 s/\xc2\x49/Í/gm ;
735                 s/\xc2\x4f/Ó/gm ;
736                 s/\xc2\x55/Ú/gm ;
737                 s/\xc2\x59/Ý/gm ;
738                 s/\xc2\x61/á/gm ;
739                 s/\xc2\x65/é/gm ;
740                 s/\xc2\x69/í/gm ;
741                 s/\xc2\x6f/ó/gm ;
742                 s/\xc2\x75/ú/gm ;
743                 s/\xc2\x79/ý/gm ;
744                 s/\xc3\x41/Â/gm ;
745                 s/\xc3\x45/Ê/gm ;
746                 s/\xc3\x49/Î/gm ;
747                 s/\xc3\x4f/Ô/gm ;
748                 s/\xc3\x55/Û/gm ;
749                 s/\xc3\x61/â/gm ;
750                 s/\xc3\x65/ê/gm ;
751                 s/\xc3\x69/î/gm ;
752                 s/\xc3\x6f/ô/gm ;
753                 s/\xc3\x75/û/gm ;
754                 s/\xc4\x41/Ã/gm ;
755                 s/\xc4\x4e/Ñ/gm ;
756                 s/\xc4\x4f/Õ/gm ;
757                 s/\xc4\x61/ã/gm ;
758                 s/\xc4\x6e/ñ/gm ;
759                 s/\xc4\x6f/õ/gm ;
760                 s/\xc8\x45/Ë/gm ;
761                 s/\xc8\x49/Ï/gm ;
762                 s/\xc8\x65/ë/gm ;
763                 s/\xc8\x69/ï/gm ;
764                 s/\xc8\x76/ÿ/gm ;
765                 s/\xc9\x41/Ä/gm ;
766                 s/\xc9\x4f/Ö/gm ;
767                 s/\xc9\x55/Ü/gm ;
768                 s/\xc9\x61/ä/gm ;
769                 s/\xc9\x6f/ö/gm ;
770                 s/\xc9\x75/ü/gm ;
771                 s/\xca\x41/Å/gm ;
772                 s/\xca\x61/å/gm ;
773                 s/\xd0\x43/Ç/gm ;
774                 s/\xd0\x63/ç/gm ;
775                 # this handles non-sorting blocks (if implementation requires this)
776                 $string = nsb_clean($_) ;
777         } elsif ($encoding eq "USMARC" || $encoding eq "MARC21") {
778                 if(/[\xc1-\xff]/) {
779                         s/\xe1\x61/à/gm ;
780                         s/\xe1\x65/è/gm ;
781                         s/\xe1\x69/ì/gm ;
782                         s/\xe1\x6f/ò/gm ;
783                         s/\xe1\x75/ù/gm ;
784                         s/\xe1\x41/À/gm ;
785                         s/\xe1\x45/È/gm ;
786                         s/\xe1\x49/Ì/gm ;
787                         s/\xe1\x4f/Ò/gm ;
788                         s/\xe1\x55/Ù/gm ;
789                         s/\xe2\x41/Á/gm ;
790                         s/\xe2\x45/É/gm ;
791                         s/\xe2\x49/Í/gm ;
792                         s/\xe2\x4f/Ó/gm ;
793                         s/\xe2\x55/Ú/gm ;
794                         s/\xe2\x59/Ý/gm ;
795                         s/\xe2\x61/á/gm ;
796                         s/\xe2\x65/é/gm ;
797                         s/\xe2\x69/í/gm ;
798                         s/\xe2\x6f/ó/gm ;
799                         s/\xe2\x75/ú/gm ;
800                         s/\xe2\x79/ý/gm ;
801                         s/\xe3\x41/Â/gm ;
802                         s/\xe3\x45/Ê/gm ;
803                         s/\xe3\x49/Î/gm ;
804                         s/\xe3\x4f/Ô/gm ;
805                         s/\xe3\x55/Û/gm ;
806                         s/\xe3\x61/â/gm ;
807                         s/\xe3\x65/ê/gm ;
808                         s/\xe3\x69/î/gm ;
809                         s/\xe3\x6f/ô/gm ;
810                         s/\xe3\x75/û/gm ;
811                         s/\xe4\x41/Ã/gm ;
812                         s/\xe4\x4e/Ñ/gm ;
813                         s/\xe4\x4f/Õ/gm ;
814                         s/\xe4\x61/ã/gm ;
815                         s/\xe4\x6e/ñ/gm ;
816                         s/\xe4\x6f/õ/gm ;
817                         s/\xe8\x45/Ë/gm ;
818                         s/\xe8\x49/Ï/gm ;
819                         s/\xe8\x65/ë/gm ;
820                         s/\xe8\x69/ï/gm ;
821                         s/\xe8\x76/ÿ/gm ;
822                         s/\xe9\x41/Ä/gm ;
823                         s/\xe9\x4f/Ö/gm ;
824                         s/\xe9\x55/Ü/gm ;
825                         s/\xe9\x61/ä/gm ;
826                         s/\xe9\x6f/ö/gm ;
827                         s/\xe9\x75/ü/gm ;
828                         s/\xea\x41/Å/gm ;
829                         s/\xea\x61/å/gm ;
830                         # this handles non-sorting blocks (if implementation requires this)
831                         $string = nsb_clean($_) ;
832                 }
833         }
834         return($string) ;
835 }
836
837 sub nsb_clean {
838         my $NSB = '\x88' ;              # NSB : begin Non Sorting Block
839         my $NSE = '\x89' ;              # NSE : Non Sorting Block end
840         # handles non sorting blocks
841         my ($string) = @_ ;
842         $_ = $string ;
843         s/$NSB/(/gm ;
844         s/[ ]{0,1}$NSE/) /gm ;
845         $string = $_ ;
846         return($string) ;
847 }
848
849 sub FindDuplicate {
850         my ($record,$authtypecode)=@_;
851         warn "IN for ".$record->as_formatted;
852         my $dbh = C4::Context->dbh;
853
854 #       warn "".$record->as_formatted;
855         my $sth = $dbh->prepare("select auth_tag_to_report,summary from auth_types where authtypecode=?");
856         $sth->execute($authtypecode);
857         my ($auth_tag_to_report,$taglist) = $sth->fetchrow;
858         $sth->finish;
859         # build a request for authoritysearch
860         my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
861         # search on biblio.title
862 #       warn " tag a reporter : $auth_tag_to_report";
863 #       warn "taglist ".$taglist;
864         my @subfield = split /\[/,  $taglist;
865         my $max = @subfield;
866         for (my $i=1; $i<$max;$i++){
867                 warn " ".$subfield[$i];
868                 $subfield[$i]=substr($subfield[$i],3,1);
869 #               warn " ".$subfield[$i];
870         }
871         
872         if ($record->fields($auth_tag_to_report)) {
873                 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where tagfield=? and authtypecode=? ");
874                 $sth->execute($auth_tag_to_report,$authtypecode);
875 #               warn " field $auth_tag_to_report exists";
876                 while (my ($tag,$subfield) = $sth->fetchrow){
877                         if ($record->field($tag)->subfield($subfield)) {
878                                 warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
879                                 push @tags, $tag.$subfield;
880 #                               warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
881                                 push @and_or, "and";
882                                 push @excluding, "";
883                                 push @operator, "=";
884                                 push @value, $record->field($tag)->subfield($subfield);
885                         }
886                 }
887         }
888  
889         my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
890         # there is at least 1 result => return the 1st one
891         if ($nbresult) {
892                 warn "XXXXX $nbresult => ".@$finalresult[0]->{authid},@$finalresult[0]->{summary};
893                 return @$finalresult[0]->{authid},@$finalresult[0]->{summary};
894         }
895         # no result, returns nothing
896         return;
897 }
898
899
900 END { }       # module clean-up code here (global destructor)
901
902 =back
903
904 =head1 AUTHOR
905
906 Koha Developement team <info@koha.org>
907
908 Paul POULAIN paul.poulain@free.fr
909
910 =cut
911
912 # $Id$
913 # $Log$
914 # Revision 1.16  2005/05/04 15:43:43  tipaul
915 # synch'ing 2.2 and head
916 #
917 # Revision 1.9.2.3  2005/04/28 08:45:33  tipaul
918 # porting FindDuplicate feature for authorities from HEAD to rel_2_2, works correctly now.
919 #
920 # Revision 1.9.2.2  2005/02/28 14:03:13  tipaul
921 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
922 # * adding a select box to requet "contain" or "begin with" search.
923 # * fixing some bug in authority search (related to "main entry" search)
924 #
925 # Revision 1.9.2.1  2005/02/24 13:12:13  tipaul
926 # saving authority modif in a text file. This will be used soon with another script (in crontab). The script in crontab will retrieve every authorityid in the directory localfile/authorities and modify every biblio using this authority. Those modifs may be long. So they can't be done through http, because we may encounter a webserver timeout, and kill the process before end of the job.
927 # So, it will be done through a cron job.
928 # (/me agree we need some doc for command line scripts)
929 #
930 # Revision 1.9  2004/12/23 09:48:11  tipaul
931 # Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
932 #
933 # Revision 1.8  2004/11/05 10:11:39  tipaul
934 # export auth_count_usage (bugfix)
935 #
936 # Revision 1.7  2004/09/23 16:13:00  tipaul
937 # Bugfix in modification
938 #
939 # Revision 1.6  2004/08/18 16:00:24  tipaul
940 # fixes for authorities management
941 #
942 # Revision 1.5  2004/07/05 13:37:22  doxulting
943 # First step for working authorities
944 #
945 # Revision 1.4  2004/06/22 11:35:37  tipaul
946 # removing % at the beginning of a string to avoid loooonnnngggg searchs
947 #
948 # Revision 1.3  2004/06/17 08:02:13  tipaul
949 # merging tag & subfield in auth_word for better perfs
950 #
951 # Revision 1.2  2004/06/10 08:29:01  tipaul
952 # MARC authority management (continued)
953 #
954 # Revision 1.1  2004/06/07 07:35:01  tipaul
955 # MARC authority management package
956 #
957 package C4::AuthoritiesMarc;
958 # Copyright 2000-2002 Katipo Communications
959 #
960 # This file is part of Koha.
961 #
962 # Koha is free software; you can redistribute it and/or modify it under the
963 # terms of the GNU General Public License as published by the Free Software
964 # Foundation; either version 2 of the License, or (at your option) any later
965 # version.
966 #
967 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
968 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
969 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
970 #
971 # You should have received a copy of the GNU General Public License along with
972 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
973 # Suite 330, Boston, MA  02111-1307 USA
974
975 use strict;
976 require Exporter;
977 use C4::Context;
978 use C4::Database;
979 use C4::Koha;
980 use MARC::Record;
981 use C4::Biblio;
982
983 use vars qw($VERSION @ISA @EXPORT);
984
985 # set the version for version checking
986 $VERSION = 0.01;
987
988 @ISA = qw(Exporter);
989 @EXPORT = qw(
990         &AUTHgettagslib
991         &AUTHfindsubfield
992         &AUTHfind_authtypecode
993
994         &AUTHaddauthority
995         &AUTHmodauthority
996         &AUTHdelauthority
997         &AUTHaddsubfield
998         &AUTHgetauthority
999         
1000         &AUTHgetauth_type
1001         &AUTHcount_usage
1002         
1003         &authoritysearch
1004         
1005         &MARCmodsubfield
1006         &AUTHhtml2marc
1007         &AUTHaddword
1008         &MARCaddword &MARCdelword
1009         &char_decode
1010         &FindDuplicate
1011  );
1012
1013 sub authoritysearch {
1014         my ($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode) = @_;
1015         # build the sql request. She will look like :
1016         # select m1.bibid
1017         #               from auth_subfield_table as m1, auth_subfield_table as m2
1018         #               where m1.authid=m2.authid and
1019         #               (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
1020
1021         # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
1022         # the authtypecode. Then, search on $a of this tag_to_report
1023         for (my $i=0;$i<$#{$tags};$i++) {
1024                 if (@$tags[$i] eq "mainentry") {
1025                         my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
1026                         $sth->execute($authtypecode);
1027                         my ($tag_to_report) = $sth->fetchrow;
1028                         @$tags[$i] = $tag_to_report."a";
1029                 }
1030         }
1031
1032         # "Normal" statements
1033         # quote marc fields/subfields
1034         for (my $i=0;$i<=$#{$tags};$i++) {
1035 #               warn " $i: ".@$tags[$i];
1036                 if (@$tags[$i]) {
1037                         @$tags[$i] = $dbh->quote(@$tags[$i]);
1038 #                       warn " $i After process: ".@$tags[$i];
1039                 }
1040         }
1041         my @normal_tags = ();
1042         my @normal_and_or = ();
1043         my @normal_operator = ();
1044         my @normal_value = ();
1045         # Extracts the NOT statements from the list of statements
1046         for(my $i = 0 ; $i <= $#{$value} ; $i++)
1047         {
1048                 if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
1049                 {
1050                         foreach my $word (split(/ /, @$value[$i]))
1051                         {
1052                                 unless (C4::Context->stopwords->{uc($word)}) {  #it's NOT a stopword => use it. Otherwise, ignore
1053                                         my $tag = substr(@$tags[$i],0,3);
1054                                         my $subf = substr(@$tags[$i],3,1);
1055                                         push @normal_tags, @$tags[$i];
1056                                         push @normal_and_or, "and";     # assumes "foo" and "bar" if "foo bar" is entered
1057                                         push @normal_operator, @$operator[$i];
1058                                         push @normal_value, $word;
1059                                 }
1060                         }
1061                 }
1062                 else
1063                 {
1064                         push @normal_tags, @$tags[$i];
1065                         push @normal_and_or, @$and_or[$i];
1066                         push @normal_operator, @$operator[$i];
1067                         push @normal_value, @$value[$i];
1068                 }
1069         }
1070
1071         # Finds the basic results without the NOT requests
1072         my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
1073
1074         my $sth;
1075
1076         if ($sql_where2) {
1077                 $sth = $dbh->prepare("select distinct m1.authid from auth_header,$sql_tables where  m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where2 and ($sql_where1)");
1078                 warn "Q2 : select distinct m1.authid from auth_header,$sql_tables where  m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where2 and ($sql_where1)";
1079         } else {
1080                 $sth = $dbh->prepare("select distinct m1.authid from auth_header,$sql_tables where  m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where1");
1081                 warn "Q : select distinct m1.authid from auth_header,$sql_tables where  m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where1";
1082         }
1083         $sth->execute($authtypecode);
1084         my @result = ();
1085         while (my ($authid) = $sth->fetchrow) {
1086                         push @result,$authid;
1087                 }
1088
1089         # we have authid list. Now, loads summary from [offset] to [offset]+[length]
1090         my $counter = $offset;
1091         my @finalresult = ();
1092         my $oldline;
1093         while (($counter <= $#result) && ($counter <= ($offset + $length))) {
1094 #               warn " HERE : $counter, $#result, $offset, $length";
1095                 # get MARC::Record of the authority
1096                 my $record = AUTHgetauthority($dbh,$result[$counter]);
1097                 # then build the summary
1098                 my $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
1099                 my $authref = getauthtype($authtypecode);
1100                 my $summary = $authref->{summary};
1101                 my @fields = $record->fields();
1102                 foreach my $field (@fields) {
1103                         my $tag = $field->tag();
1104                         if ($tag<10) {
1105                         } else {
1106                                 my @subf = $field->subfields;
1107                                 for my $i (0..$#subf) {
1108                                         my $subfieldcode = $subf[$i][0];
1109                                         my $subfieldvalue = $subf[$i][1];
1110                                         my $tagsubf = $tag.$subfieldcode;
1111                                         $summary =~ s/\[(.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
1112                                 }
1113                         }
1114                 }
1115                 $summary =~ s/\[(.*?)]//g;
1116                 $summary =~ s/\n/<br>/g;
1117
1118                 # find biblio MARC field using this authtypecode (to jump to biblio)
1119                 my $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
1120                 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1121                 $sth->execute($authtypecode);
1122                 my $tags_using_authtype;
1123                 while (my ($tagfield) = $sth->fetchrow) {
1124 #                       warn "TAG : $tagfield";
1125                         $tags_using_authtype.= $tagfield."9,";
1126                 }
1127                 chop $tags_using_authtype;
1128                 
1129                 # then add a line for the template loop
1130                 my %newline;
1131                 $newline{summary} = $summary;
1132                 $newline{authid} = $result[$counter];
1133                 $newline{used} = &AUTHcount_usage($result[$counter]);
1134                 $newline{biblio_fields} = $tags_using_authtype;
1135                 $newline{marcrecord} = $record;
1136                 $counter++;
1137                 push @finalresult, \%newline;
1138         }
1139         my $nbresults = $#result + 1;
1140         return (\@finalresult, $nbresults);
1141 }
1142
1143 # Creates the SQL Request
1144
1145 sub create_request {
1146         my ($dbh,$tags, $and_or, $operator, $value) = @_;
1147
1148         my $sql_tables; # will contain marc_subfield_table as m1,...
1149         my $sql_where1; # will contain the "true" where
1150         my $sql_where2 = "("; # will contain m1.authid=m2.authid
1151         my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
1152         my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR  is provided.
1153
1154
1155         for(my $i=0; $i<=@$value;$i++) {
1156                 if (@$value[$i]) {
1157                         $nb_active++;
1158 #                       warn " @$tags[$i]";
1159                         if ($nb_active==1) {
1160                                 if (@$operator[$i] eq "start") {
1161                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
1162                                         $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]%");
1163                                         if (@$tags[$i]) {
1164                                                 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
1165                                         }
1166                                         $sql_where1.=")";
1167                                 } elsif (@$operator[$i] eq "contains") {        
1168                                 $sql_tables .= "auth_word as m$nb_table,";
1169                                         $sql_where1 .= "(m1.word  like ".$dbh->quote("@$value[$i]%");
1170                                         if (@$tags[$i]) {
1171                                                  $sql_where1 .=" and m1.tagsubfield in (@$tags[$i])";
1172                                         }
1173                                         $sql_where1.=")";
1174                                 } else {
1175
1176                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
1177                                         $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
1178                                         if (@$tags[$i]) {
1179                                                  $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
1180                                         }
1181                                         $sql_where1.=")";
1182                                 }
1183                         } else {
1184                                 if (@$operator[$i] eq "start") {
1185                                         $nb_table++;
1186                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
1187                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]%");
1188                                         if (@$tags[$i]) {
1189                                                 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
1190                                         }
1191                                         $sql_where1.=")";
1192                                         $sql_where2 .= "m1.authid=m$nb_table.authid and ";
1193                                 } elsif (@$operator[$i] eq "contains") {
1194                                         if (@$and_or[$i] eq 'and') {
1195                                                 $nb_table++;
1196                                                 $sql_tables .= "auth_word as m$nb_table,";
1197                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
1198                                                 if (@$tags[$i]) {
1199                                                         $sql_where1 .=" and m$nb_table.tagsubfield in(@$tags[$i])";
1200                                                 }
1201                                                 $sql_where1.=")";
1202                                                 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
1203                                         } else {
1204                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
1205                                                 if (@$tags[$i]) {
1206                                                         $sql_where1 .="  and m$nb_table.tag+m$nb_table.subfieldid in (@$tags[$i])";
1207                                                 }
1208                                                 $sql_where1.=")";
1209                                                 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
1210                                         }
1211                                 } else {
1212                                         $nb_table++;
1213                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
1214                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
1215                                         if (@$tags[$i]) {
1216                                                 $sql_where1 .="  and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
1217                                         }
1218                                         $sql_where2 .= "m1.authid=m$nb_table.authid and ";
1219                                         $sql_where1.=")";
1220                                 }
1221                         }
1222                 }
1223         }
1224
1225         if($sql_where2 ne "(")  # some datas added to sql_where2, processing
1226         {
1227                 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
1228                 $sql_where2 .= ")";
1229         }
1230         else    # no sql_where2 statement, deleting '('
1231         {
1232                 $sql_where2 = "";
1233         }
1234         chop $sql_tables;       # deletes the trailing ','
1235         
1236         return ($sql_tables, $sql_where1, $sql_where2);
1237 }
1238
1239
1240 sub AUTHcount_usage {
1241         my ($authid) = @_;
1242         my $dbh = C4::Context->dbh;
1243         # find MARC fields using this authtype
1244         my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
1245         my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1246         $sth->execute($authtypecode);
1247         my $tags_using_authtype;
1248         while (my ($tagfield) = $sth->fetchrow) {
1249 #               warn "TAG : $tagfield";
1250                 $tags_using_authtype.= "'".$tagfield."9',";
1251         }
1252         chop $tags_using_authtype;
1253         if ($tags_using_authtype) {
1254                 $sth = $dbh->prepare("select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=?");
1255         } else {
1256                 $sth = $dbh->prepare("select count(*) from marc_subfield_table where subfieldvalue=?");
1257         }
1258 #       warn "Q : select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=$authid";
1259         $sth->execute($authid);
1260         my ($result) = $sth->fetchrow;
1261 #       warn "Authority $authid TOTAL USED : $result";
1262         return $result;
1263 }
1264
1265 # merging 2 authority entries. After a merge, the "from" can be deleted.
1266 # sub AUTHmerge {
1267 #       my ($auth_merge_from,$auth_merge_to) = @_;
1268 #       my $dbh = C4::Context->dbh;
1269 #       # find MARC fields using this authtype
1270 #       my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
1271 #       # retrieve records
1272 #       my $record_from = AUTHgetauthority($dbh,$auth_merge_from);
1273 #       my $record_to = AUTHgetauthority($dbh,$auth_merge_to);
1274 #       my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
1275 #       $sth->execute($authtypecode);
1276 #       my $tags_using_authtype;
1277 #       while (my ($tagfield) = $sth->fetchrow) {
1278 #               warn "TAG : $tagfield";
1279 #               $tags_using_authtype.= "'".$tagfield."9',";
1280 #       }
1281 #       chop $tags_using_authtype;
1282 #       # now, find every biblio using this authority
1283 #       $sth = $dbh->prepare("select bibid,tag,tag_indicator,tagorder from marc_subfield_table where tag+subfieldid in ($tags_using_authtype) and subfieldvalue=?");
1284 #       $sth->execute($authid);
1285 #       # and delete entries before recreating them
1286 #       while (my ($bibid,$tag,$tag_indicator,$tagorder) = $sth->fetchrow) {
1287 #               &MARCdelsubfield($dbh,$bibid,$tag);
1288 #               
1289 #       }
1290
1291 # }
1292
1293 sub AUTHfind_authtypecode {
1294         my ($dbh,$authid) = @_;
1295         my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
1296         $sth->execute($authid);
1297         my ($authtypecode) = $sth->fetchrow;
1298         return $authtypecode;
1299 }
1300  
1301
1302 sub AUTHgettagslib {
1303         my ($dbh,$forlibrarian,$authtypecode)= @_;
1304         $authtypecode="" unless $authtypecode;
1305         my $sth;
1306         my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
1307         # check that framework exists
1308         $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
1309         $sth->execute($authtypecode);
1310         my ($total) = $sth->fetchrow;
1311         $authtypecode="" unless ($total >0);
1312         $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory,repeatable from auth_tag_structure where authtypecode=? order by tagfield");
1313         $sth->execute($authtypecode);
1314         my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
1315         while ( ($tag,$lib,$mandatory,$repeatable) = $sth->fetchrow) {
1316                 $res->{$tag}->{lib}=$lib;
1317                 $res->{$tab}->{tab}=""; # XXX
1318                 $res->{$tag}->{mandatory}=$mandatory;
1319                 $res->{$tag}->{repeatable}=$repeatable;
1320         }
1321
1322         $sth=$dbh->prepare("select tagfield,tagsubfield,$libfield as lib,tab, mandatory, repeatable,authorised_value,value_builder,seealso from auth_subfield_structure where authtypecode=? order by tagfield,tagsubfield");
1323         $sth->execute($authtypecode);
1324
1325         my $subfield;
1326         my $authorised_value;
1327         my $thesaurus_category;
1328         my $value_builder;
1329         my $kohafield;
1330         my $seealso;
1331         my $hidden;
1332         my $isurl;
1333         while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$value_builder,$seealso) = $sth->fetchrow) {
1334                 $res->{$tag}->{$subfield}->{lib}=$lib;
1335                 $res->{$tag}->{$subfield}->{tab}=$tab;
1336                 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
1337                 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
1338                 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
1339                 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
1340                 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
1341                 $res->{$tag}->{$subfield}->{seealso}=$seealso;
1342                 $res->{$tag}->{$subfield}->{hidden}=$hidden;
1343                 $res->{$tag}->{$subfield}->{isurl}=$isurl;
1344         }
1345         return $res;
1346 }
1347
1348 sub AUTHaddauthority {
1349 # pass the MARC::Record to this function, and it will create the records in the marc tables
1350         my ($dbh,$record,$authid,$authtypecode) = @_;
1351         my @fields=$record->fields();
1352 #       warn "IN AUTHaddauthority $authid => ".$record->as_formatted;
1353 # adding main table, and retrieving authid
1354 # if authid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
1355 # if authid empty => true add, find a new authid number
1356         unless ($authid) {
1357                 $dbh->do("lock tables auth_header WRITE,auth_subfield_table WRITE, auth_word WRITE, stopwords READ");
1358                 my $sth=$dbh->prepare("insert into auth_header (datecreated,authtypecode) values (now(),?)");
1359                 $sth->execute($authtypecode);
1360                 $sth=$dbh->prepare("select max(authid) from auth_header");
1361                 $sth->execute;
1362                 ($authid)=$sth->fetchrow;
1363                 $sth->finish;
1364         }
1365         my $fieldcount=0;
1366         # now, add subfields...
1367         foreach my $field (@fields) {
1368                 $fieldcount++;
1369                 if ($field->tag() <10) {
1370                                 &AUTHaddsubfield($dbh,$authid,
1371                                                 $field->tag(),
1372                                                 '',
1373                                                 $fieldcount,
1374                                                 '',
1375                                                 1,
1376                                                 $field->data()
1377                                                 );
1378                 } else {
1379                         my @subfields=$field->subfields();
1380                         foreach my $subfieldcount (0..$#subfields) {
1381                                 &AUTHaddsubfield($dbh,$authid,
1382                                                 $field->tag(),
1383                                                 $field->indicator(1).$field->indicator(2),
1384                                                 $fieldcount,
1385                                                 $subfields[$subfieldcount][0],
1386                                                 $subfieldcount+1,
1387                                                 $subfields[$subfieldcount][1]
1388                                                 );
1389                         }
1390                 }
1391         }
1392         $dbh->do("unlock tables");
1393         return $authid;
1394 }
1395
1396
1397 sub AUTHaddsubfield {
1398 # Add a new subfield to a tag into the DB.
1399         my ($dbh,$authid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
1400         # if not value, end of job, we do nothing
1401         if (length($subfieldvalues) ==0) {
1402                 return;
1403         }
1404         if (not($subfieldcode)) {
1405                 $subfieldcode=' ';
1406         }
1407         my @subfieldvalues = split /\|/,$subfieldvalues;
1408         foreach my $subfieldvalue (@subfieldvalues) {
1409                 my $sth=$dbh->prepare("insert into auth_subfield_table (authid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
1410                 $sth->execute($authid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
1411                 if ($sth->errstr) {
1412                         warn "ERROR ==> insert into auth_subfield_table (authid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values ($authid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue)\n";
1413                 }
1414                 &AUTHaddword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
1415         }
1416 }
1417
1418 sub AUTHgetauthority {
1419 # Returns MARC::Record of the biblio passed in parameter.
1420     my ($dbh,$authid)=@_;
1421     my $record = MARC::Record->new();
1422 #---- TODO : the leader is missing
1423         $record->leader('                        ');
1424     my $sth=$dbh->prepare("select authid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue
1425                                  from auth_subfield_table
1426                                  where authid=? order by tag,tagorder,subfieldcode
1427                          ");
1428         $sth->execute($authid);
1429         my $prevtagorder=1;
1430         my $prevtag='XXX';
1431         my $previndicator;
1432         my $field; # for >=10 tags
1433         my $prevvalue; # for <10 tags
1434         while (my $row=$sth->fetchrow_hashref) {
1435                 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
1436                         $previndicator.="  ";
1437                         if ($prevtag <10) {
1438                         $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
1439                         } else {
1440                                 $record->add_fields($field) unless $prevtag eq "XXX";
1441                         }
1442                         undef $field;
1443                         $prevtagorder=$row->{tagorder};
1444                         $prevtag = $row->{tag};
1445                         $previndicator=$row->{tag_indicator};
1446                         if ($row->{tag}<10) {
1447                                 $prevvalue = $row->{subfieldvalue};
1448                         } else {
1449                                 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.'  ',0,1), substr($row->{tag_indicator}.'  ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
1450                         }
1451                 } else {
1452                         if ($row->{tag} <10) {
1453                                 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
1454                         } else {
1455                                 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
1456                         }
1457                         $prevtag= $row->{tag};
1458                         $previndicator=$row->{tag_indicator};
1459                 }
1460         }
1461         # the last has not been included inside the loop... do it now !
1462         if ($prevtag ne "XXX") { # check that we have found something. Otherwise, prevtag is still XXX and we
1463                                                 # must return an empty record, not make MARC::Record fail because we try to
1464                                                 # create a record with XXX as field :-(
1465                 if ($prevtag <10) {
1466                         $record->add_fields($prevtag,$prevvalue);
1467                 } else {
1468         #               my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
1469                         $record->add_fields($field);
1470                 }
1471         }
1472         return $record;
1473 }
1474
1475 sub AUTHgetauth_type {
1476         my ($authtypecode) = @_;
1477         my $dbh=C4::Context->dbh;
1478         my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
1479         $sth->execute($authtypecode);
1480         return $sth->fetchrow_hashref;
1481 }
1482 sub AUTHmodauthority {
1483         my ($dbh,$authid,$record,$delete)=@_;
1484         my $oldrecord=&AUTHgetauthority($dbh,$authid);
1485         if ($oldrecord eq $record) {
1486                 return;
1487         }
1488 # 1st delete the authority,
1489 # 2nd recreate it
1490         &AUTHdelauthority($dbh,$authid,1);
1491         &AUTHaddauthority($dbh,$record,$authid,AUTHfind_authtypecode($dbh,$authid));
1492         # save the file in localfile/modified_authorities
1493         my $filename = C4::Context->config("intranetdir")."/localfile/modified_authorities/$authid.authid";
1494         open AUTH, "> $filename";
1495         print AUTH $authid;
1496         close AUTH;
1497 }
1498
1499 sub AUTHdelauthority {
1500         my ($dbh,$authid,$keep_biblio) = @_;
1501 # if the keep_biblio is set to 1, then authority entries in biblio are preserved.
1502 # This flag is set when the delauthority is called by modauthority
1503 # due to a too complex structure of MARC (repeatable fields and subfields),
1504 # the best solution for a modif is to delete / recreate the record.
1505
1506         my $record = AUTHgetauthority($dbh,$authid);
1507         $dbh->do("delete from auth_header where authid=$authid") unless $keep_biblio;
1508         $dbh->do("delete from auth_subfield_table where authid=$authid");
1509         $dbh->do("delete from auth_word where authid=$authid");
1510 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag)
1511 }
1512
1513 sub AUTHmodsubfield {
1514 # Subroutine changes a subfield value given a subfieldid.
1515         my ($dbh, $subfieldid, $subfieldvalue )=@_;
1516         $dbh->do("lock tables auth_subfield_table WRITE");
1517         my $sth=$dbh->prepare("update auth_subfield_table set subfieldvalue=? where subfieldid=?");
1518         $sth->execute($subfieldvalue, $subfieldid);
1519         $dbh->do("unlock tables");
1520         $sth->finish;
1521         $sth=$dbh->prepare("select authid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from auth_subfield_table where subfieldid=?");
1522         $sth->execute($subfieldid);
1523         my ($authid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
1524         $subfieldid=$x;
1525         &AUTHdelword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
1526         &AUTHaddword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
1527         return($subfieldid, $subfieldvalue);
1528 }
1529
1530 sub AUTHfindsubfield {
1531     my ($dbh,$authid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
1532     my $resultcounter=0;
1533     my $subfieldid;
1534     my $lastsubfieldid;
1535     my $query="select subfieldid from auth_subfield_table where authid=? and tag=? and subfieldcode=?";
1536     my @bind_values = ($authid,$tag, $subfieldcode);
1537     if ($subfieldvalue) {
1538         $query .= " and subfieldvalue=?";
1539         push(@bind_values,$subfieldvalue);
1540     } else {
1541         if ($subfieldorder<1) {
1542             $subfieldorder=1;
1543         }
1544         $query .= " and subfieldorder=?";
1545         push(@bind_values,$subfieldorder);
1546     }
1547     my $sti=$dbh->prepare($query);
1548     $sti->execute(@bind_values);
1549     while (($subfieldid) = $sti->fetchrow) {
1550         $resultcounter++;
1551         $lastsubfieldid=$subfieldid;
1552     }
1553     if ($resultcounter>1) {
1554                 # Error condition.  Values given did not resolve into a unique record.  Don't know what to edit
1555                 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
1556                 return -1;
1557     } else {
1558                 return $lastsubfieldid;
1559     }
1560 }
1561
1562 sub AUTHfindsubfieldid {
1563         my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1564         my $sth=$dbh->prepare("select subfieldid from auth_subfield_table
1565                                 where authid=? and tag=? and tagorder=?
1566                                         and subfieldcode=? and subfieldorder=?");
1567         $sth->execute($authid,$tag,$tagorder,$subfield,$subfieldorder);
1568         my ($res) = $sth->fetchrow;
1569         unless ($res) {
1570                 $sth=$dbh->prepare("select subfieldid from auth_subfield_table
1571                                 where authid=? and tag=? and tagorder=?
1572                                         and subfieldcode=?");
1573                 $sth->execute($authid,$tag,$tagorder,$subfield);
1574                 ($res) = $sth->fetchrow;
1575         }
1576     return $res;
1577 }
1578
1579 sub AUTHfind_authtypecode {
1580         my ($dbh,$authid) = @_;
1581         my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
1582         $sth->execute($authid);
1583         my ($authtypecode) = $sth->fetchrow;
1584         return $authtypecode;
1585 }
1586
1587 sub AUTHdelsubfield {
1588 # delete a subfield for $authid / tag / tagorder / subfield / subfieldorder
1589     my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1590     $dbh->do("delete from auth_subfield_table where authid='$authid' and
1591                         tag='$tag' and tagorder='$tagorder'
1592                         and subfieldcode='$subfield' and subfieldorder='$subfieldorder'
1593                         ");
1594 }
1595
1596 sub AUTHhtml2marc {
1597         my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1598         my $prevtag = -1;
1599         my $record = MARC::Record->new();
1600 #       my %subfieldlist=();
1601         my $prevvalue; # if tag <10
1602         my $field; # if tag >=10
1603         for (my $i=0; $i< @$rtags; $i++) {
1604                 # rebuild MARC::Record
1605                 if (@$rtags[$i] ne $prevtag) {
1606                         if ($prevtag < 10) {
1607                                 if ($prevvalue) {
1608                                         $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1609                                 }
1610                         } else {
1611                                 if ($field) {
1612                                         $record->add_fields($field);
1613                                 }
1614                         }
1615                         $indicators{@$rtags[$i]}.='  ';
1616                         if (@$rtags[$i] <10) {
1617                                 $prevvalue= @$rvalues[$i];
1618                         } else {
1619                                 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1620                         }
1621                         $prevtag = @$rtags[$i];
1622                 } else {
1623                         if (@$rtags[$i] <10) {
1624                                 $prevvalue=@$rvalues[$i];
1625                         } else {
1626                                 if (@$rvalues[$i]) {
1627                                         $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1628                                 }
1629                         }
1630                         $prevtag= @$rtags[$i];
1631                 }
1632         }
1633         # the last has not been included inside the loop... do it now !
1634         $record->add_fields($field);
1635 #       warn $record->as_formatted;
1636         return $record;
1637 }
1638
1639 sub AUTHaddword {
1640 # split a subfield string and adds it into the word table.
1641 # removes stopwords
1642     my ($dbh,$authid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1643     $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
1644     my @words = split / /,$sentence;
1645     my $stopwords= C4::Context->stopwords;
1646     my $sth=$dbh->prepare("insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word)
1647                         values (?,concat(?,?),?,?,?,soundex(?))");
1648     foreach my $word (@words) {
1649 # we record only words longer than 2 car and not in stopwords hash
1650         if (length($word)>2 and !($stopwords->{uc($word)})) {
1651             $sth->execute($authid,$tag,$subfieldid,$tagorder,$subfieldorder,$word,$word);
1652             if ($sth->err()) {
1653                 warn "ERROR ==> insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word) values ($authid,concat($tag,$subfieldid),$tagorder,$subfieldorder,$word,soundex($word))\n";
1654             }
1655         }
1656     }
1657 }
1658
1659 sub AUTHdelword {
1660 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1661     my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1662     my $sth=$dbh->prepare("delete from auth_word where authid=? and tagsubfield=concat(?,?) and tagorder=? and subfieldorder=?");
1663     $sth->execute($authid,$tag,$subfield,$tagorder,$subfieldorder);
1664 }
1665
1666 sub char_decode {
1667         # converts ISO 5426 coded string to ISO 8859-1
1668         # sloppy code : should be improved in next issue
1669         my ($string,$encoding) = @_ ;
1670         $_ = $string ;
1671 #       $encoding = C4::Context->preference("marcflavour") unless $encoding;
1672         if ($encoding eq "UNIMARC") {
1673                 s/\xe1/Æ/gm ;
1674                 s/\xe2/Ð/gm ;
1675                 s/\xe9/Ø/gm ;
1676                 s/\xec/þ/gm ;
1677                 s/\xf1/æ/gm ;
1678                 s/\xf3/ð/gm ;
1679                 s/\xf9/ø/gm ;
1680                 s/\xfb/ß/gm ;
1681                 s/\xc1\x61/à/gm ;
1682                 s/\xc1\x65/è/gm ;
1683                 s/\xc1\x69/ì/gm ;
1684                 s/\xc1\x6f/ò/gm ;
1685                 s/\xc1\x75/ù/gm ;
1686                 s/\xc1\x41/À/gm ;
1687                 s/\xc1\x45/È/gm ;
1688                 s/\xc1\x49/Ì/gm ;
1689                 s/\xc1\x4f/Ò/gm ;
1690                 s/\xc1\x55/Ù/gm ;
1691                 s/\xc2\x41/Á/gm ;
1692                 s/\xc2\x45/É/gm ;
1693                 s/\xc2\x49/Í/gm ;
1694                 s/\xc2\x4f/Ó/gm ;
1695                 s/\xc2\x55/Ú/gm ;
1696                 s/\xc2\x59/Ý/gm ;
1697                 s/\xc2\x61/á/gm ;
1698                 s/\xc2\x65/é/gm ;
1699                 s/\xc2\x69/í/gm ;
1700                 s/\xc2\x6f/ó/gm ;
1701                 s/\xc2\x75/ú/gm ;
1702                 s/\xc2\x79/ý/gm ;
1703                 s/\xc3\x41/Â/gm ;
1704                 s/\xc3\x45/Ê/gm ;
1705                 s/\xc3\x49/Î/gm ;
1706                 s/\xc3\x4f/Ô/gm ;
1707                 s/\xc3\x55/Û/gm ;
1708                 s/\xc3\x61/â/gm ;
1709                 s/\xc3\x65/ê/gm ;
1710                 s/\xc3\x69/î/gm ;
1711                 s/\xc3\x6f/ô/gm ;
1712                 s/\xc3\x75/û/gm ;
1713                 s/\xc4\x41/Ã/gm ;
1714                 s/\xc4\x4e/Ñ/gm ;
1715                 s/\xc4\x4f/Õ/gm ;
1716                 s/\xc4\x61/ã/gm ;
1717                 s/\xc4\x6e/ñ/gm ;
1718                 s/\xc4\x6f/õ/gm ;
1719                 s/\xc8\x45/Ë/gm ;
1720                 s/\xc8\x49/Ï/gm ;
1721                 s/\xc8\x65/ë/gm ;
1722                 s/\xc8\x69/ï/gm ;
1723                 s/\xc8\x76/ÿ/gm ;
1724                 s/\xc9\x41/Ä/gm ;
1725                 s/\xc9\x4f/Ö/gm ;
1726                 s/\xc9\x55/Ü/gm ;
1727                 s/\xc9\x61/ä/gm ;
1728                 s/\xc9\x6f/ö/gm ;
1729                 s/\xc9\x75/ü/gm ;
1730                 s/\xca\x41/Å/gm ;
1731                 s/\xca\x61/å/gm ;
1732                 s/\xd0\x43/Ç/gm ;
1733                 s/\xd0\x63/ç/gm ;
1734                 # this handles non-sorting blocks (if implementation requires this)
1735                 $string = nsb_clean($_) ;
1736         } elsif ($encoding eq "USMARC" || $encoding eq "MARC21") {
1737                 if(/[\xc1-\xff]/) {
1738                         s/\xe1\x61/à/gm ;
1739                         s/\xe1\x65/è/gm ;
1740                         s/\xe1\x69/ì/gm ;
1741                         s/\xe1\x6f/ò/gm ;
1742                         s/\xe1\x75/ù/gm ;
1743                         s/\xe1\x41/À/gm ;
1744                         s/\xe1\x45/È/gm ;
1745                         s/\xe1\x49/Ì/gm ;
1746                         s/\xe1\x4f/Ò/gm ;
1747                         s/\xe1\x55/Ù/gm ;
1748                         s/\xe2\x41/Á/gm ;
1749                         s/\xe2\x45/É/gm ;
1750                         s/\xe2\x49/Í/gm ;
1751                         s/\xe2\x4f/Ó/gm ;
1752                         s/\xe2\x55/Ú/gm ;
1753                         s/\xe2\x59/Ý/gm ;
1754                         s/\xe2\x61/á/gm ;
1755                         s/\xe2\x65/é/gm ;
1756                         s/\xe2\x69/í/gm ;
1757                         s/\xe2\x6f/ó/gm ;
1758                         s/\xe2\x75/ú/gm ;
1759                         s/\xe2\x79/ý/gm ;
1760                         s/\xe3\x41/Â/gm ;
1761                         s/\xe3\x45/Ê/gm ;
1762                         s/\xe3\x49/Î/gm ;
1763                         s/\xe3\x4f/Ô/gm ;
1764                         s/\xe3\x55/Û/gm ;
1765                         s/\xe3\x61/â/gm ;
1766                         s/\xe3\x65/ê/gm ;
1767                         s/\xe3\x69/î/gm ;
1768                         s/\xe3\x6f/ô/gm ;
1769                         s/\xe3\x75/û/gm ;
1770                         s/\xe4\x41/Ã/gm ;
1771                         s/\xe4\x4e/Ñ/gm ;
1772                         s/\xe4\x4f/Õ/gm ;
1773                         s/\xe4\x61/ã/gm ;
1774                         s/\xe4\x6e/ñ/gm ;
1775                         s/\xe4\x6f/õ/gm ;
1776                         s/\xe8\x45/Ë/gm ;
1777                         s/\xe8\x49/Ï/gm ;
1778                         s/\xe8\x65/ë/gm ;
1779                         s/\xe8\x69/ï/gm ;
1780                         s/\xe8\x76/ÿ/gm ;
1781                         s/\xe9\x41/Ä/gm ;
1782                         s/\xe9\x4f/Ö/gm ;
1783                         s/\xe9\x55/Ü/gm ;
1784                         s/\xe9\x61/ä/gm ;
1785                         s/\xe9\x6f/ö/gm ;
1786                         s/\xe9\x75/ü/gm ;
1787                         s/\xea\x41/Å/gm ;
1788                         s/\xea\x61/å/gm ;
1789                         # this handles non-sorting blocks (if implementation requires this)
1790                         $string = nsb_clean($_) ;
1791                 }
1792         }
1793         return($string) ;
1794 }
1795
1796 sub nsb_clean {
1797         my $NSB = '\x88' ;              # NSB : begin Non Sorting Block
1798         my $NSE = '\x89' ;              # NSE : Non Sorting Block end
1799         # handles non sorting blocks
1800         my ($string) = @_ ;
1801         $_ = $string ;
1802         s/$NSB/(/gm ;
1803         s/[ ]{0,1}$NSE/) /gm ;
1804         $string = $_ ;
1805         return($string) ;
1806 }
1807
1808 sub FindDuplicate {
1809         my ($record,$authtypecode)=@_;
1810         my $dbh = C4::Context->dbh;
1811         
1812 #       warn "".$record->as_formatted;
1813         # search duplicate on ISBN, easy and fast...
1814         my $sth = $dbh->prepare("select auth_tag_to_report,summary from auth_types where authtypecode=?");
1815         $sth->execute($authtypecode);
1816         my ($auth_tag_to_report,$taglist) = $sth->fetchrow;
1817         $sth->finish;
1818         # a more complex search : build a request for authoritysearch
1819         my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
1820         # search on biblio.title
1821 #       warn " tag a reporter : $auth_tag_to_report";
1822         warn "taglist ".$taglist;
1823         my @subfield = split /\[/,  $taglist;
1824         my $max = @subfield;
1825         for (my $i=1; $i<$max;$i++){
1826                 warn " ".$subfield[$i];
1827                 $subfield[$i]=substr($subfield[$i],3,1);
1828                 warn " ".$subfield[$i];
1829         }
1830         
1831         if ($record->fields($auth_tag_to_report)) {
1832 #               foreach my $subfieldcount (1..$#subfield){
1833 #                       if ($record->field($auth_tag_to_report)->subfields($subfield[$subfieldcount])) {
1834 # #                             warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
1835 #                               push @tags, $auth_tag_to_report.$subfield[$subfieldcount];
1836 # #                             warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
1837 #                               push @and_or, "and";
1838 #                               push @excluding, "";
1839 #                               push @operator, "contains";
1840 #                               push @value, $record->field($auth_tag_to_report)->subfield($subfield[$subfieldcount]);
1841 #                       }
1842 #               }
1843                 
1844                 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where tagfield=? and authtypecode=? ");
1845                 $sth->execute($auth_tag_to_report,$authtypecode);
1846  #              warn " field $auth_tag_to_report exists";
1847                 while (my ($tag,$subfield) = $sth->fetchrow){
1848                         if ($record->field($tag)->subfield($subfield)) {
1849  #                              warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
1850                                 push @tags, $tag.$subfield;
1851  #                              warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
1852                                 push @and_or, "and";
1853                                 push @excluding, "";
1854                                 push @operator, "contains";
1855                                 push @value, $record->field($tag)->subfield($subfield);
1856                         }
1857                 }
1858         }
1859  
1860         my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
1861         # there is at least 1 result => return the 1st one
1862         if ($nbresult) {
1863                 warn "$nbresult => ".@$finalresult[0]->{authid},$record->field($auth_tag_to_report)->subfield('a');
1864                 return @$finalresult[0]->{authid},@$finalresult[0]->{authid},$record->field($auth_tag_to_report)->subfield('a');
1865         }
1866         # no result, returns nothing
1867         return;
1868 }
1869
1870 END { }       # module clean-up code here (global destructor)
1871
1872 =back
1873
1874 =head1 AUTHOR
1875
1876 Koha Developement team <info@koha.org>
1877
1878 Paul POULAIN paul.poulain@free.fr
1879
1880 =cut
1881
1882 # $Id$
1883 # $Log$
1884 # Revision 1.16  2005/05/04 15:43:43  tipaul
1885 # synch'ing 2.2 and head
1886 #
1887 # Revision 1.15  2005/05/04 14:18:39  hdl
1888 # adding Marc Record to authoritysearch report
1889 #
1890 # Revision 1.14  2005/04/05 17:07:46  hdl
1891 # Scanning every the Subfields of auth_tag_to_report for FindDuplicate
1892 #
1893 # Revision 1.13  2005/04/05 15:23:41  hdl
1894 # Searching for double entries when adding a new authority.
1895 #
1896 # Revision 1.12  2005/04/05 09:58:48  hdl
1897 # Adding double authority search before creating a new authority
1898 #
1899 # Revision 1.11  2005/03/07 08:55:29  tipaul
1900 # synch'ing with 2.2
1901 #
1902 # Revision 1.9.2.2  2005/02/28 14:03:13  tipaul
1903 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
1904 # * adding a select box to requet "contain" or "begin with" search.
1905 # * fixing some bug in authority search (related to "main entry" search)
1906 #
1907 # Revision 1.9.2.1  2005/02/24 13:12:13  tipaul
1908 # saving authority modif in a text file. This will be used soon with another script (in crontab). The script in crontab will retrieve every authorityid in the directory localfile/authorities and modify every biblio using this authority. Those modifs may be long. So they can't be done through http, because we may encounter a webserver timeout, and kill the process before end of the job.
1909 # So, it will be done through a cron job.
1910 # (/me agree we need some doc for command line scripts)
1911 #
1912 # Revision 1.9  2004/12/23 09:48:11  tipaul
1913 # Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
1914 #
1915 # Revision 1.8  2004/11/05 10:11:39  tipaul
1916 # export auth_count_usage (bugfix)
1917 #
1918 # Revision 1.7  2004/09/23 16:13:00  tipaul
1919 # Bugfix in modification
1920 #
1921 # Revision 1.6  2004/08/18 16:00:24  tipaul
1922 # fixes for authorities management
1923 #
1924 # Revision 1.5  2004/07/05 13:37:22  doxulting
1925 # First step for working authorities
1926 #
1927 # Revision 1.4  2004/06/22 11:35:37  tipaul
1928 # removing % at the beginning of a string to avoid loooonnnngggg searchs
1929 #
1930 # Revision 1.3  2004/06/17 08:02:13  tipaul
1931 # merging tag & subfield in auth_word for better perfs
1932 #
1933 # Revision 1.2  2004/06/10 08:29:01  tipaul
1934 # MARC authority management (continued)
1935 #
1936 # Revision 1.1  2004/06/07 07:35:01  tipaul
1937 # MARC authority management package
1938 #