Bug 18432: code comments assume male gender
[koha.git] / installer / install.pl
index 687d35e..8e44dea 100755 (executable)
@@ -4,15 +4,18 @@ use strict;
 use warnings;
 use diagnostics;
 
-use InstallAuth;
-use CGI;
-use IPC::Cmd;
+use C4::InstallAuth;
+use CGI qw ( -utf8 );
+use POSIX qw(strftime);
 
 use C4::Context;
 use C4::Output;
+use C4::Templates;
 use C4::Languages qw(getAllLanguages getTranslatedLanguages);
 use C4::Installer;
 
+use Koha;
+
 my $query = new CGI;
 my $step  = $query->param('step');
 
@@ -22,11 +25,11 @@ my ( $template, $loggedinuser, $cookie );
 my $all_languages = getAllLanguages();
 
 if ( defined($language) ) {
-    setlanguagecookie( $query, $language, "install.pl?step=1" );
+    C4::Templates::setlanguagecookie( $query, $language, "install.pl?step=1" );
 }
 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {
-        template_name => "installer/step" . ( $step ? $step : 1 ) . ".tmpl",
+        template_name => "installer/step" . ( $step ? $step : 1 ) . ".tt",
         query         => $query,
         type          => "intranet",
         authnotrequired => 0,
@@ -45,25 +48,36 @@ $info{'hostname'} = C4::Context->config("hostname");
 $info{'port'}     = C4::Context->config("port");
 $info{'user'}     = C4::Context->config("user");
 $info{'password'} = C4::Context->config("pass");
+$info{'tls'} = C4::Context->config("tls");
+    if ($info{'tls'} && $info{'tls'} eq 'yes'){
+        $info{'ca'} = C4::Context->config('ca');
+        $info{'cert'} = C4::Context->config('cert');
+        $info{'key'} = C4::Context->config('key');
+        $info{'tlsoptions'} = ";mysql_ssl=1;mysql_ssl_client_key=".$info{key}.";mysql_ssl_client_cert=".$info{cert}.";mysql_ssl_ca_file=".$info{ca};
+        $info{'tlscmdline'} =  " --ssl-cert ". $info{cert} . " --ssl-key " . $info{key} . " --ssl-ca ".$info{ca}." "
+    }
+
 my $dbh = DBI->connect(
     "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
-      . ( $info{port} ? ";port=$info{port}" : "" ),
+      . ( $info{port} ? ";port=$info{port}" : "" )
+      . ( $info{tlsoptions} ? $info{tlsoptions} : "" ),
     $info{'user'}, $info{'password'}
 );
 
 if ( $step && $step == 1 ) {
-    #First Step
+    #First Step (for both fresh installations and upgrades)
     #Checking ALL perl Modules and services needed are installed.
     #Whenever there is an error, adding a report to the page
+    my $op = $query->param('op') || 'noop';
     $template->param( language => 1 );
     $template->param( 'checkmodule' => 1 ); # we start with the assumption that there are no problems and set this to 0 if there are
 
-    unless ( $] >= 5.008008 ) {    # Bug 4505
+    unless ( $] >= 5.010000 ) {    # Bug 7375
         $template->param( problems => 1, perlversion => 1, checkmodule => 0 );
     }
 
     my $perl_modules = C4::Installer::PerlModules->new;
-    $perl_modules->version_info;
+    $perl_modules->versions_info;
 
     my $modules = $perl_modules->get_attr('missing_pm');
     if (scalar(@$modules)) {
@@ -83,7 +97,7 @@ if ( $step && $step == 1 ) {
             );
         }
         @components = sort {$a->{'name'} cmp $b->{'name'}} @components;
-        $template->param( missing_modules => \@components, checkmodule => $checkmodule );
+        $template->param( missing_modules => \@components, checkmodule => $checkmodule, op => $op );
     }
 }
 elsif ( $step && $step == 2 ) {
@@ -184,11 +198,10 @@ elsif ( $step && $step == 3 ) {
         # we have finished, just redirect to mainpage.
         #
         print $query->redirect("/cgi-bin/koha/mainpage.pl");
-        exit 1;
+        exit;
     }
     elsif ( $op && $op eq 'finish' ) {
         $installer->set_version_syspref();
-        $installer->set_indexing_engine(0); # use Zebra
 
         # Installation is finished.
         # We just deny anybody access to install
@@ -206,12 +219,14 @@ elsif ( $step && $step == 3 ) {
             "fwklanguage" => $fwk_language,
             "list"        => $list
         );
+        use Koha::SearchEngine::Elasticsearch;
+        Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
         $template->param( "$op" => 1 );
     }
     elsif ( $op && $op eq 'selectframeworks' ) {
         #
         #
-        # 1ST install, 2nd sub-step : show the user the sql datas he can insert in the database.
+        # 1ST install, 2nd sub-step : show the user the sql datas he or she can insert in the database.
         #
         #
         # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
@@ -228,6 +243,7 @@ elsif ( $step && $step == 3 ) {
         # Framework Selection is achieved through checking boxes.
         my $langchoice = $query->param('fwklanguage');
         $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
+       $langchoice =~ s/[^a-zA-Z_-]*//g;
         my $marcflavour = $query->param('marcflavour');
         if ($marcflavour){
             $installer->set_marcflavour_syspref($marcflavour);
@@ -240,7 +256,7 @@ elsif ( $step && $step == 3 ) {
         $template->param( "frameworksloop" => $fwklist );
         $template->param( "marcflavour" => ucfirst($marcflavour));
 
-        my ($sample_defaulted_to_en, $levellist) = $installer->sample_data_sql_list($langchoice, $marcflavour);
+        my ($sample_defaulted_to_en, $levellist) = $installer->sample_data_sql_list($langchoice);
         $template->param( "en_sample_data" => $sample_defaulted_to_en);
         $template->param( "levelloop" => $levellist );
         $template->param( "$op"       => 1 );
@@ -265,6 +281,7 @@ elsif ( $step && $step == 3 ) {
         # Marcflavour Selection is achieved through radiobuttons.
         my $langchoice = $query->param('fwklanguage');
         $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
+       $langchoice =~ s/[^a-zA-Z_-]*//g;
         my $dir =
           C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice/marcflavour";
         unless (opendir( MYDIR, $dir )) {
@@ -309,32 +326,62 @@ elsif ( $step && $step == 3 ) {
         # Not 1st install, the only sub-step : update database
         #
         #Do updatedatabase And report
-        my $cmd = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl";
-        my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) = IPC::Cmd::run(command => $cmd, verbose => 0);
 
-        if (@$stdout_buf) {
-            $template->param(update_report => [ map { { line => $_ } } split(/\n/, join('', @$stdout_buf)) ] );
-            $template->param(has_update_succeeds => 1);
+        if ( ! defined $ENV{PERL5LIB} ) {
+            my $find = "C4/Context.pm";
+            my $path = $INC{$find};
+            $path =~ s/\Q$find\E//;
+            $ENV{PERL5LIB} = "$path:$path/installer";
+            warn "# plack? inserted PERL5LIB $ENV{PERL5LIB}\n";
+        }
+
+        my $now = POSIX::strftime( "%Y-%m-%dT%H:%M:%S", localtime() );
+        my $logdir = C4::Context->config('logdir');
+        my $dbversion = C4::Context->preference('Version');
+        my $kohaversion = Koha::version;
+        $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
+
+        my $filename_suffix = join '_', $now, $dbversion, $kohaversion;
+        my ( $logfilepath, $logfilepath_errors ) = ( chk_log($logdir, "updatedatabase_$filename_suffix"), chk_log($logdir, "updatedatabase-error_$filename_suffix") );
+
+        my $cmd = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl >> $logfilepath 2>> $logfilepath_errors";
+
+        system($cmd );
+
+        my $fh;
+        open( $fh, "<", $logfilepath ) or die "Cannot open log file $logfilepath: $!";
+        my @report = <$fh>;
+        close $fh;
+        if (@report) {
+            $template->param( update_report => [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ] );
+            $template->param( has_update_succeeds => 1 );
+        } else {
+            eval{ `rm $logfilepath` };
         }
-        if (@$stderr_buf) {
-            $template->param(update_errors => [ map { { line => $_ } } split(/\n/, join('', @$stderr_buf)) ] );
-            $template->param(has_update_errors => 1);
+        open( $fh, "<", $logfilepath_errors ) or die "Cannot open log file $logfilepath_errors: $!";
+        @report = <$fh>;
+        close $fh;
+        if (@report) {
+            $template->param( update_errors => [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ] );
+            $template->param( has_update_errors => 1 );
             warn "The following errors were returned while attempting to run the updatedatabase.pl script:\n";
-            foreach my $line (@$stderr_buf) {warn "$line\n";}
+            foreach my $line (@report) { warn "$line\n"; }
+        } else {
+            eval{ `rm $logfilepath_errors` };
         }
-
         $template->param( $op => 1 );
     }
     else {
         #
-        # check wether it's a 1st install or an update
+        # check whether it's a 1st install or an update
         #
         #Check if there are enough tables.
         # Paul has cleaned up tables so reduced the count
         #I put it there because it implied a data import if condition was not satisfied.
         my $dbh = DBI->connect(
                "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
-               . ( $info{port} ? ";port=$info{port}" : "" ),
+               . ( $info{port} ? ";port=$info{port}" : "" )
+                . ( $info{tlsoptions} ? $info{tlsoptions} : "" ),
                $info{'user'}, $info{'password'}
         );
        my $rq;
@@ -367,12 +414,10 @@ elsif ( $step && $step == 3 ) {
                 $dbversion = "$1.$2.$3.$4";
                 $template->param("upgrading" => 1,
                                 "dbversion" => $dbversion,
-                                "kohaversion" => C4::Context->KOHAVERSION,
+                                "kohaversion" => Koha::version(),
                                 );
             }
         }
-
-        $dbh->disconnect;
     }
 }
 else {
@@ -388,10 +433,22 @@ else {
         if ( $rq->execute ) {
             my ($version) = $rq->fetchrow;
             if ($version) {
-                $query->redirect("install.pl?step=3");
-                               exit;
+                print $query->redirect("/cgi-bin/koha/installer/install.pl?step=3");
+                exit;
             }
         }
     }
 }
 output_html_with_http_headers $query, $cookie, $template->output;
+
+sub chk_log { #returns a logfile in $dir or - if that failed - in temp dir
+    my ($dir, $name) = @_;
+    my $fn=$dir.'/'.$name.'.log';
+    if( ! open my $fh, '>', $fn ) {
+        $name.= '_XXXX';
+        require File::Temp;
+        ($fh, $fn)= File::Temp::tempfile( $name, TMPDIR => 1, SUFFIX => '.log');
+        #if this should not work, let croak take over
+    }
+    return $fn;
+}