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
21 use vars qw($VERSION $AUTOLOAD $context @context_stack);
24 if ($ENV{'HTTP_USER_AGENT'}) {
26 # FIXME for future reference, CGI::Carp doc says
27 # "Note that fatalsToBrowser does not work with mod_perl version 2.0 and higher."
28 import CGI::Carp qw(fatalsToBrowser);
32 eval {C4::Context->dbh();};
37 $debug_level = C4::Context->preference("DebugLevel");
40 print q(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
41 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
42 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
43 <head><title>Koha Error</title></head>
46 if ($debug_level eq "2"){
47 # debug 2 , print extra info too.
48 my %versions = get_versions();
50 # a little example table with various version info";
53 <p>The following fatal error has occurred:</p>
54 <pre><code>$msg</code></pre>
56 <tr><th>Apache</th><td> $versions{apacheVersion}</td></tr>
57 <tr><th>Koha</th><td> $versions{kohaVersion}</td></tr>
58 <tr><th>Koha DB</th><td> $versions{kohaDbVersion}</td></tr>
59 <tr><th>MySQL</th><td> $versions{mysqlVersion}</td></tr>
60 <tr><th>OS</th><td> $versions{osVersion}</td></tr>
61 <tr><th>Perl</th><td> $versions{perlVersion}</td></tr>
64 } elsif ($debug_level eq "1"){
67 <p>The following fatal error has occurred:</p>
68 <pre><code>$msg</code></pre>";
70 print "<p>production mode - trapped fatal error</p>";
72 print "</body></html>";
74 CGI::Carp::set_message(\&handle_errors);
75 ## give a stack backtrace if KOHA_BACKTRACES is set
76 ## can't rely on DebugLevel for this, as we're not yet connected
77 if ($ENV{KOHA_BACKTRACES}) {
78 $main::SIG{__DIE__} = \&CGI::Carp::confess;
80 } # else there is no browser to send fatals to!
81 $VERSION = '3.00.00.036';
92 C4::Context - Maintain and manipulate the context of a Koha script
98 use C4::Context("/path/to/koha-conf.xml");
100 $config_value = C4::Context->config("config_variable");
102 $koha_preference = C4::Context->preference("preference");
104 $db_handle = C4::Context->dbh;
106 $Zconn = C4::Context->Zconn;
108 $stopwordhash = C4::Context->stopwords;
112 When a Koha script runs, it makes use of a certain number of things:
113 configuration settings in F</etc/koha/koha-conf.xml>, a connection to the Koha
114 databases, and so forth. These things make up the I<context> in which
117 This module takes care of setting up the context for a script:
118 figuring out which configuration file to load, and loading it, opening
119 a connection to the right database, and so forth.
121 Most scripts will only use one context. They can simply have
127 Other scripts may need to use several contexts. For instance, if a
128 library has two databases, one for a certain collection, and the other
129 for everything else, it might be necessary for a script to use two
130 different contexts to search both databases. Such scripts should use
131 the C<&set_context> and C<&restore_context> functions, below.
133 By default, C4::Context reads the configuration from
134 F</etc/koha/koha-conf.xml>. This may be overridden by setting the C<$KOHA_CONF>
135 environment variable to the pathname of a configuration file to use.
144 # In addition to what is said in the POD above, a Context object is a
145 # reference-to-hash with the following fields:
148 # A reference-to-hash whose keys and values are the
149 # configuration variables and values specified in the config
150 # file (/etc/koha/koha-conf.xml).
152 # A handle to the appropriate database for this context.
154 # Used by &set_dbh and &restore_dbh to hold other database
155 # handles for this context.
157 # A connection object for the Zebra server
159 # Koha's main configuration file koha-conf.xml
160 # is searched for according to this priority list:
162 # 1. Path supplied via use C4::Context '/path/to/koha-conf.xml'
163 # 2. Path supplied in KOHA_CONF environment variable.
164 # 3. Path supplied in INSTALLED_CONFIG_FNAME, as long
165 # as value has changed from its default of
166 # '__KOHA_CONF_DIR__/koha-conf.xml', as happens
167 # when Koha is installed in 'standard' or 'single'
169 # 4. Path supplied in CONFIG_FNAME.
171 # The first entry that refers to a readable file is used.
173 use constant CONFIG_FNAME => "/etc/koha/koha-conf.xml";
174 # Default config file, if none is specified
176 my $INSTALLED_CONFIG_FNAME = '__KOHA_CONF_DIR__/koha-conf.xml';
177 # path to config file set by installer
178 # __KOHA_CONF_DIR__ is set by rewrite-confg.PL
179 # when Koha is installed in 'standard' or 'single'
180 # mode. If Koha was installed in 'dev' mode,
181 # __KOHA_CONF_DIR__ is *not* rewritten; instead
182 # developers should set the KOHA_CONF environment variable
184 $context = undef; # Initially, no context is set
185 @context_stack = (); # Initially, no saved contexts
189 returns the kohaversion stored in kohaversion.pl file
194 my $cgidir = C4::Context->intranetdir;
196 # Apparently the GIT code does not run out of a CGI-BIN subdirectory
197 # but distribution code does? (Stan, 1jan08)
198 if(-d $cgidir . "/cgi-bin"){
199 my $cgidir .= "/cgi-bin";
202 do $cgidir."/kohaversion.pl" || die "NO $cgidir/kohaversion.pl";
203 return kohaversion();
205 =item read_config_file
209 Reads the specified Koha config file.
211 Returns an object containing the configuration variables. The object's
212 structure is a bit complex to the uninitiated ... take a look at the
213 koha-conf.xml file as well as the XML::Simple documentation for details. Or,
214 here are a few examples that may give you what you need:
216 The simple elements nested within the <config> element:
218 my $pass = $koha->{'config'}->{'pass'};
220 The <listen> elements:
222 my $listen = $koha->{'listen'}->{'biblioserver'}->{'content'};
224 The elements nested within the <server> element:
226 my $ccl2rpn = $koha->{'server'}->{'biblioserver'}->{'cql2rpn'};
228 Returns undef in case of error.
234 sub read_config_file { # Pass argument naming config file to read
235 my $koha = XMLin(shift, keyattr => ['id'], forcearray => ['listen', 'server', 'serverinfo']);
236 return $koha; # Return value: ref-to-hash holding the configuration
240 # Translates the full text name of a database into de appropiate dbi name
246 # FIXME - Should have other databases.
247 if (/mysql/i) { return("mysql"); }
248 if (/Postgres|Pg|PostgresSQL/) { return("Pg"); }
249 if (/oracle/i) { return("Oracle"); }
251 return undef; # Just in case
255 # Create the default context ($C4::Context::Context)
256 # the first time the module is called
257 # (a config file can be optionaly passed)
259 # default context allready exists?
263 my ($pkg,$config_file) = @_ ;
264 my $new_ctx = __PACKAGE__->new($config_file);
265 return unless $new_ctx;
267 # if successfully loaded, use it by default
268 $new_ctx->set_context;
274 $context = new C4::Context;
275 $context = new C4::Context("/path/to/koha-conf.xml");
277 Allocates a new context. Initializes the context from the specified
278 file, which defaults to either the file given by the C<$KOHA_CONF>
279 environment variable, or F</etc/koha/koha-conf.xml>.
281 C<&new> does not set this context as the new default context; for
282 that, use C<&set_context>.
288 # 2004-08-10 A. Tarallo: Added check if the conf file is not empty
291 my $conf_fname = shift; # Config file to load
294 # check that the specified config file exists and is not empty
295 undef $conf_fname unless
296 (defined $conf_fname && -s $conf_fname);
297 # Figure out a good config file to load if none was specified.
298 if (!defined($conf_fname))
300 # If the $KOHA_CONF environment variable is set, use
301 # that. Otherwise, use the built-in default.
302 if (exists $ENV{"KOHA_CONF"} and $ENV{'KOHA_CONF'} and -s $ENV{"KOHA_CONF"}) {
303 $conf_fname = $ENV{"KOHA_CONF"};
304 } elsif ($INSTALLED_CONFIG_FNAME !~ /__KOHA_CONF_DIR/ and -s $INSTALLED_CONFIG_FNAME) {
305 # NOTE: be careful -- don't change __KOHA_CONF_DIR in the above
306 # regex to anything else -- don't want installer to rewrite it
307 $conf_fname = $INSTALLED_CONFIG_FNAME;
308 } elsif (-s CONFIG_FNAME) {
309 $conf_fname = CONFIG_FNAME;
311 warn "unable to locate Koha configuration file koha-conf.xml";
315 # Load the desired config file.
316 $self = read_config_file($conf_fname);
317 $self->{"config_file"} = $conf_fname;
319 warn "read_config_file($conf_fname) returned undef" if !defined($self->{"config"});
320 return undef if !defined($self->{"config"});
322 $self->{"dbh"} = undef; # Database handle
323 $self->{"Zconn"} = undef; # Zebra Connections
324 $self->{"stopwords"} = undef; # stopwords list
325 $self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield
326 $self->{"userenv"} = undef; # User env
327 $self->{"activeuser"} = undef; # current active user
328 $self->{"shelves"} = undef;
336 $context = new C4::Context;
337 $context->set_context();
339 set_context C4::Context $context;
342 restore_context C4::Context;
344 In some cases, it might be necessary for a script to use multiple
345 contexts. C<&set_context> saves the current context on a stack, then
346 sets the context to C<$context>, which will be used in future
347 operations. To restore the previous context, use C<&restore_context>.
355 my $new_context; # The context to set
357 # Figure out whether this is a class or instance method call.
359 # We're going to make the assumption that control got here
360 # through valid means, i.e., that the caller used an instance
361 # or class method call, and that control got here through the
362 # usual inheritance mechanisms. The caller can, of course,
363 # break this assumption by playing silly buggers, but that's
364 # harder to do than doing it properly, and harder to check
366 if (ref($self) eq "")
368 # Class method. The new context is the next argument.
369 $new_context = shift;
371 # Instance method. The new context is $self.
372 $new_context = $self;
375 # Save the old context, if any, on the stack
376 push @context_stack, $context if defined($context);
378 # Set the new context
379 $context = $new_context;
382 =item restore_context
386 Restores the context set by C<&set_context>.
395 if ($#context_stack < 0)
398 die "Context stack underflow";
401 # Pop the old context and set it.
402 $context = pop @context_stack;
404 # FIXME - Should this return something, like maybe the context
405 # that was current when this was called?
410 $value = C4::Context->config("config_variable");
412 $value = C4::Context->config_variable;
414 Returns the value of a variable specified in the configuration file
415 from which the current context was created.
417 The second form is more compact, but of course may conflict with
418 method names. If there is a configuration variable called "new", then
419 C<C4::Config-E<gt>new> will not return it.
423 sub _common_config ($$) {
426 return undef if !defined($context->{$term});
427 # Presumably $self->{$term} might be
428 # undefined if the config file given to &new
429 # didn't exist, and the caller didn't bother
430 # to check the return value.
432 # Return the value of the requested config variable
433 return $context->{$term}->{$var};
437 return _common_config($_[1],'config');
440 return _common_config($_[1],'server');
443 return _common_config($_[1],'serverinfo');
448 $sys_preference = C4::Context->preference('some_variable');
450 Looks up the value of the given system preference in the
451 systempreferences table of the Koha database, and returns it. If the
452 variable is not set or does not exist, undef is returned.
454 In case of an error, this may return 0.
456 Note: It is impossible to tell the difference between system
457 preferences which do not exist, and those whose values are set to NULL
462 # FIXME: running this under mod_perl will require a means of
463 # flushing the caching mechanism.
469 my $var = shift; # The system preference to return
471 if (exists $sysprefs{$var}) {
472 return $sysprefs{$var};
475 my $dbh = C4::Context->dbh or return 0;
477 # Look up systempreferences.variable==$var
478 my $sql = <<'END_SQL';
480 FROM systempreferences
484 $sysprefs{$var} = $dbh->selectrow_array( $sql, {}, $var );
485 return $sysprefs{$var};
488 sub boolean_preference ($) {
490 my $var = shift; # The system preference to return
491 my $it = preference($self, $var);
492 return defined($it)? C4::Boolean::true_p($it): undef;
495 =item clear_syspref_cache
497 C4::Context->clear_syspref_cache();
499 cleans the internal cache of sysprefs. Please call this method if
500 you update the systempreferences table. Otherwise, your new changes
501 will not be seen by this process.
505 sub clear_syspref_cache {
510 # This implements C4::Config->foo, and simply returns
511 # C4::Context->config("foo"), as described in the documentation for
514 # FIXME - Perhaps this should be extended to check &config first, and
515 # then &preference if that fails. OTOH, AUTOLOAD could lead to crappy
516 # code, so it'd probably be best to delete it altogether so as not to
517 # encourage people to use it.
522 $AUTOLOAD =~ s/.*:://; # Chop off the package name,
523 # leaving only the function name.
524 return $self->config($AUTOLOAD);
529 $Zconn = C4::Context->Zconn
531 Returns a connection to the Zebra database for the current
532 context. If no connection has yet been made, this method
533 creates one and connects.
537 C<$server> one of the servers defined in the koha-conf.xml file
539 C<$async> whether this is a asynchronous connection
541 C<$auth> whether this connection has rw access (1) or just r access (0 or NULL)
553 if ( defined($context->{"Zconn"}->{$server}) && (0 == $context->{"Zconn"}->{$server}->errcode()) ) {
554 return $context->{"Zconn"}->{$server};
555 # No connection object or it died. Create one.
557 # release resources if we're closing a connection and making a new one
558 # FIXME: this needs to be smarter -- an error due to a malformed query or
559 # a missing index does not necessarily require us to close the connection
560 # and make a new one, particularly for a batch job. However, at
561 # first glance it does not look like there's a way to easily check
562 # the basic health of a ZOOM::Connection
563 $context->{"Zconn"}->{$server}->destroy() if defined($context->{"Zconn"}->{$server});
565 $context->{"Zconn"}->{$server} = &_new_Zconn($server,$async,$auth,$piggyback,$syntax);
566 return $context->{"Zconn"}->{$server};
572 $context->{"Zconn"} = &_new_Zconn($server,$async);
574 Internal function. Creates a new database connection from the data given in the current context and returns it.
576 C<$server> one of the servers defined in the koha-conf.xml file
578 C<$async> whether this is a asynchronous connection
580 C<$auth> whether this connection has rw access (1) or just r access (0 or NULL)
585 my ($server,$async,$auth,$piggyback,$syntax) = @_;
587 my $tried=0; # first attempt
588 my $Zconn; # connection object
589 $server = "biblioserver" unless $server;
590 $syntax = "usmarc" unless $syntax;
592 my $host = $context->{'listen'}->{$server}->{'content'};
593 my $servername = $context->{"config"}->{$server};
594 my $user = $context->{"serverinfo"}->{$server}->{"user"};
595 my $password = $context->{"serverinfo"}->{$server}->{"password"};
596 $auth = 1 if($user && $password);
600 my $o = new ZOOM::Options();
601 $o->option(user=>$user) if $auth;
602 $o->option(password=>$password) if $auth;
603 $o->option(async => 1) if $async;
604 $o->option(count => $piggyback) if $piggyback;
605 $o->option(cqlfile=> $context->{"server"}->{$server}->{"cql2rpn"});
606 $o->option(cclfile=> $context->{"serverinfo"}->{$server}->{"ccl2rpn"});
607 $o->option(preferredRecordSyntax => $syntax);
608 $o->option(elementSetName => "F"); # F for 'full' as opposed to B for 'brief'
609 $o->option(databaseName => ($servername?$servername:"biblios"));
611 # create a new connection object
612 $Zconn= create ZOOM::Connection($o);
615 $Zconn->connect($host, 0);
617 # check for errors and warn
618 if ($Zconn->errcode() !=0) {
619 warn "something wrong with the connection: ". $Zconn->errmsg();
624 # # Koha manages the Zebra server -- this doesn't work currently for me because of permissions issues
625 # # Also, I'm skeptical about whether it's the best approach
626 # warn "problem with Zebra";
627 # if ( C4::Context->preference("ManageZebra") ) {
628 # if ($@->code==10000 && $tried==0) { ##No connection try restarting Zebra
630 # warn "trying to restart Zebra";
631 # my $res=system("zebrasrv -f $ENV{'KOHA_CONF'} >/koha/log/zebra-error.log");
634 # warn "Error ", $@->code(), ": ", $@->message(), "\n";
644 # Internal helper function (not a method!). This creates a new
645 # database connection from the data given in the current context, and
651 ## correct name for db_schme
653 if ($context->config("db_scheme")){
654 $db_driver=db_scheme2dbi($context->config("db_scheme"));
659 my $db_name = $context->config("database");
660 my $db_host = $context->config("hostname");
661 my $db_port = $context->config("port") || '';
662 my $db_user = $context->config("user");
663 my $db_passwd = $context->config("pass");
664 # MJR added or die here, as we can't work without dbh
665 my $dbh= DBI->connect("DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port",
666 $db_user, $db_passwd) or die $DBI::errstr;
668 if ( $db_driver eq 'mysql' ) {
669 # Koha 3.0 is utf-8, so force utf8 communication between mySQL and koha, whatever the mysql default config.
670 # this is better than modifying my.cnf (and forcing all communications to be in utf8)
671 $dbh->{'mysql_enable_utf8'}=1; #enable
672 $dbh->do("set NAMES 'utf8'");
673 ($tz) and $dbh->do(qq(SET time_zone = "$tz"));
675 elsif ( $db_driver eq 'Pg' ) {
676 $dbh->do( "set client_encoding = 'UTF8';" );
677 ($tz) and $dbh->do(qq(SET TIME ZONE = "$tz"));
684 $dbh = C4::Context->dbh;
686 Returns a database handle connected to the Koha database for the
687 current context. If no connection has yet been made, this method
688 creates one, and connects to the database.
690 This database handle is cached for future use: if you call
691 C<C4::Context-E<gt>dbh> twice, you will get the same handle both
692 times. If you need a second database handle, use C<&new_dbh> and
693 possibly C<&set_dbh>.
703 if (defined($context->{"dbh"}) && $context->{"dbh"}->ping()) {
704 return $context->{"dbh"};
707 # No database handle or it died . Create one.
708 $context->{"dbh"} = &_new_dbh();
710 return $context->{"dbh"};
715 $dbh = C4::Context->new_dbh;
717 Creates a new connection to the Koha database for the current context,
718 and returns the database handle (a C<DBI::db> object).
720 The handle is not saved anywhere: this method is strictly a
721 convenience function; the point is that it knows which database to
722 connect to so that the caller doesn't have to know.
736 $my_dbh = C4::Connect->new_dbh;
737 C4::Connect->set_dbh($my_dbh);
739 C4::Connect->restore_dbh;
741 C<&set_dbh> and C<&restore_dbh> work in a manner analogous to
742 C<&set_context> and C<&restore_context>.
744 C<&set_dbh> saves the current database handle on a stack, then sets
745 the current database handle to C<$my_dbh>.
747 C<$my_dbh> is assumed to be a good database handle.
757 # Save the current database handle on the handle stack.
758 # We assume that $new_dbh is all good: if the caller wants to
759 # screw himself by passing an invalid handle, that's fine by
761 push @{$context->{"dbh_stack"}}, $context->{"dbh"};
762 $context->{"dbh"} = $new_dbh;
767 C4::Context->restore_dbh;
769 Restores the database handle saved by an earlier call to
770 C<C4::Context-E<gt>set_dbh>.
779 if ($#{$context->{"dbh_stack"}} < 0)
782 die "DBH stack underflow";
785 # Pop the old database handle and set it.
786 $context->{"dbh"} = pop @{$context->{"dbh_stack"}};
788 # FIXME - If it is determined that restore_context should
789 # return something, then this function should, too.
792 =item marcfromkohafield
794 $dbh = C4::Context->marcfromkohafield;
796 Returns a hash with marcfromkohafield.
798 This hash is cached for future use: if you call
799 C<C4::Context-E<gt>marcfromkohafield> twice, you will get the same hash without real DB access
804 sub marcfromkohafield
808 # If the hash already exists, return it.
809 return $context->{"marcfromkohafield"} if defined($context->{"marcfromkohafield"});
811 # No hash. Create one.
812 $context->{"marcfromkohafield"} = &_new_marcfromkohafield();
814 return $context->{"marcfromkohafield"};
817 # _new_marcfromkohafield
818 # Internal helper function (not a method!). This creates a new
819 # hash with stopwords
820 sub _new_marcfromkohafield
822 my $dbh = C4::Context->dbh;
823 my $marcfromkohafield;
824 my $sth = $dbh->prepare("select frameworkcode,kohafield,tagfield,tagsubfield from marc_subfield_structure where kohafield > ''");
826 while (my ($frameworkcode,$kohafield,$tagfield,$tagsubfield) = $sth->fetchrow) {
828 $marcfromkohafield->{$frameworkcode}->{$kohafield} = [$tagfield,$tagsubfield];
830 return $marcfromkohafield;
835 $dbh = C4::Context->stopwords;
837 Returns a hash with stopwords.
839 This hash is cached for future use: if you call
840 C<C4::Context-E<gt>stopwords> twice, you will get the same hash without real DB access
849 # If the hash already exists, return it.
850 return $context->{"stopwords"} if defined($context->{"stopwords"});
852 # No hash. Create one.
853 $context->{"stopwords"} = &_new_stopwords();
855 return $context->{"stopwords"};
859 # Internal helper function (not a method!). This creates a new
860 # hash with stopwords
863 my $dbh = C4::Context->dbh;
865 my $sth = $dbh->prepare("select word from stopwords");
867 while (my $stopword = $sth->fetchrow_array) {
869 $stopwordlist->{$stopword} = uc($stopword);
871 $stopwordlist->{A} = "A" unless $stopwordlist;
872 return $stopwordlist;
877 C4::Context->userenv;
879 Builds a hash for user environment variables.
881 This hash shall be cached for future use: if you call
882 C<C4::Context-E<gt>userenv> twice, you will get the same hash without real DB access
884 set_userenv is called in Auth.pm
891 my $var = $context->{"activeuser"};
892 return $context->{"userenv"}->{$var} if (defined $context->{"userenv"}->{$var});
893 # insecure=1 management
894 if ($context->{"dbh"} && $context->preference('insecure')) {
896 $insecure{flags} = '16382';
897 $insecure{branchname} ='Insecure';
898 $insecure{number} ='0';
899 $insecure{cardnumber} ='0';
900 $insecure{id} = 'insecure';
901 $insecure{branch} = 'INS';
902 $insecure{emailaddress} = 'test@mode.insecure.com';
911 C4::Context->set_userenv($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $userflags, $emailaddress);
913 Informs a hash for user environment variables.
915 This hash shall be cached for future use: if you call
916 C<C4::Context-E<gt>userenv> twice, you will get the same hash without real DB access
918 set_userenv is called in Auth.pm
924 my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_;
925 my $var=$context->{"activeuser"};
927 "number" => $usernum,
929 "cardnumber" => $usercnum,
930 "firstname" => $userfirstname,
931 "surname" => $usersurname,
932 #possibly a law problem
933 "branch" => $userbranch,
934 "branchname" => $branchname,
935 "flags" => $userflags,
936 "emailaddress" => $emailaddress,
937 "branchprinter" => $branchprinter
939 $context->{userenv}->{$var} = $cell;
943 sub set_shelves_userenv ($$) {
944 my ($type, $shelves) = @_ or return undef;
945 my $activeuser = $context->{activeuser} or return undef;
946 $context->{userenv}->{$activeuser}->{barshelves} = $shelves if $type eq 'bar';
947 $context->{userenv}->{$activeuser}->{pubshelves} = $shelves if $type eq 'pub';
948 $context->{userenv}->{$activeuser}->{totshelves} = $shelves if $type eq 'tot';
951 sub get_shelves_userenv () {
953 unless ($active = $context->{userenv}->{$context->{activeuser}}) {
954 $debug and warn "get_shelves_userenv cannot retrieve context->{userenv}->{context->{activeuser}}";
957 my $totshelves = $active->{totshelves} or undef;
958 my $pubshelves = $active->{pubshelves} or undef;
959 my $barshelves = $active->{barshelves} or undef;
960 return ($totshelves, $pubshelves, $barshelves);
965 C4::Context->_new_userenv($session);
967 Builds a hash for user environment variables.
969 This hash shall be cached for future use: if you call
970 C<C4::Context-E<gt>userenv> twice, you will get the same hash without real DB access
972 _new_userenv is called in Auth.pm
981 $context->{"activeuser"}=$sessionID;
986 C4::Context->_unset_userenv;
988 Destroys the hash for activeuser user environment variables.
997 undef $context->{"activeuser"} if ($context->{"activeuser"} eq $sessionID);
1003 C4::Context->get_versions
1005 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'.
1011 # A little example sub to show more debugging info for CGI::Carp
1014 $versions{kohaVersion} = KOHAVERSION();
1015 $versions{kohaDbVersion} = C4::Context->preference('version');
1016 $versions{osVersion} = `uname -a`;
1017 $versions{perlVersion} = $];
1018 $versions{mysqlVersion} = `mysql -V`;
1019 $versions{apacheVersion} = `httpd -v`;
1020 $versions{apacheVersion} = `httpd2 -v` unless $versions{apacheVersion} ;
1021 $versions{apacheVersion} = `apache2 -v` unless $versions{apacheVersion} ;
1022 $versions{apacheVersion} = `/usr/sbin/apache2 -v` unless $versions{apacheVersion} ;
1038 Specifies the configuration file to read.
1048 Andrew Arensburger <arensb at ooblick dot com>
1050 Joshua Ferraro <jmf at liblime dot com>
1054 # Revision 1.57 2007/05/22 09:13:55 tipaul
1055 # Bugfixes & improvements (various and minor) :
1056 # - updating templates to have tmpl_process3.pl running without any errors
1057 # - adding a drupal-like css for prog templates (with 3 small images)
1058 # - fixing some bugs in circulation & other scripts
1059 # - updating french translation
1060 # - fixing some typos in templates
1062 # Revision 1.56 2007/04/23 15:21:17 tipaul
1063 # renaming currenttransfers to transferstoreceive
1065 # Revision 1.55 2007/04/17 08:48:00 tipaul
1066 # circulation cleaning continued: bufixing
1068 # Revision 1.54 2007/03/29 16:45:53 tipaul
1069 # Code cleaning of Biblio.pm (continued)
1071 # All subs have be cleaned :
1074 # - reordering Biblio.pm completly
1075 # - using only naming conventions
1077 # Seems to have broken nothing, but it still has to be heavily tested.
1078 # Note that Biblio.pm is now much more efficient than previously & probably more reliable as well.
1080 # Revision 1.53 2007/03/29 13:30:31 tipaul
1082 # == Biblio.pm cleaning (useless) ==
1083 # * some sub declaration dropped
1084 # * removed modbiblio sub
1085 # * removed moditem sub
1086 # * removed newitems. It was used only in finishrecieve. Replaced by a TransformKohaToMarc+AddItem, that is better.
1087 # * removed MARCkoha2marcItem
1088 # * removed MARCdelsubfield declaration
1089 # * removed MARCkoha2marcBiblio
1091 # == Biblio.pm cleaning (naming conventions) ==
1092 # * MARCgettagslib renamed to GetMarcStructure
1093 # * MARCgetitems renamed to GetMarcItem
1094 # * MARCfind_frameworkcode renamed to GetFrameworkCode
1095 # * MARCmarc2koha renamed to TransformMarcToKoha
1096 # * MARChtml2marc renamed to TransformHtmlToMarc
1097 # * MARChtml2xml renamed to TranformeHtmlToXml
1098 # * zebraop renamed to ModZebra
1101 # * removing MARC=OFF related scripts (in cataloguing directory)
1102 # * removed checkitems (function related to MARC=off feature, that is completly broken in head. If someone want to reintroduce it, hard work coming...)
1103 # * removed getitemsbybiblioitem (used only by MARC=OFF scripts, that is removed as well)
1105 # Revision 1.52 2007/03/16 01:25:08 kados
1106 # Using my precrash CVS copy I did the following:
1108 # cvs -z3 -d:ext:kados@cvs.savannah.nongnu.org:/sources/koha co -P koha
1109 # find koha.precrash -type d -name "CVS" -exec rm -v {} \;
1110 # cp -r koha.precrash/* koha/
1114 # This should in theory put us right back where we were before the crash
1116 # Revision 1.52 2007/03/12 21:17:05 rych
1117 # add server, serverinfo as arrays from config
1119 # Revision 1.51 2007/03/09 14:31:47 tipaul
1120 # rel_3_0 moved to HEAD
1122 # Revision 1.43.2.10 2007/02/09 17:17:56 hdl
1123 # Managing a little better database absence.
1124 # (preventing from BIG 550)
1126 # Revision 1.43.2.9 2006/12/20 16:50:48 tipaul
1127 # improving "insecure" management
1130 # you told me that you had some libraries with insecure=ON (behind a firewall).
1131 # 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.
1133 # Revision 1.43.2.8 2006/12/19 16:48:16 alaurin
1134 # reident programs, and adding branchcode value in reserves
1136 # Revision 1.43.2.7 2006/12/06 21:55:38 hdl
1137 # Adding ModZebrations for servers to get serverinfos in Context.pm
1138 # Using this function in rebuild_zebra.pl
1140 # Revision 1.43.2.6 2006/11/24 21:18:31 kados
1141 # very minor changes, no functional ones, just comments, etc.
1143 # Revision 1.43.2.5 2006/10/30 13:24:16 toins
1144 # fix some minor POD error.
1146 # Revision 1.43.2.4 2006/10/12 21:42:49 hdl
1147 # Managing multiple zebra connections
1149 # Revision 1.43.2.3 2006/10/11 14:27:26 tipaul
1150 # removing a warning
1152 # Revision 1.43.2.2 2006/10/10 15:28:16 hdl
1153 # BUG FIXING : using database name in Zconn if defined and not hard coded value
1155 # Revision 1.43.2.1 2006/10/06 13:47:28 toins
1156 # Synch with dev_week.
1157 # /!\ WARNING :: Please now use the new version of koha.xml.
1159 # Revision 1.18.2.5.2.14 2006/09/24 15:24:06 kados
1160 # remove Zebraauth routine, fold the functionality into Zconn
1161 # Zconn can now take several arguments ... this will probably
1162 # change soon as I'm not completely happy with the readability
1163 # of the current format ... see the POD for details.
1165 # cleaning up Biblio.pm, removing unnecessary routines.
1167 # DeleteBiblio - used to delete a biblio from zebra and koha tables
1168 # -- checks to make sure there are no existing issues
1169 # -- saves backups of biblio,biblioitems,items in deleted* tables
1170 # -- does commit operation
1172 # getRecord - used to retrieve one record from zebra in piggyback mode using biblionumber
1173 # brought back z3950_extended_services routine
1175 # Lots of modifications to Context.pm, you can now store user and pass info for
1176 # multiple servers (for federated searching) using the <serverinfo> element.
1177 # I'll commit my koha.xml to demonstrate this or you can refer to the POD in
1178 # Context.pm (which I also expanded on).
1180 # Revision 1.18.2.5.2.13 2006/08/10 02:10:21 kados
1181 # Turned warnings on, and running a search turned up lots of warnings.
1182 # Cleaned up those ...
1184 # removed getitemtypes from Koha.pm (one in Search.pm looks newer)
1185 # removed itemcount from Biblio.pm
1187 # made some local subs local with a _ prefix (as they were redefined
1190 # Add two new search subs to Search.pm the start of a new search API
1191 # that's a bit more scalable
1193 # Revision 1.18.2.5.2.10 2006/07/21 17:50:51 kados
1194 # moving the *.properties files to intranetdir/etc dir
1196 # Revision 1.18.2.5.2.9 2006/07/17 08:05:20 tipaul
1197 # there was a hardcoded link to /koha/etc/ I replaced it with intranetdir config value
1199 # Revision 1.18.2.5.2.8 2006/07/11 12:20:37 kados
1200 # adding ccl and cql files ... Tumer, if you want to fit these into the
1201 # config file by all means do.
1203 # Revision 1.18.2.5.2.7 2006/06/04 22:50:33 tgarip1957
1204 # We do not hard code cql2rpn conversion file in context.pm our koha.xml configuration file already describes the path for this file.
1205 # At cql searching we use method CQL not CQL2RPN as the cql2rpn conversion file is defined at server level
1207 # Revision 1.18.2.5.2.6 2006/06/02 23:11:24 kados
1208 # Committing my working dev_week. It's been tested only with
1209 # searching, and there's quite a lot of config stuff to set up
1210 # beforehand. As things get closer to a release, we'll be making
1211 # some scripts to do it for us
1213 # Revision 1.18.2.5.2.5 2006/05/28 18:49:12 tgarip1957
1214 # This is an unusual commit. The main purpose is a working model of Zebra on a modified rel2_2.
1215 # Any questions regarding these commits should be asked to Joshua Ferraro unless you are Joshua whom I'll report to
1217 # Revision 1.36 2006/05/09 13:28:08 tipaul
1218 # adding the branchname and the librarian name in every page :
1219 # - modified userenv to add branchname
1220 # - modifier menus.inc to have the librarian name & userenv displayed on every page. they are in a librarian_information div.
1222 # Revision 1.35 2006/04/13 08:40:11 plg
1223 # bug fixed: typo on Zconnauth name
1225 # Revision 1.34 2006/04/10 21:40:23 tgarip1957
1226 # 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:
1228 # zebraport=<your port>
1229 # zebrauser=<username>
1230 # zebrapass=<password>
1232 # The zebra.cfg file should read:
1235 # passw.c:<yourpasswordfile>
1237 # Password file should be prepared with Apaches htpasswd utility in encrypted mode and should exist in a folder zebra.cfg can read
1239 # Revision 1.33 2006/03/15 11:21:56 plg
1240 # bug fixed: utf-8 data where not displayed correctly in screens. Supposing
1241 # your data are truely utf-8 encoded in your database, they should be
1242 # correctly displayed. "set names 'UTF8'" on mysql connection (C4/Context.pm)
1243 # is mandatory and "binmode" to utf8 (C4/Interface/CGI/Output.pm) seemed to
1244 # converted data twice, so it was removed.
1246 # Revision 1.32 2006/03/03 17:25:01 hdl
1247 # Bug fixing : a line missed a comment sign.
1249 # Revision 1.31 2006/03/03 16:45:36 kados
1250 # Remove the search that tests the Zconn -- warning, still no fault
1253 # Revision 1.30 2006/02/22 00:56:59 kados
1254 # First go at a connection object for Zebra. You can now get a
1255 # connection object by doing:
1257 # my $Zconn = C4::Context->Zconn;
1259 # My initial tests indicate that as soon as your funcion ends
1260 # (ie, when you're done doing something) the connection will be
1261 # closed automatically. There may be some other way to make the
1262 # connection more stateful, I'm not sure...