Bug 9302: Use patron-title.inc
[koha.git] / installer / install.pl
bloba7aaa28e2f8d83a4f382dd2ecd8e8dc66ce8c97b
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright (C) YEAR YOURNAME-OR-YOUREMPLOYER
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>.
20 use strict;
21 use warnings;
22 use diagnostics;
24 use C4::InstallAuth;
25 use CGI qw ( -utf8 );
26 use POSIX qw(strftime);
28 use C4::Context;
29 use C4::Output;
30 use C4::Templates;
31 use C4::Languages qw(getAllLanguages getTranslatedLanguages);
32 use C4::Installer;
34 use Koha;
36 my $query = new CGI;
37 my $step = $query->param('step');
39 my $language = $query->param('language');
40 my ( $template, $loggedinuser, $cookie );
42 my $all_languages = getAllLanguages();
44 if ( defined($language) ) {
45 C4::Templates::setlanguagecookie( $query, $language, "install.pl?step=1" );
47 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
49 template_name => "installer/step" . ( $step ? $step : 1 ) . ".tt",
50 query => $query,
51 type => "intranet",
52 authnotrequired => 0,
53 debug => 1,
57 my $installer = C4::Installer->new();
58 my %info;
59 $info{'dbname'} = C4::Context->config("database");
60 $info{'dbms'} = (
61 C4::Context->config("db_scheme")
62 ? C4::Context->config("db_scheme")
63 : "mysql"
65 $info{'hostname'} = C4::Context->config("hostname");
66 $info{'port'} = C4::Context->config("port");
67 $info{'user'} = C4::Context->config("user");
68 $info{'password'} = C4::Context->config("pass");
69 $info{'tls'} = C4::Context->config("tls");
70 if ($info{'tls'} && $info{'tls'} eq 'yes'){
71 $info{'ca'} = C4::Context->config('ca');
72 $info{'cert'} = C4::Context->config('cert');
73 $info{'key'} = C4::Context->config('key');
74 $info{'tlsoptions'} = ";mysql_ssl=1;mysql_ssl_client_key=".$info{key}.";mysql_ssl_client_cert=".$info{cert}.";mysql_ssl_ca_file=".$info{ca};
75 $info{'tlscmdline'} = " --ssl-cert ". $info{cert} . " --ssl-key " . $info{key} . " --ssl-ca ".$info{ca}." "
78 my $dbh = DBI->connect(
79 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
80 . ( $info{port} ? ";port=$info{port}" : "" )
81 . ( $info{tlsoptions} ? $info{tlsoptions} : "" ),
82 $info{'user'}, $info{'password'}
85 if ( $step && $step == 1 ) {
87 #First Step (for both fresh installations and upgrades)
88 #Checking ALL perl Modules and services needed are installed.
89 #Whenever there is an error, adding a report to the page
90 my $op = $query->param('op') || 'noop';
91 $template->param( language => 1 );
92 my $checkmodule = 1;
93 $template->param( 'checkmodule' => 1 )
94 ; # we start with the assumption that there are no problems and set this to 0 if there are
96 unless ( $] >= 5.010000 ) { # Bug 7375
97 $template->param( problems => 1, perlversion => 1, checkmodule => 0 );
98 $checkmodule = 0;
101 my $perl_modules = C4::Installer::PerlModules->new;
102 $perl_modules->versions_info;
104 my $modules = $perl_modules->get_attr('missing_pm');
105 if ( scalar(@$modules) ) {
106 my @components = ();
107 foreach (@$modules) {
108 my ( $module, $stats ) = each %$_;
109 $checkmodule = 0 if $stats->{'required'};
110 push(
111 @components,
113 name => $module,
114 version => $stats->{'min_ver'},
115 require => $stats->{'required'},
116 usage => $stats->{'usage'},
120 @components = sort { $a->{'name'} cmp $b->{'name'} } @components;
121 $template->param(
122 missing_modules => \@components,
123 checkmodule => $checkmodule,
124 op => $op
128 elsif ( $step && $step == 2 ) {
130 #STEP 2 Check Database connection and access
132 $template->param(%info);
133 my $checkdb = $query->param("checkdb");
134 $template->param( 'dbconnection' => $checkdb );
135 if ($checkdb) {
136 if ($dbh) {
138 # Can connect to the mysql
139 $template->param( "checkdatabaseaccess" => 1 );
140 if ( $info{dbms} eq "mysql" ) {
142 #Check if database created
143 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
144 if ( $rv == 1 ) {
145 $template->param( 'checkdatabasecreated' => 1 );
148 #Check if user have all necessary grants on this database.
149 my $rq =
150 $dbh->prepare(
151 "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
152 $rq->execute;
153 my $grantaccess;
154 while ( my ($line) = $rq->fetchrow ) {
155 my $dbname = $info{dbname};
156 if ( $line =~ m/^GRANT (.*?) ON `$dbname`\.\*/
157 || index( $line, '*.*' ) > 0 )
159 $grantaccess = 1
160 if (
161 index( $line, 'ALL PRIVILEGES' ) > 0
162 || ( ( index( $line, 'SELECT' ) > 0 )
163 && ( index( $line, 'INSERT' ) > 0 )
164 && ( index( $line, 'UPDATE' ) > 0 )
165 && ( index( $line, 'DELETE' ) > 0 )
166 && ( index( $line, 'CREATE' ) > 0 )
167 && ( index( $line, 'DROP' ) > 0 ) )
171 unless ($grantaccess) {
172 $rq =
173 $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
174 $rq->execute;
175 while ( my ($line) = $rq->fetchrow ) {
176 my $dbname = $info{dbname};
177 if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
179 $grantaccess = 1
180 if (
181 index( $line, 'ALL PRIVILEGES' ) > 0
182 || ( ( index( $line, 'SELECT' ) > 0 )
183 && ( index( $line, 'INSERT' ) > 0 )
184 && ( index( $line, 'UPDATE' ) > 0 )
185 && ( index( $line, 'DELETE' ) > 0 )
186 && ( index( $line, 'CREATE' ) > 0 )
187 && ( index( $line, 'DROP' ) > 0 ) )
192 $template->param( "checkgrantaccess" => $grantaccess );
193 } # End mysql connect check...
195 elsif ( $info{dbms} eq "Pg" ) {
197 # Check if database has been created...
198 my $rv = $dbh->do(
199 "SELECT * FROM pg_catalog.pg_database WHERE datname = \'$info{dbname}\';"
201 if ( $rv == 1 ) {
202 $template->param( 'checkdatabasecreated' => 1 );
205 # Check if user has all necessary grants on this database...
206 my $rq = $dbh->do(
207 "SELECT u.usesuper
208 FROM pg_catalog.pg_user as u
209 WHERE u.usename = \'$info{user}\';"
211 if ( $rq == 1 ) {
212 $template->param( "checkgrantaccess" => 1 );
214 } # End Pg connect check...
216 else {
217 $template->param( "error" => DBI::err, "message" => DBI::errstr );
221 elsif ( $step && $step == 3 ) {
223 # STEP 3 : database setup
225 my $op = $query->param('op');
226 if ( $op && $op eq 'finished' ) {
228 # we have finished, just redirect to mainpage.
230 print $query->redirect("/cgi-bin/koha/mainpage.pl");
231 exit;
233 elsif ( $op && $op eq 'finish' ) {
234 $installer->set_version_syspref();
236 # Installation is finished.
237 # We just deny anybody access to install
238 # And we redirect people to mainpage.
239 # The installer will have to relogin since we do not pass cookie to redirection.
240 $template->param( "$op" => 1 );
243 elsif ( $op && $op eq 'addframeworks' ) {
245 # 1ST install, 3rd sub-step : insert the SQL files the user has selected
247 my ( $fwk_language, $list ) =
248 $installer->load_sql_in_order( $all_languages,
249 $query->multi_param('framework') );
250 $template->param(
251 "fwklanguage" => $fwk_language,
252 "list" => $list
254 use Koha::SearchEngine::Elasticsearch;
255 Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
256 $template->param( "$op" => 1 );
258 elsif ( $op && $op eq 'selectframeworks' ) {
261 # 1ST install, 2nd sub-step : show the user the sql datas they can insert in the database.
264 # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
266 #Framework Selection
267 #sql data for import are supposed to be located in installer/data/<language>/<level>
268 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
269 # Where <level> is a category of requirement : required, recommended optional
270 # level should contain :
271 # SQL File for import With a readable name.
272 # txt File that explains what this SQL File is meant for.
273 # Could be VERY useful to have A Big file for a kind of library.
274 # But could also be useful to have some Authorised values data set prepared here.
275 # Framework Selection is achieved through checking boxes.
276 my $langchoice = $query->param('fwklanguage');
277 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
278 $langchoice =~ s/[^a-zA-Z_-]*//g;
279 my $marcflavour = $query->param('marcflavour');
280 if ($marcflavour) {
281 $installer->set_marcflavour_syspref($marcflavour);
283 $marcflavour = C4::Context->preference('marcflavour')
284 unless ($marcflavour);
286 #Insert into database the selected marcflavour
287 undef $/;
288 my ( $marc_defaulted_to_en, $fwklist ) =
289 $installer->marc_framework_sql_list( $langchoice, $marcflavour );
290 $template->param( 'en_marc_frameworks' => $marc_defaulted_to_en );
291 $template->param( "frameworksloop" => $fwklist );
292 $template->param( "marcflavour" => ucfirst($marcflavour) );
294 my ( $sample_defaulted_to_en, $levellist ) =
295 $installer->sample_data_sql_list($langchoice);
296 $template->param( "en_sample_data" => $sample_defaulted_to_en );
297 $template->param( "levelloop" => $levellist );
298 $template->param( "$op" => 1 );
301 elsif ( $op && $op eq 'choosemarc' ) {
304 # 1ST install, 2nd sub-step : show the user the marcflavour available.
308 #Choose Marc Flavour
309 #sql data are supposed to be located in installer/data/<dbms>/<language>/marcflavour/marcflavourname
310 # Where <dbms> is database type according to DBD syntax
311 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
312 # Where <level> is a category of requirement : required, recommended optional
313 # level should contain :
314 # SQL File for import With a readable name.
315 # txt File taht explains what this SQL File is meant for.
316 # Could be VERY useful to have A Big file for a kind of library.
317 # But could also be useful to have some Authorised values data set prepared here.
318 # Marcflavour Selection is achieved through radiobuttons.
319 my $langchoice = $query->param('fwklanguage');
321 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
322 $langchoice =~ s/[^a-zA-Z_-]*//g;
323 my $dir =
324 C4::Context->config('intranetdir')
325 . "/installer/data/$info{dbms}/$langchoice/marcflavour";
326 unless ( opendir( MYDIR, $dir ) ) {
327 if ( $langchoice eq 'en' ) {
328 warn "cannot open MARC frameworks directory $dir";
330 else {
331 # if no translated MARC framework is available,
332 # default to English
333 $dir = C4::Context->config('intranetdir')
334 . "/installer/data/$info{dbms}/en/marcflavour";
335 opendir( MYDIR, $dir )
336 or warn "cannot open English MARC frameworks directory $dir";
339 my @listdir = grep { !/^\./ && -d "$dir/$_" } readdir(MYDIR);
340 closedir MYDIR;
341 my $marcflavour = C4::Context->preference("marcflavour");
342 my @flavourlist;
343 foreach my $marc (@listdir) {
344 my %cell=(
345 "label"=> ucfirst($marc),
346 "code"=>uc($marc),
347 "checked"=> defined($marcflavour) ? uc($marc) eq $marcflavour : 0);
348 # $cell{"description"}= do { local $/ = undef; open INPUT "<$dir/$marc.txt"||"";<INPUT> };
349 push @flavourlist, \%cell;
351 $template->param( "flavourloop" => \@flavourlist );
352 $template->param( "$op" => 1 );
354 elsif ( $op && $op eq 'importdatastructure' ) {
357 # 1st install, 1st "sub-step" : import kohastructure
360 my $error = $installer->load_db_schema();
361 $template->param(
362 "error" => $error,
363 "$op" => 1,
366 elsif ( $op && $op eq 'updatestructure' ) {
368 # Not 1st install, the only sub-step : update database
370 #Do updatedatabase And report
372 if ( !defined $ENV{PERL5LIB} ) {
373 my $find = "C4/Context.pm";
374 my $path = $INC{$find};
375 $path =~ s/\Q$find\E//;
376 $ENV{PERL5LIB} = "$path:$path/installer";
377 warn "# plack? inserted PERL5LIB $ENV{PERL5LIB}\n";
380 my $now = POSIX::strftime( "%Y-%m-%dT%H:%M:%S", localtime() );
381 my $logdir = C4::Context->config('logdir');
382 my $dbversion = C4::Context->preference('Version');
383 my $kohaversion = Koha::version;
384 $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
386 my $filename_suffix = join '_', $now, $dbversion, $kohaversion;
387 my ( $logfilepath, $logfilepath_errors ) = (
388 chk_log( $logdir, "updatedatabase_$filename_suffix" ),
389 chk_log( $logdir, "updatedatabase-error_$filename_suffix" )
392 my $cmd = C4::Context->config("intranetdir")
393 . "/installer/data/$info{dbms}/updatedatabase.pl >> $logfilepath 2>> $logfilepath_errors";
395 system($cmd );
397 my $fh;
398 open( $fh, "<:encoding(utf-8)", $logfilepath )
399 or die "Cannot open log file $logfilepath: $!";
400 my @report = <$fh>;
401 close $fh;
402 if (@report) {
403 $template->param( update_report =>
404 [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ]
406 $template->param( has_update_succeeds => 1 );
408 else {
409 eval { `rm $logfilepath` };
411 open( $fh, "<:encoding(utf-8)", $logfilepath_errors )
412 or die "Cannot open log file $logfilepath_errors: $!";
413 @report = <$fh>;
414 close $fh;
415 if (@report) {
416 $template->param( update_errors =>
417 [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ]
419 $template->param( has_update_errors => 1 );
420 warn
421 "The following errors were returned while attempting to run the updatedatabase.pl script:\n";
422 foreach my $line (@report) { warn "$line\n"; }
424 else {
425 eval { `rm $logfilepath_errors` };
427 $template->param( $op => 1 );
429 else {
431 # check whether it's a 1st install or an update
433 #Check if there are enough tables.
434 # Paul has cleaned up tables so reduced the count
435 #I put it there because it implied a data import if condition was not satisfied.
436 my $dbh = DBI->connect(
437 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
438 . ( $info{port} ? ";port=$info{port}" : "" )
439 . ( $info{tlsoptions} ? $info{tlsoptions} : "" ),
440 $info{'user'}, $info{'password'}
442 my $rq;
443 if ( $info{dbms} eq 'mysql' ) { $rq = $dbh->prepare("SHOW TABLES"); }
444 elsif ( $info{dbms} eq 'Pg' ) {
445 $rq = $dbh->prepare(
446 "SELECT *
447 FROM information_schema.tables
448 WHERE table_schema='public' and table_type='BASE TABLE';"
451 $rq->execute;
452 my $data = $rq->fetchall_arrayref( {} );
453 my $count = scalar(@$data);
455 # we don't have tables, propose DB import
457 if ( $count < 70 ) {
458 $template->param( "count" => $count, "proposeimport" => 1 );
460 else {
462 # we have tables, propose to select files to upload or updatedatabase
464 $template->param( "count" => $count, "default" => 1 );
466 # 1st part of step 3 : check if there is a databaseversion systempreference
467 # if there is, then we just need to upgrade
468 # if there is none, then we need to install the database
470 if ( C4::Context->preference('Version') ) {
471 my $dbversion = C4::Context->preference('Version');
472 $dbversion =~ /(.*)\.(..)(..)(...)/;
473 $dbversion = "$1.$2.$3.$4";
474 $template->param(
475 "upgrading" => 1,
476 "dbversion" => $dbversion,
477 "kohaversion" => Koha::version(),
483 else {
485 # LANGUAGE SELECTION page by default
486 # using opendir + language Hash
487 my $languages_loop = getTranslatedLanguages('intranet');
488 $template->param( installer_languages_loop => $languages_loop );
489 if ($dbh) {
490 my $rq =
491 $dbh->prepare(
492 "SELECT * from systempreferences WHERE variable='Version'");
493 if ( $rq->execute ) {
494 my ($version) = $rq->fetchrow;
495 if ($version) {
496 print $query->redirect(
497 "/cgi-bin/koha/installer/install.pl?step=3");
498 exit;
503 output_html_with_http_headers $query, $cookie, $template->output;
505 sub chk_log { #returns a logfile in $dir or - if that failed - in temp dir
506 my ( $dir, $name ) = @_;
507 my $fn = $dir . '/' . $name . '.log';
508 if ( !open my $fh, '>', $fn ) {
509 $name .= '_XXXX';
510 require File::Temp;
511 ( $fh, $fn ) =
512 File::Temp::tempfile( $name, TMPDIR => 1, SUFFIX => '.log' );
514 #if this should not work, let croak take over
516 return $fn;