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>.
30 our %NOT_SET_PREFS = map { $_, 1 } qw( Version );
34 koha-preferences - Get, set, dump and load Koha system preferences
38 misc/admin/koha-preferences COMMAND ...
46 pod2usage( -verbose => 1, -exitval => 1, -output => \*STDERR );
48 pod2usage( -verbose => 1, -exitval => 0 );
55 print STDERR $message . "\n" if ( $C4::Debug::debug );
59 my ( $preference, $value ) = @_;
61 _debug( "Setting $preference to $value" );
63 C4::Context->set_preference( $preference, $value );
67 my $dbh = C4::Context->dbh;
70 map { $_->{'variable'}, $_ }
71 @{ $dbh->selectall_arrayref( "
74 FROM systempreferences
75 ", { Slice => {} } ) }
80 my ( %preferences ) = @_;
82 my $dbh = C4::Context->dbh;
84 # First, a quick check to make sure all of the system preferences exist
85 my $current_state = $dbh->selectall_arrayref( "
88 FROM systempreferences
89 WHERE variable IN (" . join( ', ', map( "?", keys %preferences ) ) . ")
90 ", { Slice => {} }, keys %preferences );
92 exit 2 if ( scalar( @$current_state ) != scalar( keys %preferences ) );
94 # Iterate through again, now that we've checked all of the YesNo sysprefs
96 foreach my $row ( @$current_state ) {
97 next if ( $preferences{$row->{'variable'}} eq $row->{'value'} );
99 _set_preference( $row->{'variable'}, $preferences{$row->{'variable'}} );
102 # FIXME This may be not needed
103 C4::Context->clear_syspref_cache();
106 sub _fetch_preference {
107 my ( $preference ) = @_;
109 my $dbh = C4::Context->dbh;
111 my $row = $dbh->selectrow_hashref( "
113 variable, value, type
114 FROM systempreferences
117 ", {}, $preference );
119 exit 2 unless ( $row );
125 my ( $preference ) = @_;
127 return _fetch_preference( $preference );
131 my ( $preference, $value ) = @_;
133 my $row = _fetch_preference( $preference );
135 exit 3 if ( $value eq $row->{'value'} ); #FIXME exit??
137 _set_preference( $preference, $value );
140 sub ClearPreference {
141 my ( $preference ) = @_;
145 my $row = _fetch_preference( $preference );
147 $value = 0 if ( $row->{'type'} eq 'YesNo' );
149 exit 3 if ( $value eq $row->{'value'} );
151 _set_preference( $preference, $value );
156 COMMAND can be any of the following:
160 =item B<dump> [ -o I<OUTFILE> ]
162 Dump all of Koha's system preferences as a simple YAML mapping into OUTFILE or
165 =item B<load> [ -i I<INFILE> ] [ -f|--force ]
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.
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
176 =item B<get> I<PREFERENCE>
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.
181 =item B<set> I<PREFERENCE> I<VALUE>
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.
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).
190 =item B<clear> I<PREFERENCE>
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'.
197 Print a longer, more detailed manual.
207 ) || _print_usage( 1 );
210 YAML::XS::DumpFile( $outfile, GetPreferences() );
212 print YAML::XS::Dump( Encode::encode_utf8(GetPreferences()) );
216 my ( $infile, $force_version );
220 'f' => \$force_version,
223 my $preferences = YAML::XS::LoadFile($infile || \*STDIN);
225 die "Expected a YAML mapping" if ( ref($preferences) ne 'HASH' );
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'} );
230 map { $_, $preferences->{$_} }
231 grep { !$NOT_SET_PREFS{$_} }
235 SetPreferences( %prefs_to_set );
238 my ( $preference ) = @_;
240 print_usage() unless ( $preference );
242 print GetPreference( $preference ) . "\n";
245 my ( $preference, $value ) = @_;
247 print_usage() unless ( $preference && defined($value) );
249 SetPreference( $preference, $value );
252 my ( $preference ) = @_;
254 print_usage() unless ( $preference );
256 ClearPreference( $preference );
259 pod2usage( -verbose => 2 );
263 print_usage() if ( $ARGV[0] =~ /^(-h|--help|-help|help)$/ );
265 print_usage( 1 ) if ( !$ARGV[0] || ref($commands{$ARGV[0]}) ne 'CODE' );
267 my $command = $commands{$ARGV[0]};
273 Print a short usage message.
279 $ export KOHA_DEBUG=1 # Used here to show what is being stored
280 $ misc/admin/koha-preferences get viewISBD
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
293 Setting viewMARC to 1