Merge branch 'bug_8220' into 3.12-master
[koha_fer] / C4 / Context.pm
index bd1f235..c666cd8 100644 (file)
@@ -19,6 +19,8 @@ package C4::Context;
 use strict;
 use warnings;
 use vars qw($VERSION $AUTOLOAD $context @context_stack $servers $memcached $ismemcached);
+use Koha::Cache;
+use Carp;
 
 BEGIN {
        if ($ENV{'HTTP_USER_AGENT'})    {
@@ -105,6 +107,7 @@ use C4::Boolean;
 use C4::Debug;
 use POSIX ();
 use DateTime::TimeZone;
+use Module::Load::Conditional qw(can_load);
 
 =head1 NAME
 
@@ -220,6 +223,18 @@ sub KOHAVERSION {
     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. 
@@ -522,28 +537,55 @@ with this method.
 # FIXME: running this under mod_perl will require a means of
 # flushing the caching mechanism.
 
-my %sysprefs;
+my $sysprefs;
 my $use_syspref_cache = 1;
+my $cache;
 
 sub preference {
     my $self = shift;
     my $var  = lc(shift);                          # The system preference to return
 
-    if ($use_syspref_cache && exists $sysprefs{$var}) {
-        return $sysprefs{$var};
+    unless (defined $sysprefs) {
+        unless ($cache) {
+            $cache = Koha::Cache->new();
+        }
+        $sysprefs = $cache->create_hash(
+            {
+                'key'         => 'syspref',
+                'allowupdate' => 1,
+                'cache_type' => $use_syspref_cache ? '' : 'null',
+                'preload'     => sub {
+                    my $dbh      = C4::Context->dbh or return {};
+                    my $vars = $dbh->selectall_arrayref("SELECT variable, value FROM systempreferences");
+                    my %sysprefs = ();
+                    foreach my $row (@$vars) {
+                        $sysprefs{$row->[0]} = $row->[1];
+                    }
+                    return \%sysprefs;
+                },
+                'constructor' => sub {
+
+                    # Look up systempreferences.variable==$var
+                    my $var      = pop;
+                    my $sysprefs = pop || {};
+                    my $dbh      = C4::Context->dbh or return 0;
+                    my $sql =
+"SELECT value FROM systempreferences WHERE variable=? LIMIT 1";
+                    $ENV{DEBUG} && carp "Retrieving syspref $var from database";
+                    my $sth = $dbh->prepare_cached($sql);
+                    $sth->execute($var);
+                    my $res = $sth->fetchrow_hashref;
+                    if ($res && $res->{'value'}) {
+                        $sysprefs->{$var} = $res->{'value'};
+                    } else {
+                        $sysprefs->{$var} = '';
+                    }
+                    return $sysprefs;
+                },
+            }
+        );
     }
-
-    my $dbh  = C4::Context->dbh or return 0;
-
-    # Look up systempreferences.variable==$var
-    my $sql = <<'END_SQL';
-        SELECT    value
-        FROM    systempreferences
-        WHERE    variable=?
-        LIMIT    1
-END_SQL
-    $sysprefs{$var} = $dbh->selectrow_array( $sql, {}, $var );
-    return $sysprefs{$var};
+    return $sysprefs->{$var};
 }
 
 sub boolean_preference {
@@ -579,7 +621,7 @@ used with Plack and other persistent environments.
 sub disable_syspref_cache {
     my ($self) = @_;
     $use_syspref_cache = 0;
-    $self->clear_syspref_cache();
+    $self->clear_syspref_cache() if defined($sysprefs);
 }
 
 =head2 clear_syspref_cache
@@ -593,7 +635,8 @@ will not be seen by this process.
 =cut
 
 sub clear_syspref_cache {
-    %sysprefs = ();
+    %{$sysprefs} = ();
+    return;
 }
 
 =head2 set_preference
@@ -624,7 +667,7 @@ sub set_preference {
     " );
 
     if($sth->execute( $var, $value )) {
-        $sysprefs{$var} = $value;
+        $sysprefs->{$var} = $value;
     }
     $sth->finish;
 }
@@ -929,6 +972,51 @@ sub restore_dbh
     # return something, then this function should, too.
 }
 
+=head2 queryparser
+
+  $queryparser = C4::Context->queryparser
+
+Returns a handle to an initialized Koha::QueryParser::Driver::PQF object.
+
+=cut
+
+sub queryparser {
+    my $self = shift;
+    unless (defined $context->{"queryparser"}) {
+        $context->{"queryparser"} = &_new_queryparser();
+    }
+
+    return
+      defined( $context->{"queryparser"} )
+      ? $context->{"queryparser"}->new
+      : undef;
+}
+
+=head2 _new_queryparser
+
+Internal helper function to create a new QueryParser object. QueryParser
+is loaded dynamically so as to keep the lack of the QueryParser library from
+getting in anyone's way.
+
+=cut
+
+sub _new_queryparser {
+    my $qpmodules = {
+        'OpenILS::QueryParser'           => undef,
+        'Koha::QueryParser::Driver::PQF' => undef
+    };
+    if ( can_load( 'modules' => $qpmodules ) ) {
+        my $QParser     = Koha::QueryParser::Driver::PQF->new();
+        my $config_file = $context->config('queryparser_config');
+        $config_file ||= '/etc/koha/searchengine/queryparser.yaml';
+        if ( $QParser->load_config($config_file) ) {
+            # TODO: allow indexes to be configured in the database
+            return $QParser;
+        }
+    }
+    return;
+}
+
 =head2 marcfromkohafield
 
   $dbh = C4::Context->marcfromkohafield;
@@ -1025,18 +1113,8 @@ C<C4::Context-E<gt>userenv> twice, you will get the same hash without real DB ac
 #'
 sub userenv {
     my $var = $context->{"activeuser"};
-    return $context->{"userenv"}->{$var} if (defined $var and defined $context->{"userenv"}->{$var});
-    # insecure=1 management
-    if ($context->{"dbh"} && $context->preference('insecure') eq 'yes') {
-        my %insecure;
-        $insecure{flags} = '16382';
-        $insecure{branchname} ='Insecure';
-        $insecure{number} ='0';
-        $insecure{cardnumber} ='0';
-        $insecure{id} = 'insecure';
-        $insecure{branch} = 'INS';
-        $insecure{emailaddress} = 'test@mode.insecure.com';
-        return \%insecure;
+    if (defined $var and defined $context->{"userenv"}->{$var}) {
+        return $context->{"userenv"}->{$var};
     } else {
         return;
     }
@@ -1045,7 +1123,8 @@ sub userenv {
 =head2 set_userenv
 
   C4::Context->set_userenv($usernum, $userid, $usercnum, $userfirstname, 
-                  $usersurname, $userbranch, $userflags, $emailaddress);
+                  $usersurname, $userbranch, $userflags, $emailaddress, $branchprinter,
+                  $persona);
 
 Establish a hash of user environment variables.
 
@@ -1055,7 +1134,7 @@ set_userenv is called in Auth.pm
 
 #'
 sub set_userenv {
-    my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_;
+    my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter, $persona)= @_;
     my $var=$context->{"activeuser"} || '';
     my $cell = {
         "number"     => $usernum,
@@ -1068,7 +1147,8 @@ sub set_userenv {
         "branchname" => $branchname,
         "flags"      => $userflags,
         "emailaddress"     => $emailaddress,
-        "branchprinter"    => $branchprinter
+        "branchprinter"    => $branchprinter,
+        "persona"    => $persona,
     };
     $context->{userenv}->{$var} = $cell;
     return $cell;