2 # Copyright 2002 Katipo Communications
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA 02111-1307 USA
20 use vars qw($VERSION $AUTOLOAD $context @context_stack);
23 if ($ENV{'HTTP_USER_AGENT'}) {
25 # FIXME for future reference, CGI::Carp doc says
26 # "Note that fatalsToBrowser does not work with mod_perl version 2.0 and higher."
27 import CGI::Carp qw(fatalsToBrowser);
31 eval {C4::Context->dbh();};
36 $debug_level = C4::Context->preference("DebugLevel");
39 print q(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
40 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
41 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
42 <head><title>Koha Error</title></head>
45 if ($debug_level eq "2"){
46 # debug 2 , print extra info too.
47 my %versions = get_versions();
49 # a little example table with various version info";
52 <p>The following fatal error has occurred:</p>
53 <pre><code>$msg</code></pre>
55 <tr><th>Apache</th><td> $versions{apacheVersion}</td></tr>
56 <tr><th>Koha</th><td> $versions{kohaVersion}</td></tr>
57 <tr><th>Koha DB</th><td> $versions{kohaDbVersion}</td></tr>
58 <tr><th>MySQL</th><td> $versions{mysqlVersion}</td></tr>
59 <tr><th>OS</th><td> $versions{osVersion}</td></tr>
60 <tr><th>Perl</th><td> $versions{perlVersion}</td></tr>
63 } elsif ($debug_level eq "1"){
66 <p>The following fatal error has occurred:</p>
67 <pre><code>$msg</code></pre>";
69 print "<p>production mode - trapped fatal error</p>";
71 print "</body></html>";
73 CGI::Carp::set_message(\&handle_errors);
74 ## give a stack backtrace if KOHA_BACKTRACES is set
75 ## can't rely on DebugLevel for this, as we're not yet connected
76 if ($ENV{KOHA_BACKTRACES}) {
77 $main::SIG{__DIE__} = \&CGI::Carp::confess;
79 } # else there is no browser to send fatals to!
80 $VERSION = '3.00.00.036';
91 C4::Context - Maintain and manipulate the context of a Koha script
97 use C4::Context("/path/to/koha-conf.xml");
99 $config_value = C4::Context->config("config_variable");
101 $koha_preference = C4::Context->preference("preference");
103 $db_handle = C4::Context->dbh;
105 $Zconn = C4::Context->Zconn;
107 $stopwordhash = C4::Context->stopwords;
111 When a Koha script runs, it makes use of a certain number of things:
112 configuration settings in F</etc/koha/koha-conf.xml>, a connection to the Koha
113 databases, and so forth. These things make up the I<context> in which
116 This module takes care of setting up the context for a script:
117 figuring out which configuration file to load, and loading it, opening
118 a connection to the right database, and so forth.
120 Most scripts will only use one context. They can simply have
126 Other scripts may need to use several contexts. For instance, if a
127 library has two databases, one for a certain collection, and the other
128 for everything else, it might be necessary for a script to use two
129 different contexts to search both databases. Such scripts should use
130 the C<&set_context> and C<&restore_context> functions, below.
132 By default, C4::Context reads the configuration from
133 F</etc/koha/koha-conf.xml>. This may be overridden by setting the C<$KOHA_CONF>
134 environment variable to the pathname of a configuration file to use.
143 # In addition to what is said in the POD above, a Context object is a
144 # reference-to-hash with the following fields:
147 # A reference-to-hash whose keys and values are the
148 # configuration variables and values specified in the config
149 # file (/etc/koha/koha-conf.xml).
151 # A handle to the appropriate database for this context.
153 # Used by &set_dbh and &restore_dbh to hold other database
154 # handles for this context.
156 # A connection object for the Zebra server
158 # Koha's main configuration file koha-conf.xml
159 # is searched for according to this priority list:
161 # 1. Path supplied via use C4::Context '/path/to/koha-conf.xml'
162 # 2. Path supplied in KOHA_CONF environment variable.
163 # 3. Path supplied in INSTALLED_CONFIG_FNAME, as long
164 # as value has changed from its default of
165 # '__KOHA_CONF_DIR__/koha-conf.xml', as happens
166 # when Koha is installed in 'standard' or 'single'
168 # 4. Path supplied in CONFIG_FNAME.
170 # The first entry that refers to a readable file is used.
172 use constant CONFIG_FNAME => "/etc/koha/koha-conf.xml";
173 # Default config file, if none is specified
175 my $INSTALLED_CONFIG_FNAME = '__KOHA_CONF_DIR__/koha-conf.xml';
176 # path to config file set by installer
177 # __KOHA_CONF_DIR__ is set by rewrite-confg.PL
178 # when Koha is installed in 'standard' or 'single'
179 # mode. If Koha was installed in 'dev' mode,
180 # __KOHA_CONF_DIR__ is *not* rewritten; instead
181 # developers should set the KOHA_CONF environment variable
183 $context = undef; # Initially, no context is set
184 @context_stack = (); # Initially, no saved contexts
188 returns the kohaversion stored in kohaversion.pl file
193 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
195 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
196 # on a standard install, /cgi-bin need to be added.
197 # test one, then the other
198 # FIXME - is this all really necessary?
199 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
200 $cgidir = C4::Context->intranetdir;
204 do $cgidir."/kohaversion.pl" || die "NO $cgidir/kohaversion.pl";
205 return kohaversion();
207 =item read_config_file
211 Reads the specified Koha config file.
213 Returns an object containing the configuration variables. The object's
214 structure is a bit complex to the uninitiated ... take a look at the
215 koha-conf.xml file as well as the XML::Simple documentation for details. Or,
216 here are a few examples that may give you what you need:
218 The simple elements nested within the <config> element:
220 my $pass = $koha->{'config'}->{'pass'};
222 The <listen> elements:
224 my $listen = $koha->{'listen'}->{'biblioserver'}->{'content'};
226 The elements nested within the <server> element:
228 my $ccl2rpn = $koha->{'server'}->{'biblioserver'}->{'cql2rpn'};
230 Returns undef in case of error.
236 sub read_config_file { # Pass argument naming config file to read
237 my $koha = XMLin(shift, keyattr => ['id'], forcearray => ['listen', 'server', 'serverinfo']);
238 return $koha; # Return value: ref-to-hash holding the configuration
242 # Translates the full text name of a database into de appropiate dbi name
248 # FIXME - Should have other databases.
249 if (/mysql/i) { return("mysql"); }
250 if (/Postgres|Pg|PostgresSQL/) { return("Pg"); }
251 if (/oracle/i) { return("Oracle"); }
253 return undef; # Just in case
257 # Create the default context ($C4::Context::Context)
258 # the first time the module is called
259 # (a config file can be optionaly passed)
261 # default context allready exists?
265 my ($pkg,$config_file) = @_ ;
266 my $new_ctx = __PACKAGE__->new($config_file);
267 return unless $new_ctx;
269 # if successfully loaded, use it by default
270 $new_ctx->set_context;
276 $context = new C4::Context;
277 $context = new C4::Context("/path/to/koha-conf.xml");
279 Allocates a new context. Initializes the context from the specified
280 file, which defaults to either the file given by the C<$KOHA_CONF>
281 environment variable, or F</etc/koha/koha-conf.xml>.
283 C<&new> does not set this context as the new default context; for
284 that, use C<&set_context>.
290 # 2004-08-10 A. Tarallo: Added check if the conf file is not empty
293 my $conf_fname = shift; # Config file to load
296 # check that the specified config file exists and is not empty
297 undef $conf_fname unless
298 (defined $conf_fname && -s $conf_fname);
299 # Figure out a good config file to load if none was specified.
300 if (!defined($conf_fname))
302 # If the $KOHA_CONF environment variable is set, use
303 # that. Otherwise, use the built-in default.
304 if (exists $ENV{"KOHA_CONF"} and $ENV{'KOHA_CONF'} and -s $ENV{"KOHA_CONF"}) {
305 $conf_fname = $ENV{"KOHA_CONF"};
306 } elsif ($INSTALLED_CONFIG_FNAME !~ /__KOHA_CONF_DIR/ and -s $INSTALLED_CONFIG_FNAME) {
307 # NOTE: be careful -- don't change __KOHA_CONF_DIR in the above
308 # regex to anything else -- don't want installer to rewrite it
309 $conf_fname = $INSTALLED_CONFIG_FNAME;
310 } elsif (-s CONFIG_FNAME) {
311 $conf_fname = CONFIG_FNAME;
313 warn "unable to locate Koha configuration file koha-conf.xml";
317 # Load the desired config file.
318 $self = read_config_file($conf_fname);
319 $self->{"config_file"} = $conf_fname;
321 warn "read_config_file($conf_fname) returned undef" if !defined($self->{"config"});
322 return undef if !defined($self->{"config"});
324 $self->{"dbh"} = undef; # Database handle
325 $self->{"Zconn"} = undef; # Zebra Connections
326 $self->{"stopwords"} = undef; # stopwords list
327 $self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield
328 $self->{"userenv"} = undef; # User env
329 $self->{"activeuser"} = undef; # current active user
330 $self->{"shelves"} = undef;
338 $context = new C4::Context;
339 $context->set_context();
341 set_context C4::Context $context;
344 restore_context C4::Context;
346 In some cases, it might be necessary for a script to use multiple
347 contexts. C<&set_context> saves the current context on a stack, then
348 sets the context to C<$context>, which will be used in future
349 operations. To restore the previous context, use C<&restore_context>.
357 my $new_context; # The context to set
359 # Figure out whether this is a class or instance method call.
361 # We're going to make the assumption that control got here
362 # through valid means, i.e., that the caller used an instance
363 # or class method call, and that control got here through the
364 # usual inheritance mechanisms. The caller can, of course,
365 # break this assumption by playing silly buggers, but that's
366 # harder to do than doing it properly, and harder to check
368 if (ref($self) eq "")
370 # Class method. The new context is the next argument.
371 $new_context = shift;
373 # Instance method. The new context is $self.
374 $new_context = $self;
377 # Save the old context, if any, on the stack
378 push @context_stack, $context if defined($context);
380 # Set the new context
381 $context = $new_context;
384 =item restore_context
388 Restores the context set by C<&set_context>.
397 if ($#context_stack < 0)
400 die "Context stack underflow";
403 # Pop the old context and set it.
404 $context = pop @context_stack;
406 # FIXME - Should this return something, like maybe the context
407 # that was current when this was called?
412 $value = C4::Context->config("config_variable");
414 $value = C4::Context->config_variable;
416 Returns the value of a variable specified in the configuration file
417 from which the current context was created.
419 The second form is more compact, but of course may conflict with
420 method names. If there is a configuration variable called "new", then
421 C<C4::Config-E<gt>new> will not return it.
425 sub _common_config ($$) {
428 return undef if !defined($context->{$term});
429 # Presumably $self->{$term} might be
430 # undefined if the config file given to &new
431 # didn't exist, and the caller didn't bother
432 # to check the return value.
434 # Return the value of the requested config variable
435 return $context->{$term}->{$var};
439 return _common_config($_[1],'config');
442 return _common_config($_[1],'server');
445 return _common_config($_[1],'serverinfo');
450 $sys_preference = C4::Context->preference('some_variable');
452 Looks up the value of the given system preference in the
453 systempreferences table of the Koha database, and returns it. If the
454 variable is not set or does not exist, undef is returned.
456 In case of an error, this may return 0.
458 Note: It is impossible to tell the difference between system
459 preferences which do not exist, and those whose values are set to NULL
464 # FIXME: running this under mod_perl will require a means of
465 # flushing the caching mechanism.
471 my $var = shift; # The system preference to return
473 if (exists $sysprefs{$var}) {
474 return $sysprefs{$var};
477 my $dbh = C4::Context->dbh or return 0;
479 # Look up systempreferences.variable==$var
480 my $sql = <<'END_SQL';
482 FROM systempreferences
486 $sysprefs{$var} = $dbh->selectrow_array( $sql, {}, $var );
487 return $sysprefs{$var};
490 sub boolean_preference ($) {
492 my $var = shift; # The system preference to return
493 my $it = preference($self, $var);
494 return defined($it)? C4::Boolean::true_p($it): undef;
498 # This implements C4::Config->foo, and simply returns
499 # C4::Context->config("foo"), as described in the documentation for
502 # FIXME - Perhaps this should be extended to check &config first, and
503 # then &preference if that fails. OTOH, AUTOLOAD could lead to crappy
504 # code, so it'd probably be best to delete it altogether so as not to
505 # encourage people to use it.
510 $AUTOLOAD =~ s/.*:://; # Chop off the package name,
511 # leaving only the function name.
512 return $self->config($AUTOLOAD);
517 $Zconn = C4::Context->Zconn
519 Returns a connection to the Zebra database for the current
520 context. If no connection has yet been made, this method
521 creates one and connects.
525 C<$server> one of the servers defined in the koha-conf.xml file
527 C<$async> whether this is a asynchronous connection
529 C<$auth> whether this connection has rw access (1) or just r access (0 or NULL)
541 if ( defined($context->{"Zconn"}->{$server}) && (0 == $context->{"Zconn"}->{$server}->errcode()) ) {
542 return $context->{"Zconn"}->{$server};
543 # No connection object or it died. Create one.
545 # release resources if we're closing a connection and making a new one
546 # FIXME: this needs to be smarter -- an error due to a malformed query or
547 # a missing index does not necessarily require us to close the connection
548 # and make a new one, particularly for a batch job. However, at
549 # first glance it does not look like there's a way to easily check
550 # the basic health of a ZOOM::Connection
551 $context->{"Zconn"}->{$server}->destroy() if defined($context->{"Zconn"}->{$server});
553 $context->{"Zconn"}->{$server} = &_new_Zconn($server,$async,$auth,$piggyback,$syntax);
554 return $context->{"Zconn"}->{$server};
560 $context->{"Zconn"} = &_new_Zconn($server,$async);
562 Internal function. Creates a new database connection from the data given in the current context and returns it.
564 C<$server> one of the servers defined in the koha-conf.xml file
566 C<$async> whether this is a asynchronous connection
568 C<$auth> whether this connection has rw access (1) or just r access (0 or NULL)
573 my ($server,$async,$auth,$piggyback,$syntax) = @_;
575 my $tried=0; # first attempt
576 my $Zconn; # connection object
577 $server = "biblioserver" unless $server;
578 $syntax = "usmarc" unless $syntax;
580 my $host = $context->{'listen'}->{$server}->{'content'};
581 my $servername = $context->{"config"}->{$server};
582 my $user = $context->{"serverinfo"}->{$server}->{"user"};
583 my $password = $context->{"serverinfo"}->{$server}->{"password"};
584 $auth = 1 if($user && $password);
588 my $o = new ZOOM::Options();
589 $o->option(user=>$user) if $auth;
590 $o->option(password=>$password) if $auth;
591 $o->option(async => 1) if $async;
592 $o->option(count => $piggyback) if $piggyback;
593 $o->option(cqlfile=> $context->{"server"}->{$server}->{"cql2rpn"});
594 $o->option(cclfile=> $context->{"serverinfo"}->{$server}->{"ccl2rpn"});
595 $o->option(preferredRecordSyntax => $syntax);
596 $o->option(elementSetName => "F"); # F for 'full' as opposed to B for 'brief'
597 $o->option(databaseName => ($servername?$servername:"biblios"));
599 # create a new connection object
600 $Zconn= create ZOOM::Connection($o);
603 $Zconn->connect($host, 0);
605 # check for errors and warn
606 if ($Zconn->errcode() !=0) {
607 warn "something wrong with the connection: ". $Zconn->errmsg();
612 # # Koha manages the Zebra server -- this doesn't work currently for me because of permissions issues
613 # # Also, I'm skeptical about whether it's the best approach
614 # warn "problem with Zebra";
615 # if ( C4::Context->preference("ManageZebra") ) {
616 # if ($@->code==10000 && $tried==0) { ##No connection try restarting Zebra
618 # warn "trying to restart Zebra";
619 # my $res=system("zebrasrv -f $ENV{'KOHA_CONF'} >/koha/log/zebra-error.log");
622 # warn "Error ", $@->code(), ": ", $@->message(), "\n";
632 # Internal helper function (not a method!). This creates a new
633 # database connection from the data given in the current context, and
639 ##correct name for db_schme
641 if ($context->config("db_scheme")){
642 $db_driver=db_scheme2dbi($context->config("db_scheme"));
647 my $db_name = $context->config("database");
648 my $db_host = $context->config("hostname");
649 my $db_port = $context->config("port") || '';
650 my $db_user = $context->config("user");
651 my $db_passwd = $context->config("pass");
652 # MJR added or die here, as we can't work without dbh
653 my $dbh= DBI->connect("DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port",
654 $db_user, $db_passwd) or die $DBI::errstr;
656 if ( $db_driver eq 'mysql' ) {
657 # Koha 3.0 is utf-8, so force utf8 communication between mySQL and koha, whatever the mysql default config.
658 # this is better than modifying my.cnf (and forcing all communications to be in utf8)
659 $dbh->{'mysql_enable_utf8'}=1; #enable
660 $dbh->do("set NAMES 'utf8'");
661 ($tz) and $dbh->do(qq(SET time_zone = "$tz"));
663 elsif ( $db_driver eq 'Pg' ) {
664 $dbh->do( "set client_encoding = 'UTF8';" );
665 ($tz) and $dbh->do(qq(SET TIME ZONE = "$tz"));
672 $dbh = C4::Context->dbh;
674 Returns a database handle connected to the Koha database for the
675 current context. If no connection has yet been made, this method
676 creates one, and connects to the database.
678 This database handle is cached for future use: if you call
679 C<C4::Context-E<gt>dbh> twice, you will get the same handle both
680 times. If you need a second database handle, use C<&new_dbh> and
681 possibly C<&set_dbh>.
691 if (defined($context->{"dbh"}) && $context->{"dbh"}->ping()) {
692 return $context->{"dbh"};
695 # No database handle or it died . Create one.
696 $context->{"dbh"} = &_new_dbh();
698 return $context->{"dbh"};
703 $dbh = C4::Context->new_dbh;
705 Creates a new connection to the Koha database for the current context,
706 and returns the database handle (a C<DBI::db> object).
708 The handle is not saved anywhere: this method is strictly a
709 convenience function; the point is that it knows which database to
710 connect to so that the caller doesn't have to know.
724 $my_dbh = C4::Connect->new_dbh;
725 C4::Connect->set_dbh($my_dbh);
727 C4::Connect->restore_dbh;
729 C<&set_dbh> and C<&restore_dbh> work in a manner analogous to
730 C<&set_context> and C<&restore_context>.
732 C<&set_dbh> saves the current database handle on a stack, then sets
733 the current database handle to C<$my_dbh>.
735 C<$my_dbh> is assumed to be a good database handle.
745 # Save the current database handle on the handle stack.
746 # We assume that $new_dbh is all good: if the caller wants to
747 # screw himself by passing an invalid handle, that's fine by
749 push @{$context->{"dbh_stack"}}, $context->{"dbh"};
750 $context->{"dbh"} = $new_dbh;
755 C4::Context->restore_dbh;
757 Restores the database handle saved by an earlier call to
758 C<C4::Context-E<gt>set_dbh>.
767 if ($#{$context->{"dbh_stack"}} < 0)
770 die "DBH stack underflow";
773 # Pop the old database handle and set it.
774 $context->{"dbh"} = pop @{$context->{"dbh_stack"}};
776 # FIXME - If it is determined that restore_context should
777 # return something, then this function should, too.
780 =item marcfromkohafield
782 $dbh = C4::Context->marcfromkohafield;
784 Returns a hash with marcfromkohafield.
786 This hash is cached for future use: if you call
787 C<C4::Context-E<gt>marcfromkohafield> twice, you will get the same hash without real DB access
792 sub marcfromkohafield
796 # If the hash already exists, return it.
797 return $context->{"marcfromkohafield"} if defined($context->{"marcfromkohafield"});
799 # No hash. Create one.
800 $context->{"marcfromkohafield"} = &_new_marcfromkohafield();
802 return $context->{"marcfromkohafield"};
805 # _new_marcfromkohafield
806 # Internal helper function (not a method!). This creates a new
807 # hash with stopwords
808 sub _new_marcfromkohafield
810 my $dbh = C4::Context->dbh;
811 my $marcfromkohafield;
812 my $sth = $dbh->prepare("select frameworkcode,kohafield,tagfield,tagsubfield from marc_subfield_structure where kohafield > ''");
814 while (my ($frameworkcode,$kohafield,$tagfield,$tagsubfield) = $sth->fetchrow) {
816 $marcfromkohafield->{$frameworkcode}->{$kohafield} = [$tagfield,$tagsubfield];
818 return $marcfromkohafield;
823 $dbh = C4::Context->stopwords;
825 Returns a hash with stopwords.
827 This hash is cached for future use: if you call
828 C<C4::Context-E<gt>stopwords> twice, you will get the same hash without real DB access
837 # If the hash already exists, return it.
838 return $context->{"stopwords"} if defined($context->{"stopwords"});
840 # No hash. Create one.
841 $context->{"stopwords"} = &_new_stopwords();
843 return $context->{"stopwords"};
847 # Internal helper function (not a method!). This creates a new
848 # hash with stopwords
851 my $dbh = C4::Context->dbh;
853 my $sth = $dbh->prepare("select word from stopwords");
855 while (my $stopword = $sth->fetchrow_array) {
857 $stopwordlist->{$stopword} = uc($stopword);
859 $stopwordlist->{A} = "A" unless $stopwordlist;
860 return $stopwordlist;
865 C4::Context->userenv;
867 Builds a hash for user environment variables.
869 This hash shall be cached for future use: if you call
870 C<C4::Context-E<gt>userenv> twice, you will get the same hash without real DB access
872 set_userenv is called in Auth.pm
879 my $var = $context->{"activeuser"};
880 return $context->{"userenv"}->{$var} if (defined $context->{"userenv"}->{$var});
881 # insecure=1 management
882 if ($context->{"dbh"} && $context->preference('insecure')) {
884 $insecure{flags} = '16382';
885 $insecure{branchname} ='Insecure';
886 $insecure{number} ='0';
887 $insecure{cardnumber} ='0';
888 $insecure{id} = 'insecure';
889 $insecure{branch} = 'INS';
890 $insecure{emailaddress} = 'test@mode.insecure.com';
899 C4::Context->set_userenv($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $userflags, $emailaddress);
901 Informs a hash for user environment variables.
903 This hash shall be cached for future use: if you call
904 C<C4::Context-E<gt>userenv> twice, you will get the same hash without real DB access
906 set_userenv is called in Auth.pm
912 my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_;
913 my $var=$context->{"activeuser"};
915 "number" => $usernum,
917 "cardnumber" => $usercnum,
918 "firstname" => $userfirstname,
919 "surname" => $usersurname,
920 #possibly a law problem
921 "branch" => $userbranch,
922 "branchname" => $branchname,
923 "flags" => $userflags,
924 "emailaddress" => $emailaddress,
925 "branchprinter" => $branchprinter
927 $context->{userenv}->{$var} = $cell;
931 sub set_shelves_userenv ($$) {
932 my ($type, $shelves) = @_ or return undef;
933 my $activeuser = $context->{activeuser} or return undef;
934 $context->{userenv}->{$activeuser}->{barshelves} = $shelves if $type eq 'bar';
935 $context->{userenv}->{$activeuser}->{pubshelves} = $shelves if $type eq 'pub';
936 $context->{userenv}->{$activeuser}->{totshelves} = $shelves if $type eq 'tot';
939 sub get_shelves_userenv () {
941 unless ($active = $context->{userenv}->{$context->{activeuser}}) {
942 $debug and warn "get_shelves_userenv cannot retrieve context->{userenv}->{context->{activeuser}}";
945 my $totshelves = $active->{totshelves} or undef;
946 my $pubshelves = $active->{pubshelves} or undef;
947 my $barshelves = $active->{barshelves} or undef;
948 return ($totshelves, $pubshelves, $barshelves);
953 C4::Context->_new_userenv($session);
955 Builds a hash for user environment variables.
957 This hash shall be cached for future use: if you call
958 C<C4::Context-E<gt>userenv> twice, you will get the same hash without real DB access
960 _new_userenv is called in Auth.pm
969 $context->{"activeuser"}=$sessionID;
974 C4::Context->_unset_userenv;
976 Destroys the hash for activeuser user environment variables.
985 undef $context->{"activeuser"} if ($context->{"activeuser"} eq $sessionID);
991 C4::Context->get_versions
993 Gets various version info, for core Koha packages, Currently called from carp handle_errors() sub, to send to browser if 'DebugLevel' syspref is set to '2'.
999 # A little example sub to show more debugging info for CGI::Carp
1002 $versions{kohaVersion} = KOHAVERSION();
1003 $versions{kohaDbVersion} = C4::Context->preference('version');
1004 $versions{osVersion} = `uname -a`;
1005 $versions{perlVersion} = $];
1006 $versions{mysqlVersion} = `mysql -V`;
1007 $versions{apacheVersion} = `httpd -v`;
1008 $versions{apacheVersion} = `httpd2 -v` unless $versions{apacheVersion} ;
1009 $versions{apacheVersion} = `apache2 -v` unless $versions{apacheVersion} ;
1010 $versions{apacheVersion} = `/usr/sbin/apache2 -v` unless $versions{apacheVersion} ;
1026 Specifies the configuration file to read.
1036 Andrew Arensburger <arensb at ooblick dot com>
1038 Joshua Ferraro <jmf at liblime dot com>
1042 # Revision 1.57 2007/05/22 09:13:55 tipaul
1043 # Bugfixes & improvements (various and minor) :
1044 # - updating templates to have tmpl_process3.pl running without any errors
1045 # - adding a drupal-like css for prog templates (with 3 small images)
1046 # - fixing some bugs in circulation & other scripts
1047 # - updating french translation
1048 # - fixing some typos in templates
1050 # Revision 1.56 2007/04/23 15:21:17 tipaul
1051 # renaming currenttransfers to transferstoreceive
1053 # Revision 1.55 2007/04/17 08:48:00 tipaul
1054 # circulation cleaning continued: bufixing
1056 # Revision 1.54 2007/03/29 16:45:53 tipaul
1057 # Code cleaning of Biblio.pm (continued)
1059 # All subs have be cleaned :
1062 # - reordering Biblio.pm completly
1063 # - using only naming conventions
1065 # Seems to have broken nothing, but it still has to be heavily tested.
1066 # Note that Biblio.pm is now much more efficient than previously & probably more reliable as well.
1068 # Revision 1.53 2007/03/29 13:30:31 tipaul
1070 # == Biblio.pm cleaning (useless) ==
1071 # * some sub declaration dropped
1072 # * removed modbiblio sub
1073 # * removed moditem sub
1074 # * removed newitems. It was used only in finishrecieve. Replaced by a TransformKohaToMarc+AddItem, that is better.
1075 # * removed MARCkoha2marcItem
1076 # * removed MARCdelsubfield declaration
1077 # * removed MARCkoha2marcBiblio
1079 # == Biblio.pm cleaning (naming conventions) ==
1080 # * MARCgettagslib renamed to GetMarcStructure
1081 # * MARCgetitems renamed to GetMarcItem
1082 # * MARCfind_frameworkcode renamed to GetFrameworkCode
1083 # * MARCmarc2koha renamed to TransformMarcToKoha
1084 # * MARChtml2marc renamed to TransformHtmlToMarc
1085 # * MARChtml2xml renamed to TranformeHtmlToXml
1086 # * zebraop renamed to ModZebra
1089 # * removing MARC=OFF related scripts (in cataloguing directory)
1090 # * removed checkitems (function related to MARC=off feature, that is completly broken in head. If someone want to reintroduce it, hard work coming...)
1091 # * removed getitemsbybiblioitem (used only by MARC=OFF scripts, that is removed as well)
1093 # Revision 1.52 2007/03/16 01:25:08 kados
1094 # Using my precrash CVS copy I did the following:
1096 # cvs -z3 -d:ext:kados@cvs.savannah.nongnu.org:/sources/koha co -P koha
1097 # find koha.precrash -type d -name "CVS" -exec rm -v {} \;
1098 # cp -r koha.precrash/* koha/
1102 # This should in theory put us right back where we were before the crash
1104 # Revision 1.52 2007/03/12 21:17:05 rych
1105 # add server, serverinfo as arrays from config
1107 # Revision 1.51 2007/03/09 14:31:47 tipaul
1108 # rel_3_0 moved to HEAD
1110 # Revision 1.43.2.10 2007/02/09 17:17:56 hdl
1111 # Managing a little better database absence.
1112 # (preventing from BIG 550)
1114 # Revision 1.43.2.9 2006/12/20 16:50:48 tipaul
1115 # improving "insecure" management
1118 # you told me that you had some libraries with insecure=ON (behind a firewall).
1119 # In this commit, I created a "fake" user when insecure=ON. It has a fake branch. You may find better to have the 1st branch in branch table instead of a fake one.
1121 # Revision 1.43.2.8 2006/12/19 16:48:16 alaurin
1122 # reident programs, and adding branchcode value in reserves
1124 # Revision 1.43.2.7 2006/12/06 21:55:38 hdl
1125 # Adding ModZebrations for servers to get serverinfos in Context.pm
1126 # Using this function in rebuild_zebra.pl
1128 # Revision 1.43.2.6 2006/11/24 21:18:31 kados
1129 # very minor changes, no functional ones, just comments, etc.
1131 # Revision 1.43.2.5 2006/10/30 13:24:16 toins
1132 # fix some minor POD error.
1134 # Revision 1.43.2.4 2006/10/12 21:42:49 hdl
1135 # Managing multiple zebra connections
1137 # Revision 1.43.2.3 2006/10/11 14:27:26 tipaul
1138 # removing a warning
1140 # Revision 1.43.2.2 2006/10/10 15:28:16 hdl
1141 # BUG FIXING : using database name in Zconn if defined and not hard coded value
1143 # Revision 1.43.2.1 2006/10/06 13:47:28 toins
1144 # Synch with dev_week.
1145 # /!\ WARNING :: Please now use the new version of koha.xml.
1147 # Revision 1.18.2.5.2.14 2006/09/24 15:24:06 kados
1148 # remove Zebraauth routine, fold the functionality into Zconn
1149 # Zconn can now take several arguments ... this will probably
1150 # change soon as I'm not completely happy with the readability
1151 # of the current format ... see the POD for details.
1153 # cleaning up Biblio.pm, removing unnecessary routines.
1155 # DeleteBiblio - used to delete a biblio from zebra and koha tables
1156 # -- checks to make sure there are no existing issues
1157 # -- saves backups of biblio,biblioitems,items in deleted* tables
1158 # -- does commit operation
1160 # getRecord - used to retrieve one record from zebra in piggyback mode using biblionumber
1161 # brought back z3950_extended_services routine
1163 # Lots of modifications to Context.pm, you can now store user and pass info for
1164 # multiple servers (for federated searching) using the <serverinfo> element.
1165 # I'll commit my koha.xml to demonstrate this or you can refer to the POD in
1166 # Context.pm (which I also expanded on).
1168 # Revision 1.18.2.5.2.13 2006/08/10 02:10:21 kados
1169 # Turned warnings on, and running a search turned up lots of warnings.
1170 # Cleaned up those ...
1172 # removed getitemtypes from Koha.pm (one in Search.pm looks newer)
1173 # removed itemcount from Biblio.pm
1175 # made some local subs local with a _ prefix (as they were redefined
1178 # Add two new search subs to Search.pm the start of a new search API
1179 # that's a bit more scalable
1181 # Revision 1.18.2.5.2.10 2006/07/21 17:50:51 kados
1182 # moving the *.properties files to intranetdir/etc dir
1184 # Revision 1.18.2.5.2.9 2006/07/17 08:05:20 tipaul
1185 # there was a hardcoded link to /koha/etc/ I replaced it with intranetdir config value
1187 # Revision 1.18.2.5.2.8 2006/07/11 12:20:37 kados
1188 # adding ccl and cql files ... Tumer, if you want to fit these into the
1189 # config file by all means do.
1191 # Revision 1.18.2.5.2.7 2006/06/04 22:50:33 tgarip1957
1192 # We do not hard code cql2rpn conversion file in context.pm our koha.xml configuration file already describes the path for this file.
1193 # At cql searching we use method CQL not CQL2RPN as the cql2rpn conversion file is defined at server level
1195 # Revision 1.18.2.5.2.6 2006/06/02 23:11:24 kados
1196 # Committing my working dev_week. It's been tested only with
1197 # searching, and there's quite a lot of config stuff to set up
1198 # beforehand. As things get closer to a release, we'll be making
1199 # some scripts to do it for us
1201 # Revision 1.18.2.5.2.5 2006/05/28 18:49:12 tgarip1957
1202 # This is an unusual commit. The main purpose is a working model of Zebra on a modified rel2_2.
1203 # Any questions regarding these commits should be asked to Joshua Ferraro unless you are Joshua whom I'll report to
1205 # Revision 1.36 2006/05/09 13:28:08 tipaul
1206 # adding the branchname and the librarian name in every page :
1207 # - modified userenv to add branchname
1208 # - modifier menus.inc to have the librarian name & userenv displayed on every page. they are in a librarian_information div.
1210 # Revision 1.35 2006/04/13 08:40:11 plg
1211 # bug fixed: typo on Zconnauth name
1213 # Revision 1.34 2006/04/10 21:40:23 tgarip1957
1214 # A new handler defined for zebra Zconnauth with read/write permission. Zconnauth should only be called in biblio.pm where write operations are. Use of this handler will break things unless koha.conf contains new variables:
1216 # zebraport=<your port>
1217 # zebrauser=<username>
1218 # zebrapass=<password>
1220 # The zebra.cfg file should read:
1223 # passw.c:<yourpasswordfile>
1225 # Password file should be prepared with Apaches htpasswd utility in encrypted mode and should exist in a folder zebra.cfg can read
1227 # Revision 1.33 2006/03/15 11:21:56 plg
1228 # bug fixed: utf-8 data where not displayed correctly in screens. Supposing
1229 # your data are truely utf-8 encoded in your database, they should be
1230 # correctly displayed. "set names 'UTF8'" on mysql connection (C4/Context.pm)
1231 # is mandatory and "binmode" to utf8 (C4/Interface/CGI/Output.pm) seemed to
1232 # converted data twice, so it was removed.
1234 # Revision 1.32 2006/03/03 17:25:01 hdl
1235 # Bug fixing : a line missed a comment sign.
1237 # Revision 1.31 2006/03/03 16:45:36 kados
1238 # Remove the search that tests the Zconn -- warning, still no fault
1241 # Revision 1.30 2006/02/22 00:56:59 kados
1242 # First go at a connection object for Zebra. You can now get a
1243 # connection object by doing:
1245 # my $Zconn = C4::Context->Zconn;
1247 # My initial tests indicate that as soon as your funcion ends
1248 # (ie, when you're done doing something) the connection will be
1249 # closed automatically. There may be some other way to make the
1250 # connection more stateful, I'm not sure...