3 # Copyright 2010 Jesse Weaver, Koha Dev Team
5 # This file is part of Koha.
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.
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.
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>.
29 our %NOT_SET_PREFS = map { $_, 1 } qw( Version );
33 koha-preferences - Get, set, dump and load Koha system preferences
37 misc/admin/koha-preferences COMMAND ...
45 pod2usage( -verbose => 1, -exitval => 1, -output => \*STDERR );
47 pod2usage( -verbose => 1, -exitval => 0 );
54 print STDERR $message . "\n" if ( $C4::Debug::debug );
58 my ( $preference, $value ) = @_;
60 _debug( "Setting $preference to $value" );
62 if ( $preference->{type} eq 'YesNo'
66 print STDERR sprintf "System preference %s is YesNo and expects 1 or 0. '%s' was given, using '0'\n",
67 $preference->{variable}, $value;
71 C4::Context->set_preference( $preference->{variable}, $value );
75 my $dbh = C4::Context->dbh;
78 map { $_->{variable}, $_->{value} }
79 @{ $dbh->selectall_arrayref( "
82 FROM systempreferences
83 ", { Slice => {} } ) }
88 my ( %preferences ) = @_;
90 my $dbh = C4::Context->dbh;
92 # First, a quick check to make sure all of the system preferences exist
93 my $current_state = $dbh->selectall_arrayref( "
96 FROM systempreferences
97 WHERE variable IN (" . join( ', ', map( "?", keys %preferences ) ) . ")
98 ", { Slice => {} }, keys %preferences );
100 exit 2 if ( scalar( @$current_state ) != scalar( keys %preferences ) );
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};
106 _set_preference( $row, $new_value );
109 # FIXME This may be not needed
110 C4::Context->clear_syspref_cache();
113 sub _fetch_preference {
114 my ( $preference ) = @_;
116 my $dbh = C4::Context->dbh;
118 my $row = $dbh->selectrow_hashref( "
120 variable, value, type
121 FROM systempreferences
124 ", {}, $preference );
126 exit 2 unless ( $row );
132 my ( $preference ) = @_;
134 return _fetch_preference( $preference );
138 my ( $preference, $value ) = @_;
140 my $row = _fetch_preference( $preference );
142 exit 3 if ( $value eq $row->{'value'} ); #FIXME exit??
144 _set_preference( $row, $value );
147 sub ClearPreference {
148 my ( $preference ) = @_;
152 my $row = _fetch_preference( $preference );
154 $value = 0 if ( $row->{'type'} eq 'YesNo' );
156 exit 3 if ( $value eq $row->{'value'} );
158 _set_preference( $row, $value );
163 COMMAND can be any of the following:
167 =item B<dump> [ -o I<OUTFILE> ]
169 Dump all of Koha's system preferences as a simple YAML mapping into OUTFILE or
172 =item B<load> [ -i I<INFILE> ] [ -f|--force ]
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.
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
183 =item B<get> I<PREFERENCE>
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.
188 =item B<set> I<PREFERENCE> I<VALUE>
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.
194 If the syspref is YesNo, will accept only a boolean value. The syntax is *not*
195 lax and must be 1 or 0.
197 =item B<clear> I<PREFERENCE>
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'.
204 Print a longer, more detailed manual.
214 ) || _print_usage( 1 );
216 YAML::XS::DumpFile( $outfile || \*STDOUT, GetPreferences() );
219 my ( $infile, $force_version );
223 'f' => \$force_version,
226 my $preferences = YAML::XS::LoadFile($infile || \*STDIN);
228 die "Expected a YAML mapping" if ( ref($preferences) ne 'HASH' );
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'} );
233 map { $_, $preferences->{$_} }
234 grep { !$NOT_SET_PREFS{$_} }
238 SetPreferences( %prefs_to_set );
241 my ( $preference ) = @_;
243 print_usage() unless ( $preference );
245 print GetPreference( $preference ) . "\n";
248 my ( $preference, $value ) = @_;
250 print_usage() unless ( $preference && defined($value) );
252 SetPreference( $preference, $value );
255 my ( $preference ) = @_;
257 print_usage() unless ( $preference );
259 ClearPreference( $preference );
262 pod2usage( -verbose => 2 );
266 print_usage() if ( $ARGV[0] =~ /^(-h|--help|-help|help)$/ );
268 print_usage( 1 ) if ( !$ARGV[0] || ref($commands{$ARGV[0]}) ne 'CODE' );
270 my $command = $commands{$ARGV[0]};
276 Print a short usage message.
282 $ export KOHA_DEBUG=1 # Used here to show what is being stored
283 $ misc/admin/koha-preferences get viewISBD
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
296 Setting viewMARC to 1