# Suite 330, Boston, MA 02111-1307 USA
# $Id$
-# Revision History:
-# 2004-08-11 A. Tarallo: Added the function db_escheme2dbi, tested my bugfixes,
-# further details about them in the code.
-# 2004-11-23 A. Tarallo, E. Silva: Bugfixes for running in a mod_perl environment.
-# Clean up of previous bugfixes, better documentation of what was done.
package C4::Context;
use strict;
=over 2
=cut
+
#'
# In addition to what is said in the POD above, a Context object is a
# reference-to-hash with the following fields:
# dbh_stack
# Used by &set_dbh and &restore_dbh to hold other database
# handles for this context.
+# Zconn
+# A connection object for the Zebra server
use constant CONFIG_FNAME => "/etc/koha.conf";
# Default config file, if none is specified
that, use C<&set_context>.
=cut
+
#'
# Revision History:
# 2004-08-10 A. Tarallo: Added check if the conf file is not empty
return undef if !defined($self->{"config"});
$self->{"dbh"} = undef; # Database handle
+ $self->{"Zconn"} = undef; # Zebra Connection
$self->{"stopwords"} = undef; # stopwords list
$self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield
$self->{"userenv"} = undef; # User env
operations. To restore the previous context, use C<&restore_context>.
=cut
+
#'
sub set_context
{
Restores the context set by C<&set_context>.
=cut
+
#'
sub restore_context
{
C<C4::Config-E<gt>new> will not return it.
=cut
+
#'
sub config
{
variable is not set, or in case of error, returns the undefined value.
=cut
+
#'
# FIXME - The preferences aren't likely to change over the lifetime of
# the script (and things might break if they did change), so perhaps
return $self->config($AUTOLOAD);
}
+=item Zconn
+
+$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.
+
+=cut
+
+sub Zconn {
+ my $self = shift;
+ my $rs;
+ my $Zconn;
+ if (defined($context->{"Zconn"})) {
+ $Zconn = $context->{"Zconn"};
+ $rs=$Zconn->search_pqf('@attr 1=4 mineral');
+ if ($Zconn->errcode() != 0) {
+ $context->{"Zconn"} = &new_Zconn();
+ return $context->{"Zconn"};
+ }
+ return $context->{"Zconn"};
+ } else {
+ $context->{"Zconn"} = &new_Zconn();
+ return $context->{"Zconn"};
+ }
+}
+
+=item new_Zconn
+
+Internal helper function. creates a new database connection from
+the data given in the current context and returns it.
+
+=cut
+
+sub new_Zconn {
+ use ZOOM;
+ my $Zconn;
+ eval {
+ $Zconn = new ZOOM::Connection(C4::Context->config("zebradb"));
+ };
+ if ($@){
+ warn "Error ", $@->code(), ": ", $@->message(), "\n";
+ die "Fatal error, cant connect to z3950 server";
+ }
+ $Zconn->option(cqlfile => C4::Context->config("intranetdir")."/zebra/pqf.properties");
+ $Zconn->option(preferredRecordSyntax => "xml");
+ return $Zconn;
+}
+
# _new_dbh
# Internal helper function (not a method!). This creates a new
# database connection from the data given in the current context, and
possibly C<&set_dbh>.
=cut
+
#'
sub dbh
{
connect to so that the caller doesn't have to know.
=cut
+
#'
sub new_dbh
{
C<$my_dbh> is assumed to be a good database handle.
=cut
+
#'
sub set_dbh
{
C<C4::Context-E<gt>set_dbh>.
=cut
+
#'
sub restore_dbh
{
C<C4::Context-E<gt>marcfromkohafield> twice, you will get the same hash without real DB access
=cut
+
#'
sub marcfromkohafield
{
C<C4::Context-E<gt>stopwords> twice, you will get the same hash without real DB access
=cut
+
#'
sub stopwords
{
set_userenv is called in Auth.pm
=cut
+
#'
sub userenv
{
_new_userenv is called in Auth.pm
=cut
+
#'
sub _new_userenv
{
Andrew Arensburger <arensb at ooblick dot com>
=cut
+# $Log$
+# Revision 1.30 2006/02/22 00:56:59 kados
+# First go at a connection object for Zebra. You can now get a
+# connection object by doing:
+#
+# my $Zconn = C4::Context->Zconn;
+#
+# My initial tests indicate that as soon as your funcion ends
+# (ie, when you're done doing something) the connection will be
+# closed automatically. There may be some other way to make the
+# connection more stateful, I'm not sure...
+#
+# Local Variables:
+# tab-width: 4
+# End: