Bug 15006: Introduce client_timeout in SIPconfig.xml
[srvgit] / C4 / AuthoritiesMarc.pm
index 4860eba..e14c85f 100644 (file)
@@ -26,20 +26,21 @@ use C4::AuthoritiesMarc::MARC21;
 use C4::AuthoritiesMarc::UNIMARC;
 use C4::Charset;
 use C4::Log;
+use Koha::MetadataRecord::Authority;
+use Koha::Authorities;
+use Koha::Authority::Types;
 use Koha::Authority;
+use Koha::SearchEngine;
+use Koha::SearchEngine::Search;
 
-use vars qw($VERSION @ISA @EXPORT);
+use vars qw(@ISA @EXPORT);
 
 BEGIN {
-       # set the version for version checking
-    $VERSION = 3.07.00.049;
 
        require Exporter;
        @ISA = qw(Exporter);
        @EXPORT = qw(
            &GetTagsLabels
-           &GetAuthType
-           &GetAuthTypeCode
        &GetAuthMARCFromKohaField 
 
        &AddAuthority
@@ -87,7 +88,6 @@ sub GetAuthMARCFromKohaField {
   my $dbh=C4::Context->dbh;
   return 0, 0 unless $kohafield;
   $authtypecode="" unless $authtypecode;
-  my $marcfromkohafield;
   my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where kohafield= ? and authtypecode=? ");
   $sth->execute($kohafield,$authtypecode);
   my ($tagfield,$tagsubfield) = $sth->fetchrow;
@@ -109,6 +109,7 @@ sub SearchAuthorities {
     my ($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby,$skipmetadata) = @_;
     # warn Dumper($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby);
     my $dbh=C4::Context->dbh;
+    $sortby="" unless $sortby;
     my $query;
     my $qpquery = '';
     my $QParser;
@@ -290,11 +291,9 @@ sub SearchAuthorities {
         my %newline;
         $newline{authid} = $authid;
         if ( !$skipmetadata ) {
-            my $query_auth_tag =
-"SELECT auth_tag_to_report FROM auth_types WHERE authtypecode=?";
-            my $sth = $dbh->prepare($query_auth_tag);
-            $sth->execute($authtypecode);
-            my $auth_tag_to_report = $sth->fetchrow;
+            my $auth_tag_to_report;
+            $auth_tag_to_report = Koha::Authority::Types->find($authtypecode)->auth_tag_to_report
+                if $authtypecode;
             my $reported_tag;
             my $mainentry = $authrecord->field($auth_tag_to_report);
             if ($mainentry) {
@@ -302,16 +301,20 @@ sub SearchAuthorities {
                     $reported_tag .= '$' . $_->[0] . $_->[1];
                 }
             }
-            my $thisauthtypecode = GetAuthTypeCode($authid);
-            my $thisauthtype = GetAuthType($thisauthtypecode);
+
+            my ( $thisauthtype, $thisauthtypecode );
+            if ( my $authority = Koha::Authorities->find($authid) ) {
+                $thisauthtypecode = $authority->authtypecode;
+                $thisauthtype = Koha::Authority::Types->find($thisauthtypecode);
+            }
             unless (defined $thisauthtype) {
                 $thisauthtypecode = $authtypecode;
-                $thisauthtype = GetAuthType($authtypecode);
+                $thisauthtype = Koha::Authority::Types->find($thisauthtypecode);
             }
             my $summary = BuildSummary( $authrecord, $authid, $thisauthtypecode );
 
             $newline{authtype}     = defined($thisauthtype) ?
-                                        $thisauthtype->{'authtypetext'} : '';
+                                        $thisauthtype->authtypetext : '';
             $newline{summary}      = $summary;
             $newline{even}         = $counter % 2;
             $newline{reported_tag} = $reported_tag;
@@ -348,7 +351,10 @@ sub CountUsage {
         ### ZOOM search here
         my $query;
         $query= "an:".$authid;
-               my ($err,$res,$result) = C4::Search::SimpleSearch($query,0,10);
+        # Should really be replaced with a real count call, this is a
+        # bad way.
+        my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
+        my ($err,$res,$result) = $searcher->simple_search_compat($query,0,1);
         if ($err) {
             warn "Error: $err from search $query";
             $result = 0;
@@ -369,24 +375,6 @@ sub CountUsageChildren {
   my ($authid) = @_;
 }
 
-=head2 GetAuthTypeCode
-
-  $authtypecode= &GetAuthTypeCode($authid)
-
-returns authtypecode of an authid
-
-=cut
-
-sub GetAuthTypeCode {
-#AUTHfind_authtypecode
-  my ($authid) = @_;
-  my $dbh=C4::Context->dbh;
-  my $sth = $dbh->prepare("select authtypecode from auth_header where authid=?");
-  $sth->execute($authid);
-  my $authtypecode = $sth->fetchrow;
-  return $authtypecode;
-}
 =head2 GuessAuthTypeCode
 
   my $authtypecode = GuessAuthTypeCode($record);
@@ -800,37 +788,11 @@ Returns MARC::Record of the authority passed in parameter.
 
 sub GetAuthority {
     my ($authid)=@_;
-    my $authority = Koha::Authority->get_from_authid($authid);
+    my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
     return unless $authority;
     return ($authority->record);
 }
 
-=head2 GetAuthType 
-
-  $result = &GetAuthType($authtypecode)
-
-If the authority type specified by C<$authtypecode> exists,
-returns a hashref of the type's fields.  If the type
-does not exist, returns undef.
-
-=cut
-
-sub GetAuthType {
-    my ($authtypecode) = @_;
-    my $dbh=C4::Context->dbh;
-    my $sth;
-    if (defined $authtypecode){ # NOTE - in MARC21 framework, '' is a valid authority 
-                                # type (FIXME but why?)
-        $sth=$dbh->prepare("select * from auth_types where authtypecode=?");
-        $sth->execute($authtypecode);
-        if (my $res = $sth->fetchrow_hashref) {
-            return $res; 
-        }
-    }
-    return;
-}
-
-
 =head2 FindDuplicateAuthority
 
   $record= &FindDuplicateAuthority( $record, $authtypecode)
@@ -847,10 +809,7 @@ sub FindDuplicateAuthority {
 #    warn "IN for ".$record->as_formatted;
     my $dbh = C4::Context->dbh;
 #    warn "".$record->as_formatted;
-    my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
-    $sth->execute($authtypecode);
-    my ($auth_tag_to_report) = $sth->fetchrow;
-    $sth->finish;
+    my $auth_tag_to_report = Koha::Authority::Types->find($authtypecode)->auth_tag_to_report;
 #     warn "record :".$record->as_formatted."  auth_tag_to_report :$auth_tag_to_report";
     # build a request for SearchAuthorities
     my $QParser;
@@ -868,7 +827,8 @@ sub FindDuplicateAuthority {
             $_->[1]=~s/$filtervalues/ /g; $query.= " $op he:\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/);
         }
     }
-    my ($error, $results, $total_hits) = C4::Search::SimpleSearch( $query, 0, 1, [ "authorityserver" ] );
+    my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::AUTHORITIES_INDEX});
+    my ($error, $results, $total_hits) = $searcher->simple_search_compat( $query, 0, 1 );
     # there is at least 1 result => return the 1st one
     if (!defined $error && @{$results} ) {
         my $marcrecord = C4::Search::new_record_from_zebra(
@@ -887,7 +847,7 @@ sub FindDuplicateAuthority {
 
 Returns a hashref with a summary of the specified record.
 
-Comment : authtypecode can be infered from both record and authid.
+Comment : authtypecode can be inferred from both record and authid.
 Moreover, authid can also be inferred from $record.
 Would it be interesting to delete those things.
 
@@ -901,14 +861,18 @@ sub BuildSummary {
     my $summary_template;
     # handle $authtypecode is NULL or eq ""
     if ($authtypecode) {
-        my $authref = GetAuthType($authtypecode);
-        $summary{authtypecode} = $authref->{authtypecode};
-        $summary{type} = $authref->{authtypetext};
-        $summary_template = $authref->{summary};
-        # for MARC21, the authority type summary displays a label meant for
-        # display
-        if (C4::Context->preference('marcflavour') ne 'UNIMARC') {
-            $summary{summary} = $authref->{summary};
+        my $authref = Koha::Authority::Types->find($authtypecode);
+        if ( $authref ) {
+            $summary{authtypecode} = $authref->authtypecode;
+            $summary{type} = $authref->authtypetext;
+            $summary_template = $authref->summary;
+            # for MARC21, the authority type summary displays a label meant for
+            # display
+            if (C4::Context->preference('marcflavour') ne 'UNIMARC') {
+                $summary{label} = $authref->summary;
+            } else {
+                $summary{summary} = $authref->summary;
+            }
         }
     }
     my $marc21subfields = 'abcdfghjklmnopqrstuvxyz68';
@@ -942,35 +906,37 @@ sub BuildSummary {
 #         suit the MARC21 version, so for now the "templating"
 #         feature will be enabled only for UNIMARC for backwards
 #         compatibility.
-    if ($summary_template and C4::Context->preference('marcflavour') eq 'UNIMARC') {
-        my @fields = $record->fields();
-#             $reported_tag = '$9'.$result[$counter];
-        my @repets;
-        foreach my $field (@fields) {
-            my $tag = $field->tag();
-            my $tagvalue = $field->as_string();
-            my $localsummary= $summary_template;
-            $localsummary =~ s/\[(.?.?.?.?)$tag\*(.*?)\]/$1$tagvalue$2\[$1$tag$2\]/g;
-            if ($tag<10) {
-                if ($tag eq '001') {
-                    $reported_tag.='$3'.$field->data();
-                }
-            } else {
-                my @subf = $field->subfields;
-                for my $i (0..$#subf) {
-                    my $subfieldcode = $subf[$i][0];
-                    my $subfieldvalue = $subf[$i][1];
-                    my $tagsubf = $tag.$subfieldcode;
-                    $localsummary =~ s/\[(.?.?.?.?)$tagsubf(.*?)\]/$1$subfieldvalue$2\[$1$tagsubf$2\]/g;
+    if ($summary{summary} and C4::Context->preference('marcflavour') eq 'UNIMARC') {
+        my @matches = ($summary{summary} =~ m/\[(.*?)(\d{3})([\*a-z0-9])(.*?)\]/g);
+        my (@textbefore, @tag, @subtag, @textafter);
+        for(my $i = 0; $i < scalar @matches; $i++){
+            push @textbefore, $matches[$i] if($i%4 == 0);
+            push @tag,        $matches[$i] if($i%4 == 1);
+            push @subtag,     $matches[$i] if($i%4 == 2);
+            push @textafter,  $matches[$i] if($i%4 == 3);
+        }
+        for(my $i = scalar @tag; $i >= 0; $i--){
+            my $textbefore = $textbefore[$i] || '';
+            my $tag = $tag[$i] || '';
+            my $subtag = $subtag[$i] || '';
+            my $textafter = $textafter[$i] || '';
+            my $value = '';
+            my $field = $record->field($tag);
+            if ( $field ) {
+                if($subtag eq '*') {
+                    if($tag < 10) {
+                        $value = $textbefore . $field->data() . $textafter;
+                    }
+                } else {
+                    my @subfields = $field->subfield($subtag);
+                    if(@subfields > 0) {
+                        $value = $textbefore . join (" - ", @subfields) . $textafter;
+                    }
                 }
             }
-            if ($localsummary ne $summary_template) {
-                $localsummary =~ s/\[(.*?)\]//g;
-                $localsummary =~ s/\n/<br>/g;
-                push @repets, $localsummary;
-            }
+            $summary{summary} =~ s/\[\Q$textbefore$tag$subtag$textafter\E\]/$value/;
         }
-        $summary{repets} = \@repets;
+        $summary{summary} =~ s/\\n/<br \/>/g;
     }
     my @authorized;
     my @notes;
@@ -1444,19 +1410,17 @@ sub merge {
     my ($mergefrom,$MARCfrom,$mergeto,$MARCto) = @_;
     my ($counteditedbiblio,$countunmodifiedbiblio,$counterrors)=(0,0,0);        
     my $dbh=C4::Context->dbh;
-    my $authtypecodefrom = GetAuthTypeCode($mergefrom);
-    my $authtypecodeto = GetAuthTypeCode($mergeto);
-#     warn "mergefrom : $authtypecodefrom $mergefrom mergeto : $authtypecodeto $mergeto ";
-    # return if authority does not exist
+    my $authfrom = Koha::Authorities->find($mergefrom);
+    my $authto = Koha::Authorities->find($mergeto);
+    my $authtypefrom = Koha::Authority::Types->find($authfrom->authtypecode);
+    my $authtypeto   = Koha::Authority::Types->find($authto->authtypecode);
+
     return "error MARCFROM not a marcrecord ".Data::Dumper::Dumper($MARCfrom) if scalar($MARCfrom->fields()) == 0;
     return "error MARCTO not a marcrecord".Data::Dumper::Dumper($MARCto) if scalar($MARCto->fields()) == 0;
     # search the tag to report
-    my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
-    $sth->execute($authtypecodefrom);
-    my ($auth_tag_to_report_from) = $sth->fetchrow;
-    $sth->execute($authtypecodeto);
-    my ($auth_tag_to_report_to) = $sth->fetchrow;
-    
+    my $auth_tag_to_report_from = $authtypefrom->auth_tag_to_report;
+    my $auth_tag_to_report_to   = $authtypeto->auth_tag_to_report;
+
     my @record_to;
     @record_to = $MARCto->field($auth_tag_to_report_to)->subfields() if $MARCto->field($auth_tag_to_report_to);
     my @record_from;
@@ -1494,23 +1458,23 @@ sub merge {
     #warn scalar(@reccache)." biblios to update";
     # Get All candidate Tags for the change 
     # (This will reduce the search scope in marc records).
-    $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
-    $sth->execute($authtypecodefrom);
+    my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
+    $sth->execute($authtypefrom->authtypecode);
     my @tags_using_authtype;
     while (my ($tagfield) = $sth->fetchrow) {
         push @tags_using_authtype,$tagfield ;
     }
     my $tag_to=0;  
-    if ($authtypecodeto ne $authtypecodefrom){  
+    if ($authtypeto->authtypecode ne $authtypefrom->authtypecode){
         # If many tags, take the first
-        $sth->execute($authtypecodeto);    
+        $sth->execute($authtypeto->authtypecode);
         $tag_to=$sth->fetchrow;
         #warn $tag_to;    
     }  
     # BulkEdit marc records
     # May be used as a template for a bulkedit field  
     foreach my $marcrecord(@reccache){
-        my $update;           
+        my $update = 0;
         foreach my $tagfield (@tags_using_authtype){
 #             warn "tagfield : $tagfield ";
             foreach my $field ($marcrecord->field($tagfield)){
@@ -1597,7 +1561,7 @@ sub merge {
 #         $update=1;
 #       }#for each tag
 #     }#foreach tagfield
-#     my $authoritynumber = TransformMarcToKoha($dbh,$marcrecord,"") ;
+#     my $authoritynumber = TransformMarcToKoha($marcrecord,"") ;
 #     if ($update==1){
 #       &ModAuthority($marcrecord,$authoritynumber,GetAuthTypeCode($authoritynumber)) ;
 #     }