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