Bug 14117: Silence warnings t/SearchPazPar2.t
[koha.git] / C4 / Context.pm
index 5a76137..a51f5c7 100644 (file)
@@ -3,18 +3,18 @@ package C4::Context;
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
@@ -97,11 +97,13 @@ BEGIN {
     $VERSION = '3.07.00.049';
 }
 
-use DBI;
+use DBIx::Connector;
+use Encode;
 use ZOOM;
 use XML::Simple;
 use C4::Boolean;
 use C4::Debug;
+use Koha;
 use POSIX ();
 use DateTime::TimeZone;
 use Module::Load::Conditional qw(can_load);
@@ -203,36 +205,6 @@ $context = undef;        # Initially, no context is set
 @context_stack = ();        # Initially, no saved contexts
 
 
-=head2 KOHAVERSION
-
-returns the kohaversion stored in kohaversion.pl file
-
-=cut
-
-sub KOHAVERSION {
-    my $cgidir = C4::Context->intranetdir;
-
-    # Apparently the GIT code does not run out of a CGI-BIN subdirectory
-    # but distribution code does?  (Stan, 1jan08)
-    if(-d $cgidir . "/cgi-bin"){
-        my $cgidir .= "/cgi-bin";
-    }
-    
-    do $cgidir."/kohaversion.pl" || die "NO $cgidir/kohaversion.pl";
-    return kohaversion();
-}
-
-=head2 final_linear_version
-
-Returns the version number of the final update to run in updatedatabase.pl.
-This number is equal to the version in kohaversion.pl
-
-=cut
-
-sub final_linear_version {
-    return KOHAVERSION;
-}
-
 =head2 read_config_file
 
 Reads the specified Koha config file. 
@@ -672,9 +644,7 @@ sub AUTOLOAD
 
   $Zconn = C4::Context->Zconn
 
-Returns a connection to the Zebra database for the current
-context. If no connection has yet been made, this method 
-creates one and connects.
+Returns a connection to the Zebra database
 
 C<$self> 
 
@@ -682,33 +652,18 @@ C<$server> one of the servers defined in the koha-conf.xml file
 
 C<$async> whether this is a asynchronous connection
 
