Bug 28572: Remove C4::Debug
[koha.git] / misc / admin / koha-preferences
1 #!/usr/bin/perl
2 #
3 # Copyright 2010 Jesse Weaver, Koha Dev Team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 #
20
21 use Modern::Perl;
22 use Koha::Script;
23 use C4::Context;
24 use C4::Log;
25 use Getopt::Long;
26 use Pod::Usage;
27 use YAML::XS;
28
29 use Koha::Logger;
30
31 our %NOT_SET_PREFS = map { $_, 1 } qw( Version );
32
33 =head1 NAME
34
35 koha-preferences - Get, set, dump and load Koha system preferences
36
37 =head1 SYNOPSIS
38
39 misc/admin/koha-preferences COMMAND ...
40
41 =cut
42
43 sub print_usage {
44     my ( $annoyed ) = @_;
45
46     if ( $annoyed ) {
47         pod2usage( -verbose => 1, -exitval => 1, -output => \*STDERR );
48     } else {
49         pod2usage( -verbose => 1, -exitval => 0 );
50     }
51 }
52
53 sub _set_preference {
54     my ( $preference, $value ) = @_;
55
56     Koha::Logger->get->debug($message);
57
58     if (   $preference->{type} eq 'YesNo'
59         && $value ne '0'
60         && $value ne '1' )
61     {
62         print STDERR sprintf "System preference %s is YesNo and expects 1 or 0. '%s' was given, using '0'\n",
63           $preference->{variable}, $value;
64         $value = 0;
65     }
66
67     C4::Context->set_preference( $preference->{variable}, $value );
68 }
69
70 sub GetPreferences {
71     my $dbh = C4::Context->dbh;
72
73     return {
74         map { $_->{variable},  $_->{value} }
75         @{ $dbh->selectall_arrayref( "
76             SELECT
77               variable, value, type
78               FROM systempreferences
79         ", { Slice => {} } ) }
80     };
81 }
82
83 sub SetPreferences {
84     my ( %preferences ) = @_;
85
86     my $dbh = C4::Context->dbh;
87
88     # First, a quick check to make sure all of the system preferences exist
89     my $current_state = $dbh->selectall_arrayref( "
90         SELECT
91           variable, type, value
92           FROM systempreferences
93           WHERE variable IN (" . join( ', ', map( "?", keys %preferences ) ) . ")
94     ", { Slice => {} }, keys %preferences );
95
96     exit 2 if ( scalar( @$current_state ) != scalar( keys %preferences ) );
97
98     foreach my $row ( @$current_state ) {
99         my $new_value = $preferences{ $row->{variable} };
100         next if $new_value && $row->{value} && $new_value eq $row->{value};
101
102         _set_preference( $row, $new_value );
103     }
104
105     # FIXME This may be not needed
106     C4::Context->clear_syspref_cache();
107 }
108
109 sub _fetch_preference {
110     my ( $preference ) = @_;
111
112     my $dbh = C4::Context->dbh;
113
114     my $row = $dbh->selectrow_hashref( "
115         SELECT
116           variable, value, type
117           FROM systempreferences
118           WHERE variable = ?
119           LIMIT 1
120     ", {}, $preference );
121
122     exit 2 unless ( $row );
123
124     return $row;
125 }
126
127 sub GetPreference {
128     my ( $preference ) = @_;
129
130     return _fetch_preference( $preference );
131 }
132
133 sub SetPreference {
134     my ( $preference, $value ) = @_;
135
136     my $row = _fetch_preference( $preference );
137
138     exit 3 if ( $value eq $row->{'value'} ); #FIXME exit??
139
140     _set_preference( $row, $value );
141 }
142
143 sub ClearPreference {
144     my ( $preference ) = @_;
145
146     my $value = '';
147
148     my $row = _fetch_preference( $preference );
149
150     $value = 0 if ( $row->{'type'} eq 'YesNo' );
151
152     exit 3 if ( $value eq $row->{'value'} );
153
154     _set_preference( $row, $value );
155 }
156
157 =head1 OPTIONS
158
159 COMMAND can be any of the following:
160
161 =over
162
163 =item B<dump> [ -o I<OUTFILE> ]
164
165 Dump all of Koha's system preferences as a simple YAML mapping into OUTFILE or
166 STDOUT.
167
168 =item B<load> [ -i I<INFILE> ] [ -f|--force ]
169
170 Reads system preferences specified in YAML in INFILE or STDIN.  Will exit with a
171 status of 2, without setting any sysprefs, if any of the sysprefs do not exist.
172 Will also exit if any of the sysprefs are YesNo and have an invalid value.
173
174 If there is a Version syspref in the input, it will not be set in the database,
175 but it will be checked to make sure the running Koha version is equal or higher.
176 The script will exit with a status of 4 if this is not true. Pass the -f option
177 to skip this check.
178
179 =item B<get> I<PREFERENCE>
180
181 Print the value of the system preference PREFERENCE, followed by a newline.  If
182 no such syspref exists, will exit with a status of 2.
183
184 =item B<set> I<PREFERENCE> I<VALUE>
185
186 Set the system preference PREFERENCE to the value VALUE. If no such syspref
187 exists, will exit with a status of 2. If the syspref already has that value,
188 will exit with a status of 3.
189
190 If the syspref is YesNo, will accept only a boolean value. The syntax is *not*
191 lax and must be 1 or 0.
192
193 =item B<clear> I<PREFERENCE>
194
195 Clears the value of the system preference PREFERENCE. If no such syspref exists,
196 will exit with a status of 2. Will set YesNo sysprefs to 'false'.
197
198 =item B<manual>
199
200 Print a longer, more detailed manual.
201
202 =cut
203
204 my %commands = (
205     dump => sub{
206         my ( $outfile );
207
208         GetOptions(
209             'o:s' => \$outfile
210         ) || _print_usage( 1 );
211
212         YAML::XS::DumpFile( $outfile || \*STDOUT, GetPreferences() );
213     },
214     load => sub {
215         my ( $infile, $force_version );
216
217         GetOptions(
218             'i:s' => \$infile,
219             'f' => \$force_version,
220         );
221
222         my $preferences = YAML::XS::LoadFile($infile || \*STDIN);
223
224         die "Expected a YAML mapping" if ( ref($preferences) ne 'HASH' );
225
226         die "Tried to load preferences for version " . $preferences->{'Version'} . ", we are " . C4::Context->preference( 'Version' ) if ( $preferences->{'Version'} && C4::Context->preference( 'Version' ) < $preferences->{'Version'} );
227
228         my %prefs_to_set = (
229             map { $_, $preferences->{$_} }
230             grep { !$NOT_SET_PREFS{$_} }
231             keys %$preferences
232         );
233
234         SetPreferences( %prefs_to_set );
235     },
236     get => sub {
237         my ( $preference ) = @_;
238
239         print_usage() unless ( $preference );
240
241         print GetPreference( $preference ) . "\n";
242     },
243     set => sub {
244         my ( $preference, $value ) = @_;
245
246         print_usage() unless ( $preference && defined($value) );
247
248         SetPreference( $preference, $value );
249     },
250     clear => sub {
251         my ( $preference ) = @_;
252
253         print_usage() unless ( $preference );
254
255         ClearPreference( $preference );
256     },
257     manual => sub {
258         pod2usage( -verbose => 2 );
259     }
260 );
261
262 print_usage() if ( $ARGV[0] =~ /^(-h|--help|-help|help)$/ );
263
264 print_usage( 1 ) if ( !$ARGV[0] || ref($commands{$ARGV[0]}) ne 'CODE' );
265
266 my $command = $commands{$ARGV[0]};
267 shift @ARGV;
268 $command->(@ARGV);
269
270 =item B<help>
271
272 Print a short usage message.
273
274 =back
275
276 =head1 EXAMPLES
277
278   $ export KOHA_DEBUG=1 # Used here to show what is being stored
279   $ misc/admin/koha-preferences get viewISBD
280   0
281   $ misc/admin/koha-preferences set viewISBD on
282   Setting viewISBD to 1
283   $ misc/admin/koha-preferences dump -o preferences.yaml
284   $ [ edit preferences.yaml ]
285   $ misc/admin/koha-preferences load -i preferences.yaml
286   $ misc/admin/koha-preferences load -i preferences-too-new.yaml
287   Tried to load preferences for version 3.0500012, we are 3.0300009 at misc/admin/koha-preferences line 255
288   $ misc/admin/koha-preferences load # Can also work from STDIN
289   XISBN: false
290   viewMARC: y
291   [ Control-D ]
292   Setting viewMARC to 1
293   Setting XISBN to 0
294
295 =cut