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