[Koha-cvs] koha/C4 Context.pm

Joshua Ferraro jmf at kados.org
Wed Feb 22 01:56:59 CET 2006


CVSROOT:	/sources/koha
Module name:	koha
Branch: 	
Changes by:	Joshua Ferraro <kados at savannah.gnu.org>	06/02/22 00:56:59

Modified files:
	C4             : Context.pm 

Log message:
	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...

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/koha/koha/C4/Context.pm.diff?tr1=1.29&tr2=1.30&r1=text&r2=text

Patches:
Index: koha/C4/Context.pm
diff -u koha/C4/Context.pm:1.29 koha/C4/Context.pm:1.30
--- koha/C4/Context.pm:1.29	Fri Feb 17 15:25:59 2006
+++ koha/C4/Context.pm	Wed Feb 22 00:56:59 2006
@@ -15,12 +15,7 @@
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
-# $Id: Context.pm,v 1.29 2006/02/17 15:25:59 tipaul Exp $
-# 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. 
+# $Id: Context.pm,v 1.30 2006/02/22 00:56:59 kados Exp $
 
 package C4::Context;
 use strict;
@@ -31,7 +26,7 @@
 	qw($context),
 	qw(@context_stack);
 
-$VERSION = do { my @v = '$Revision: 1.29 $' =~ /\d+/g;
+$VERSION = do { my @v = '$Revision: 1.30 $' =~ /\d+/g;
 		shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
 
 =head1 NAME
@@ -80,6 +75,7 @@
 =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:
@@ -93,6 +89,8 @@
 # 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
@@ -203,6 +201,7 @@
 that, use C<&set_context>.
 
 =cut
+
 #'
 # Revision History:
 # 2004-08-10 A. Tarallo: Added check if the conf file is not empty
@@ -230,6 +229,7 @@
 	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
@@ -255,6 +255,7 @@
 operations. To restore the previous context, use C<&restore_context>.
 
 =cut
+
 #'
 sub set_context
 {
@@ -293,6 +294,7 @@
 Restores the context set by C<&set_context>.
 
 =cut
+
 #'
 sub restore_context
 {
@@ -325,6 +327,7 @@
 C<C4::Config-E<gt>new> will not return it.
 
 =cut
+
 #'
 sub config
 {
@@ -350,6 +353,7 @@
 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
@@ -397,6 +401,56 @@
 	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
@@ -430,6 +484,7 @@
 possibly C<&set_dbh>.
 
 =cut
+
 #'
 sub dbh
 {
@@ -459,6 +514,7 @@
 connect to so that the caller doesn't have to know.
 
 =cut
+
 #'
 sub new_dbh
 {
@@ -483,6 +539,7 @@
 C<$my_dbh> is assumed to be a good database handle.
 
 =cut
+
 #'
 sub set_dbh
 {
@@ -505,6 +562,7 @@
 C<C4::Context-E<gt>set_dbh>.
 
 =cut
+
 #'
 sub restore_dbh
 {
@@ -533,6 +591,7 @@
 C<C4::Context-E<gt>marcfromkohafield> twice, you will get the same hash without real DB access
 
 =cut
+
 #'
 sub marcfromkohafield
 {
@@ -573,6 +632,7 @@
 C<C4::Context-E<gt>stopwords> twice, you will get the same hash without real DB access
 
 =cut
+
 #'
 sub stopwords
 {
@@ -616,6 +676,7 @@
 set_userenv is called in Auth.pm
 
 =cut
+
 #'
 sub userenv
 {
@@ -668,6 +729,7 @@
 _new_userenv is called in Auth.pm
 
 =cut
+
 #'
 sub _new_userenv
 {
@@ -717,3 +779,18 @@
 Andrew Arensburger <arensb at ooblick dot com>
 
 =cut
+# $Log: Context.pm,v $
+# 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:





More information about the Koha-cvs mailing list