From e5d8b25cb6497423d7a17d518f93af38950871fe Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Wed, 20 Apr 2016 12:25:19 +0300 Subject: [PATCH] Bug 16306 - C4::Context->setCommandlineEnvironment() convenience function There is no unified way to set the environment for commandline scripts, even if the C4::Context->interface('commandline') explicitly allows setting the cli interface. Also a default user environment must be set. This function wraps the complexity of environment setting in the command line. Usage: use C4::Context; BEGIN { C4::Context->setCommandlineEnvironment(); } ... #Load other dependencies that might depend on the environment or just: ... use C4::Context; C4::Context->setCommandlineEnvironment(); ... --- C4/Context.pm | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/C4/Context.pm b/C4/Context.pm index 05d761e..5cb50e5 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -1293,6 +1293,20 @@ sub IsSuperLibrarian { return ($userenv->{flags}//0) % 2; } +=head2 setCommandlineEnvironment + +Sets the Koha environment for command line scripts. + +=cut + +sub setCommandlineEnvironment { + my ($class) = @_; + + C4::Context->interface('commandline'); #Set interface for logger and friends + C4::Context->_new_userenv('commandline'); + C4::Context::set_userenv(1,0,0,'commandline',$0, '', '', {}, '', '', ''); +} + =head2 interface Sets the current interface for later retrieval in any Perl module -- 1.9.1