Koha::Script - Koha scripts base class
use Koha::Script
use Koha::Script -cron;
This class should be used in all scripts. It sets the interface and userenv appropriately.
use Koha::Script;
use Koha::Script -cron;
Sets the interface and userenv appropriately based on the flags passed.
When the -cron flag is used the script is automatically logged to the CRONJOBS action log (start and end) when the CronjobLog system preference is enabled.
Without -cron, the script is automatically logged to the SCRIPTS action log when the ScriptLog system preference is enabled.
In both cases, command-line arguments whose flag names contain sensitive patterns (password, secret, token, etc.) have their values replaced with [REDACTED] before logging.
my @scrubbed = Koha::Script::_scrub_argv(@ARGV);
Returns a copy of the argument list with values redacted for any flag whose name matches a sensitive pattern (password, passwd, secret, token, apikey, api_key, credential; case-insensitive substring match). Handles both --flag value and --flag=value forms.
my $script = Koha::Script->new(
{
script => $0, # mandatory
[ lock_name => 'my_script' ]
}
);
Create a new Koha::Script object. The script parameter is mandatory, and will usually be passed $0 in the caller script. The lock_name parameter is optional, and is used to generate the lock file if passed.
# die if cannot get the lock
try {
$script->lock_exec;
}
catch {
die "$_";
};
# wait for the lock to be released
$script->lock_exec({ wait => 1 });
This method sets an execution lock to prevent concurrent execution of the caller script. If passed the wait parameter with a true value, it will make the caller wait until it can be granted the lock (flock's LOCK_NB behaviour). It will otherwise throw an exception immediately.
my $opt = Koha::Script->describe_options(
'Move cart items back to their original shelving location.
%c %o', [ 'hours|h=i', 'hours since item was placed on the cart', { required => 1 } ], { epilog => "Examples:\n %c --hours 24\n" }, );
# a required positional argument (e.g. one or more report IDs):
my $opt = Koha::Script->describe_options(
'%c %o reportID [ reportID ... ]',
[ 'verbose|v', 'verbose output' ],
{ args => { min => 1, name => 'reportID', variadic => 1 } },
);
# or, when a post-parse check describe_options can't express needs $usage too:
my ( $opt, $usage ) = Koha::Script->describe_options( ... );
# options that must not be combined (e.g. --code and --exclude-code):
my $opt = Koha::Script->describe_options(
'%c %o',
[ 'code|c:s@', 'letter code to process, repeatable' ],
[ 'exclude-code|x:s@', 'letter code to exclude, repeatable' ],
{ exclusive => [ [qw(code exclude-code)] ] },
);
Thin wrapper around Getopt::Long::Descriptive's describe_options, and the recommended way for scripts to declare and parse their command-line options. Options can be declared required, which is enforced before control returns to the caller: a missing required option makes the script die with a clear, consistent error and the generated usage text, before any script logic runs.
The $format string is passed straight through to Getopt::Long::Descriptive. %c expands to the program name and %o to a short flags summary; anything else in $format is free-form and always rendered before the per-option description list, so it is the place for a short description of what the script does, not for examples (see below).
An optional trailing hashref may include an epilog, which is printed after the per-option description list -- the natural place for "Examples:" text, since $format is always rendered first regardless of where %o appears within it. Unlike $format, epilog is not passed to Getopt::Long::Descriptive itself, but %c and %% are still expanded in it for consistency, so example invocations can use the program name.
The same trailing hashref may also include args, declaring a minimum number of positional (non-option) arguments -- something Getopt::Long::Descriptive has no way to express as an option, but which scripts commonly require (e.g. one or more record IDs on the command line). args => { min => 1, name => 'reportID' } makes this exactly as declarative and enforced-for-you as { required => 1 } is for a named option: if @ARGV has fewer than min elements after parsing, the script dies with a clear, consistent error and the usage text, the same as a missing required option would. name is used in that error message. variadic is accepted as documentation only (whether more than min arguments are meaningful); it is not itself enforced. Note that $format is not updated automatically to mention the positional argument(s) -- describe them there yourself, as in the SYNOPSIS above.
The same trailing hashref may also include exclusive, declaring one or more groups of options that must not be given together, e.g. [qw(code exclude-code)] for a script whose --code and --exclude-code options are alternatives. exclusive => [ \@group1, \@group2, ... ] takes a list of such groups (most scripts need only one); each group is a list of option names as first declared in their spec string, hyphens and all (e.g. exclude-code, not the accessor's exclude_code -- Getopt::Long::Descriptive's introspection needs the former). If more than one option in the same group is actually given a value, the script dies with a clear, consistent error naming the conflicting options (as --flags) and the usage text, the same as a missing required option would. An option is considered "given" if it was specified on the command line and resolved to a non-empty value -- a bare flag with no value left over from Getopt::Long's optional-value (:s) types (see bug 37075) does not by itself trigger the conflict. A negatable option given as --no-foo is reported as such. A group member that is not written as an option's first-declared name (an alias, an underscore form, or an unknown option) is a programming error: a Koha::Exceptions::WrongParameter is thrown, naming the expected name when there is one, instead of the group being silently never enforced.
Two options are added automatically to every script using this convention:
--helpPrints the usage text (and epilog, if any) and exits cleanly (status 0).
--manPrints the calling script's own POD as a full manual page (via Pod::Usage's -verbose => 2) and exits cleanly (status 0). This is for scripts whose POD is thorough enough to be worth showing in full; $format/epilog above remain the place for a short overview.
Neither returns control to the caller; some options being declared required does not stop --help or --man from working, as both shortcircuit that check.
Scripts converted to this convention used to be parsed by plain Getopt::Long, which also accepted (and many scripts documented) a long option with a single dash, e.g. -days 5; Getopt::Long::Descriptive parses with bundling and no longer understands it, so existing command lines such as cron jobs would stop working. To give them time to migrate, such an argument is rewritten as the standard --days and a deprecation warning is printed to STDERR (cron mails it to the administrator). Only an argument that is a single dash followed by at least two characters is considered, and only if it is a declared long option name or the unique prefix of one and is not a bundle of declared one-character flags (-vc is still -v -c). A negatable option (e.g. send!) is also accepted as -nosend or -no-send, rewritten as --no-send. The values given to options, and anything after --, are left alone. This is a compatibility shim and will be removed in a future release; use --long in new command lines and documentation.
Returns the $opt object in scalar context. In list context, also returns the $usage object, useful for scripts that need to enforce something Getopt::Long::Descriptive can't express as an option (e.g. a required positional argument) with a consistent error, via $usage->die({ pre_text => "..." }).
Whatever the error (unknown option, missing required option or argument, or a $usage->die call by the script), the script exits with status 1, the same as pod2usage(1) did.
my $bool = Koha::Script::_opt_is_given( $opt, 'exclude-code' );
Returns true if the option first declared as $name (hyphens and all -- see exclusive in "describe_options") was both specified on the command line and resolved to a non-empty value: a scalar option with a defined, non-empty-string value, or an array option with at least one non-empty element. A bare optional-value (:s) flag left over as an empty string (bug 37075) is not mistaken for a real conflicting value.
my $names = Koha::Script::_option_names(@opt_specs);
Returns a hashref mapping every name an option can be referred to by (its first-declared name, its aliases, and their underscore forms) to its first-declared name, the one exclusive groups must use.
my @names = Koha::Script::_negatable_option_names(@opt_specs);
Returns the first-declared names of the negatable (!) options, so a conflict on --no-foo is reported as such.
my @argv = _translate_legacy_options( \@opt_specs, @ARGV );
Rewrites legacy single-dash long options (-days 5, -days=5) as the standard --days, warning that the old form is deprecated. See "describe_options" for the rules.
$self->_initialize_locking
This method initializes the locking configuration.
Martin Renvoize <martin.renvoize@ptfs-europe.com>