|
Lines 1-200
Link Here
|
| 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 Getopt::Long; |
| 23 |
use Pod::Usage; |
| 24 |
|
| 25 |
use Koha::Script; |
| 26 |
use C4::Installer; |
| 27 |
use C4::Context; |
| 28 |
|
| 29 |
use Koha::SearchEngine::Elasticsearch; |
| 30 |
|
| 31 |
=head1 NAME |
| 32 |
|
| 33 |
populate_db.pl - Load included sample data into the DB |
| 34 |
|
| 35 |
=head1 SYNOPSIS |
| 36 |
|
| 37 |
populate_db.pl [--marcflavour MARCFLAVOUR] |
| 38 |
|
| 39 |
Options: |
| 40 |
--help Brief help message |
| 41 |
--marcflavour m Specify the MARC flavour to use (MARC21|UNIMARC). Defaults |
| 42 |
to MARC21. |
| 43 |
-v Be verbose. |
| 44 |
|
| 45 |
=head1 OPTIONS |
| 46 |
|
| 47 |
=over 8 |
| 48 |
|
| 49 |
=item B<--help> |
| 50 |
|
| 51 |
Prints a brief help message and exits. |
| 52 |
|
| 53 |
=item B<--marcflavour> |
| 54 |
|
| 55 |
Lets you choose the desired MARC flavour for the sample data. Valid options are MARC21 and UNIMARC. |
| 56 |
It defaults to MARC21. |
| 57 |
|
| 58 |
=item B<--verbose> |
| 59 |
|
| 60 |
Make the output more verbose. |
| 61 |
|
| 62 |
=back |
| 63 |
|
| 64 |
=cut |
| 65 |
|
| 66 |
my $help; |
| 67 |
my $verbose; |
| 68 |
my $marcflavour = 'MARC21'; |
| 69 |
|
| 70 |
GetOptions( |
| 71 |
'help|?' => \$help, |
| 72 |
'verbose' => \$verbose, |
| 73 |
'marcflavour=s' => \$marcflavour |
| 74 |
) or pod2usage; |
| 75 |
|
| 76 |
if ( $help ) { |
| 77 |
pod2usage; |
| 78 |
} |
| 79 |
|
| 80 |
$marcflavour = uc($marcflavour); |
| 81 |
|
| 82 |
if ( $marcflavour ne 'MARC21' |
| 83 |
and $marcflavour ne 'UNIMARC' ) { |
| 84 |
say "Invalid MARC flavour '$marcflavour' passed."; |
| 85 |
pod2usage; |
| 86 |
} |
| 87 |
|
| 88 |
$ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} = 1; |
| 89 |
my $dbh = C4::Context->dbh; # At the beginning to die if DB does not exist. |
| 90 |
|
| 91 |
my ( $prefs_count ) = $dbh->selectrow_array(q|SELECT COUNT(*) FROM systempreferences|); |
| 92 |
my ( $patrons_count ) = $dbh->selectrow_array(q|SELECT COUNT(*) FROM borrowers|); |
| 93 |
if ( $prefs_count or $patrons_count ) { |
| 94 |
die "Database is not empty!"; |
| 95 |
} |
| 96 |
$dbh->disconnect; |
| 97 |
$ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} = 0; |
| 98 |
|
| 99 |
our $root = C4::Context->config('intranetdir'); |
| 100 |
our $data_dir = "$root/installer/data/mysql"; |
| 101 |
our $installer = C4::Installer->new; |
| 102 |
my $lang = 'en'; |
| 103 |
my $koha_structure_file = "$data_dir/kohastructure.sql"; |
| 104 |
my @sample_files_mandatory = ( |
| 105 |
glob("$data_dir/mandatory/*.sql"), |
| 106 |
"$data_dir/audio_alerts.sql", |
| 107 |
"$data_dir/sysprefs.sql", |
| 108 |
"$data_dir/userflags.sql", |
| 109 |
"$data_dir/userpermissions.sql", |
| 110 |
"$data_dir/account_offset_types.sql", |
| 111 |
"$data_dir/account_debit_types.sql", |
| 112 |
); |
| 113 |
my @sample_lang_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/mandatory/*.sql" ); |
| 114 |
my @sample_lang_files_optional = ( glob $root . "/installer/data/mysql/$lang/optional/*.sql" ); |
| 115 |
my @marc21_sample_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/marcflavour/marc21/*/*.sql" ); |
| 116 |
my @unimarc_sample_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/marcflavour/unimarc/*/*.sql" ); |
| 117 |
|
| 118 |
my $version = get_version(); |
| 119 |
|
| 120 |
initialize_data(); |
| 121 |
update_database(); |
| 122 |
|
| 123 |
sub initialize_data { |
| 124 |
say "Inserting koha db structure..." |
| 125 |
if $verbose; |
| 126 |
my $error = $installer->load_db_schema; |
| 127 |
die $error if $error; |
| 128 |
|
| 129 |
for my $f (@sample_files_mandatory) { |
| 130 |
execute_sqlfile($f); |
| 131 |
} |
| 132 |
|
| 133 |
for my $f (@sample_lang_files_mandatory) { |
| 134 |
execute_sqlfile($f); |
| 135 |
} |
| 136 |
|
| 137 |
for my $f (@sample_lang_files_optional) { |
| 138 |
execute_sqlfile($f); |
| 139 |
} |
| 140 |
|
| 141 |
if ( $marcflavour eq 'UNIMARC' ) { |
| 142 |
for my $f (@unimarc_sample_files_mandatory) { |
| 143 |
execute_sqlfile($f); |
| 144 |
} |
| 145 |
} else { |
| 146 |
for my $f (@marc21_sample_files_mandatory) { |
| 147 |
execute_sqlfile($f); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
# set marcflavour (MARC21) |
| 152 |
my $dbh = C4::Context->dbh; |
| 153 |
|
| 154 |
say "Setting the MARC flavour on the sysprefs..." |
| 155 |
if $verbose; |
| 156 |
$dbh->do(qq{ |
| 157 |
INSERT INTO `systempreferences` (variable,value,explanation,options,type) |
| 158 |
VALUES ('marcflavour',?,'Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice') |
| 159 |
},undef,$marcflavour); |
| 160 |
|
| 161 |
# set version |
| 162 |
say "Setting Koha version to $version..." |
| 163 |
if $verbose; |
| 164 |
$dbh->do(qq{ |
| 165 |
INSERT INTO systempreferences(variable, value, options, explanation, type) |
| 166 |
VALUES ('Version', '$version', NULL, 'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller', NULL) |
| 167 |
}); |
| 168 |
|
| 169 |
# Initialize ES mappings |
| 170 |
Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings; |
| 171 |
} |
| 172 |
|
| 173 |
sub execute_sqlfile { |
| 174 |
my ($filepath) = @_; |
| 175 |
say "Inserting $filepath..." |
| 176 |
if $verbose; |
| 177 |
my $error = $installer->load_sql($filepath); |
| 178 |
die $error if $error; |
| 179 |
} |
| 180 |
|
| 181 |
sub get_version { |
| 182 |
do $root . '/kohaversion.pl'; |
| 183 |
my $version = kohaversion(); |
| 184 |
$version =~ s/(\d)\.(\d{2})\.(\d{2})\.(\d{3})/$1.$2$3$4/; |
| 185 |
return $version; |
| 186 |
} |
| 187 |
|
| 188 |
sub update_database { |
| 189 |
my $update_db_path = $root . '/installer/data/mysql/updatedatabase.pl'; |
| 190 |
say "Updating database..." |
| 191 |
if $verbose; |
| 192 |
my $file = `cat $update_db_path`; |
| 193 |
$file =~ s/exit;//; |
| 194 |
eval $file; |
| 195 |
if ($@) { |
| 196 |
die "updatedatabase.pl process failed: $@"; |
| 197 |
} else { |
| 198 |
say "updatedatabase.pl process succeeded."; |
| 199 |
} |
| 200 |
} |
| 201 |
- |