|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Copyright 2016 Koha Development Team |
| 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 |
use Modern::Perl; |
| 21 |
|
| 22 |
use C4::Installer; |
| 23 |
use C4::Context; |
| 24 |
use t::lib::Mocks; |
| 25 |
|
| 26 |
$ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} = 1; |
| 27 |
my $dbh = C4::Context->dbh; # At the beginning to die if DB does not exist. |
| 28 |
|
| 29 |
my ( $prefs_count ) = $dbh->selectrow_array(q|SELECT COUNT(*) FROM systempreferences|); |
| 30 |
my ( $patrons_count ) = $dbh->selectrow_array(q|SELECT COUNT(*) FROM borrowers|); |
| 31 |
if ( $prefs_count or $patrons_count ) { |
| 32 |
die "Database is not empty!"; |
| 33 |
} |
| 34 |
$dbh->disconnect; |
| 35 |
$ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} = 0; |
| 36 |
|
| 37 |
our $root = C4::Context->config('intranetdir'); |
| 38 |
our $data_dir = "$root/installer/data/mysql"; |
| 39 |
our $installer = C4::Installer->new; |
| 40 |
my $lang = 'en'; |
| 41 |
my $koha_structure_file = "$data_dir/kohastructure.sql"; |
| 42 |
my @sample_files_mandatory = ( |
| 43 |
glob("$data_dir/mandatory/*.sql"), |
| 44 |
"$data_dir/audio_alerts.sql", |
| 45 |
"$data_dir/sysprefs.sql", |
| 46 |
"$data_dir/userflags.sql", |
| 47 |
"$data_dir/userpermissions.sql", |
| 48 |
); |
| 49 |
my @sample_lang_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/mandatory/*.sql" ); |
| 50 |
my @sample_lang_files_optional = ( glob $root . "/installer/data/mysql/$lang/optional/*.sql" ); |
| 51 |
my @marc21_sample_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/marcflavour/marc21/*/*.sql" ); |
| 52 |
my @unimarc_sample_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/marcflavour/unimarc/*/*.sql" ); |
| 53 |
|
| 54 |
my $version = get_version(); |
| 55 |
|
| 56 |
initialize_data(); |
| 57 |
update_database(); |
| 58 |
|
| 59 |
sub initialize_data { |
| 60 |
say "Inserting koha db structure..."; |
| 61 |
my $error = $installer->load_db_schema; |
| 62 |
die $error if $error; |
| 63 |
|
| 64 |
for my $f (@sample_files_mandatory) { |
| 65 |
execute_sqlfile($f); |
| 66 |
} |
| 67 |
|
| 68 |
for my $f (@sample_lang_files_mandatory) { |
| 69 |
execute_sqlfile($f); |
| 70 |
} |
| 71 |
|
| 72 |
for my $f (@sample_lang_files_optional) { |
| 73 |
execute_sqlfile($f); |
| 74 |
} |
| 75 |
|
| 76 |
for my $f (@marc21_sample_files_mandatory) { |
| 77 |
execute_sqlfile($f); |
| 78 |
} |
| 79 |
|
| 80 |
# set marcflavour (MARC21) |
| 81 |
my $dbh = C4::Context->dbh; |
| 82 |
$dbh->do( |
| 83 |
q{ |
| 84 |
INSERT INTO `systempreferences` (variable,value,explanation,options,type) |
| 85 |
VALUES ('marcflavour','MARC21','Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice') |
| 86 |
} |
| 87 |
); |
| 88 |
|
| 89 |
# set version |
| 90 |
$dbh->do( |
| 91 |
qq{ |
| 92 |
INSERT INTO systempreferences(variable, value, options, explanation, type) |
| 93 |
VALUES ('Version', '$version', NULL, 'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller', NULL) |
| 94 |
} |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
sub execute_sqlfile { |
| 99 |
my ($filepath) = @_; |
| 100 |
say "Inserting $filepath..."; |
| 101 |
my $error = $installer->load_sql($filepath); |
| 102 |
die $error if $error; |
| 103 |
} |
| 104 |
|
| 105 |
sub get_version { |
| 106 |
do $root . '/kohaversion.pl'; |
| 107 |
my $version = kohaversion(); |
| 108 |
$version =~ s/(\d)\.(\d{2})\.(\d{2})\.(\d{3})/$1.$2$3$4/; |
| 109 |
return $version; |
| 110 |
} |
| 111 |
|
| 112 |
sub update_database { |
| 113 |
my $update_db_path = $root . '/installer/data/mysql/updatedatabase.pl'; |
| 114 |
|
| 115 |
my $file = `cat $update_db_path`; |
| 116 |
$file =~ s/exit;//; |
| 117 |
eval $file; |
| 118 |
if ($@) { |
| 119 |
die "updatedatabase.pl process failed: $@"; |
| 120 |
} else { |
| 121 |
say "updatedatabase.pl process succeeded."; |
| 122 |
} |
| 123 |
} |