Hmm there seem to be quite a few subroutines twice in this module....
[koha.git] / C4 / AuthoritiesMarc.pm
index ca8b601..cf8fb0a 100644 (file)
@@ -41,6 +41,9 @@ $VERSION = 0.01;
        &AUTHaddsubfield
        &AUTHgetauthority
        
+       &AUTHgetauth_type
+       &AUTHcount_usage
+       
        &authoritysearch
        
        &MARCmodsubfield
@@ -48,6 +51,7 @@ $VERSION = 0.01;
        &AUTHaddword
        &MARCaddword &MARCdelword
        &char_decode
+       &FindDuplicate
  );
 
 sub authoritysearch {
@@ -58,7 +62,27 @@ sub authoritysearch {
        #               where m1.authid=m2.authid and
        #               (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
 
+       # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
+       # the authtypecode. Then, search on $a of this tag_to_report
+       # also store main entry MARC tag, to extract it at end of search
+       my $mainentrytag;
+       my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
+       $sth->execute($authtypecode);
+       my ($tag_to_report) = $sth->fetchrow;
+       $mainentrytag = $tag_to_report;
+       for (my $i=0;$i<$#{$tags};$i++) {
+               if (@$tags[$i] eq "mainentry") {
+                       @$tags[$i] = $tag_to_report."a";
+               }
+       }
+
        # "Normal" statements
+       # quote marc fields/subfields
+       for (my $i=0;$i<=$#{$tags};$i++) {
+               if (@$tags[$i]) {
+                       @$tags[$i] = $dbh->quote(@$tags[$i]);
+               }
+       }
        my @normal_tags = ();
        my @normal_and_or = ();
        my @normal_operator = ();
@@ -66,6 +90,11 @@ sub authoritysearch {
        # Extracts the NOT statements from the list of statements
        for(my $i = 0 ; $i <= $#{$value} ; $i++)
        {
+               # replace * by %
+               @$value[$i] =~ s/\*/%/g;
+               # remove % at the beginning
+               @$value[$i] =~ s/^%//g;
+           @$value[$i] =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\}|\/)/ /g if @$operator[$i] eq "contains";
                if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
                {
                        foreach my $word (split(/ /, @$value[$i]))
@@ -92,7 +121,7 @@ sub authoritysearch {
        # Finds the basic results without the NOT requests
        my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
 
-       my $sth;
+
 
        if ($sql_where2) {
                $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)");
@@ -104,15 +133,15 @@ sub authoritysearch {
        $sth->execute($authtypecode);
        my @result = ();
        while (my ($authid) = $sth->fetchrow) {
-                       warn "AUTH: $authid";
                        push @result,$authid;
                }
-
        # we have authid list. Now, loads summary from [offset] to [offset]+[length]
-       my $counter = $offset;
+#      my $counter = $offset;
        my @finalresult = ();
        my $oldline;
-       while (($counter <= $#result) && ($counter <= ($offset + $length))) {
+#      while (($counter <= $#result) && ($counter <= ($offset + $length))) {
+       # retrieve everything
+       for (my $counter=0;$counter <=$#result;$counter++) {
 #              warn " HERE : $counter, $#result, $offset, $length";
                # get MARC::Record of the authority
                my $record = AUTHgetauthority($dbh,$result[$counter]);
@@ -130,7 +159,7 @@ sub authoritysearch {
                                        my $subfieldcode = $subf[$i][0];
                                        my $subfieldvalue = $subf[$i][1];
                                        my $tagsubf = $tag.$subfieldcode;
-                                       $summary =~ s/\[(.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue\[$1$tagsubf$2]$2$3/g;
+                                       $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
                                }
                        }
                }
@@ -138,7 +167,7 @@ sub authoritysearch {
                $summary =~ s/\n/<br>/g;
 
                # find biblio MARC field using this authtypecode (to jump to biblio)
-               my $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
+               $authtypecode = AUTHfind_authtypecode($dbh,$result[$counter]);
                my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
                $sth->execute($authtypecode);
                my $tags_using_authtype;
@@ -154,11 +183,20 @@ sub authoritysearch {
                $newline{authid} = $result[$counter];
                $newline{used} = &AUTHcount_usage($result[$counter]);
                $newline{biblio_fields} = $tags_using_authtype;
-               $counter++;
+               $newline{even} = $counter % 2;
+               $newline{mainentry} = $record->field($mainentrytag)->subfield('a')." ".$record->field($mainentrytag)->subfield('b') if $record->field($mainentrytag);
                push @finalresult, \%newline;
        }
+       # sort everything
+       my @finalresult3= sort {$a->{summary} cmp $b->{summary}} @finalresult;
+       # cut from $offset to $offset+$length;
+       my @finalresult2;
+       for (my $i=$offset;$i<=$offset+$length;$i++) {
+               push @finalresult2,$finalresult3[$i] if $finalresult3[$i];
+       }
        my $nbresults = $#result + 1;
-       return (\@finalresult, $nbresults);
+
+       return (\@finalresult2, $nbresults);
 }
 
 # Creates the SQL Request
@@ -176,6 +214,7 @@ sub create_request {
        for(my $i=0; $i<=@$value;$i++) {
                if (@$value[$i]) {
                        $nb_active++;
+#                      warn " @$tags[$i]";
                        if ($nb_active==1) {
                                if (@$operator[$i] eq "start") {
                                        $sql_tables .= "auth_subfield_table as m$nb_table,";
@@ -270,7 +309,11 @@ sub AUTHcount_usage {
                $tags_using_authtype.= "'".$tagfield."9',";
        }
        chop $tags_using_authtype;
-       $sth = $dbh->prepare("select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=?");
+       if ($tags_using_authtype) {
+               $sth = $dbh->prepare("select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=?");
+#      } else {
+#              $sth = $dbh->prepare("select count(*) from marc_subfield_table where subfieldvalue=?");
+       }
 #      warn "Q : select count(*) from marc_subfield_table where concat(tag,subfieldcode) in ($tags_using_authtype) and subfieldvalue=$authid";
        $sth->execute($authid);
        my ($result) = $sth->fetchrow;
@@ -317,9 +360,7 @@ sub AUTHfind_authtypecode {
 
 sub AUTHgettagslib {
        my ($dbh,$forlibrarian,$authtypecode)= @_;
-#      warn "AUTH : $authtypecode";
        $authtypecode="" unless $authtypecode;
-#      warn "AUTH : $authtypecode";
        my $sth;
        my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
        # check that framework exists
@@ -367,7 +408,6 @@ sub AUTHaddauthority {
 # pass the MARC::Record to this function, and it will create the records in the marc tables
        my ($dbh,$record,$authid,$authtypecode) = @_;
        my @fields=$record->fields();
-#      warn "IN AUTHaddauthority $authid => ".$record->as_formatted;
 # adding main table, and retrieving authid
 # if authid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
 # if authid empty => true add, find a new authid number
@@ -380,7 +420,6 @@ sub AUTHaddauthority {
                ($authid)=$sth->fetchrow;
                $sth->finish;
        }
-       warn "auth : $authid";
        my $fieldcount=0;
        # now, add subfields...
        foreach my $field (@fields) {
@@ -396,15 +435,19 @@ sub AUTHaddauthority {
                                                );
                } else {
                        my @subfields=$field->subfields();
-                       foreach my $subfieldcount (0..$#subfields) {
-                               &AUTHaddsubfield($dbh,$authid,
-                                               $field->tag(),
-                                               $field->indicator(1).$field->indicator(2),
-                                               $fieldcount,
-                                               $subfields[$subfieldcount][0],
-                                               $subfieldcount+1,
-                                               $subfields[$subfieldcount][1]
-                                               );
+                       my $subfieldorder;
+                       foreach my $subfield (@subfields) {
+                               foreach (split /\|/,@$subfield[1]) {
+                                       $subfieldorder++;
+                                       &AUTHaddsubfield($dbh,$authid,
+                                                       $field->tag(),
+                                                       $field->indicator(1).$field->indicator(2),
+                                                       $fieldcount,
+                                                       @$subfield[0],
+                                                       $subfieldorder,
+                                                       $_
+                                                       );
+                               }
                        }
                }
        }
@@ -426,6 +469,7 @@ sub AUTHaddsubfield {
        my @subfieldvalues = split /\|/,$subfieldvalues;
        foreach my $subfieldvalue (@subfieldvalues) {
                my $sth=$dbh->prepare("insert into auth_subfield_table (authid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
+#              warn "==> $authid,".(sprintf "%03s",$tagid).",TAG : $tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue";
                $sth->execute($authid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
                if ($sth->errstr) {
                        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";
@@ -442,7 +486,7 @@ sub AUTHgetauthority {
        $record->leader('                        ');
     my $sth=$dbh->prepare("select authid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue
                                 from auth_subfield_table
-                                where authid=? order by tag,tagorder,subfieldcode
+                                where authid=? order by tag,tagorder,subfieldorder
                         ");
        $sth->execute($authid);
        my $prevtagorder=1;
@@ -491,6 +535,13 @@ sub AUTHgetauthority {
        return $record;
 }
 
+sub AUTHgetauth_type {
+       my ($authtypecode) = @_;
+       my $dbh=C4::Context->dbh;
+       my $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
+       $sth->execute($authtypecode);
+       return $sth->fetchrow_hashref;
+}
 sub AUTHmodauthority {
        my ($dbh,$authid,$record,$delete)=@_;
        my $oldrecord=&AUTHgetauthority($dbh,$authid);
@@ -500,8 +551,17 @@ sub AUTHmodauthority {
 # 1st delete the authority,
 # 2nd recreate it
        &AUTHdelauthority($dbh,$authid,1);
-       &AUTHaddauthority($dbh,$record,$authid);
-       # FIXME : modify the authority in biblio too.
+       &AUTHaddauthority($dbh,$record,$authid,AUTHfind_authtypecode($dbh,$authid));
+       # save the file in localfile/modified_authorities
+       my $cgidir = C4::Context->intranetdir ."/cgi-bin";
+       unless (opendir(DIR, "$cgidir")) {
+                       $cgidir = C4::Context->intranetdir."/";
+       } 
+
+       my $filename = $cgidir."/localfile/modified_authorities/$authid.authid";
+       open AUTH, "> $filename";
+       print AUTH $authid;
+       close AUTH;
 }
 
 sub AUTHdelauthority {
@@ -512,7 +572,7 @@ sub AUTHdelauthority {
 # the best solution for a modif is to delete / recreate the record.
 
        my $record = AUTHgetauthority($dbh,$authid);
-       $dbh->do("delete from auth_header where authid=$authid");
+       $dbh->do("delete from auth_header where authid=$authid") unless $keep_biblio;
        $dbh->do("delete from auth_subfield_table where authid=$authid");
        $dbh->do("delete from auth_word where authid=$authid");
 # FIXME : delete or not in biblio tables (depending on $keep_biblio flag)
@@ -584,13 +644,13 @@ sub AUTHfindsubfieldid {
     return $res;
 }
 
-sub AUTHfind_authtypecode {
-       my ($dbh,$authid) = @_;
-       my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
-       $sth->execute($authid);
-       my ($authtypecode) = $sth->fetchrow;
-       return $authtypecode;
-}
+sub AUTHfind_authtypecode {
+#      my ($dbh,$authid) = @_;
+#      my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
+#      $sth->execute($authid);
+#      my ($authtypecode) = $sth->fetchrow;
+#      return $authtypecode;
+}
 
 sub AUTHdelsubfield {
 # delete a subfield for $authid / tag / tagorder / subfield / subfieldorder
@@ -623,7 +683,9 @@ sub AUTHhtml2marc {
                        $indicators{@$rtags[$i]}.='  ';
                        if (@$rtags[$i] <10) {
                                $prevvalue= @$rvalues[$i];
+                               undef $field;
                        } else {
+                               undef $prevvalue;
                                $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
                        }
                        $prevtag = @$rtags[$i];
@@ -631,7 +693,7 @@ sub AUTHhtml2marc {
                        if (@$rtags[$i] <10) {
                                $prevvalue=@$rvalues[$i];
                        } else {
-                               if (@$rvalues[$i]) {
+                               if (length(@$rvalues[$i])>0) {
                                        $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
                                }
                        }
@@ -639,8 +701,7 @@ sub AUTHhtml2marc {
                }
        }
        # the last has not been included inside the loop... do it now !
-       $record->add_fields($field);
-#      warn $record->as_formatted;
+       $record->add_fields($field) if $field;
        return $record;
 }
 
@@ -813,6 +874,57 @@ sub nsb_clean {
        return($string) ;
 }
 
+sub FindDuplicate {
+       my ($record,$authtypecode)=@_;
+       warn "IN for ".$record->as_formatted;
+       my $dbh = C4::Context->dbh;
+
+#      warn "".$record->as_formatted;
+       my $sth = $dbh->prepare("select auth_tag_to_report,summary from auth_types where authtypecode=?");
+       $sth->execute($authtypecode);
+       my ($auth_tag_to_report,$taglist) = $sth->fetchrow;
+       $sth->finish;
+       # build a request for authoritysearch
+       my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
+       # search on biblio.title
+#      warn " tag a reporter : $auth_tag_to_report";
+#      warn "taglist ".$taglist;
+       my @subfield = split /\[/,  $taglist;
+       my $max = @subfield;
+       for (my $i=1; $i<$max;$i++){
+               warn " ".$subfield[$i];
+               $subfield[$i]=substr($subfield[$i],3,1);
+#              warn " ".$subfield[$i];
+       }
+       
+       if ($record->fields($auth_tag_to_report)) {
+               my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where tagfield=? and authtypecode=? ");
+               $sth->execute($auth_tag_to_report,$authtypecode);
+#              warn " field $auth_tag_to_report exists";
+               while (my ($tag,$subfield) = $sth->fetchrow){
+                       if ($record->field($tag)->subfield($subfield)) {
+                               warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
+                               push @tags, $tag.$subfield;
+#                              warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
+                               push @and_or, "and";
+                               push @excluding, "";
+                               push @operator, "=";
+                               push @value, $record->field($tag)->subfield($subfield);
+                       }
+               }
+       }
+       my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
+       # there is at least 1 result => return the 1st one
+       if ($nbresult) {
+               warn "XXXXX $nbresult => ".@$finalresult[0]->{authid},@$finalresult[0]->{summary};
+               return @$finalresult[0]->{authid},@$finalresult[0]->{summary};
+       }
+       # no result, returns nothing
+       return;
+}
+
+
 END { }       # module clean-up code here (global destructor)
 
 =back
@@ -827,6 +939,68 @@ Paul POULAIN paul.poulain@free.fr
 
 # $Id$
 # $Log$
+# Revision 1.24  2006/02/09 01:56:20  rangi
+# Hmm there seem to be quite a few subroutines twice in this module....
+#
+# Paul could you take a look and remove the ones that shouldnt be there please
+#
+# Revision 1.23  2006/02/09 01:52:14  rangi
+# Cleaning up some unessecary my statements
+#
+# Revision 1.22  2006/01/06 16:39:37  tipaul
+# synch'ing head and rel_2_2 (from 2.2.5, including npl templates)
+# Seems not to break too many things, but i'm probably wrong here.
+# at least, new features/bugfixes from 2.2.5 are here (tested on some features on my head local copy)
+#
+# - removing useless directories (koha-html and koha-plucene)
+#
+# Revision 1.21  2005/10/26 09:12:33  tipaul
+# big commit, still breaking things...
+#
+# * synch with rel_2_2. Probably the last non manual synch, as rel_2_2 should not be modified deeply.
+# * code cleaning (cleaning warnings from perl -w) continued
+#
+# Revision 1.9.2.8  2005/10/25 12:38:59  tipaul
+# * fixing bug in summary (separator before subfield was in fact after)
+# * fixing bug in authority order : authorities are not ordered alphabetically instead of no order. Requires all the dataset to be retrieved, but the benefits is important !
+#
+# Revision 1.9.2.7  2005/08/01 15:14:50  tipaul
+# minor change in summary handling (accepting 4 digits before the field)
+#
+# Revision 1.9.2.6  2005/06/07 10:02:00  tipaul
+# porting dictionnary search from head to 2.2. there is now a ... facing titles, author & subject, to search in biblio & authorities existing values.
+#
+# Revision 1.9.2.5  2005/05/31 14:50:46  tipaul
+# fix for authority merging. There was a bug on official installs
+#
+# Revision 1.9.2.4  2005/05/30 11:24:15  tipaul
+# 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)
+#
+# Revision 1.9.2.3  2005/04/28 08:45:33  tipaul
+# porting FindDuplicate feature for authorities from HEAD to rel_2_2, works correctly now.
+#
+# Revision 1.9.2.2  2005/02/28 14:03:13  tipaul
+# * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).
+# * adding a select box to requet "contain" or "begin with" search.
+# * fixing some bug in authority search (related to "main entry" search)
+#
+# Revision 1.9.2.1  2005/02/24 13:12:13  tipaul
+# 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.
+# So, it will be done through a cron job.
+# (/me agree we need some doc for command line scripts)
+#
+# Revision 1.9  2004/12/23 09:48:11  tipaul
+# Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
+#
+# Revision 1.8  2004/11/05 10:11:39  tipaul
+# export auth_count_usage (bugfix)
+#
+# Revision 1.7  2004/09/23 16:13:00  tipaul
+# Bugfix in modification
+#
+# Revision 1.6  2004/08/18 16:00:24  tipaul
+# fixes for authorities management
+#
 # Revision 1.5  2004/07/05 13:37:22  doxulting
 # First step for working authorities
 #