1 package C4::AuthoritiesMarc;
2 # Copyright 2000-2002 Katipo Communications
4 # This file is part of Koha.
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
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.
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
27 use vars qw($VERSION @ISA @EXPORT);
29 # set the version for version checking
36 &AUTHfind_authtypecode
52 &MARCaddword &MARCdelword
58 my ($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode) = @_;
59 # build the sql request. She will look like :
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%")
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 # also store main entry MARC tag, to extract it at end of search
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 $mainentrytag = $tag_to_report;
73 for (my $i=0;$i<$#{$tags};$i++) {
74 if (@$tags[$i] eq "mainentry") {
75 @$tags[$i] = $tag_to_report."a";
80 # quote marc fields/subfields
81 for (my $i=0;$i<=$#{$tags};$i++) {
83 @$tags[$i] = $dbh->quote(@$tags[$i]);
87 my @normal_and_or = ();
88 my @normal_operator = ();
89 my @normal_value = ();
90 # Extracts the NOT statements from the list of statements
91 for(my $i = 0 ; $i <= $#{$value} ; $i++)
94 @$value[$i] =~ s/\*/%/g;
95 # remove % at the beginning
96 @$value[$i] =~ s/^%//g;
97 @$value[$i] =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\}|\/)/ /g if @$operator[$i] eq "contains";
98 if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
100 foreach my $word (split(/ /, @$value[$i]))
102 unless (C4::Context->stopwords->{uc($word)}) { #it's NOT a stopword => use it. Otherwise, ignore
103 my $tag = substr(@$tags[$i],0,3);
104 my $subf = substr(@$tags[$i],3,1);
105 push @normal_tags, @$tags[$i];
106 push @normal_and_or, "and"; # assumes "foo" and "bar" if "foo bar" is entered
107 push @normal_operator, @$operator[$i];
108 push @normal_value, $word;
114 push @normal_tags, @$tags[$i];
115 push @normal_and_or, @$and_or[$i];
116 push @normal_operator, @$operator[$i];
117 push @normal_value, @$value[$i];
121 # Finds the basic results without the NOT requests
122 my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
127 $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)");
128 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)";
130 $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");
131 warn "Q : select distinct m1.authid from auth_header,$sql_tables where m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where1";
133 $sth->execute($authtypecode);
135 while (my ($authid) = $sth->fetchrow) {
136 push @result,$authid;
139 # we have authid list. Now, loads summary from [offset] to [offset]+[length]
140 my $counter = $offset;
141 my @finalresult = ();
143 while (($counter <= $#result) && ($counter <= ($offset + $length))) {
144 # warn " HERE : $counter, $#result, $offset, $length";
145 # get MARC::Record of the authority
146 my $record = AUTHgetauthority($dbh,$result[$counter]);
147 # then build the summary
148 my $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
149 my $authref = getauthtype($authtypecode);
150 my $summary = $authref->{summary};
151 my @fields = $record->fields();
152 foreach my $field (@fields) {
153 my $tag = $field->tag();
156 my @subf = $field->subfields;
157 for my $i (0..$#subf) {
158 my $subfieldcode = $subf[$i][0];
159 my $subfieldvalue = $subf[$i][1];
160 my $tagsubf = $tag.$subfieldcode;
161 $summary =~ s/\[(.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
165 $summary =~ s/\[(.*?)]//g;
166 $summary =~ s/\n/<br>/g;
168 # find biblio MARC field using this authtypecode (to jump to biblio)
169 my $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
170 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
171 $sth->execute($authtypecode);
172 my $tags_using_authtype;
173 while (my ($tagfield) = $sth->fetchrow) {
174 # warn "TAG : $tagfield";
175 $tags_using_authtype.= $tagfield."9,";
177 chop $tags_using_authtype;
179 # then add a line for the template loop
181 $newline{summary} = $summary;
182 $newline{authid} = $result[$counter];
183 $newline{used} = &AUTHcount_usage($result[$counter]);
184 $newline{biblio_fields} = $tags_using_authtype;
185 $newline{even} = $counter % 2;
186 $newline{mainentry} = $record->field($mainentrytag)->subfield('a') if $record->field($mainentrytag);
188 push @finalresult, \%newline;
190 my $nbresults = $#result + 1;
191 return (\@finalresult, $nbresults);
194 # Creates the SQL Request
197 my ($dbh,$tags, $and_or, $operator, $value) = @_;
199 my $sql_tables; # will contain marc_subfield_table as m1,...
200 my $sql_where1; # will contain the "true" where
201 my $sql_where2 = "("; # will contain m1.authid=m2.authid
202 my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
203 my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR is provided.
206 for(my $i=0; $i<=@$value;$i++) {
209 # warn " @$tags[$i]";
211 if (@$operator[$i] eq "start") {
212 $sql_tables .= "auth_subfield_table as m$nb_table,";
213 $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]%");
215 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
218 } elsif (@$operator[$i] eq "contains") {
219 $sql_tables .= "auth_word as m$nb_table,";
220 $sql_where1 .= "(m1.word like ".$dbh->quote("@$value[$i]%");
222 $sql_where1 .=" and m1.tagsubfield in (@$tags[$i])";
227 $sql_tables .= "auth_subfield_table as m$nb_table,";
228 $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
230 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
235 if (@$operator[$i] eq "start") {
237 $sql_tables .= "auth_subfield_table as m$nb_table,";
238 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]%");
240 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
243 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
244 } elsif (@$operator[$i] eq "contains") {
245 if (@$and_or[$i] eq 'and') {
247 $sql_tables .= "auth_word as m$nb_table,";
248 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
250 $sql_where1 .=" and m$nb_table.tagsubfield in(@$tags[$i])";
253 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
255 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
257 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldid in (@$tags[$i])";
260 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
264 $sql_tables .= "auth_subfield_table as m$nb_table,";
265 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
267 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
269 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
276 if($sql_where2 ne "(") # some datas added to sql_where2, processing
278 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
281 else # no sql_where2 statement, deleting '('
285 chop $sql_tables; # deletes the trailing ','
287 return ($sql_tables, $sql_where1, $sql_where2);
291 sub AUTHcount_usage {
293 my $dbh = C4::Context->dbh;
294 # find MARC fields using this authtype
295 my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
296 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
297 $sth->execute($authtypecode);
298 my $tags_using_authtype;
299 while (my ($tagfield) = $sth->fetchrow) {
300 # warn "TAG : $tagfield";
301 $tags_using_authtype.= "'".$tagfield."9',";
303 chop $tags_using_authtype;
304 if ($tags_using_authtype) {
305 $sth = $dbh->prepare("select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=?");
307 $sth = $dbh->prepare("select count(*) from marc_subfield_table where subfieldvalue=?");
309 # warn "Q : select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=$authid";
310 $sth->execute($authid);
311 my ($result) = $sth->fetchrow;
312 # warn "Authority $authid TOTAL USED : $result";
316 # merging 2 authority entries. After a merge, the "from" can be deleted.
318 # my ($auth_merge_from,$auth_merge_to) = @_;
319 # my $dbh = C4::Context->dbh;
320 # # find MARC fields using this authtype
321 # my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
323 # my $record_from = AUTHgetauthority($dbh,$auth_merge_from);
324 # my $record_to = AUTHgetauthority($dbh,$auth_merge_to);
325 # my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
326 # $sth->execute($authtypecode);
327 # my $tags_using_authtype;
328 # while (my ($tagfield) = $sth->fetchrow) {
329 # warn "TAG : $tagfield";
330 # $tags_using_authtype.= "'".$tagfield."9',";
332 # chop $tags_using_authtype;
333 # # now, find every biblio using this authority
334 # $sth = $dbh->prepare("select bibid,tag,tag_indicator,tagorder from marc_subfield_table where tag+subfieldid in ($tags_using_authtype) and subfieldvalue=?");
335 # $sth->execute($authid);
336 # # and delete entries before recreating them
337 # while (my ($bibid,$tag,$tag_indicator,$tagorder) = $sth->fetchrow) {
338 # &MARCdelsubfield($dbh,$bibid,$tag);
344 sub AUTHfind_authtypecode {
345 my ($dbh,$authid) = @_;
346 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
347 $sth->execute($authid);
348 my ($authtypecode) = $sth->fetchrow;
349 return $authtypecode;
354 my ($dbh,$forlibrarian,$authtypecode)= @_;
355 $authtypecode="" unless $authtypecode;
357 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
358 # check that framework exists
359 $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
360 $sth->execute($authtypecode);
361 my ($total) = $sth->fetchrow;
362 $authtypecode="" unless ($total >0);
363 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory,repeatable from auth_tag_structure where authtypecode=? order by tagfield");
364 $sth->execute($authtypecode);
365 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
366 while ( ($tag,$lib,$mandatory,$repeatable) = $sth->fetchrow) {
367 $res->{$tag}->{lib}=$lib;
368 $res->{$tab}->{tab}=""; # XXX
369 $res->{$tag}->{mandatory}=$mandatory;
370 $res->{$tag}->{repeatable}=$repeatable;
373 $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");
374 $sth->execute($authtypecode);
377 my $authorised_value;
378 my $thesaurus_category;
384 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$value_builder,$seealso) = $sth->fetchrow) {
385 $res->{$tag}->{$subfield}->{lib}=$lib;
386 $res->{$tag}->{$subfield}->{tab}=$tab;
387 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
388 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
389 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
390 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
391 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
392 $res->{$tag}->{$subfield}->{seealso}=$seealso;
393 $res->{$tag}->{$subfield}->{hidden}=$hidden;
394 $res->{$tag}->{$subfield}->{isurl}=$isurl;
399 sub AUTHaddauthority {
400 # pass the MARC::Record to this function, and it will create the records in the marc tables
401 my ($dbh,$record,$authid,$authtypecode) = @_;
402 my @fields=$record->fields();
403 # warn "IN AUTHaddauthority $authid => ".$record->as_formatted;
404 # adding main table, and retrieving authid
405 # if authid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
406 # if authid empty => true add, find a new authid number
408 $dbh->do("lock tables auth_header WRITE,auth_subfield_table WRITE, auth_word WRITE, stopwords READ");
409 my $sth=$dbh->prepare("insert into auth_header (datecreated,authtypecode) values (now(),?)");
410 $sth->execute($authtypecode);
411 $sth=$dbh->prepare("select max(authid) from auth_header");
413 ($authid)=$sth->fetchrow;
417 # now, add subfields...
418 foreach my $field (@fields) {
420 if ($field->tag() <10) {
421 &AUTHaddsubfield($dbh,$authid,
430 my @subfields=$field->subfields();
431 foreach my $subfieldcount (0..$#subfields) {
432 &AUTHaddsubfield($dbh,$authid,
434 $field->indicator(1).$field->indicator(2),
436 $subfields[$subfieldcount][0],
438 $subfields[$subfieldcount][1]
443 $dbh->do("unlock tables");
448 sub AUTHaddsubfield {
449 # Add a new subfield to a tag into the DB.
450 my ($dbh,$authid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
451 # if not value, end of job, we do nothing
452 if (length($subfieldvalues) ==0) {
455 if (not($subfieldcode)) {
458 my @subfieldvalues = split /\|/,$subfieldvalues;
459 foreach my $subfieldvalue (@subfieldvalues) {
460 my $sth=$dbh->prepare("insert into auth_subfield_table (authid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
461 $sth->execute($authid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
463 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";
465 &AUTHaddword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
469 sub AUTHgetauthority {
470 # Returns MARC::Record of the biblio passed in parameter.
471 my ($dbh,$authid)=@_;
472 my $record = MARC::Record->new();
473 #---- TODO : the leader is missing
474 $record->leader(' ');
475 my $sth=$dbh->prepare("select authid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue
476 from auth_subfield_table
477 where authid=? order by tag,tagorder,subfieldcode
479 $sth->execute($authid);
483 my $field; # for >=10 tags
484 my $prevvalue; # for <10 tags
485 while (my $row=$sth->fetchrow_hashref) {
486 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
489 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
491 $record->add_fields($field) unless $prevtag eq "XXX";
494 $prevtagorder=$row->{tagorder};
495 $prevtag = $row->{tag};
496 $previndicator=$row->{tag_indicator};
497 if ($row->{tag}<10) {
498 $prevvalue = $row->{subfieldvalue};
500 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
503 if ($row->{tag} <10) {
504 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
506 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
508 $prevtag= $row->{tag};
509 $previndicator=$row->{tag_indicator};
512 # the last has not been included inside the loop... do it now !
513 if ($prevtag ne "XXX") { # check that we have found something. Otherwise, prevtag is still XXX and we
514 # must return an empty record, not make MARC::Record fail because we try to
515 # create a record with XXX as field :-(
517 $record->add_fields($prevtag,$prevvalue);
519 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
520 $record->add_fields($field);
526 sub AUTHgetauth_type {
527 my ($authtypecode) = @_;
528 my $dbh=C4::Context->dbh;
529 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
530 $sth->execute($authtypecode);
531 return $sth->fetchrow_hashref;
533 sub AUTHmodauthority {
534 my ($dbh,$authid,$record,$delete)=@_;
535 my $oldrecord=&AUTHgetauthority($dbh,$authid);
536 if ($oldrecord eq $record) {
539 # 1st delete the authority,
541 &AUTHdelauthority($dbh,$authid,1);
542 &AUTHaddauthority($dbh,$record,$authid,AUTHfind_authtypecode($dbh,$authid));
543 # save the file in localfile/modified_authorities
544 my $filename = C4::Context->config("intranetdir")."/localfile/modified_authorities/$authid.authid";
545 open AUTH, "> $filename";
550 sub AUTHdelauthority {
551 my ($dbh,$authid,$keep_biblio) = @_;
552 # if the keep_biblio is set to 1, then authority entries in biblio are preserved.
553 # This flag is set when the delauthority is called by modauthority
554 # due to a too complex structure of MARC (repeatable fields and subfields),
555 # the best solution for a modif is to delete / recreate the record.
557 my $record = AUTHgetauthority($dbh,$authid);
558 $dbh->do("delete from auth_header where authid=$authid") unless $keep_biblio;
559 $dbh->do("delete from auth_subfield_table where authid=$authid");
560 $dbh->do("delete from auth_word where authid=$authid");
561 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag)
564 sub AUTHmodsubfield {
565 # Subroutine changes a subfield value given a subfieldid.
566 my ($dbh, $subfieldid, $subfieldvalue )=@_;
567 $dbh->do("lock tables auth_subfield_table WRITE");
568 my $sth=$dbh->prepare("update auth_subfield_table set subfieldvalue=? where subfieldid=?");
569 $sth->execute($subfieldvalue, $subfieldid);
570 $dbh->do("unlock tables");
572 $sth=$dbh->prepare("select authid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from auth_subfield_table where subfieldid=?");
573 $sth->execute($subfieldid);
574 my ($authid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
576 &AUTHdelword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
577 &AUTHaddword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
578 return($subfieldid, $subfieldvalue);
581 sub AUTHfindsubfield {
582 my ($dbh,$authid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
586 my $query="select subfieldid from auth_subfield_table where authid=? and tag=? and subfieldcode=?";
587 my @bind_values = ($authid,$tag, $subfieldcode);
588 if ($subfieldvalue) {
589 $query .= " and subfieldvalue=?";
590 push(@bind_values,$subfieldvalue);
592 if ($subfieldorder<1) {
595 $query .= " and subfieldorder=?";
596 push(@bind_values,$subfieldorder);
598 my $sti=$dbh->prepare($query);
599 $sti->execute(@bind_values);
600 while (($subfieldid) = $sti->fetchrow) {
602 $lastsubfieldid=$subfieldid;
604 if ($resultcounter>1) {
605 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
606 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
609 return $lastsubfieldid;
613 sub AUTHfindsubfieldid {
614 my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
615 my $sth=$dbh->prepare("select subfieldid from auth_subfield_table
616 where authid=? and tag=? and tagorder=?
617 and subfieldcode=? and subfieldorder=?");
618 $sth->execute($authid,$tag,$tagorder,$subfield,$subfieldorder);
619 my ($res) = $sth->fetchrow;
621 $sth=$dbh->prepare("select subfieldid from auth_subfield_table
622 where authid=? and tag=? and tagorder=?
623 and subfieldcode=?");
624 $sth->execute($authid,$tag,$tagorder,$subfield);
625 ($res) = $sth->fetchrow;
630 sub AUTHfind_authtypecode {
631 my ($dbh,$authid) = @_;
632 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
633 $sth->execute($authid);
634 my ($authtypecode) = $sth->fetchrow;
635 return $authtypecode;
638 sub AUTHdelsubfield {
639 # delete a subfield for $authid / tag / tagorder / subfield / subfieldorder
640 my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
641 $dbh->do("delete from auth_subfield_table where authid='$authid' and
642 tag='$tag' and tagorder='$tagorder'
643 and subfieldcode='$subfield' and subfieldorder='$subfieldorder'
648 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
650 my $record = MARC::Record->new();
651 # my %subfieldlist=();
652 my $prevvalue; # if tag <10
653 my $field; # if tag >=10
654 for (my $i=0; $i< @$rtags; $i++) {
655 # rebuild MARC::Record
656 if (@$rtags[$i] ne $prevtag) {
659 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
663 $record->add_fields($field);
666 $indicators{@$rtags[$i]}.=' ';
667 if (@$rtags[$i] <10) {
668 $prevvalue= @$rvalues[$i];
670 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
672 $prevtag = @$rtags[$i];
674 if (@$rtags[$i] <10) {
675 $prevvalue=@$rvalues[$i];
678 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
681 $prevtag= @$rtags[$i];
684 # the last has not been included inside the loop... do it now !
685 $record->add_fields($field);
686 # warn $record->as_formatted;
691 # split a subfield string and adds it into the word table.
693 my ($dbh,$authid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
694 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
695 my @words = split / /,$sentence;
696 my $stopwords= C4::Context->stopwords;
697 my $sth=$dbh->prepare("insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word)
698 values (?,concat(?,?),?,?,?,soundex(?))");
699 foreach my $word (@words) {
700 # we record only words longer than 2 car and not in stopwords hash
701 if (length($word)>2 and !($stopwords->{uc($word)})) {
702 $sth->execute($authid,$tag,$subfieldid,$tagorder,$subfieldorder,$word,$word);
704 warn "ERROR ==> insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word) values ($authid,concat($tag,$subfieldid),$tagorder,$subfieldorder,$word,soundex($word))\n";
711 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
712 my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
713 my $sth=$dbh->prepare("delete from auth_word where authid=? and tagsubfield=concat(?,?) and tagorder=? and subfieldorder=?");
714 $sth->execute($authid,$tag,$subfield,$tagorder,$subfieldorder);
718 # converts ISO 5426 coded string to ISO 8859-1
719 # sloppy code : should be improved in next issue
720 my ($string,$encoding) = @_ ;
722 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
723 if ($encoding eq "UNIMARC") {
785 # this handles non-sorting blocks (if implementation requires this)
786 $string = nsb_clean($_) ;
787 } elsif ($encoding eq "USMARC" || $encoding eq "MARC21") {
840 # this handles non-sorting blocks (if implementation requires this)
841 $string = nsb_clean($_) ;
848 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
849 my $NSE = '\x89' ; # NSE : Non Sorting Block end
850 # handles non sorting blocks
854 s/[ ]{0,1}$NSE/) /gm ;
860 my ($record,$authtypecode)=@_;
861 warn "IN for ".$record->as_formatted;
862 my $dbh = C4::Context->dbh;
864 # warn "".$record->as_formatted;
865 my $sth = $dbh->prepare("select auth_tag_to_report,summary from auth_types where authtypecode=?");
866 $sth->execute($authtypecode);
867 my ($auth_tag_to_report,$taglist) = $sth->fetchrow;
869 # build a request for authoritysearch
870 my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
871 # search on biblio.title
872 # warn " tag a reporter : $auth_tag_to_report";
873 # warn "taglist ".$taglist;
874 my @subfield = split /\[/, $taglist;
876 for (my $i=1; $i<$max;$i++){
877 warn " ".$subfield[$i];
878 $subfield[$i]=substr($subfield[$i],3,1);
879 # warn " ".$subfield[$i];
882 if ($record->fields($auth_tag_to_report)) {
883 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where tagfield=? and authtypecode=? ");
884 $sth->execute($auth_tag_to_report,$authtypecode);
885 # warn " field $auth_tag_to_report exists";
886 while (my ($tag,$subfield) = $sth->fetchrow){
887 if ($record->field($tag)->subfield($subfield)) {
888 warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
889 push @tags, $tag.$subfield;
890 # warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
894 push @value, $record->field($tag)->subfield($subfield);
899 my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
900 # there is at least 1 result => return the 1st one
902 warn "XXXXX $nbresult => ".@$finalresult[0]->{authid},@$finalresult[0]->{summary};
903 return @$finalresult[0]->{authid},@$finalresult[0]->{summary};
905 # no result, returns nothing
910 END { } # module clean-up code here (global destructor)
916 Koha Developement team <info@koha.org>
918 Paul POULAIN paul.poulain@free.fr
924 # Revision 1.17 2005/06/01 12:51:02 tipaul
925 # some fixes & improvements for dictionnary search in librarian interface
927 # Revision 1.16 2005/05/04 15:43:43 tipaul
928 # synch'ing 2.2 and head
930 # Revision 1.9.2.3 2005/04/28 08:45:33 tipaul
931 # porting FindDuplicate feature for authorities from HEAD to rel_2_2, works correctly now.
933 # Revision 1.9.2.2 2005/02/28 14:03:13 tipaul
934 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
935 # * adding a select box to requet "contain" or "begin with" search.
936 # * fixing some bug in authority search (related to "main entry" search)
938 # Revision 1.9.2.1 2005/02/24 13:12:13 tipaul
939 # 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.
940 # So, it will be done through a cron job.
941 # (/me agree we need some doc for command line scripts)
943 # Revision 1.9 2004/12/23 09:48:11 tipaul
944 # Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
946 # Revision 1.8 2004/11/05 10:11:39 tipaul
947 # export auth_count_usage (bugfix)
949 # Revision 1.7 2004/09/23 16:13:00 tipaul
950 # Bugfix in modification
952 # Revision 1.6 2004/08/18 16:00:24 tipaul
953 # fixes for authorities management
955 # Revision 1.5 2004/07/05 13:37:22 doxulting
956 # First step for working authorities
958 # Revision 1.4 2004/06/22 11:35:37 tipaul
959 # removing % at the beginning of a string to avoid loooonnnngggg searchs
961 # Revision 1.3 2004/06/17 08:02:13 tipaul
962 # merging tag & subfield in auth_word for better perfs
964 # Revision 1.2 2004/06/10 08:29:01 tipaul
965 # MARC authority management (continued)
967 # Revision 1.1 2004/06/07 07:35:01 tipaul
968 # MARC authority management package