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 for (my $i=0;$i<$#{$tags};$i++) {
68 if (@$tags[$i] eq "mainentry") {
69 my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
70 $sth->execute($authtypecode);
71 my ($tag_to_report) = $sth->fetchrow;
72 @$tags[$i] = $tag_to_report."a";
77 # quote marc fields/subfields
78 for (my $i=0;$i<=$#{$tags};$i++) {
79 # warn " $i: ".@$tags[$i];
81 @$tags[$i] = $dbh->quote(@$tags[$i]);
82 # warn " $i After process: ".@$tags[$i];
86 my @normal_and_or = ();
87 my @normal_operator = ();
88 my @normal_value = ();
89 # Extracts the NOT statements from the list of statements
90 for(my $i = 0 ; $i <= $#{$value} ; $i++)
92 if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
94 foreach my $word (split(/ /, @$value[$i]))
96 unless (C4::Context->stopwords->{uc($word)}) { #it's NOT a stopword => use it. Otherwise, ignore
97 my $tag = substr(@$tags[$i],0,3);
98 my $subf = substr(@$tags[$i],3,1);
99 push @normal_tags, @$tags[$i];
100 push @normal_and_or, "and"; # assumes "foo" and "bar" if "foo bar" is entered
101 push @normal_operator, @$operator[$i];
102 push @normal_value, $word;
108 push @normal_tags, @$tags[$i];
109 push @normal_and_or, @$and_or[$i];
110 push @normal_operator, @$operator[$i];
111 push @normal_value, @$value[$i];
115 # Finds the basic results without the NOT requests
116 my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
121 $sth = $dbh->prepare("select distinct m1.authid from auth_header,$sql_tables where m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where2 and ($sql_where1)");
122 warn "Q2 : select distinct m1.authid from auth_header,$sql_tables where m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where2 and ($sql_where1)";
124 $sth = $dbh->prepare("select distinct m1.authid from auth_header,$sql_tables where m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where1");
125 warn "Q : select distinct m1.authid from auth_header,$sql_tables where m1.authid=auth_header.authid and auth_header.authtypecode=? and $sql_where1";
127 $sth->execute($authtypecode);
129 while (my ($authid) = $sth->fetchrow) {
130 push @result,$authid;
133 # we have authid list. Now, loads summary from [offset] to [offset]+[length]
134 my $counter = $offset;
135 my @finalresult = ();
137 while (($counter <= $#result) && ($counter <= ($offset + $length))) {
138 # warn " HERE : $counter, $#result, $offset, $length";
139 # get MARC::Record of the authority
140 my $record = AUTHgetauthority($dbh,$result[$counter]);
141 # then build the summary
142 my $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
143 my $authref = getauthtype($authtypecode);
144 my $summary = $authref->{summary};
145 my @fields = $record->fields();
146 foreach my $field (@fields) {
147 my $tag = $field->tag();
150 my @subf = $field->subfields;
151 for my $i (0..$#subf) {
152 my $subfieldcode = $subf[$i][0];
153 my $subfieldvalue = $subf[$i][1];
154 my $tagsubf = $tag.$subfieldcode;
155 $summary =~ s/\[(.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
159 $summary =~ s/\[(.*?)]//g;
160 $summary =~ s/\n/<br>/g;
162 # find biblio MARC field using this authtypecode (to jump to biblio)
163 my $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
164 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
165 $sth->execute($authtypecode);
166 my $tags_using_authtype;
167 while (my ($tagfield) = $sth->fetchrow) {
168 # warn "TAG : $tagfield";
169 $tags_using_authtype.= $tagfield."9,";
171 chop $tags_using_authtype;
173 # then add a line for the template loop
175 $newline{summary} = $summary;
176 $newline{authid} = $result[$counter];
177 $newline{used} = &AUTHcount_usage($result[$counter]);
178 $newline{biblio_fields} = $tags_using_authtype;
179 $newline{marcrecord} = $record;
181 push @finalresult, \%newline;
183 my $nbresults = $#result + 1;
184 return (\@finalresult, $nbresults);
187 # Creates the SQL Request
190 my ($dbh,$tags, $and_or, $operator, $value) = @_;
192 my $sql_tables; # will contain marc_subfield_table as m1,...
193 my $sql_where1; # will contain the "true" where
194 my $sql_where2 = "("; # will contain m1.authid=m2.authid
195 my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
196 my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR is provided.
199 for(my $i=0; $i<=@$value;$i++) {
202 # warn " @$tags[$i]";
204 if (@$operator[$i] eq "start") {
205 $sql_tables .= "auth_subfield_table as m$nb_table,";
206 $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]%");
208 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
211 } elsif (@$operator[$i] eq "contains") {
212 $sql_tables .= "auth_word as m$nb_table,";
213 $sql_where1 .= "(m1.word like ".$dbh->quote("@$value[$i]%");
215 $sql_where1 .=" and m1.tagsubfield in (@$tags[$i])";
220 $sql_tables .= "auth_subfield_table as m$nb_table,";
221 $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
223 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
228 if (@$operator[$i] eq "start") {
230 $sql_tables .= "auth_subfield_table as m$nb_table,";
231 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]%");
233 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
236 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
237 } elsif (@$operator[$i] eq "contains") {
238 if (@$and_or[$i] eq 'and') {
240 $sql_tables .= "auth_word as m$nb_table,";
241 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
243 $sql_where1 .=" and m$nb_table.tagsubfield in(@$tags[$i])";
246 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
248 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
250 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldid in (@$tags[$i])";
253 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
257 $sql_tables .= "auth_subfield_table as m$nb_table,";
258 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
260 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
262 $sql_where2 .= "m1.authid=m$nb_table.authid and ";
269 if($sql_where2 ne "(") # some datas added to sql_where2, processing
271 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
274 else # no sql_where2 statement, deleting '('
278 chop $sql_tables; # deletes the trailing ','
280 return ($sql_tables, $sql_where1, $sql_where2);
284 sub AUTHcount_usage {
286 my $dbh = C4::Context->dbh;
287 # find MARC fields using this authtype
288 my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
289 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
290 $sth->execute($authtypecode);
291 my $tags_using_authtype;
292 while (my ($tagfield) = $sth->fetchrow) {
293 # warn "TAG : $tagfield";
294 $tags_using_authtype.= "'".$tagfield."9',";
296 chop $tags_using_authtype;
297 if ($tags_using_authtype) {
298 $sth = $dbh->prepare("select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=?");
300 $sth = $dbh->prepare("select count(*) from marc_subfield_table where subfieldvalue=?");
302 # warn "Q : select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=$authid";
303 $sth->execute($authid);
304 my ($result) = $sth->fetchrow;
305 # warn "Authority $authid TOTAL USED : $result";
309 # merging 2 authority entries. After a merge, the "from" can be deleted.
311 # my ($auth_merge_from,$auth_merge_to) = @_;
312 # my $dbh = C4::Context->dbh;
313 # # find MARC fields using this authtype
314 # my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
316 # my $record_from = AUTHgetauthority($dbh,$auth_merge_from);
317 # my $record_to = AUTHgetauthority($dbh,$auth_merge_to);
318 # my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
319 # $sth->execute($authtypecode);
320 # my $tags_using_authtype;
321 # while (my ($tagfield) = $sth->fetchrow) {
322 # warn "TAG : $tagfield";
323 # $tags_using_authtype.= "'".$tagfield."9',";
325 # chop $tags_using_authtype;
326 # # now, find every biblio using this authority
327 # $sth = $dbh->prepare("select bibid,tag,tag_indicator,tagorder from marc_subfield_table where tag+subfieldid in ($tags_using_authtype) and subfieldvalue=?");
328 # $sth->execute($authid);
329 # # and delete entries before recreating them
330 # while (my ($bibid,$tag,$tag_indicator,$tagorder) = $sth->fetchrow) {
331 # &MARCdelsubfield($dbh,$bibid,$tag);
337 sub AUTHfind_authtypecode {
338 my ($dbh,$authid) = @_;
339 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
340 $sth->execute($authid);
341 my ($authtypecode) = $sth->fetchrow;
342 return $authtypecode;
347 my ($dbh,$forlibrarian,$authtypecode)= @_;
348 $authtypecode="" unless $authtypecode;
350 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
351 # check that framework exists
352 $sth=$dbh->prepare("select count(*) from auth_tag_structure where authtypecode=?");
353 $sth->execute($authtypecode);
354 my ($total) = $sth->fetchrow;
355 $authtypecode="" unless ($total >0);
356 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory,repeatable from auth_tag_structure where authtypecode=? order by tagfield");
357 $sth->execute($authtypecode);
358 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
359 while ( ($tag,$lib,$mandatory,$repeatable) = $sth->fetchrow) {
360 $res->{$tag}->{lib}=$lib;
361 $res->{$tab}->{tab}=""; # XXX
362 $res->{$tag}->{mandatory}=$mandatory;
363 $res->{$tag}->{repeatable}=$repeatable;
366 $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");
367 $sth->execute($authtypecode);
370 my $authorised_value;
371 my $thesaurus_category;
377 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$value_builder,$seealso) = $sth->fetchrow) {
378 $res->{$tag}->{$subfield}->{lib}=$lib;
379 $res->{$tag}->{$subfield}->{tab}=$tab;
380 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
381 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
382 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
383 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
384 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
385 $res->{$tag}->{$subfield}->{seealso}=$seealso;
386 $res->{$tag}->{$subfield}->{hidden}=$hidden;
387 $res->{$tag}->{$subfield}->{isurl}=$isurl;
392 sub AUTHaddauthority {
393 # pass the MARC::Record to this function, and it will create the records in the marc tables
394 my ($dbh,$record,$authid,$authtypecode) = @_;
395 my @fields=$record->fields();
396 # warn "IN AUTHaddauthority $authid => ".$record->as_formatted;
397 # adding main table, and retrieving authid
398 # if authid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
399 # if authid empty => true add, find a new authid number
401 $dbh->do("lock tables auth_header WRITE,auth_subfield_table WRITE, auth_word WRITE, stopwords READ");
402 my $sth=$dbh->prepare("insert into auth_header (datecreated,authtypecode) values (now(),?)");
403 $sth->execute($authtypecode);
404 $sth=$dbh->prepare("select max(authid) from auth_header");
406 ($authid)=$sth->fetchrow;
410 # now, add subfields...
411 foreach my $field (@fields) {
413 if ($field->tag() <10) {
414 &AUTHaddsubfield($dbh,$authid,
423 my @subfields=$field->subfields();
424 foreach my $subfieldcount (0..$#subfields) {
425 &AUTHaddsubfield($dbh,$authid,
427 $field->indicator(1).$field->indicator(2),
429 $subfields[$subfieldcount][0],
431 $subfields[$subfieldcount][1]
436 $dbh->do("unlock tables");
441 sub AUTHaddsubfield {
442 # Add a new subfield to a tag into the DB.
443 my ($dbh,$authid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
444 # if not value, end of job, we do nothing
445 if (length($subfieldvalues) ==0) {
448 if (not($subfieldcode)) {
451 my @subfieldvalues = split /\|/,$subfieldvalues;
452 foreach my $subfieldvalue (@subfieldvalues) {
453 my $sth=$dbh->prepare("insert into auth_subfield_table (authid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
454 $sth->execute($authid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
456 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";
458 &AUTHaddword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
462 sub AUTHgetauthority {
463 # Returns MARC::Record of the biblio passed in parameter.
464 my ($dbh,$authid)=@_;
465 my $record = MARC::Record->new();
466 #---- TODO : the leader is missing
467 $record->leader(' ');
468 my $sth=$dbh->prepare("select authid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue
469 from auth_subfield_table
470 where authid=? order by tag,tagorder,subfieldcode
472 $sth->execute($authid);
476 my $field; # for >=10 tags
477 my $prevvalue; # for <10 tags
478 while (my $row=$sth->fetchrow_hashref) {
479 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
482 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
484 $record->add_fields($field) unless $prevtag eq "XXX";
487 $prevtagorder=$row->{tagorder};
488 $prevtag = $row->{tag};
489 $previndicator=$row->{tag_indicator};
490 if ($row->{tag}<10) {
491 $prevvalue = $row->{subfieldvalue};
493 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
496 if ($row->{tag} <10) {
497 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
499 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
501 $prevtag= $row->{tag};
502 $previndicator=$row->{tag_indicator};
505 # the last has not been included inside the loop... do it now !
506 if ($prevtag ne "XXX") { # check that we have found something. Otherwise, prevtag is still XXX and we
507 # must return an empty record, not make MARC::Record fail because we try to
508 # create a record with XXX as field :-(
510 $record->add_fields($prevtag,$prevvalue);
512 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
513 $record->add_fields($field);
519 sub AUTHgetauth_type {
520 my ($authtypecode) = @_;
521 my $dbh=C4::Context->dbh;
522 my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
523 $sth->execute($authtypecode);
524 return $sth->fetchrow_hashref;
526 sub AUTHmodauthority {
527 my ($dbh,$authid,$record,$delete)=@_;
528 my $oldrecord=&AUTHgetauthority($dbh,$authid);
529 if ($oldrecord eq $record) {
532 # 1st delete the authority,
534 &AUTHdelauthority($dbh,$authid,1);
535 &AUTHaddauthority($dbh,$record,$authid,AUTHfind_authtypecode($dbh,$authid));
536 # save the file in localfile/modified_authorities
537 my $filename = C4::Context->config("intranetdir")."/localfile/modified_authorities/$authid.authid";
538 open AUTH, "> $filename";
543 sub AUTHdelauthority {
544 my ($dbh,$authid,$keep_biblio) = @_;
545 # if the keep_biblio is set to 1, then authority entries in biblio are preserved.
546 # This flag is set when the delauthority is called by modauthority
547 # due to a too complex structure of MARC (repeatable fields and subfields),
548 # the best solution for a modif is to delete / recreate the record.
550 my $record = AUTHgetauthority($dbh,$authid);
551 $dbh->do("delete from auth_header where authid=$authid") unless $keep_biblio;
552 $dbh->do("delete from auth_subfield_table where authid=$authid");
553 $dbh->do("delete from auth_word where authid=$authid");
554 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag)
557 sub AUTHmodsubfield {
558 # Subroutine changes a subfield value given a subfieldid.
559 my ($dbh, $subfieldid, $subfieldvalue )=@_;
560 $dbh->do("lock tables auth_subfield_table WRITE");
561 my $sth=$dbh->prepare("update auth_subfield_table set subfieldvalue=? where subfieldid=?");
562 $sth->execute($subfieldvalue, $subfieldid);
563 $dbh->do("unlock tables");
565 $sth=$dbh->prepare("select authid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from auth_subfield_table where subfieldid=?");
566 $sth->execute($subfieldid);
567 my ($authid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
569 &AUTHdelword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
570 &AUTHaddword($dbh,$authid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
571 return($subfieldid, $subfieldvalue);
574 sub AUTHfindsubfield {
575 my ($dbh,$authid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
579 my $query="select subfieldid from auth_subfield_table where authid=? and tag=? and subfieldcode=?";
580 my @bind_values = ($authid,$tag, $subfieldcode);
581 if ($subfieldvalue) {
582 $query .= " and subfieldvalue=?";
583 push(@bind_values,$subfieldvalue);
585 if ($subfieldorder<1) {
588 $query .= " and subfieldorder=?";
589 push(@bind_values,$subfieldorder);
591 my $sti=$dbh->prepare($query);
592 $sti->execute(@bind_values);
593 while (($subfieldid) = $sti->fetchrow) {
595 $lastsubfieldid=$subfieldid;
597 if ($resultcounter>1) {
598 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
599 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
602 return $lastsubfieldid;
606 sub AUTHfindsubfieldid {
607 my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
608 my $sth=$dbh->prepare("select subfieldid from auth_subfield_table
609 where authid=? and tag=? and tagorder=?
610 and subfieldcode=? and subfieldorder=?");
611 $sth->execute($authid,$tag,$tagorder,$subfield,$subfieldorder);
612 my ($res) = $sth->fetchrow;
614 $sth=$dbh->prepare("select subfieldid from auth_subfield_table
615 where authid=? and tag=? and tagorder=?
616 and subfieldcode=?");
617 $sth->execute($authid,$tag,$tagorder,$subfield);
618 ($res) = $sth->fetchrow;
623 sub AUTHfind_authtypecode {
624 my ($dbh,$authid) = @_;
625 my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
626 $sth->execute($authid);
627 my ($authtypecode) = $sth->fetchrow;
628 return $authtypecode;
631 sub AUTHdelsubfield {
632 # delete a subfield for $authid / tag / tagorder / subfield / subfieldorder
633 my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
634 $dbh->do("delete from auth_subfield_table where authid='$authid' and
635 tag='$tag' and tagorder='$tagorder'
636 and subfieldcode='$subfield' and subfieldorder='$subfieldorder'
641 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
643 my $record = MARC::Record->new();
644 # my %subfieldlist=();
645 my $prevvalue; # if tag <10
646 my $field; # if tag >=10
647 for (my $i=0; $i< @$rtags; $i++) {
648 # rebuild MARC::Record
649 if (@$rtags[$i] ne $prevtag) {
652 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
656 $record->add_fields($field);
659 $indicators{@$rtags[$i]}.=' ';
660 if (@$rtags[$i] <10) {
661 $prevvalue= @$rvalues[$i];
663 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
665 $prevtag = @$rtags[$i];
667 if (@$rtags[$i] <10) {
668 $prevvalue=@$rvalues[$i];
671 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
674 $prevtag= @$rtags[$i];
677 # the last has not been included inside the loop... do it now !
678 $record->add_fields($field);
679 # warn $record->as_formatted;
684 # split a subfield string and adds it into the word table.
686 my ($dbh,$authid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
687 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
688 my @words = split / /,$sentence;
689 my $stopwords= C4::Context->stopwords;
690 my $sth=$dbh->prepare("insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word)
691 values (?,concat(?,?),?,?,?,soundex(?))");
692 foreach my $word (@words) {
693 # we record only words longer than 2 car and not in stopwords hash
694 if (length($word)>2 and !($stopwords->{uc($word)})) {
695 $sth->execute($authid,$tag,$subfieldid,$tagorder,$subfieldorder,$word,$word);
697 warn "ERROR ==> insert into auth_word (authid, tagsubfield, tagorder, subfieldorder, word, sndx_word) values ($authid,concat($tag,$subfieldid),$tagorder,$subfieldorder,$word,soundex($word))\n";
704 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
705 my ($dbh,$authid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
706 my $sth=$dbh->prepare("delete from auth_word where authid=? and tagsubfield=concat(?,?) and tagorder=? and subfieldorder=?");
707 $sth->execute($authid,$tag,$subfield,$tagorder,$subfieldorder);
711 # converts ISO 5426 coded string to ISO 8859-1
712 # sloppy code : should be improved in next issue
713 my ($string,$encoding) = @_ ;
715 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
716 if ($encoding eq "UNIMARC") {
778 # this handles non-sorting blocks (if implementation requires this)
779 $string = nsb_clean($_) ;
780 } elsif ($encoding eq "USMARC" || $encoding eq "MARC21") {
833 # this handles non-sorting blocks (if implementation requires this)
834 $string = nsb_clean($_) ;
841 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
842 my $NSE = '\x89' ; # NSE : Non Sorting Block end
843 # handles non sorting blocks
847 s/[ ]{0,1}$NSE/) /gm ;
853 my ($record,$authtypecode)=@_;
854 my $dbh = C4::Context->dbh;
856 # warn "".$record->as_formatted;
857 # search duplicate on ISBN, easy and fast...
858 my $sth = $dbh->prepare("select auth_tag_to_report,summary from auth_types where authtypecode=?");
859 $sth->execute($authtypecode);
860 my ($auth_tag_to_report,$taglist) = $sth->fetchrow;
862 # a more complex search : build a request for authoritysearch
863 my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
864 # search on biblio.title
865 # warn " tag a reporter : $auth_tag_to_report";
866 warn "taglist ".$taglist;
867 my @subfield = split /\[/, $taglist;
869 for (my $i=1; $i<$max;$i++){
870 warn " ".$subfield[$i];
871 $subfield[$i]=substr($subfield[$i],3,1);
872 warn " ".$subfield[$i];
875 if ($record->fields($auth_tag_to_report)) {
876 # foreach my $subfieldcount (1..$#subfield){
877 # if ($record->field($auth_tag_to_report)->subfields($subfield[$subfieldcount])) {
878 # # warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
879 # push @tags, $auth_tag_to_report.$subfield[$subfieldcount];
880 # # warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
881 # push @and_or, "and";
882 # push @excluding, "";
883 # push @operator, "contains";
884 # push @value, $record->field($auth_tag_to_report)->subfield($subfield[$subfieldcount]);
888 my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where tagfield=? and authtypecode=? ");
889 $sth->execute($auth_tag_to_report,$authtypecode);
890 # warn " field $auth_tag_to_report exists";
891 while (my ($tag,$subfield) = $sth->fetchrow){
892 if ($record->field($tag)->subfield($subfield)) {
893 # warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
894 push @tags, $tag.$subfield;
895 # warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
898 push @operator, "contains";
899 push @value, $record->field($tag)->subfield($subfield);
904 my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
905 # there is at least 1 result => return the 1st one
907 warn "$nbresult => ".@$finalresult[0]->{authid},$record->field($auth_tag_to_report)->subfield('a');
908 return @$finalresult[0]->{authid},@$finalresult[0]->{authid},$record->field($auth_tag_to_report)->subfield('a');
910 # no result, returns nothing
914 END { } # module clean-up code here (global destructor)
920 Koha Developement team <info@koha.org>
922 Paul POULAIN paul.poulain@free.fr
928 # Revision 1.15 2005/05/04 14:18:39 hdl
929 # adding Marc Record to authoritysearch report
931 # Revision 1.14 2005/04/05 17:07:46 hdl
932 # Scanning every the Subfields of auth_tag_to_report for FindDuplicate
934 # Revision 1.13 2005/04/05 15:23:41 hdl
935 # Searching for double entries when adding a new authority.
937 # Revision 1.12 2005/04/05 09:58:48 hdl
938 # Adding double authority search before creating a new authority
940 # Revision 1.11 2005/03/07 08:55:29 tipaul
943 # Revision 1.9.2.2 2005/02/28 14:03:13 tipaul
944 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
945 # * adding a select box to requet "contain" or "begin with" search.
946 # * fixing some bug in authority search (related to "main entry" search)
948 # Revision 1.9.2.1 2005/02/24 13:12:13 tipaul
949 # 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.
950 # So, it will be done through a cron job.
951 # (/me agree we need some doc for command line scripts)
953 # Revision 1.9 2004/12/23 09:48:11 tipaul
954 # Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
956 # Revision 1.8 2004/11/05 10:11:39 tipaul
957 # export auth_count_usage (bugfix)
959 # Revision 1.7 2004/09/23 16:13:00 tipaul
960 # Bugfix in modification
962 # Revision 1.6 2004/08/18 16:00:24 tipaul
963 # fixes for authorities management
965 # Revision 1.5 2004/07/05 13:37:22 doxulting
966 # First step for working authorities
968 # Revision 1.4 2004/06/22 11:35:37 tipaul
969 # removing % at the beginning of a string to avoid loooonnnngggg searchs
971 # Revision 1.3 2004/06/17 08:02:13 tipaul
972 # merging tag & subfield in auth_word for better perfs
974 # Revision 1.2 2004/06/10 08:29:01 tipaul
975 # MARC authority management (continued)
977 # Revision 1.1 2004/06/07 07:35:01 tipaul
978 # MARC authority management package