3 # Copyright (C) 2008 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
33 my $installer = C4::Installer->new();
35 my $all_languages = getAllLanguages();
37 my $error = $installer->load_db_schema();
39 my $list = $installer->sql_file_list('en', 'marc21', { optional => 1, mandatory => 1 });
41 my ($fwk_language, $error_list) = $installer->load_sql_in_order($all_languages, @$list);
43 $installer->set_version_syspref();
45 $installer->set_marcflavour_syspref('MARC21');
47 $installer->set_indexing_engine(0);
57 my $installer = C4::Installer->new();
61 Creates a new installer.
70 # get basic information from context
71 $self->{'dbname'} = C4::Context->config("database");
72 $self->{'dbms'} = C4::Context->config("db_scheme") ? C4::Context->config("db_scheme") : "mysql";
73 $self->{'hostname'} = C4::Context->config("hostname");
74 $self->{'port'} = C4::Context->config("port");
75 $self->{'user'} = C4::Context->config("user");
76 $self->{'password'} = C4::Context->config("pass");
77 $self->{'dbh'} = DBI->connect("DBI:$self->{dbms}:dbname=$self->{dbname};host=$self->{hostname}" .
78 ( $self->{port} ? ";port=$self->{port}" : "" ),
79 $self->{'user'}, $self->{'password'});
80 $self->{'language'} = undef;
81 $self->{'marcflavour'} = undef;
88 =head2 marc_framework_sql_list
92 my ($defaulted_to_en, $list) = $installer->marc_framework_sql_list($lang, $marcflavour);
96 Returns in C<$list> a structure listing the filename, description, section,
97 and mandatory/optional status of MARC framework scripts available for C<$lang>
100 If the C<$defaulted_to_en> return value is true, no scripts are available
101 for language C<$lang> and the 'en' ones are returned.
105 sub marc_framework_sql_list {
108 my $marcflavour = shift;
110 my $defaulted_to_en = 0;
113 my $dir = C4::Context->config('intranetdir') . "/installer/data/$self->{dbms}/$lang/marcflavour/".lc($marcflavour);
114 unless (opendir( MYDIR, $dir )) {
116 warn "cannot open MARC frameworks directory $dir";
118 # if no translated MARC framework is available,
120 $dir = C4::Context->config('intranetdir') . "/installer/data/$self->{dbms}/en/marcflavour/".lc($marcflavour);
121 opendir(MYDIR, $dir) or warn "cannot open English MARC frameworks directory $dir";
122 $defaulted_to_en = 1;
125 my @listdir = sort grep { !/^\.|marcflavour/ && -d "$dir/$_" } readdir(MYDIR);
129 my $request = $self->{'dbh'}->prepare("SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'");
131 my ($frameworksloaded) = $request->fetchrow;
132 $frameworksloaded = '' unless defined $frameworksloaded; # avoid warning
133 my %frameworksloaded;
134 foreach ( split( /\|/, $frameworksloaded ) ) {
135 $frameworksloaded{$_} = 1;
138 foreach my $requirelevel (@listdir) {
139 opendir( MYDIR, "$dir/$requirelevel" );
140 my @listname = grep { !/^\./ && -f "$dir/$requirelevel/$_" && $_ =~ m/\.sql$/ } readdir(MYDIR);
145 my $name = substr( $_, 0, -4 );
146 open FILE, "<:utf8","$dir/$requirelevel/$name.txt";
148 $lines =~ s/\n|\r/<br \/>/g;
150 utf8::encode($lines) unless ( utf8::is_utf8($lines) );
151 my $mandatory = ($requirelevel =~ /(mandatory|requi|oblig|necess)/i);
155 'fwkfile' => "$dir/$requirelevel/$_",
156 'fwkdescription' => $lines,
157 'checked' => ( ( $frameworksloaded{$_} || $mandatory ) ? 1 : 0 ),
158 'mandatory' => $mandatory,
162 sort { $a->{'fwkname'} cmp $b->{'fwkname'} } @frameworklist;
164 $cell{"frameworks"} = \@fwks;
165 $cell{"label"} = ucfirst($requirelevel);
166 $cell{"code"} = lc($requirelevel);
167 push @fwklist, \%cell;
170 return ($defaulted_to_en, \@fwklist);
173 =head2 sample_data_sql_list
177 my ($defaulted_to_en, $list) = $installer->sample_data_sql_list($lang);
181 Returns in C<$list> a structure listing the filename, description, section,
182 and mandatory/optional status of sample data scripts available for C<$lang>.
183 If the C<$defaulted_to_en> return value is true, no scripts are available
184 for language C<$lang> and the 'en' ones are returned.
188 sub sample_data_sql_list {
192 my $defaulted_to_en = 0;
195 my $dir = C4::Context->config('intranetdir') . "/installer/data/$self->{dbms}/$lang";
196 unless (opendir( MYDIR, $dir )) {
198 warn "cannot open sample data directory $dir";
200 # if no sample data is available,
202 $dir = C4::Context->config('intranetdir') . "/installer/data/$self->{dbms}/en";
203 opendir(MYDIR, $dir) or warn "cannot open English sample data directory $dir";
204 $defaulted_to_en = 1;
207 my @listdir = sort grep { !/^\.|marcflavour/ && -d "$dir/$_" } readdir(MYDIR);
211 my $request = $self->{'dbh'}->prepare("SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'");
213 my ($frameworksloaded) = $request->fetchrow;
214 $frameworksloaded = '' unless defined $frameworksloaded; # avoid warning
215 my %frameworksloaded;
216 foreach ( split( /\|/, $frameworksloaded ) ) {
217 $frameworksloaded{$_} = 1;
220 foreach my $requirelevel (@listdir) {
221 opendir( MYDIR, "$dir/$requirelevel" );
222 my @listname = grep { !/^\./ && -f "$dir/$requirelevel/$_" && $_ =~ m/\.sql$/ } readdir(MYDIR);
227 my $name = substr( $_, 0, -4 );
228 open FILE, "<:utf8","$dir/$requirelevel/$name.txt";
230 $lines =~ s/\n|\r/<br \/>/g;
232 utf8::encode($lines) unless ( utf8::is_utf8($lines) );
233 my $mandatory = ($requirelevel =~ /(mandatory|requi|oblig|necess)/i);
237 'fwkfile' => "$dir/$requirelevel/$_",
238 'fwkdescription' => $lines,
239 'checked' => ( ( $frameworksloaded{$_} || $mandatory ) ? 1 : 0 ),
240 'mandatory' => $mandatory,
243 my @fwks = sort { $a->{'fwkname'} cmp $b->{'fwkname'} } @frameworklist;
245 $cell{"frameworks"} = \@fwks;
246 $cell{"label"} = ucfirst($requirelevel);
247 $cell{"code"} = lc($requirelevel);
248 push @levellist, \%cell;
251 return ($defaulted_to_en, \@levellist);
258 my $list = $installer->sql_file_list($lang, $marcflavour, $subset_wanted);
262 Returns an arrayref containing the filepaths of installer SQL scripts
263 available for laod. The C<$lang> and C<$marcflavour> arguments
264 specify the desired language and MARC flavour. while C<$subset_wanted>
265 is a hashref containing possible named parameters 'mandatory' and 'optional'.
272 my $marcflavour = shift;
273 my $subset_wanted = shift;
275 my ($marc_defaulted_to_en, $marc_sql) = $self->marc_framework_sql_list($lang, $marcflavour);
276 my ($sample_defaulted_to_en, $sample_sql) = $self->sample_data_sql_list($lang);
281 if ($subset_wanted->{'mandatory'}) {
282 push @sql_list, $_->{'fwkfile'} if $_->{'mandatory'};
284 if ($subset_wanted->{'optional'}) {
285 push @sql_list, $_->{'fwkfile'} unless $_->{'mandatory'};
287 } @{ $_->{'frameworks'} }
288 } (@$marc_sql, @$sample_sql);
293 =head2 load_db_schema
297 my $error = $installer->load_db_schema();
301 Loads the SQL script that creates Koha's tables and indexes. The
302 return value is a string containing error messages reported by the
310 my $datadir = C4::Context->config('intranetdir') . "/installer/data/$self->{dbms}";
311 my $error = $self->load_sql("$datadir/kohastructure.sql");
316 =head2 load_sql_in_order
320 my ($fwk_language, $list) = $installer->load_sql_in_order($all_languages, @sql_list);
324 Given a list of SQL scripts supplied in C<@sql_list>, loads each of them
325 into the database and sets the FrameworksLoaded system preference to names
326 of the scripts that were loaded.
328 The SQL files are loaded in alphabetical order by filename (not including
329 directory path). This means that dependencies among the scripts are to
330 be resolved by carefully naming them, keeping in mind that the directory name
331 does *not* currently count.
333 FIXME: this is a rather delicate way of dealing with dependencies between
336 The return value C<$list> is an arrayref containing a hashref for each
337 "level" or directory containing SQL scripts; the hashref in turns contains
338 a list of hashrefs containing a list of each script load and any error
339 messages associated with the loading of each script.
341 FIXME: The C<$fwk_language> code probably doesn't belong and needs to be
342 moved to a different method.
346 sub load_sql_in_order {
348 my $all_languages = shift;
354 my @aa = split /\/|\\/, ($a);
355 my @bb = split /\/|\\/, ($b);
358 my $request = $self->{'dbh'}->prepare( "SELECT value FROM systempreferences WHERE variable='FrameworksLoaded'" );
360 my ($systempreference) = $request->fetchrow;
361 $systempreference = '' unless defined $systempreference; # avoid warning
362 foreach my $file (@fnames) {
365 my $error = $self->load_sql($file);
366 my @file = split qr(\/|\\), $file;
367 $lang = $file[ scalar(@file) - 3 ] unless ($lang);
368 my $level = $file[ scalar(@file) - 2 ];
370 $systempreference .= "$file[scalar(@file)-1]|"
371 unless ( index( $systempreference, $file[ scalar(@file) - 1 ] ) >= 0 );
374 #Bulding here a hierarchy to display files by level.
375 push @{ $hashlevel{$level} },
376 { "fwkname" => $file[ scalar(@file) - 1 ], "error" => $error };
379 #systempreference contains an ending |
380 chop $systempreference;
382 map { push @list, { "level" => $_, "fwklist" => $hashlevel{$_} } } keys %hashlevel;
384 for my $each_language (@$all_languages) {
386 # warn "CODE".$each_language->{'language_code'};
387 # warn "LANG:".$lang;
388 if ( $lang eq $each_language->{'language_code'} ) {
389 $fwk_language = $each_language->{language_locale_name};
394 "UPDATE systempreferences set value=\"$systempreference\" where variable='FrameworksLoaded'"
397 unless ( $updateflag == 1 ) {
399 "INSERT INTO systempreferences (value, variable, explanation, type) VALUES (\"$systempreference\",'FrameworksLoaded','Frameworks loaded through webinstaller','choice')";
400 my $rq = $self->{'dbh'}->prepare($string);
403 return ($fwk_language, \@list);
406 =head2 set_marcflavour_syspref
410 $installer->set_marcflavour_syspref($marcflavour);
414 Set the 'marcflavour' system preference. The incoming
415 C<$marcflavour> references to a subdirectory of
416 installer/data/$dbms/$lang/marcflavour, and is
417 normalized to MARC21 or UNIMARC.
419 FIXME: this method assumes that the MARC flavour will be either
424 sub set_marcflavour_syspref {
426 my $marcflavour = shift;
428 # we can have some variants of marc flavour, by having different directories, like : unimarc_small and unimarc_full, for small and complete unimarc frameworks.
429 # marc_cleaned finds the marcflavour, without the variant.
430 my $marc_cleaned = 'MARC21';
431 $marc_cleaned = 'UNIMARC' if $marcflavour =~ /unimarc/i;
433 $self->{'dbh'}->prepare(
434 "INSERT IGNORE INTO `systempreferences` (variable,value,explanation,options,type) VALUES('marcflavour','$marc_cleaned','Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice');"
439 =head2 set_indexing_engine
443 $installer->set_indexing_engine($nozebra);
447 Sets system preferences related to the indexing
448 engine. The C<$nozebra> argument is a boolean;
449 if true, turn on NoZebra mode and turn off QueryFuzzy,
450 QueryWeightFields, and QueryStemming. If false, turn
451 off NoZebra mode (i.e., use the Zebra search engine).
455 sub set_indexing_engine {
460 $self->{'dbh'}->do("UPDATE systempreferences SET value=1 WHERE variable='NoZebra'");
461 $self->{'dbh'}->do("UPDATE systempreferences SET value=0 WHERE variable in ('QueryFuzzy','QueryWeightFields','QueryStemming')");
463 $self->{'dbh'}->do("UPDATE systempreferences SET value=0 WHERE variable='NoZebra'");
468 =head2 set_version_syspref
472 $installer->set_version_syspref();
476 Set or update the 'Version' system preference to the current
477 Koha software version.
481 sub set_version_syspref {
484 my $kohaversion=C4::Context::KOHAVERSION;
485 # remove the 3 last . to have a Perl number
486 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
487 if (C4::Context->preference('Version')) {
488 warn "UPDATE Version";
489 my $finish=$self->{'dbh'}->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
490 $finish->execute($kohaversion);
492 warn "INSERT Version";
493 my $finish=$self->{'dbh'}->prepare("INSERT into systempreferences (variable,value,explanation) values ('Version',?,'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller')");
494 $finish->execute($kohaversion);
502 my $error = $installer->load_sql($filename);
506 Runs a the specified SQL using the DB's command-line
507 SQL tool, and returns any strings sent to STDERR
508 by the command-line tool.
510 FIXME: there has been a long-standing desire to
511 replace this with an SQL loader that goes
512 through DBI; partly for portability issues
513 and partly to improve error handling.
515 FIXME: even using the command-line loader, some more
516 basic error handling should be added - deal
517 with missing files, e.g.
523 my $filename = shift;
525 my $datadir = C4::Context->config('intranetdir') . "/installer/data/$self->{dbms}";
528 if ( $self->{dbms} eq 'mysql' ) {
530 . ( $self->{hostname} ? " -h $self->{hostname} " : "" )
531 . ( $self->{port} ? " -P $self->{port} " : "" )
532 . ( $self->{user} ? " -u $self->{user} " : "" )
533 . ( $self->{password} ? " -p'$self->{password}'" : "" )
534 . " $self->{dbname} ";
535 $error = qx($strcmd <$filename 2>&1 1>/dev/null);
536 } elsif ( $self->{dbms} eq 'Pg' ) {
538 . ( $self->{hostname} ? " -h $self->{hostname} " : "" )
539 . ( $self->{port} ? " -p $self->{port} " : "" )
540 . ( $self->{user} ? " -U $self->{user} " : "" )
541 # . ( $self->{password} ? " -W $self->{password}" : "" ) # psql will NOT accept a password, but prompts...
542 . " $self->{dbname} "; # Therefore, be sure to run 'trust' on localhost in pg_hba.conf -fbcit
543 $error = qx($strcmd -f $filename 2>&1 1>/dev/null);
544 # Be sure to set 'client_min_messages = error' in postgresql.conf
545 # so that only true errors are returned to stderr or else the installer will
546 # report the import a failure although it really succeded -fbcit
553 C4::Installer is a refactoring of logic originally from installer/installer.pl, which was
554 originally written by Henri-Damien Laurant.
556 Koha Developement team <info@koha.org>
558 Galen Charlton <galen.charlton@liblime.com>