Bug 7857 - [SIGNED-OFF] database upgrade fails with plack
[koha.git] / installer / install.pl
blobd9feac4b4373c634ac8b6cedf0758528938248dc
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
5 use diagnostics;
7 use InstallAuth;
8 use CGI;
9 use IPC::Cmd;
11 use C4::Context;
12 use C4::Output;
13 use C4::Templates;
14 use C4::Languages qw(getAllLanguages getTranslatedLanguages);
15 use C4::Installer;
17 my $query = new CGI;
18 my $step = $query->param('step');
20 my $language = $query->param('language');
21 my ( $template, $loggedinuser, $cookie );
23 my $all_languages = getAllLanguages();
25 if ( defined($language) ) {
26 C4::Templates::setlanguagecookie( $query, $language, "install.pl?step=1" );
28 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
30 template_name => "installer/step" . ( $step ? $step : 1 ) . ".tmpl",
31 query => $query,
32 type => "intranet",
33 authnotrequired => 0,
34 debug => 1,
38 my $installer = C4::Installer->new();
39 my %info;
40 $info{'dbname'} = C4::Context->config("database");
41 $info{'dbms'} =
42 ( C4::Context->config("db_scheme")
43 ? C4::Context->config("db_scheme")
44 : "mysql" );
45 $info{'hostname'} = C4::Context->config("hostname");
46 $info{'port'} = C4::Context->config("port");
47 $info{'user'} = C4::Context->config("user");
48 $info{'password'} = C4::Context->config("pass");
49 my $dbh = DBI->connect(
50 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
51 . ( $info{port} ? ";port=$info{port}" : "" ),
52 $info{'user'}, $info{'password'}
55 if ( $step && $step == 1 ) {
56 #First Step
57 #Checking ALL perl Modules and services needed are installed.
58 #Whenever there is an error, adding a report to the page
59 $template->param( language => 1 );
60 $template->param( 'checkmodule' => 1 ); # we start with the assumption that there are no problems and set this to 0 if there are
62 unless ( $] >= 5.010000 ) { # Bug 7375
63 $template->param( problems => 1, perlversion => 1, checkmodule => 0 );
66 my $perl_modules = C4::Installer::PerlModules->new;
67 $perl_modules->version_info;
69 my $modules = $perl_modules->get_attr('missing_pm');
70 if (scalar(@$modules)) {
71 my @components = ();
72 my $checkmodule = 1;
73 foreach (@$modules) {
74 my ($module, $stats) = each %$_;
75 $checkmodule = 0 if $stats->{'required'};
76 push(
77 @components,
79 name => $module,
80 version => $stats->{'min_ver'},
81 require => $stats->{'required'},
82 usage => $stats->{'usage'},
86 @components = sort {$a->{'name'} cmp $b->{'name'}} @components;
87 $template->param( missing_modules => \@components, checkmodule => $checkmodule );
90 elsif ( $step && $step == 2 ) {
92 #STEP 2 Check Database connection and access
94 $template->param(%info);
95 my $checkdb = $query->param("checkdb");
96 $template->param( 'dbconnection' => $checkdb );
97 if ($checkdb) {
98 if ($dbh) {
100 # Can connect to the mysql
101 $template->param( "checkdatabaseaccess" => 1 );
102 if ( $info{dbms} eq "mysql" ) {
104 #Check if database created
105 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
106 if ( $rv == 1 ) {
107 $template->param( 'checkdatabasecreated' => 1 );
110 #Check if user have all necessary grants on this database.
111 my $rq =
112 $dbh->prepare(
113 "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
114 $rq->execute;
115 my $grantaccess;
116 while ( my ($line) = $rq->fetchrow ) {
117 my $dbname = $info{dbname};
118 if ( $line =~ m/^GRANT (.*?) ON `$dbname`\.\*/ || index( $line, '*.*' ) > 0 ) {
119 $grantaccess = 1
120 if (
121 index( $line, 'ALL PRIVILEGES' ) > 0
122 || ( ( index( $line, 'SELECT' ) > 0 )
123 && ( index( $line, 'INSERT' ) > 0 )
124 && ( index( $line, 'UPDATE' ) > 0 )
125 && ( index( $line, 'DELETE' ) > 0 )
126 && ( index( $line, 'CREATE' ) > 0 )
127 && ( index( $line, 'DROP' ) > 0 ) )
131 unless ($grantaccess) {
132 $rq =
133 $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
134 $rq->execute;
135 while ( my ($line) = $rq->fetchrow ) {
136 my $dbname = $info{dbname};
137 if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
139 $grantaccess = 1
140 if (
141 index( $line, 'ALL PRIVILEGES' ) > 0
142 || ( ( index( $line, 'SELECT' ) > 0 )
143 && ( index( $line, 'INSERT' ) > 0 )
144 && ( index( $line, 'UPDATE' ) > 0 )
145 && ( index( $line, 'DELETE' ) > 0 )
146 && ( index( $line, 'CREATE' ) > 0 )
147 && ( index( $line, 'DROP' ) > 0 ) )
152 $template->param( "checkgrantaccess" => $grantaccess );
153 } # End mysql connect check...
155 elsif ( $info{dbms} eq "Pg" ) {
156 # Check if database has been created...
157 my $rv = $dbh->do( "SELECT * FROM pg_catalog.pg_database WHERE datname = \'$info{dbname}\';" );
158 if ( $rv == 1 ) {
159 $template->param( 'checkdatabasecreated' => 1 );
162 # Check if user has all necessary grants on this database...
163 my $rq = $dbh->do( "SELECT u.usesuper
164 FROM pg_catalog.pg_user as u
165 WHERE u.usename = \'$info{user}\';" );
166 if ( $rq == 1 ) {
167 $template->param( "checkgrantaccess" => 1 );
169 } # End Pg connect check...
171 else {
172 $template->param( "error" => DBI::err, "message" => DBI::errstr );
176 elsif ( $step && $step == 3 ) {
179 # STEP 3 : database setup
182 my $op = $query->param('op');
183 if ( $op && $op eq 'finished' ) {
185 # we have finished, just redirect to mainpage.
187 print $query->redirect("/cgi-bin/koha/mainpage.pl");
188 exit;
190 elsif ( $op && $op eq 'finish' ) {
191 $installer->set_version_syspref();
192 $installer->set_indexing_engine(0); # use Zebra
194 # Installation is finished.
195 # We just deny anybody access to install
196 # And we redirect people to mainpage.
197 # The installer will have to relogin since we do not pass cookie to redirection.
198 $template->param( "$op" => 1 );
200 elsif ( $op && $op eq 'addframeworks' ) {
202 # 1ST install, 3rd sub-step : insert the SQL files the user has selected
205 my ($fwk_language, $list) = $installer->load_sql_in_order($all_languages, $query->param('framework'));
206 $template->param(
207 "fwklanguage" => $fwk_language,
208 "list" => $list
210 $template->param( "$op" => 1 );
212 elsif ( $op && $op eq 'selectframeworks' ) {
215 # 1ST install, 2nd sub-step : show the user the sql datas he can insert in the database.
218 # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
220 #Framework Selection
221 #sql data for import are supposed to be located in installer/data/<language>/<level>
222 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
223 # Where <level> is a category of requirement : required, recommended optional
224 # level should contain :
225 # SQL File for import With a readable name.
226 # txt File that explains what this SQL File is meant for.
227 # Could be VERY useful to have A Big file for a kind of library.
228 # But could also be useful to have some Authorised values data set prepared here.
229 # Framework Selection is achieved through checking boxes.
230 my $langchoice = $query->param('fwklanguage');
231 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
232 $langchoice =~ s/[^a-zA-Z_-]*//g;
233 my $marcflavour = $query->param('marcflavour');
234 if ($marcflavour){
235 $installer->set_marcflavour_syspref($marcflavour);
237 $marcflavour = C4::Context->preference('marcflavour') unless ($marcflavour);
238 #Insert into database the selected marcflavour
239 undef $/;
240 my ($marc_defaulted_to_en, $fwklist) = $installer->marc_framework_sql_list($langchoice, $marcflavour);
241 $template->param('en_marc_frameworks' => $marc_defaulted_to_en);
242 $template->param( "frameworksloop" => $fwklist );
243 $template->param( "marcflavour" => ucfirst($marcflavour));
245 my ($sample_defaulted_to_en, $levellist) = $installer->sample_data_sql_list($langchoice, $marcflavour);
246 $template->param( "en_sample_data" => $sample_defaulted_to_en);
247 $template->param( "levelloop" => $levellist );
248 $template->param( "$op" => 1 );
250 elsif ( $op && $op eq 'choosemarc' ) {
253 # 1ST install, 2nd sub-step : show the user the marcflavour available.
257 #Choose Marc Flavour
258 #sql data are supposed to be located in installer/data/<dbms>/<language>/marcflavour/marcflavourname
259 # Where <dbms> is database type according to DBD syntax
260 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
261 # Where <level> is a category of requirement : required, recommended optional
262 # level should contain :
263 # SQL File for import With a readable name.
264 # txt File taht explains what this SQL File is meant for.
265 # Could be VERY useful to have A Big file for a kind of library.
266 # But could also be useful to have some Authorised values data set prepared here.
267 # Marcflavour Selection is achieved through radiobuttons.
268 my $langchoice = $query->param('fwklanguage');
269 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
270 $langchoice =~ s/[^a-zA-Z_-]*//g;
271 my $dir =
272 C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice/marcflavour";
273 unless (opendir( MYDIR, $dir )) {
274 if ($langchoice eq 'en') {
275 warn "cannot open MARC frameworks directory $dir";
276 } else {
277 # if no translated MARC framework is available,
278 # default to English
279 $dir = C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/en/marcflavour";
280 opendir(MYDIR, $dir) or warn "cannot open English MARC frameworks directory $dir";
283 my @listdir = grep { !/^\./ && -d "$dir/$_" } readdir(MYDIR);
284 closedir MYDIR;
285 my $marcflavour=C4::Context->preference("marcflavour");
286 my @flavourlist;
287 foreach my $marc (@listdir) {
288 my %cell=(
289 "label"=> ucfirst($marc),
290 "code"=>uc($marc),
291 "checked"=> defined($marcflavour) ? uc($marc) eq $marcflavour : 0);
292 # $cell{"description"}= do { local $/ = undef; open INPUT "<$dir/$marc.txt"||"";<INPUT> };
293 push @flavourlist, \%cell;
295 $template->param( "flavourloop" => \@flavourlist );
296 $template->param( "$op" => 1 );
298 elsif ( $op && $op eq 'importdatastructure' ) {
301 # 1st install, 1st "sub-step" : import kohastructure
304 my $error = $installer->load_db_schema();
305 $template->param(
306 "error" => $error,
307 "$op" => 1,
310 elsif ( $op && $op eq 'updatestructure' ) {
312 # Not 1st install, the only sub-step : update database
314 #Do updatedatabase And report
316 if ( ! defined $ENV{PERL5LIB} ) {
317 my $find = "C4/Context.pm";
318 my $path = $INC{$find};
319 $path =~ s/\Q$find\E//;
320 $ENV{PERL5LIB} = "$path:$path/installer";
321 warn "# plack? inserted PERL5LIB $ENV{PERL5LIB}\n";
324 my $cmd = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl";
325 my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) = IPC::Cmd::run(command => $cmd, verbose => 0);
327 if (@$stdout_buf) {
328 $template->param(update_report => [ map { { line => $_ } } split(/\n/, join('', @$stdout_buf)) ] );
329 $template->param(has_update_succeeds => 1);
331 if (@$stderr_buf) {
332 $template->param(update_errors => [ map { { line => $_ } } split(/\n/, join('', @$stderr_buf)) ] );
333 $template->param(has_update_errors => 1);
334 warn "The following errors were returned while attempting to run the updatedatabase.pl script:\n";
335 foreach my $line (@$stderr_buf) {warn "$line\n";}
338 $template->param( $op => 1 );
340 else {
342 # check whether it's a 1st install or an update
344 #Check if there are enough tables.
345 # Paul has cleaned up tables so reduced the count
346 #I put it there because it implied a data import if condition was not satisfied.
347 my $dbh = DBI->connect(
348 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
349 . ( $info{port} ? ";port=$info{port}" : "" ),
350 $info{'user'}, $info{'password'}
352 my $rq;
353 if ( $info{dbms} eq 'mysql' ) { $rq = $dbh->prepare( "SHOW TABLES" ); }
354 elsif ( $info{dbms} eq 'Pg' ) { $rq = $dbh->prepare( "SELECT *
355 FROM information_schema.tables
356 WHERE table_schema='public' and table_type='BASE TABLE';" ); }
357 $rq->execute;
358 my $data = $rq->fetchall_arrayref( {} );
359 my $count = scalar(@$data);
361 # we don't have tables, propose DB import
363 if ( $count < 70 ) {
364 $template->param( "count" => $count, "proposeimport" => 1 );
366 else {
368 # we have tables, propose to select files to upload or updatedatabase
370 $template->param( "count" => $count, "default" => 1 );
372 # 1st part of step 3 : check if there is a databaseversion systempreference
373 # if there is, then we just need to upgrade
374 # if there is none, then we need to install the database
376 if (C4::Context->preference('Version')) {
377 my $dbversion = C4::Context->preference('Version');
378 $dbversion =~ /(.*)\.(..)(..)(...)/;
379 $dbversion = "$1.$2.$3.$4";
380 $template->param("upgrading" => 1,
381 "dbversion" => $dbversion,
382 "kohaversion" => C4::Context->KOHAVERSION,
387 $dbh->disconnect;
390 else {
392 # LANGUAGE SELECTION page by default
393 # using opendir + language Hash
394 my $languages_loop = getTranslatedLanguages('intranet');
395 $template->param( installer_languages_loop => $languages_loop );
396 if ($dbh) {
397 my $rq =
398 $dbh->prepare(
399 "SELECT * from systempreferences WHERE variable='Version'");
400 if ( $rq->execute ) {
401 my ($version) = $rq->fetchrow;
402 if ($version) {
403 $query->redirect("install.pl?step=3");
404 exit;
409 output_html_with_http_headers $query, $cookie, $template->output;