View | Details | Raw Unified | Return to bug 25158
Collapse All | Expand All

(-)a/debian/scripts/koha-memcached (-1 / +115 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
# koha-memcached -- interact with items stored in memcached
3
# Copyright 2020  Libriotech AS
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
use Memcached::libmemcached qw( memcached_create memcached_server_add memcached_get memcached_delete );
19
use Data::Dumper;
20
use Getopt::Long;
21
use Modern::Perl;
22
23
Getopt::Long::Configure("bundling");
24
25
my %opts;
26
my $res = GetOptions( \%opts,
27
    "list|l=s", 
28
    "get|g=s",
29
    "delete|d=s",
30
    "help|h",
31
);
32
33
if ( !$res || $opts{help} ) {
34
    show_help( !$res );
35
    exit( !$res );
36
}
37
38
if ( !@ARGV ) {
39
    show_help( 1, "An instance name must be supplied." );
40
    exit(1);
41
}
42
my $instance = shift @ARGV;
43
if ( !-e "/etc/koha/sites/$instance" ) {
44
    show_help( 1, "The instance doesn't exist: $instance" );
45
    exit(1);
46
}
47
 
48
my $memc = memcached_create();
49
memcached_server_add( $memc, "localhost", '11211' ); # FIXME Get this from koha-conf.xml
50
51
if ( defined $opts{list} ) {
52
53
    if ( $opts{list} eq 'all' ) {
54
        say sort `memcdump --servers=localhost | grep "koha_$instance"`;
55
    } elsif ( $opts{list} eq 'authval' ) {
56
        say sort `memcdump --servers=localhost | grep "koha_$instance" | grep "::AuthorisedValues"`;
57
    } elsif ( $opts{list} eq 'syspref' ) {
58
        say sort `memcdump --servers=localhost | grep "koha_$instance" | grep "syspref:syspref"`;
59
    }
60
61
} elsif ( defined $opts{get} ) {
62
63
    $opts{get} = expand_shortcut( $opts{get}, $instance );
64
    say memcached_get( $memc, $opts{get} );
65
66
} elsif ( defined $opts{delete} ) {
67
68
    $opts{delete} = expand_shortcut( $opts{delete}, $instance );
69
    memcached_delete( $memc, $opts{delete} );
70
71
}
72
73
sub expand_shortcut {
74
75
    my ( $string, $instance ) = @_;
76
77
    my %shortcut = (
78
        'config' => "koha_$instance:config:koha_conf",
79
    );
80
81
    if ( defined $shortcut{ $string } ) {
82
        return $shortcut{ $string };
83
    } else {
84
        return $string;
85
    }
86
87
}
88
89
sub show_help {
90
    my ( $err, $msg ) = @_;
91
92
    my $fh = $err ? *STDERR : *STDOUT;
93
    say $fh "Error: " . $msg if $msg;
94
    print $fh $_ while <DATA>;
95
}
96
97
__DATA__
98
koha-memcached -- interact with items stored in memcached
99
100
Usage: koha-memcached [options] [instance name]
101
102
Options:
103
    -l, --list TYPE     list all KEYS of type TYPE (all, syspref, authval)
104
    -g, --get KEY       display the value associated with a KEY
105
    -d, --delete KEY    delete the value for a given KEY
106
    -h, --help          show this help and quit
107
108
Possible values for TYPE:
109
    all     = display all KEYS
110
    syspref = display KEYS for all systempreferences
111
    authval = display KEYS for all authorised values
112
    other   = all keys that are not in the specific categories above (not implemented)
113
114
Special values/shortcuts for KEY:
115
    config = the configuration from koha-conf.xml

Return to bug 25158