Scanning every the Subfields of auth_tag_to_report for FindDuplicate
[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 #               warn " $i: ".@$tags[$i];
80                 if (@$tags[$i]) {
81                         @$tags[$i] = $dbh->quote(@$tags[$i]);
82 #                       warn " $i After process: ".@$tags[$i];
83                 }
84         }
85         my @normal_tags = ();
86         my @normal_and_or = ();
87         my @normal_operator = ();
88         my @normal_value = ();
89         # Extracts the NOT statements from the list of statements
90         for(my $i = 0 ; $i <= $#{$value} ; $i++)
91         {
92                 if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
93                 {
94                         foreach my $word (split(/ /, @$value[$i]))
95                         {
96                                 unless (C4::Context->stopwords->{uc($word)}) {  #it's NOT a stopword => use it. Otherwise, ignore
97                                         my $tag = substr(@$tags[$i],0,3);
98                                         my $subf = substr(@$tags[$i],3,1);
99                                         push @normal_tags, @$tags[$i];
100                                         push @normal_and_or, "and";     # assumes "foo" and "bar" if "foo bar" is entered
101                                         push @normal_operator, @$operator[$i];
102                                         push @normal_value, $word;
103                                 }
104                         }
105                 }
106                 else
107                 {
108                         push @normal_tags, @$tags[$i];
109                         push @normal_and_or, @$and_or[$i];
110                         push @normal_operator, @$operator[$i];
111                         push @normal_value, @$value[$i];
112                 }
113         }
114
115         # Finds the basic results without the NOT requests
116         my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
117
118         my $sth;
119
120         if ($sql_where2) {
121                 $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)");
122                 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)";
123         } else {
124                 $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");
125                 warn "Q : select distinct m1.authid from auth_header,$sql_tables where  m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where1";
126         }
127         $sth->execute($authtypecode);
128         my @result = ();
129         while (my ($authid) = $sth->fetchrow) {
130                         push @result,$authid;
131                 }
132
133         # we have authid list. Now, loads summary from [offset] to [offset]+[length]
134         my $counter = $offset;
135         my @finalresult = ();
136         my $oldline;
137         while (($counter <= $#result) && ($counter <= ($offset + $length))) {
138 #               warn " HERE : $counter, $#result, $offset, $length";
139                 # get MARC::Record of the authority
140                 my $record = AUTHgetauthority($dbh,$result[$counter]);
141                 # then build the summary
142                 my $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
143                 my $authref = getauthtype($authtypecode);
144                 my $summary = $authref->{summary};
145                 my @fields = $record->fields();
146                 foreach my $field (@fields) {
147                         my $tag = $field->tag();
148                         if ($tag<10) {
149                         } else {
150                                 my @subf = $field->subfields;
151                                 for my $i (0..$#subf) {
152                                         my $subfieldcode = $subf[$i][0];
153                                         my $subfieldvalue = $subf[$i][1];
154                                         my $tagsubf = $tag.$subfieldcode;
155                                         $summary =~ s/\[(.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
156                                 }
157                         }
158                 }
159                 $summary =~ s/\[(.*?)]//g;
160                 $summary =~ s/\n/<br>/g;
161
162                 # find biblio MARC field using this authtypecode (to jump to biblio)
163                 my $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
164                 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
165                 $sth->execute($authtypecode);
166                 my $tags_using_authtype;
167                 while (my ($tagfield) = $sth->fetchrow) {
168 #                       warn "TAG : $tagfield";
169                         $tags_using_authtype.= $tagfield."9,";
170                 }
171                 chop $tags_using_authtype;
172                 
173                 # then add a line for the template loop
174                 my %newline;
175                 $newline{summary} = $summary;
176                 $newline{authid} = $result[$counter];
177                 $newline{used} = &AUTHcount_usage($result[$counter]);
178                 $newline{biblio_fields} = $tags_using_authtype;
179                 $counter++;
180                 push @finalresult, \%newline;
181         }
182         my $nbresults = $#result + 1;
183         return (\@finalresult, $nbresults);
184 }
185
186 # Creates the SQL Request
187
188 sub create_request {
189         my ($dbh,$tags, $and_or, $operator, $value) = @_;
190
191         my $sql_tables; # will contain marc_subfield_table as m1,...
192         my $sql_where1; # will contain the "true" where
193         my $sql_where2 = "("; # will contain m1.authid=m2.authid
194         my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
195         my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR  is provided.
196
197
198         for(my $i=0; $i<=@$value;$i++) {
199                 if (@$value[$i]) {
200                         $nb_active++;
201 #                       warn " @$tags[$i]";
202                         if ($nb_active==1) {
203                                 if (@$operator[$i] eq "start") {
204                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
205                                         $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]%");
206                                         if (@$tags[$i]) {
207                                                 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
208                                         }
209                                         $sql_where1.=")";
210                                 } elsif (@$operator[$i] eq "contains") {        
211                                 $sql_tables .= "auth_word as m$nb_table,";
212                                         $sql_where1 .= "(m1.word  like ".$dbh->quote("@$value[$i]%");
213                                         if (@$tags[$i]) {
214                                                  $sql_where1 .=" and m1.tagsubfield in (@$tags[$i])";
215                                         }
216                                         $sql_where1.=")";
217                                 } else {
218
219                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
220                                         $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
221                                         if (@$tags[$i]) {
222                                                  $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
223                                         }
224                                         $sql_where1.=")";
225                                 }
226                         } else {
227                                 if (@$operator[$i] eq "start") {
228                                         $nb_table++;
229                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
230                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]%");
231                                         if (@$tags[$i]) {
232                                                 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
233                                         }
234                                         $sql_where1.=")";
235                                         $sql_where2 .= "m1.authid=m$nb_table.authid and ";
236                                 } elsif (@$operator[$i] eq "contains") {
237                                         if (@$and_or[$i] eq 'and') {
238                                                 $nb_table++;
239                                                 $sql_tables .= "auth_word as m$nb_table,";
240                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
241                                                 if (@$tags[$i]) {
242                                                         $sql_where1 .=" and m$nb_table.tagsubfield in(@$tags[$i])";
243                                                 }
244                                                 $sql_where1.=")";
245                                                 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
246                                         } else {
247                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
248                                                 if (@$tags[$i]) {
249                                                         $sql_where1 .="  and m$nb_table.tag+m$nb_table.subfieldid in (@$tags[$i])";
250                                                 }
251                                                 $sql_where1.=")";
252                                                 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
253                                         }
254                                 } else {
255                                         $nb_table++;
256                                         $sql_tables .= "auth_subfield_table as m$nb_table,";
257                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
258                                         if (@$tags[$i]) {
259                                                 $sql_where1 .="  and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
260                                         }
261                                         $sql_where2 .= "m1.authid=m$nb_table.authid and ";
262                                         $sql_where1.=")";
263                                 }
264                         }
265                 }
266         }
267
268         if($sql_where2 ne "(")  # some datas added to sql_where2, processing
269         {
270                 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
271                 $sql_where2 .= ")";
272         }
273         else    # no sql_where2 statement, deleting '('
274         {
275                 $sql_where2 = "";
276         }
277         chop $sql_tables;       # deletes the trailing ','
278         
279         return ($sql_tables, $sql_where1, $sql_where2);
280 }
281
282
283 sub AUTHcount_usage {
284         my ($authid) = @_;
285         my $dbh = C4::Context->dbh;
286         # find MARC fields using this authtype
287         my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
288         my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
289         $sth->execute($authtypecode);
290         my $tags_using_authtype;
291         while (my ($tagfield) = $sth->fetchrow) {
292 #               warn "TAG : $tagfield";
293                 $tags_using_authtype.= "'".$tagfield."9',";
294         }
295         chop $tags_using_authtype;
296         if ($tags_using_authtype) {
297                 $sth = $dbh->prepare("select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=?");
298         } else {
299                 $sth = $dbh->prepare("select count(*) from marc_subfield_table where subfieldvalue=?");
300         }
301 #       warn "Q : select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=$authid";
302         $sth->execute($authid);
303         my ($result) = $sth->fetchrow;
304 #       warn "Authority $authid TOTAL USED : $result";
305         return $result;
306 }
307
308 # merging 2 authority entries. After a merge, the "from" can be deleted.
309 # sub AUTHmerge {
310 #       my ($auth_merge_from,$auth_merge_to) = @_;
311 #       my $dbh = C4::Context->dbh;
312 #       # find MARC fields using this authtype
313 #       my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
314 #       # retrieve records
315 #       my $record_from = AUTHgetauthority($dbh,$auth_merge_from);
316 #       my $record_to = AUTHgetauthority($dbh,$auth_merge_to);
317 #       my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
318 #       $sth->execute($authtypecode);
319 #       my $tags_using_authtype;
320 #       while (my ($tagfield) = $sth->fetchrow) {
321 #               warn "TAG : $tagfield";
322 #               $tags_using_authtype.= "'".$tagfield."9',";
323 #       }
324 #       chop $tags_using_authtype;
325 #       # now, find every biblio using this authority
326 #       $sth = $dbh->prepare("select bibid,tag,tag_indicator,tagorder from marc_subfield_table where tag+subfieldid in ($tags_using_authtype) and subfieldvalue=?");
327 #       $sth->execute($authid);
328 #       # and delete entries before recreating them
329 #       while (my ($bibid,$tag,$tag_indicator,$tagorder) = $sth->fetchrow) {
330 #               &MARCdelsubfield($dbh,$bibid,$tag);
331 #               
332 #       }
333
334 # }
335
336 sub AUTHfind_authtypecode {
337         my ($dbh,$authid) = @_;
338         my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
339         $sth->execute($authid);
340         my ($authtypecode) = $sth->fetchrow;
341         return $authtypecode;
342 }
343  
344
345 sub AUTHgettagslib {
346         my ($dbh,$forlibrarian,$authtypecode)= @_;
347         $authtypecode="" unless $authtypecode;
348         my $sth;
349         my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
350         # check that framework exists
351         $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
352         $sth->execute($authtypecode);
353         my ($total) = $sth->fetchrow;
354         $authtypecode="" unless ($total >0);
355         $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory,repeatable from auth_tag_structure where authtypecode=? order by tagfield");
356         $sth->execute($authtypecode);
357         my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
358         while ( ($tag,$lib,$mandatory,$repeatable) = $sth->fetchrow) {
359                 $res->{$tag}->{lib}=$lib;
360                 $res->{$tab}->{tab}=""; # XXX
361                 $res->{$tag}->{mandatory}=$mandatory;
362                 $res->{$tag}->{repeatable}=$repeatable;
363         }
364
365         $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");
366         $sth->execute($authtypecode);
367
368         my $subfield;
369         my $authorised_value;
370         my $thesaurus_category;
371         my $value_builder;
372         my $kohafield;
373         my $seealso;
374         my $hidden;
375         my $isurl;
376         while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$value_builder,$seealso) = $sth->fetchrow) {
377                 $res->{$tag}->{$subfield}->{lib}=$lib;
378                 $res->{$tag}->{$subfield}->{tab}=$tab;
379                 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
380                 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
381                 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
382                 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
383                 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
384                 $res->{$tag}->{$subfield}->{seealso}=$seealso;
385                 $res->{$tag}->{$subfield}->{hidden}=$hidden;
386                 $res->{$tag}->{$subfield}->{isurl}=$isurl;
387         }
388         return $res;
389 }
390
391 sub AUTHaddauthority {
392 # pass the MARC::Record to this function, and it will create the records in the marc tables
393         my ($dbh,$record,$authid,$authtypecode) = @_;
394         my @fields=$record->fields();
395 #       warn "IN AUTHaddauthority $authid => ".$record->as_formatted;
396 # adding main table, and retrieving authid
397 # if authid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
398 # if authid empty => true add, find a new authid number
399         unless ($authid) {
400                 $dbh->do("lock tables auth_header WRITE,auth_subfield_table WRITE, auth_word WRITE, stopwords READ");
401                 my $sth=$dbh->prepare("insert into auth_header (datecreated,authtypecode) values (now(),?)");
402                 $sth->execute($authtypecode);
403                 $sth=$dbh->prepare("select max(authid) from auth_header");
404                 $sth->execute;
405                 ($authid)=$sth->fetchrow;
406                 $sth->finish;
407         }
408         my $fieldcount=0;
409         # now, add subfields...
410         foreach my $field (@fields) {
411                 $fieldcount++;
412                 if ($field->tag() <10) {
413                                 &AUTHaddsubfield($dbh,$authid,
414                                                 $field->tag(),
415                                                 '',
416                                                 $fieldcount,
417                                                 '',
418                                                 1,
419                                                 $field->data()
420                                                 );
421                 } else {
422                         my @subfields=$field->subfields();
423                         foreach my $subfieldcount (0..$#subfields) {
424                                 &AUTHaddsubfield($dbh,$authid,
425                                                 $field->tag(),
426                                                 $field->indicator(1).$field->indicator(2),
427                                                 $fieldcount,
428                                                 $subfields[$subfieldcount][0],
429                                                 $subfieldcount+1,
430                                                 $subfields[$subfieldcount][1]
431                                                 );
432                         }
433                 }
434         }
435         $dbh->do("unlock tables");
436         return $authid;
437 }
438
439
440 sub AUTHaddsubfield {
441 # Add a new subfield to a tag into the DB.
442         my ($dbh,$authid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
443         # if not value, end of job, we do nothing
444         if (length($subfieldvalues) ==0) {
445                 return;
446         }
447         if (not($subfieldcode)) {
448                 $subfieldcode=' ';
449         }
450         my @subfieldvalues = split /\|/,$subfieldvalues;
451         foreach my $subfieldvalue (@subfieldvalues) {
452                 my $sth=$dbh->prepare("insert into auth_subfield_table (authid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
453                 $sth->execute($authid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
454                 if ($sth->errstr) {
455                         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";
456                 }
457                 &AUTHaddword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
458         }
459 }
460
461 sub AUTHgetauthority {
462 # Returns MARC::Record of the biblio passed in parameter.
463     my ($dbh,$authid)=@_;
464     my $record = MARC::Record->new();
465 #---- TODO : the leader is missing
466         $record->leader('                        ');
467     my $sth=$dbh->prepare("select authid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue
468                                  from auth_subfield_table
469                                  where authid=? order by tag,tagorder,subfieldcode
470                          ");
471         $sth->execute($authid);
472         my $prevtagorder=1;
473         my $prevtag='XXX';
474         my $previndicator;
475         my $field; # for >=10 tags
476         my $prevvalue; # for <10 tags
477         while (my $row=$sth->fetchrow_hashref) {
478                 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
479                         $previndicator.="  ";
480                         if ($prevtag <10) {
481                         $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
482                         } else {
483                                 $record->add_fields($field) unless $prevtag eq "XXX";
484                         }
485                         undef $field;
486                         $prevtagorder=$row->{tagorder};
487                         $prevtag = $row->{tag};
488                         $previndicator=$row->{tag_indicator};
489                         if ($row->{tag}<10) {
490                                 $prevvalue = $row->{subfieldvalue};
491                         } else {
492                                 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.'  ',0,1), substr($row->{tag_indicator}.'  ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
493                         }
494                 } else {
495                         if ($row->{tag} <10) {
496                                 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
497                         } else {
498                                 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
499                         }
500                         $prevtag= $row->{tag};
501                         $previndicator=$row->{tag_indicator};
502                 }
503         }
504         # the last has not been included inside the loop... do it now !
505         if ($prevtag ne "XXX") { # check that we have found something. Otherwise, prevtag is still XXX and we
506                                                 # must return an empty record, not make MARC::Record fail because we try to
507                                                 # create a record with XXX as field :-(
508                 if ($prevtag <10) {
509                         $record->add_fields($prevtag,$prevvalue);
510                 } else {
511         #               my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
512                         $record->add_fields($field);
513                 }
514         }
515         return $record;
516 }
517
518 sub AUTHgetauth_type {
519         my ($authtypecode) = @_;
520         my $dbh=C4::Context->dbh;
521         my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
522         $sth->execute($authtypecode);
523         return $sth->fetchrow_hashref;
524 }
525 sub AUTHmodauthority {
526         my ($dbh,$authid,$record,$delete)=@_;
527         my $oldrecord=&AUTHgetauthority($dbh,$authid);
528         if ($oldrecord eq $record) {
529                 return;
530         }
531 # 1st delete the authority,
532 # 2nd recreate it
533         &AUTHdelauthority($dbh,$authid,1);
534         &AUTHaddauthority($dbh,$record,$authid,AUTHfind_authtypecode($dbh,$authid));
535         # save the file in localfile/modified_authorities
536         my $filename = C4::Context->config("intranetdir")."/localfile/modified_authorities/$authid.authid";
537         open AUTH, "> $filename";
538         print AUTH $authid;
539         close AUTH;
540 }
541
542 sub AUTHdelauthority {
543         my ($dbh,$authid,$keep_biblio) = @_;
544 # if the keep_biblio is set to 1, then authority entries in biblio are preserved.
545 # This flag is set when the delauthority is called by modauthority
546 # due to a too complex structure of MARC (repeatable fields and subfields),
547 # the best solution for a modif is to delete / recreate the record.
548
549         my $record = AUTHgetauthority($dbh,$authid);
550         $dbh->do("delete from auth_header where authid=$authid") unless $keep_biblio;
551         $dbh->do("delete from auth_subfield_table where authid=$authid");
552         $dbh->do("delete from auth_word where authid=$authid");
553 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag)
554 }
555
556 sub AUTHmodsubfield {
557 # Subroutine changes a subfield value given a subfieldid.
558         my ($dbh, $subfieldid, $subfieldvalue )=@_;
559         $dbh->do("lock tables auth_subfield_table WRITE");
560         my $sth=$dbh->prepare("update auth_subfield_table set subfieldvalue=? where subfieldid=?");
561         $sth->execute($subfieldvalue, $subfieldid);
562         $dbh->do("unlock tables");
563         $sth->finish;
564         $sth=$dbh->prepare("select authid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from auth_subfield_table where subfieldid=?");
565         $sth->execute($subfieldid);
566         my ($authid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
567         $subfieldid=$x;
568         &AUTHdelword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
569         &AUTHaddword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
570         return($subfieldid, $subfieldvalue);
571 }
572
573 sub AUTHfindsubfield {
574     my ($dbh,$authid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
575     my $resultcounter=0;
576     my $subfieldid;
577     my $lastsubfieldid;
578     my $query="select subfieldid from auth_subfield_table where authid=? and tag=? and subfieldcode=?";
579     my @bind_values = ($authid,$tag, $subfieldcode);
580     if ($subfieldvalue) {
581         $query .= " and subfieldvalue=?";
582         push(@bind_values,$subfieldvalue);
583     } else {
584         if ($subfieldorder<1) {
585             $subfieldorder=1;
586         }
587         $query .= " and subfieldorder=?";
588         push(@bind_values,$subfieldorder);
589     }
590     my $sti=$dbh->prepare($query);
591     $sti->execute(@bind_values);
592     while (($subfieldid) = $sti->fetchrow) {
593         $resultcounter++;
594         $lastsubfieldid=$subfieldid;
595     }
596     if ($resultcounter>1) {
597                 # Error condition.  Values given did not resolve into a unique record.  Don't know what to edit
598                 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
599                 return -1;
600     } else {
601                 return $lastsubfieldid;
602     }
603 }
604
605 sub AUTHfindsubfieldid {
606         my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
607         my $sth=$dbh->prepare("select subfieldid from auth_subfield_table
608                                 where authid=? and tag=? and tagorder=?
609                                         and subfieldcode=? and subfieldorder=?");
610         $sth->execute($authid,$tag,$tagorder,$subfield,$subfieldorder);
611         my ($res) = $sth->fetchrow;
612         unless ($res) {
613                 $sth=$dbh->prepare("select subfieldid from auth_subfield_table
614                                 where authid=? and tag=? and tagorder=?
615                                         and subfieldcode=?");
616                 $sth->execute($authid,$tag,$tagorder,$subfield);
617                 ($res) = $sth->fetchrow;
618         }
619     return $res;
620 }
621
622 sub AUTHfind_authtypecode {
623         my ($dbh,$authid) = @_;
624         my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
625         $sth->execute($authid);
626         my ($authtypecode) = $sth->fetchrow;
627         return $authtypecode;
628 }
629
630 sub AUTHdelsubfield {
631 # delete a subfield for $authid / tag / tagorder / subfield / subfieldorder
632     my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
633     $dbh->do("delete from auth_subfield_table where authid='$authid' and
634                         tag='$tag' and tagorder='$tagorder'
635                         and subfieldcode='$subfield' and subfieldorder='$subfieldorder'
636                         ");
637 }
638
639 sub AUTHhtml2marc {
640         my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
641         my $prevtag = -1;
642         my $record = MARC::Record->new();
643 #       my %subfieldlist=();
644         my $prevvalue; # if tag <10
645         my $field; # if tag >=10
646         for (my $i=0; $i< @$rtags; $i++) {
647                 # rebuild MARC::Record
648                 if (@$rtags[$i] ne $prevtag) {
649                         if ($prevtag < 10) {
650                                 if ($prevvalue) {
651                                         $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
652                                 }
653                         } else {
654                                 if ($field) {
655                                         $record->add_fields($field);
656                                 }
657                         }
658                         $indicators{@$rtags[$i]}.='  ';
659                         if (@$rtags[$i] <10) {
660                                 $prevvalue= @$rvalues[$i];
661                         } else {
662                                 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
663                         }
664                         $prevtag = @$rtags[$i];
665                 } else {
666                         if (@$rtags[$i] <10) {
667                                 $prevvalue=@$rvalues[$i];
668                         } else {
669                                 if (@$rvalues[$i]) {
670                                         $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
671                                 }
672                         }
673                         $prevtag= @$rtags[$i];
674                 }
675         }
676         # the last has not been included inside the loop... do it now !
677         $record->add_fields($field);
678 #       warn $record->as_formatted;
679         return $record;
680 }
681
682 sub AUTHaddword {
683 # split a subfield string and adds it into the word table.
684 # removes stopwords
685     my ($dbh,$authid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
686     $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
687     my @words = split / /,$sentence;
688     my $stopwords= C4::Context->stopwords;
689     my $sth=$dbh->prepare("insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word)
690                         values (?,concat(?,?),?,?,?,soundex(?))");
691     foreach my $word (@words) {
692 # we record only words longer than 2 car and not in stopwords hash
693         if (length($word)>2 and !($stopwords->{uc($word)})) {
694             $sth->execute($authid,$tag,$subfieldid,$tagorder,$subfieldorder,$word,$word);
695             if ($sth->err()) {
696                 warn "ERROR ==> insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word) values ($authid,concat($tag,$subfieldid),$tagorder,$subfieldorder,$word,soundex($word))\n";
697             }
698         }
699     }
700 }
701
702 sub AUTHdelword {
703 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
704     my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
705     my $sth=$dbh->prepare("delete from auth_word where authid=? and tagsubfield=concat(?,?) and tagorder=? and subfieldorder=?");
706     $sth->execute($authid,$tag,$subfield,$tagorder,$subfieldorder);
707 }
708
709 sub char_decode {
710         # converts ISO 5426 coded string to ISO 8859-1
711         # sloppy code : should be improved in next issue
712         my ($string,$encoding) = @_ ;
713         $_ = $string ;
714 #       $encoding = C4::Context->preference("marcflavour") unless $encoding;
715         if ($encoding eq "UNIMARC") {
716                 s/\xe1/Æ/gm ;
717                 s/\xe2/Ð/gm ;
718                 s/\xe9/Ø/gm ;
719                 s/\xec/þ/gm ;
720                 s/\xf1/æ/gm ;
721                 s/\xf3/ð/gm ;
722                 s/\xf9/ø/gm ;
723                 s/\xfb/ß/gm ;
724                 s/\xc1\x61/à/gm ;
725                 s/\xc1\x65/è/gm ;
726                 s/\xc1\x69/ì/gm ;
727                 s/\xc1\x6f/ò/gm ;
728                 s/\xc1\x75/ù/gm ;
729                 s/\xc1\x41/À/gm ;
730                 s/\xc1\x45/È/gm ;
731                 s/\xc1\x49/Ì/gm ;
732                 s/\xc1\x4f/Ò/gm ;
733                 s/\xc1\x55/Ù/gm ;
734                 s/\xc2\x41/Á/gm ;
735                 s/\xc2\x45/É/gm ;
736                 s/\xc2\x49/Í/gm ;
737                 s/\xc2\x4f/Ó/gm ;
738                 s/\xc2\x55/Ú/gm ;
739                 s/\xc2\x59/Ý/gm ;
740                 s/\xc2\x61/á/gm ;
741                 s/\xc2\x65/é/gm ;
742                 s/\xc2\x69/í/gm ;
743                 s/\xc2\x6f/ó/gm ;
744                 s/\xc2\x75/ú/gm ;
745                 s/\xc2\x79/ý/gm ;
746                 s/\xc3\x41/Â/gm ;
747                 s/\xc3\x45/Ê/gm ;
748                 s/\xc3\x49/Î/gm ;
749                 s/\xc3\x4f/Ô/gm ;
750                 s/\xc3\x55/Û/gm ;
751                 s/\xc3\x61/â/gm ;
752                 s/\xc3\x65/ê/gm ;
753                 s/\xc3\x69/î/gm ;
754                 s/\xc3\x6f/ô/gm ;
755                 s/\xc3\x75/û/gm ;
756                 s/\xc4\x41/Ã/gm ;
757                 s/\xc4\x4e/Ñ/gm ;
758                 s/\xc4\x4f/Õ/gm ;
759                 s/\xc4\x61/ã/gm ;
760                 s/\xc4\x6e/ñ/gm ;
761                 s/\xc4\x6f/õ/gm ;
762                 s/\xc8\x45/Ë/gm ;
763                 s/\xc8\x49/Ï/gm ;
764                 s/\xc8\x65/ë/gm ;
765                 s/\xc8\x69/ï/gm ;
766                 s/\xc8\x76/ÿ/gm ;
767                 s/\xc9\x41/Ä/gm ;
768                 s/\xc9\x4f/Ö/gm ;
769                 s/\xc9\x55/Ü/gm ;
770                 s/\xc9\x61/ä/gm ;
771                 s/\xc9\x6f/ö/gm ;
772                 s/\xc9\x75/ü/gm ;
773                 s/\xca\x41/Å/gm ;
774                 s/\xca\x61/å/gm ;
775                 s/\xd0\x43/Ç/gm ;
776                 s/\xd0\x63/ç/gm ;
777                 # this handles non-sorting blocks (if implementation requires this)
778                 $string = nsb_clean($_) ;
779         } elsif ($encoding eq "USMARC" || $encoding eq "MARC21") {
780                 if(/[\xc1-\xff]/) {
781                         s/\xe1\x61/à/gm ;
782                         s/\xe1\x65/è/gm ;
783                         s/\xe1\x69/ì/gm ;
784                         s/\xe1\x6f/ò/gm ;
785                         s/\xe1\x75/ù/gm ;
786                         s/\xe1\x41/À/gm ;
787                         s/\xe1\x45/È/gm ;
788                         s/\xe1\x49/Ì/gm ;
789                         s/\xe1\x4f/Ò/gm ;
790                         s/\xe1\x55/Ù/gm ;
791                         s/\xe2\x41/Á/gm ;
792                         s/\xe2\x45/É/gm ;
793                         s/\xe2\x49/Í/gm ;
794                         s/\xe2\x4f/Ó/gm ;
795                         s/\xe2\x55/Ú/gm ;
796                         s/\xe2\x59/Ý/gm ;
797                         s/\xe2\x61/á/gm ;
798                         s/\xe2\x65/é/gm ;
799                         s/\xe2\x69/í/gm ;
800                         s/\xe2\x6f/ó/gm ;
801                         s/\xe2\x75/ú/gm ;
802                         s/\xe2\x79/ý/gm ;
803                         s/\xe3\x41/Â/gm ;
804                         s/\xe3\x45/Ê/gm ;
805                         s/\xe3\x49/Î/gm ;
806                         s/\xe3\x4f/Ô/gm ;
807                         s/\xe3\x55/Û/gm ;
808                         s/\xe3\x61/â/gm ;
809                         s/\xe3\x65/ê/gm ;
810                         s/\xe3\x69/î/gm ;
811                         s/\xe3\x6f/ô/gm ;
812                         s/\xe3\x75/û/gm ;
813                         s/\xe4\x41/Ã/gm ;
814                         s/\xe4\x4e/Ñ/gm ;
815                         s/\xe4\x4f/Õ/gm ;
816                         s/\xe4\x61/ã/gm ;
817                         s/\xe4\x6e/ñ/gm ;
818                         s/\xe4\x6f/õ/gm ;
819                         s/\xe8\x45/Ë/gm ;
820                         s/\xe8\x49/Ï/gm ;
821                         s/\xe8\x65/ë/gm ;
822                         s/\xe8\x69/ï/gm ;
823                         s/\xe8\x76/ÿ/gm ;
824                         s/\xe9\x41/Ä/gm ;
825                         s/\xe9\x4f/Ö/gm ;
826                         s/\xe9\x55/Ü/gm ;
827                         s/\xe9\x61/ä/gm ;
828                         s/\xe9\x6f/ö/gm ;
829                         s/\xe9\x75/ü/gm ;
830                         s/\xea\x41/Å/gm ;
831                         s/\xea\x61/å/gm ;
832                         # this handles non-sorting blocks (if implementation requires this)
833                         $string = nsb_clean($_) ;
834                 }
835         }
836         return($string) ;
837 }
838
839 sub nsb_clean {
840         my $NSB = '\x88' ;              # NSB : begin Non Sorting Block
841         my $NSE = '\x89' ;              # NSE : Non Sorting Block end
842         # handles non sorting blocks
843         my ($string) = @_ ;
844         $_ = $string ;
845         s/$NSB/(/gm ;
846         s/[ ]{0,1}$NSE/) /gm ;
847         $string = $_ ;
848         return($string) ;
849 }
850
851 sub FindDuplicate {
852         my ($record,$authtypecode)=@_;
853         my $dbh = C4::Context->dbh;
854         
855 #       warn "".$record->as_formatted;
856         # search duplicate on ISBN, easy and fast...
857         my $sth = $dbh->prepare("select auth_tag_to_report,summary from auth_types where authtypecode=?");
858         $sth->execute($authtypecode);
859         my ($auth_tag_to_report,$taglist) = $sth->fetchrow;
860         $sth->finish;
861         # a more complex search : build a request for authoritysearch
862         my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
863         # search on biblio.title
864 #       warn " tag a reporter : $auth_tag_to_report";
865         warn "taglist ".$taglist;
866         my @subfield = split /\[/,  $taglist;
867         my $max = @subfield;
868         for (my $i=1; $i<$max;$i++){
869                 warn " ".$subfield[$i];
870                 $subfield[$i]=substr($subfield[$i],3,1);
871                 warn " ".$subfield[$i];
872         }
873         
874         if ($record->fields($auth_tag_to_report)) {
875 #               foreach my $subfieldcount (1..$#subfield){
876 #                       if ($record->field($auth_tag_to_report)->subfields($subfield[$subfieldcount])) {
877 # #                             warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
878 #                               push @tags, $auth_tag_to_report.$subfield[$subfieldcount];
879 # #                             warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
880 #                               push @and_or, "and";
881 #                               push @excluding, "";
882 #                               push @operator, "contains";
883 #                               push @value, $record->field($auth_tag_to_report)->subfield($subfield[$subfieldcount]);
884 #                       }
885 #               }
886                 
887                 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where tagfield=? and authtypecode=? ");
888                 $sth->execute($auth_tag_to_report,$authtypecode);
889  #              warn " field $auth_tag_to_report exists";
890                 while (my ($tag,$subfield) = $sth->fetchrow){
891                         if ($record->field($tag)->subfield($subfield)) {
892  #                              warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
893                                 push @tags, $tag.$subfield;
894  #                              warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
895                                 push @and_or, "and";
896                                 push @excluding, "";
897                                 push @operator, "contains";
898                                 push @value, $record->field($tag)->subfield($subfield);
899                         }
900                 }
901         }
902  
903         my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
904         # there is at least 1 result => return the 1st one
905         if ($nbresult) {
906                 warn "$nbresult => ".@$finalresult[0]->{authid},$record->field($auth_tag_to_report)->subfield('a');
907                 return @$finalresult[0]->{authid},@$finalresult[0]->{authid},$record->field($auth_tag_to_report)->subfield('a');
908         }
909         # no result, returns nothing
910         return;
911 }
912
913 END { }       # module clean-up code here (global destructor)
914
915 =back
916
917 =head1 AUTHOR
918
919 Koha Developement team <info@koha.org>
920
921 Paul POULAIN paul.poulain@free.fr
922
923 =cut
924
925 # $Id$
926 # $Log$
927 # Revision 1.14  2005/04/05 17:07:46  hdl
928 # Scanning every the Subfields of auth_tag_to_report for FindDuplicate
929 #
930 # Revision 1.13  2005/04/05 15:23:41  hdl
931 # Searching for double entries when adding a new authority.
932 #
933 # Revision 1.12  2005/04/05 09:58:48  hdl
934 # Adding double authority search before creating a new authority
935 #
936 # Revision 1.11  2005/03/07 08:55:29  tipaul
937 # synch'ing with 2.2
938 #
939 # Revision 1.9.2.2  2005/02/28 14:03:13  tipaul
940 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
941 # * adding a select box to requet "contain" or "begin with" search.
942 # * fixing some bug in authority search (related to "main entry" search)
943 #
944 # Revision 1.9.2.1  2005/02/24 13:12:13  tipaul
945 # 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.
946 # So, it will be done through a cron job.
947 # (/me agree we need some doc for command line scripts)
948 #
949 # Revision 1.9  2004/12/23 09:48:11  tipaul
950 # Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
951 #
952 # Revision 1.8  2004/11/05 10:11:39  tipaul
953 # export auth_count_usage (bugfix)
954 #
955 # Revision 1.7  2004/09/23 16:13:00  tipaul
956 # Bugfix in modification
957 #
958 # Revision 1.6  2004/08/18 16:00:24  tipaul
959 # fixes for authorities management
960 #
961 # Revision 1.5  2004/07/05 13:37:22  doxulting
962 # First step for working authorities
963 #
964 # Revision 1.4  2004/06/22 11:35:37  tipaul
965 # removing % at the beginning of a string to avoid loooonnnngggg searchs
966 #
967 # Revision 1.3  2004/06/17 08:02:13  tipaul
968 # merging tag & subfield in auth_word for better perfs
969 #
970 # Revision 1.2  2004/06/10 08:29:01  tipaul
971 # MARC authority management (continued)
972 #
973 # Revision 1.1  2004/06/07 07:35:01  tipaul
974 # MARC authority management package
975 #