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\[$1$tagsubf$2]$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')." ".$record->field($mainentrytag)->subfield('b') 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 $cgidir = C4::Context->intranetdir ."/cgi-bin";
545 unless (opendir(DIR, "$cgidir")) {
546 $cgidir = C4::Context->intranetdir."/";
549 my $filename = $cgidir."/localfile/modified_authorities/$authid.authid";
550 open AUTH, "> $filename";
555 sub AUTHdelauthority {
556 my ($dbh,$authid,$keep_biblio) = @_;
557 # if the keep_biblio is set to 1, then authority entries in biblio are preserved.
558 # This flag is set when the delauthority is called by modauthority
559 # due to a too complex structure of MARC (repeatable fields and subfields),
560 # the best solution for a modif is to delete / recreate the record.
562 my $record = AUTHgetauthority($dbh,$authid);
563 $dbh->do("delete from auth_header where authid=$authid") unless $keep_biblio;
564 $dbh->do("delete from auth_subfield_table where authid=$authid");
565 $dbh->do("delete from auth_word where authid=$authid");
566 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag)
569 sub AUTHmodsubfield {
570 # Subroutine changes a subfield value given a subfieldid.
571 my ($dbh, $subfieldid, $subfieldvalue )=@_;
572 $dbh->do("lock tables auth_subfield_table WRITE");
573 my $sth=$dbh->prepare("update auth_subfield_table set subfieldvalue=? where subfieldid=?");
574 $sth->execute($subfieldvalue, $subfieldid);
575 $dbh->do("unlock tables");
577 $sth=$dbh->prepare("select authid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from auth_subfield_table where subfieldid=?");
578 $sth->execute($subfieldid);
579 my ($authid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
581 &AUTHdelword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
582 &AUTHaddword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
583 return($subfieldid, $subfieldvalue);
586 sub AUTHfindsubfield {
587 my ($dbh,$authid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
591 my $query="select subfieldid from auth_subfield_table where authid=? and tag=? and subfieldcode=?";
592 my @bind_values = ($authid,$tag, $subfieldcode);
593 if ($subfieldvalue) {
594 $query .= " and subfieldvalue=?";
595 push(@bind_values,$subfieldvalue);
597 if ($subfieldorder<1) {
600 $query .= " and subfieldorder=?";
601 push(@bind_values,$subfieldorder);
603 my $sti=$dbh->prepare($query);
604 $sti->execute(@bind_values);
605 while (($subfieldid) = $sti->fetchrow) {
607 $lastsubfieldid=$subfieldid;
609 if ($resultcounter>1) {
610 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
611 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
614 return $lastsubfieldid;
618 sub AUTHfindsubfieldid {
619 my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
620 my $sth=$dbh->prepare("select subfieldid from auth_subfield_table
621 where authid=? and tag=? and tagorder=?
622 and subfieldcode=? and subfieldorder=?");
623 $sth->execute($authid,$tag,$tagorder,$subfield,$subfieldorder);
624 my ($res) = $sth->fetchrow;
626 $sth=$dbh->prepare("select subfieldid from auth_subfield_table
627 where authid=? and tag=? and tagorder=?
628 and subfieldcode=?");
629 $sth->execute($authid,$tag,$tagorder,$subfield);
630 ($res) = $sth->fetchrow;
635 sub AUTHfind_authtypecode {
636 my ($dbh,$authid) = @_;
637 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
638 $sth->execute($authid);
639 my ($authtypecode) = $sth->fetchrow;
640 return $authtypecode;
643 sub AUTHdelsubfield {
644 # delete a subfield for $authid / tag / tagorder / subfield / subfieldorder
645 my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
646 $dbh->do("delete from auth_subfield_table where authid='$authid' and
647 tag='$tag' and tagorder='$tagorder'
648 and subfieldcode='$subfield' and subfieldorder='$subfieldorder'
653 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
655 my $record = MARC::Record->new();
656 # my %subfieldlist=();
657 my $prevvalue; # if tag <10
658 my $field; # if tag >=10
659 for (my $i=0; $i< @$rtags; $i++) {
660 # rebuild MARC::Record
661 if (@$rtags[$i] ne $prevtag) {
664 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
668 $record->add_fields($field);
671 $indicators{@$rtags[$i]}.=' ';
672 if (@$rtags[$i] <10) {
673 $prevvalue= @$rvalues[$i];
677 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
679 $prevtag = @$rtags[$i];
681 if (@$rtags[$i] <10) {
682 $prevvalue=@$rvalues[$i];
684 if (length(@$rvalues[$i])>0) {
685 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
688 $prevtag= @$rtags[$i];
691 # the last has not been included inside the loop... do it now !
692 $record->add_fields($field) if $field;
697 # split a subfield string and adds it into the word table.
699 my ($dbh,$authid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
700 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
701 my @words = split / /,$sentence;
702 my $stopwords= C4::Context->stopwords;
703 my $sth=$dbh->prepare("insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word)
704 values (?,concat(?,?),?,?,?,soundex(?))");
705 foreach my $word (@words) {
706 # we record only words longer than 2 car and not in stopwords hash
707 if (length($word)>2 and !($stopwords->{uc($word)})) {
708 $sth->execute($authid,$tag,$subfieldid,$tagorder,$subfieldorder,$word,$word);
710 warn "ERROR ==> insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word) values ($authid,concat($tag,$subfieldid),$tagorder,$subfieldorder,$word,soundex($word))\n";
717 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
718 my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
719 my $sth=$dbh->prepare("delete from auth_word where authid=? and tagsubfield=concat(?,?) and tagorder=? and subfieldorder=?");
720 $sth->execute($authid,$tag,$subfield,$tagorder,$subfieldorder);
724 # converts ISO 5426 coded string to ISO 8859-1
725 # sloppy code : should be improved in next issue
726 my ($string,$encoding) = @_ ;
728 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
729 if ($encoding eq "UNIMARC") {
791 # this handles non-sorting blocks (if implementation requires this)
792 $string = nsb_clean($_) ;
793 } elsif ($encoding eq "USMARC" || $encoding eq "MARC21") {
846 # this handles non-sorting blocks (if implementation requires this)
847 $string = nsb_clean($_) ;
854 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
855 my $NSE = '\x89' ; # NSE : Non Sorting Block end
856 # handles non sorting blocks
860 s/[ ]{0,1}$NSE/) /gm ;
866 my ($record,$authtypecode)=@_;
867 warn "IN for ".$record->as_formatted;
868 my $dbh = C4::Context->dbh;
870 # warn "".$record->as_formatted;
871 my $sth = $dbh->prepare("select auth_tag_to_report,summary from auth_types where authtypecode=?");
872 $sth->execute($authtypecode);
873 my ($auth_tag_to_report,$taglist) = $sth->fetchrow;
875 # build a request for authoritysearch
876 my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
877 # search on biblio.title
878 # warn " tag a reporter : $auth_tag_to_report";
879 # warn "taglist ".$taglist;
880 my @subfield = split /\[/, $taglist;
882 for (my $i=1; $i<$max;$i++){
883 warn " ".$subfield[$i];
884 $subfield[$i]=substr($subfield[$i],3,1);
885 # warn " ".$subfield[$i];
888 if ($record->fields($auth_tag_to_report)) {
889 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where tagfield=? and authtypecode=? ");
890 $sth->execute($auth_tag_to_report,$authtypecode);
891 # warn " field $auth_tag_to_report exists";
892 while (my ($tag,$subfield) = $sth->fetchrow){
893 if ($record->field($tag)->subfield($subfield)) {
894 warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
895 push @tags, $tag.$subfield;
896 # warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
900 push @value, $record->field($tag)->subfield($subfield);
905 my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
906 # there is at least 1 result => return the 1st one
908 warn "XXXXX $nbresult => ".@$finalresult[0]->{authid},@$finalresult[0]->{summary};
909 return @$finalresult[0]->{authid},@$finalresult[0]->{summary};
911 # no result, returns nothing
916 END { } # module clean-up code here (global destructor)
922 Koha Developement team <info@koha.org>
924 Paul POULAIN paul.poulain@free.fr
930 # Revision 1.20 2005/08/04 13:27:47 tipaul
931 # synch'ing 2.2 and head
933 # Revision 1.9.2.7 2005/08/01 15:14:50 tipaul
934 # minor change in summary handling (accepting 4 digits before the field)
936 # Revision 1.9.2.6 2005/06/07 10:02:00 tipaul
937 # porting dictionnary search from head to 2.2. there is now a ... facing titles, author & subject, to search in biblio & authorities existing values.
939 # Revision 1.9.2.5 2005/05/31 14:50:46 tipaul
940 # fix for authority merging. There was a bug on official installs
942 # Revision 1.9.2.4 2005/05/30 11:24:15 tipaul
943 # fixing a bug : when a field was repeated, the last field was also repeated. (Was due to the "empty" field in html between fields : to separate fields, in html, an empty field is automatically added. in AUTHhtml2marc, this empty field was not discarded correctly)
945 # Revision 1.9.2.3 2005/04/28 08:45:33 tipaul
946 # porting FindDuplicate feature for authorities from HEAD to rel_2_2, works correctly now.
948 # Revision 1.9.2.2 2005/02/28 14:03:13 tipaul
949 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
950 # * adding a select box to requet "contain" or "begin with" search.
951 # * fixing some bug in authority search (related to "main entry" search)
953 # Revision 1.9.2.1 2005/02/24 13:12:13 tipaul
954 # 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.
955 # So, it will be done through a cron job.
956 # (/me agree we need some doc for command line scripts)
958 # Revision 1.9 2004/12/23 09:48:11 tipaul
959 # Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
961 # Revision 1.8 2004/11/05 10:11:39 tipaul
962 # export auth_count_usage (bugfix)
964 # Revision 1.7 2004/09/23 16:13:00 tipaul
965 # Bugfix in modification
967 # Revision 1.6 2004/08/18 16:00:24 tipaul
968 # fixes for authorities management
970 # Revision 1.5 2004/07/05 13:37:22 doxulting
971 # First step for working authorities
973 # Revision 1.4 2004/06/22 11:35:37 tipaul
974 # removing % at the beginning of a string to avoid loooonnnngggg searchs
976 # Revision 1.3 2004/06/17 08:02:13 tipaul
977 # merging tag & subfield in auth_word for better perfs
979 # Revision 1.2 2004/06/10 08:29:01 tipaul
980 # MARC authority management (continued)
982 # Revision 1.1 2004/06/07 07:35:01 tipaul
983 # MARC authority management package