-C<$auth> whether this connection has rw access (1) or just r access (0 or NULL)
-
-
 =cut
 
 sub Zconn {
-    my $self=shift;
-    my $server=shift;
-    my $async=shift;
-    my $auth=shift;
-    my $piggyback=shift;
-    my $syntax=shift;
-    if ( defined($context->{"Zconn"}->{$server}) && (0 == $context->{"Zconn"}->{$server}->errcode()) ) {
-        return $context->{"Zconn"}->{$server};
-    # No connection object or it died. Create one.
-    }else {
-        # release resources if we're closing a connection and making a new one
-        # FIXME: this needs to be smarter -- an error due to a malformed query or
-        # a missing index does not necessarily require us to close the connection
-        # and make a new one, particularly for a batch job.  However, at
-        # first glance it does not look like there's a way to easily check
-        # the basic health of a ZOOM::Connection
-        $context->{"Zconn"}->{$server}->destroy() if defined($context->{"Zconn"}->{$server});
-
-        $context->{"Zconn"}->{$server} = &_new_Zconn($server,$async,$auth,$piggyback,$syntax);
-        return $context->{"Zconn"}->{$server};
+    my ($self, $server, $async ) = @_;
+    my $cache_key = join ('::', (map { $_ // '' } ($server, $async )));
+    if ( (!defined($ENV{GATEWAY_INTERFACE})) && defined($context->{"Zconn"}->{$cache_key}) && (0 == $context->{"Zconn"}->{$cache_key}->errcode()) ) {
+        # if we are running the script from the commandline, lets try to use the caching
+        return $context->{"Zconn"}->{$cache_key};
     }
+    $context->{"Zconn"}->{$cache_key}->destroy() if defined($context->{"Zconn"}->{$cache_key}); #destroy old connection before making a new one
+    $context->{"Zconn"}->{$cache_key} = &_new_Zconn( $server, $async );
+    return $context->{"Zconn"}->{$cache_key};
 }
 
 =head2 _new_Zconn
@@ -726,53 +681,47 @@ C<$auth> whether this connection has rw access (1) or just r access (0 or NULL)
 =cut
 
 sub _new_Zconn {
-    my ($server,$async,$auth,$piggyback,$syntax) = @_;
+    my ( $server, $async ) = @_;
 
     my $tried=0; # first attempt
     my $Zconn; # connection object
     my $elementSetName;
     my $index_mode;
+    my $syntax;
 
     $server //= "biblioserver";
-    $syntax //= "XML";
 
     if ( $server eq 'biblioserver' ) {
-        $index_mode = $context->{'config'}->{'zebra_bib_index_mode'} // 'grs1';
+        $index_mode = $context->{'config'}->{'zebra_bib_index_mode'} // 'dom';
     } elsif ( $server eq 'authorityserver' ) {
         $index_mode = $context->{'config'}->{'zebra_auth_index_mode'} // 'dom';
     }
 
     if ( $index_mode eq 'grs1' ) {
-
         $elementSetName = 'F';
         $syntax = ( $context->preference("marcflavour") eq 'UNIMARC' )
                 ? 'unimarc'
                 : 'usmarc';
 
-    } else {
-
+    } else { # $index_mode eq 'dom'
+        $syntax = 'xml';
         $elementSetName = 'marcxml';
-        $syntax = 'XML';
     }
 
     my $host = $context->{'listen'}->{$server}->{'content'};
-    my $servername = $context->{"config"}->{$server};
     my $user = $context->{"serverinfo"}->{$server}->{"user"};
     my $password = $context->{"serverinfo"}->{$server}->{"password"};
-    $auth = 1 if($user && $password);
-    retry:
     eval {
         # set options
         my $o = new ZOOM::Options();
-        $o->option(user=>$user) if $auth;
-        $o->option(password=>$password) if $auth;
+        $o->option(user => $user) if $user && $password;
+        $o->option(password => $password) if $user && $password;
         $o->option(async => 1) if $async;
-        $o->option(count => $piggyback) if $piggyback;
         $o->option(cqlfile=> $context->{"server"}->{$server}->{"cql2rpn"});
         $o->option(cclfile=> $context->{"serverinfo"}->{$server}->{"ccl2rpn"});
         $o->option(preferredRecordSyntax => $syntax);
-        $o->option(elementSetName => $elementSetName);
-        $o->option(databaseName => ($servername?$servername:"biblios"));
+        $o->option(elementSetName => $elementSetName) if $elementSetName;
+        $o->option(databaseName => $context->{"config"}->{$server}||"biblios");
 
         # create a new connection object
         $Zconn= create ZOOM::Connection($o);
@@ -784,9 +733,7 @@ sub _new_Zconn {
         if ($Zconn->errcode() !=0) {
             warn "something wrong with the connection: ". $Zconn->errmsg();
         }
-
     };
-
     return $Zconn;
 }
 
@@ -807,8 +754,13 @@ sub _new_dbh
     my $db_user   = $context->config("user");
     my $db_passwd = $context->config("pass");
     # MJR added or die here, as we can't work without dbh
-    my $dbh = DBI->connect("DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port",
-    $db_user, $db_passwd, {'RaiseError' => $ENV{DEBUG}?1:0 }) or die $DBI::errstr;
+    my $dbh = DBIx::Connector->connect(
+        "dbi:$db_driver:dbname=$db_name;host=$db_host;port=$db_port",
+        $db_user, $db_passwd,
+        {
+            'RaiseError' => $ENV{DEBUG} ? 1 : 0
+        }
+    );
 
     # Check for the existence of a systempreference table; if we don't have this, we don't
     # have a valid database and should not set RaiseError in order to allow the installer
@@ -996,6 +948,8 @@ sub _new_queryparser {
         my $config_file = $context->config('queryparser_config');
         $config_file ||= '/etc/koha/searchengine/queryparser.yaml';
         if ( $QParser->load_config($config_file) ) {
+            # Set 'keyword' as the default search class
+            $QParser->default_search_class('keyword');
             # TODO: allow indexes to be configured in the database
             return $QParser;
         }
@@ -1108,9 +1062,10 @@ sub userenv {
 
 =head2 set_userenv
 
-  C4::Context->set_userenv($usernum, $userid, $usercnum, $userfirstname, 
-                  $usersurname, $userbranch, $userflags, $emailaddress, $branchprinter,
-                  $persona);
+  C4::Context->set_userenv($usernum, $userid, $usercnum,
+                           $userfirstname, $usersurname,
+                           $userbranch, $branchname, $userflags,
+                           $emailaddress, $branchprinter, $persona);
 
 Establish a hash of user environment variables.
 
@@ -1120,7 +1075,10 @@ set_userenv is called in Auth.pm
 
 #'
 sub set_userenv {
-    my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter, $persona)= @_;
+    shift @_;
+    my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter, $persona, $shibboleth)=
+    map { Encode::is_utf8( $_ ) ? $_ : Encode::decode('UTF-8', $_) } # CGI::Session doesn't handle utf-8, so we decode it here
+    @_;
     my $var=$context->{"activeuser"} || '';
     my $cell = {
         "number"     => $usernum,
@@ -1135,6 +1093,7 @@ sub set_userenv {
         "emailaddress"     => $emailaddress,
         "branchprinter"    => $branchprinter,
         "persona"    => $persona,
+        "shibboleth" => $shibboleth,
     };
     $context->{userenv}->{$var} = $cell;
     return $cell;
@@ -1211,7 +1170,7 @@ Gets various version info, for core Koha packages, Currently called from carp ha
 # A little example sub to show more debugging info for CGI::Carp
 sub get_versions {
     my %versions;
-    $versions{kohaVersion}  = KOHAVERSION();
+    $versions{kohaVersion}  = Koha::version();
     $versions{kohaDbVersion} = C4::Context->preference('version');
     $versions{osVersion} = join(" ", POSIX::uname());
     $versions{perlVersion} = $];
@@ -1264,6 +1223,31 @@ sub IsSuperLibrarian {
     return ($userenv->{flags}//0) % 2;
 }
 
+=head2 interface
+
+Sets the current interface for later retrieval in any Perl module
+
+    C4::Context->interface('opac');
+    C4::Context->interface('intranet');
+    my $interface = C4::Context->interface;
+
+=cut
+
+sub interface {
+    my ($class, $interface) = @_;
+
+    if (defined $interface) {
+        $interface = lc $interface;
+        if ($interface eq 'opac' || $interface eq 'intranet') {
+            $context->{interface} = $interface;
+        } else {
+            warn "invalid interface : '$interface'";
+        }
+    }
+
+    return $context->{interface} // 'opac';
+}
+
 1;
 __END__
 
@@ -1283,4 +1267,3 @@ Andrew Arensburger <arensb at ooblick dot com>
 
 Joshua Ferraro <jmf at liblime dot com>
 
-=cut