German web installer files, including translation of MARC21 frameworks
[koha.git] / installer / install.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use diagnostics;
6
7 use InstallAuth;
8 use CGI;
9 use IPC::Cmd;
10
11 use C4::Context;
12 use C4::Output;
13 use C4::Languages qw(getAllLanguages getTranslatedLanguages);
14 use C4::Installer;
15
16 my $query = new CGI;
17 my $step  = $query->param('step');
18
19 my $language = $query->param('language');
20 my ( $template, $loggedinuser, $cookie );
21
22 my $all_languages = getAllLanguages();
23
24 if ( defined($language) ) {
25     setlanguagecookie( $query, $language, "install.pl?step=1" );
26 }
27 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
28     {
29         template_name => "installer/step" . ( $step ? $step : 1 ) . ".tmpl",
30         query         => $query,
31         type          => "intranet",
32         authnotrequired => 0,
33         debug           => 1,
34     }
35 );
36
37 my $installer = C4::Installer->new();
38 my %info;
39 $info{'dbname'} = C4::Context->config("database");
40 $info{'dbms'} =
41   (   C4::Context->config("db_scheme")
42     ? C4::Context->config("db_scheme")
43     : "mysql" );
44 $info{'hostname'} = C4::Context->config("hostname");
45 $info{'port'}     = C4::Context->config("port");
46 $info{'user'}     = C4::Context->config("user");
47 $info{'password'} = C4::Context->config("pass");
48 my $dbh = DBI->connect(
49     "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
50       . ( $info{port} ? ";port=$info{port}" : "" ),
51     $info{'user'}, $info{'password'}
52 );
53
54 if ( $step && $step == 1 ) {
55     #First Step
56     #Checking ALL perl Modules and services needed are installed.
57     #Whenever there is an error, adding a report to the page
58     $template->param( language => 1 );
59     $template->param( 'checkmodule' => 1 ); # we start with the assumption that there are no problems and set this to 0 if there are
60
61     unless ( $] >= 5.008008 ) {    # Bug 4505
62         $template->param( problems => 1, perlversion => 1, checkmodule => 0 );
63     }
64
65     my $perl_modules = C4::Installer::PerlModules->new;
66     $perl_modules->version_info;
67
68     my $modules = $perl_modules->get_attr('missing_pm');
69     if (scalar(@$modules)) {
70         my @components = ();
71         my $checkmodule = 1;
72         foreach (@$modules) {
73             my ($module, $stats) = each %$_;
74             $checkmodule = 0 if $stats->{'required'};
75             push(
76                 @components,
77                 {
78                     name    => $module,
79                     version => $stats->{'min_ver'},
80                     require => $stats->{'required'},
81                     usage   => $stats->{'usage'},
82                 }
83             );
84         }
85         @components = sort {$a->{'name'} cmp $b->{'name'}} @components;
86         $template->param( missing_modules => \@components, checkmodule => $checkmodule );
87     }
88 }
89 elsif ( $step && $step == 2 ) {
90 #
91 #STEP 2 Check Database connection and access
92 #
93     $template->param(%info);
94     my $checkdb = $query->param("checkdb");
95     $template->param( 'dbconnection' => $checkdb );
96     if ($checkdb) {
97         if ($dbh) {
98
99             # Can connect to the mysql
100             $template->param( "checkdatabaseaccess" => 1 );
101             if ( $info{dbms} eq "mysql" ) {
102
103                 #Check if database created
104                 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
105                 if ( $rv == 1 ) {
106                     $template->param( 'checkdatabasecreated' => 1 );
107                 }
108
109                 #Check if user have all necessary grants on this database.
110                 my $rq =
111                   $dbh->prepare(
112                     "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
113                 $rq->execute;
114                 my $grantaccess;
115                 while ( my ($line) = $rq->fetchrow ) {
116                     my $dbname = $info{dbname};
117                     if ( $line =~ m/^GRANT (.*?) ON `$dbname`\.\*/ || index( $line, '*.*' ) > 0 ) {
118                         $grantaccess = 1
119                           if (
120                             index( $line, 'ALL PRIVILEGES' ) > 0
121                             || (   ( index( $line, 'SELECT' ) > 0 )
122                                 && ( index( $line, 'INSERT' ) > 0 )
123                                 && ( index( $line, 'UPDATE' ) > 0 )
124                                 && ( index( $line, 'DELETE' ) > 0 )
125                                 && ( index( $line, 'CREATE' ) > 0 )
126                                 && ( index( $line, 'DROP' ) > 0 ) )
127                           );
128                     }
129                 }
130                 unless ($grantaccess) {
131                     $rq =
132                       $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
133                     $rq->execute;
134                     while ( my ($line) = $rq->fetchrow ) {
135                         my $dbname = $info{dbname};
136                         if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
137                         {
138                             $grantaccess = 1
139                               if (
140                                 index( $line, 'ALL PRIVILEGES' ) > 0
141                                 || (   ( index( $line, 'SELECT' ) > 0 )
142                                     && ( index( $line, 'INSERT' ) > 0 )
143                                     && ( index( $line, 'UPDATE' ) > 0 )
144                                     && ( index( $line, 'DELETE' ) > 0 )
145                                     && ( index( $line, 'CREATE' ) > 0 )
146                                     && ( index( $line, 'DROP' ) > 0 ) )
147                               );
148                         }
149                     }
150                 }
151                 $template->param( "checkgrantaccess" => $grantaccess );
152             }   # End mysql connect check...
153
154             elsif ( $info{dbms} eq "Pg" ) {
155                 # Check if database has been created...
156                 my $rv = $dbh->do( "SELECT * FROM pg_catalog.pg_database WHERE datname = \'$info{dbname}\';" );
157                 if ( $rv == 1 ) {
158                         $template->param( 'checkdatabasecreated' => 1 );
159                 }
160
161                 # Check if user has all necessary grants on this database...
162                 my $rq = $dbh->do( "SELECT u.usesuper
163                                     FROM pg_catalog.pg_user as u
164                                     WHERE u.usename = \'$info{user}\';" );
165                 if ( $rq == 1 ) {
166                         $template->param( "checkgrantaccess" => 1 );
167                 }
168             }   # End Pg connect check...
169         }
170         else {
171             $template->param( "error" => DBI::err, "message" => DBI::errstr );
172         }
173     }
174 }
175 elsif ( $step && $step == 3 ) {
176 #
177 #
178 # STEP 3 : database setup
179 #
180 #
181     my $op = $query->param('op');
182     if ( $op && $op eq 'finished' ) {
183         #
184         # we have finished, just redirect to mainpage.
185         #
186         print $query->redirect("/cgi-bin/koha/mainpage.pl");
187         exit 1;
188     }
189     elsif ( $op && $op eq 'finish' ) {
190         $installer->set_version_syspref();
191
192         # Installation is finished.
193         # We just deny anybody access to install
194         # And we redirect people to mainpage.
195         # The installer will have to relogin since we do not pass cookie to redirection.
196         $template->param( "$op" => 1 );
197     }
198     elsif ( $op && $op eq 'SetIndexingEngine' ) {
199         $installer->set_indexing_engine($query->param('NoZebra'));
200         $template->param( "$op" => 1 );
201     }
202     elsif ( $op && $op eq 'addframeworks' ) {
203     #
204     # 1ST install, 3rd sub-step : insert the SQL files the user has selected
205     #
206
207         my ($fwk_language, $list) = $installer->load_sql_in_order($all_languages, $query->param('framework'));
208         $template->param(
209             "fwklanguage" => $fwk_language,
210             "list"        => $list
211         );
212         $template->param( "$op" => 1 );
213     }
214     elsif ( $op && $op eq 'selectframeworks' ) {
215         #
216         #
217         # 1ST install, 2nd sub-step : show the user the sql datas he can insert in the database.
218         #
219         #
220         # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
221
222         #Framework Selection
223         #sql data for import are supposed to be located in installer/data/<language>/<level>
224         # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
225         # Where <level> is a category of requirement : required, recommended optional
226         # level should contain :
227         #   SQL File for import With a readable name.
228         #   txt File that explains what this SQL File is meant for.
229         # Could be VERY useful to have A Big file for a kind of library.
230         # But could also be useful to have some Authorised values data set prepared here.
231         # Framework Selection is achieved through checking boxes.
232         my $langchoice = $query->param('fwklanguage');
233         $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
234         my $marcflavour = $query->param('marcflavour');
235         if ($marcflavour){
236             $installer->set_marcflavour_syspref($marcflavour);
237         };
238         $marcflavour = C4::Context->preference('marcflavour') unless ($marcflavour);
239         #Insert into database the selected marcflavour
240         undef $/;
241         my ($marc_defaulted_to_en, $fwklist) = $installer->marc_framework_sql_list($langchoice, $marcflavour);
242         $template->param('en_marc_frameworks' => $marc_defaulted_to_en);
243         $template->param( "frameworksloop" => $fwklist );
244         $template->param( "marcflavour" => ucfirst($marcflavour));
245
246         my ($sample_defaulted_to_en, $levellist) = $installer->sample_data_sql_list($langchoice, $marcflavour);
247         $template->param( "en_sample_data" => $sample_defaulted_to_en);
248         $template->param( "levelloop" => $levellist );
249         $template->param( "$op"       => 1 );
250     }
251     elsif ( $op && $op eq 'choosemarc' ) {
252         #
253         #
254         # 1ST install, 2nd sub-step : show the user the marcflavour available.
255         #
256         #
257
258         #Choose Marc Flavour
259         #sql data are supposed to be located in installer/data/<dbms>/<language>/marcflavour/marcflavourname
260         # Where <dbms> is database type according to DBD syntax
261         # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
262         # Where <level> is a category of requirement : required, recommended optional
263         # level should contain :
264         #   SQL File for import With a readable name.
265         #   txt File taht explains what this SQL File is meant for.
266         # Could be VERY useful to have A Big file for a kind of library.
267         # But could also be useful to have some Authorised values data set prepared here.
268         # Marcflavour Selection is achieved through radiobuttons.
269         my $langchoice = $query->param('fwklanguage');
270         $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
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";
281             }
282         }
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;
294         }
295         $template->param( "flavourloop" => \@flavourlist );
296         $template->param( "$op"       => 1 );
297     }
298     elsif ( $op && $op eq 'importdatastructure' ) {
299         #
300         #
301         # 1st install, 1st "sub-step" : import kohastructure
302         #
303         #
304         my $error = $installer->load_db_schema();
305         $template->param(
306             "error" => $error,
307             "$op"   => 1,
308         );
309     }
310     elsif ( $op && $op eq 'updatestructure' ) {
311         #
312         # Not 1st install, the only sub-step : update database
313         #
314         #Do updatedatabase And report
315         my $cmd = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl";
316         my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) = IPC::Cmd::run(command => $cmd, verbose => 0);
317
318         if (@$stdout_buf) {
319             $template->param(update_report => [ map { { line => $_ } } split(/\n/, join('', @$stdout_buf)) ] );
320             $template->param(has_update_succeeds => 1);
321         }
322         if (@$stderr_buf) {
323             $template->param(update_errors => [ map { { line => $_ } } split(/\n/, join('', @$stderr_buf)) ] );
324             $template->param(has_update_errors => 1);
325             warn "The following errors were returned while attempting to run the updatedatabase.pl script:\n";
326             foreach my $line (@$stderr_buf) {warn "$line\n";}
327         }
328
329         $template->param( $op => 1 );
330     }
331     else {
332         #
333         # check wether it's a 1st install or an update
334         #
335         #Check if there are enough tables.
336         # Paul has cleaned up tables so reduced the count
337         #I put it there because it implied a data import if condition was not satisfied.
338         my $dbh = DBI->connect(
339                 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
340                 . ( $info{port} ? ";port=$info{port}" : "" ),
341                 $info{'user'}, $info{'password'}
342         );
343         my $rq;
344         if ( $info{dbms} eq 'mysql' ) { $rq = $dbh->prepare( "SHOW TABLES" ); }
345         elsif ( $info{dbms} eq 'Pg' ) { $rq = $dbh->prepare( "SELECT *
346                                                                 FROM information_schema.tables
347                                                                 WHERE table_schema='public' and table_type='BASE TABLE';" ); }
348         $rq->execute;
349         my $data = $rq->fetchall_arrayref( {} );
350         my $count = scalar(@$data);
351         #
352         # we don't have tables, propose DB import
353         #
354         if ( $count < 70 ) {
355             $template->param( "count" => $count, "proposeimport" => 1 );
356         }
357         else {
358             #
359             # we have tables, propose to select files to upload or updatedatabase
360             #
361             $template->param( "count" => $count, "default" => 1 );
362             #
363             # 1st part of step 3 : check if there is a databaseversion systempreference
364             # if there is, then we just need to upgrade
365             # if there is none, then we need to install the database
366             #
367             if (C4::Context->preference('Version')) {
368                 my $dbversion = C4::Context->preference('Version');
369                 $dbversion =~ /(.*)\.(..)(..)(...)/;
370                 $dbversion = "$1.$2.$3.$4";
371                 $template->param("upgrading" => 1,
372                                 "dbversion" => $dbversion,
373                                 "kohaversion" => C4::Context->KOHAVERSION,
374                                 );
375             }
376         }
377
378         $dbh->disconnect;
379     }
380 }
381 else {
382
383     # LANGUAGE SELECTION page by default
384     # using opendir + language Hash
385     my $languages_loop = getTranslatedLanguages('intranet');
386     $template->param( installer_languages_loop => $languages_loop );
387     if ($dbh) {
388         my $rq =
389           $dbh->prepare(
390             "SELECT * from systempreferences WHERE variable='Version'");
391         if ( $rq->execute ) {
392             my ($version) = $rq->fetchrow;
393             if ($version) {
394                 $query->redirect("install.pl?step=3");
395                                 exit;
396             }
397         }
398     }
399 }
400 output_html_with_http_headers $query, $cookie, $template->output;