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