11 use C4::Languages qw(getAllLanguages getTranslatedLanguages);
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 setlanguagecookie( $query, $language, "install.pl?step=1" );
28 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
30 template_name => "installer/step" . ( $step ? $step : 1 ) . ".tmpl",
38 my $installer = C4::Installer->new();
40 $info{'dbname'} = C4::Context->config("database");
42 ( C4::Context->config("db_scheme")
43 ? C4::Context->config("db_scheme")
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 ) {
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 );
62 unless ( $] >= 5.006001 ) { # Bug 179
63 $template->param( "problems" => 1, "perlversion" => 1 );
67 # We could here use a special find
69 unless ( eval { require ZOOM } ) {
70 push @missing, { name => "ZOOM" };
72 unless ( eval { require YAML::Syck } ) {
73 push @missing, { name => "YAML::Syck" };
75 unless ( eval { require LWP::Simple } ) {
76 push @missing, { name => "LWP::Simple" };
78 unless ( eval { require XML::Simple } ) {
79 push @missing, { name => "XML::Simple" };
81 unless ( eval { require MARC::File::XML } ) {
82 push @missing, { name => "MARC::File::XML" };
84 unless ( eval { require MARC::File::USMARC } ) {
85 push @missing, { name => "MARC::File::USMARC" };
87 unless ( eval { require DBI } ) {
88 push @missing, { name => "DBI" };
90 unless ( eval { require Date::Manip } ) {
91 push @missing, { name => "Date::Manip" };
93 unless ( eval { require DBD::mysql } ) {
94 push @missing, { name => "DBD::mysql" };
96 unless ( eval { require HTML::Template::Pro } ) {
97 push @missing, { name => "HTML::Template::Pro" };
99 unless ( eval { require Date::Calc } ) {
100 push @missing, { name => "Date::Calc" };
102 unless ( eval { require Digest::MD5 } ) {
103 push @missing, { name => "Digest::MD5" };
105 unless ( eval { require MARC::Record } ) {
106 push @missing, { name => "MARC::Record" };
108 unless ( eval { require Mail::Sendmail } ) {
109 push @missing, { name => "Mail::Sendmail", usagemail => 1 };
111 unless ( eval { require List::MoreUtils } ) {
112 push @missing, { name => "List::MoreUtils" };
114 unless ( eval { require XML::RSS } ) {
115 push @missing, { name => "XML::RSS" };
117 unless ( eval { require CGI::Carp } ) {
118 push @missing, { name => "CGI::Carp" };
122 # The following modules are not mandatory, depends on how the library want to use Koha
123 unless ( eval { require PDF::API2 } ) {
124 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
125 push @missing, { name => "PDF::API2", usagebarcode => 1 };
128 unless ( eval { require GD::Barcorde } ) {
129 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
131 { name => "GD::Barcode", usagebarcode => 1, usagespine => 1 };
134 unless ( eval { require GD } ) {
135 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
137 { name => "GD", usagepatronimages => 1 };
140 unless ( eval { require Graphics::Magick } ) {
141 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
143 { name => "Graphics::Magick", usagepatroncards => 1 };
146 unless ( eval { require Data::Random } ) {
147 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
148 push @missing, { name => "Data::Random", usagebarcode => 1 };
151 unless ( eval { require PDF::Reuse::Barcode } ) {
152 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
153 push @missing, { name => "PDF::Reuse::Barcode", usagebarcode => 1 };
156 unless ( eval { require PDF::Report } ) {
157 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
158 push @missing, { name => "PDF::Report", usagebarcode => 1 };
161 unless ( eval { require Algorithm::CheckDigits } ) {
162 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
163 push @missing, { name => "Algorithm::CheckDigits", usagebarcode => 1 };
166 unless ( eval { require GD::Barcode::UPCE } ) {
167 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
168 push @missing, { name => "GD::Barcode::UPCE", usagepine => 1 };
171 unless ( eval { require Net::LDAP } ) {
172 if ( $#missing >= 0 ) { # only when $#missing >= 0 so this isn't fatal
173 push @missing, { name => "Net::LDAP", usageLDAP => 1 };
176 $template->param( missings => \@missing ) if ( scalar(@missing) > 0 );
177 $template->param( 'checkmodule' => 1 )
178 unless ( scalar(@missing) && $problem );
181 elsif ( $step && $step == 2 ) {
183 #STEP 2 Check Database connection and access
185 $template->param(%info);
186 my $checkdb = $query->param("checkdb");
187 $template->param( 'dbconnection' => $checkdb );
191 # Can connect to the mysql
192 $template->param( "checkdatabaseaccess" => 1 );
193 if ( $info{dbms} eq "mysql" ) {
195 #Check if database created
196 my $rv = $dbh->do("SHOW DATABASES LIKE \'$info{dbname}\'");
198 $template->param( 'checkdatabasecreated' => 1 );
201 #Check if user have all necessary grants on this database.
204 "SHOW GRANTS FOR \'$info{user}\'\@'$info{hostname}'");
207 while ( my ($line) = $rq->fetchrow ) {
208 my $dbname = $info{dbname};
209 if ( $line =~ m/^GRANT (.*?) ON `$dbname`\.\*/ || index( $line, '*.*' ) > 0 ) {
212 index( $line, 'ALL PRIVILEGES' ) > 0
213 || ( ( index( $line, 'SELECT' ) > 0 )
214 && ( index( $line, 'INSERT' ) > 0 )
215 && ( index( $line, 'UPDATE' ) > 0 )
216 && ( index( $line, 'DELETE' ) > 0 )
217 && ( index( $line, 'CREATE' ) > 0 )
218 && ( index( $line, 'DROP' ) > 0 ) )
222 unless ($grantaccess) {
224 $dbh->prepare("SHOW GRANTS FOR \'$info{user}\'\@'\%'");
226 while ( my ($line) = $rq->fetchrow ) {
227 my $dbname = $info{dbname};
228 if ( $line =~ m/$dbname/ || index( $line, '*.*' ) > 0 )
232 index( $line, 'ALL PRIVILEGES' ) > 0
233 || ( ( index( $line, 'SELECT' ) > 0 )
234 && ( index( $line, 'INSERT' ) > 0 )
235 && ( index( $line, 'UPDATE' ) > 0 )
236 && ( index( $line, 'DELETE' ) > 0 )
237 && ( index( $line, 'CREATE' ) > 0 )
238 && ( index( $line, 'DROP' ) > 0 ) )
243 $template->param( "checkgrantaccess" => $grantaccess );
244 } # End mysql connect check...
246 elsif ( $info{dbms} eq "Pg" ) {
247 # Check if database has been created...
248 my $rv = $dbh->do( "SELECT * FROM pg_catalog.pg_database WHERE datname = \'$info{dbname}\';" );
250 $template->param( 'checkdatabasecreated' => 1 );
253 # Check if user has all necessary grants on this database...
254 my $rq = $dbh->do( "SELECT u.usesuper
255 FROM pg_catalog.pg_user as u
256 WHERE u.usename = \'$info{user}\';" );
258 $template->param( "checkgrantaccess" => 1 );
260 } # End Pg connect check...
263 $template->param( "error" => DBI::err, "message" => DBI::errstr );
267 elsif ( $step && $step == 3 ) {
270 # STEP 3 : database setup
273 my $op = $query->param('op');
274 if ( $op && $op eq 'finished' ) {
276 # we have finished, just redirect to mainpage.
278 print $query->redirect("/cgi-bin/koha/mainpage.pl");
281 elsif ( $op && $op eq 'finish' ) {
282 $installer->set_version_syspref();
284 # Installation is finished.
285 # We just deny anybody access to install
286 # And we redirect people to mainpage.
287 # The installer will have to relogin since we do not pass cookie to redirection.
288 $template->param( "$op" => 1 );
290 elsif ( $op && $op eq 'SetIndexingEngine' ) {
291 $installer->set_indexing_engine($query->param('NoZebra'));
292 $template->param( "$op" => 1 );
294 elsif ( $op && $op eq 'addframeworks' ) {
296 # 1ST install, 3rd sub-step : insert the SQL files the user has selected
299 my ($fwk_language, $list) = $installer->load_sql_in_order($all_languages, $query->param('framework'));
301 "fwklanguage" => $fwk_language,
304 $template->param( "$op" => 1 );
306 elsif ( $op && $op eq 'selectframeworks' ) {
309 # 1ST install, 2nd sub-step : show the user the sql datas he can insert in the database.
312 # (note that the term "selectframeworks is not correct. The user can select various files, not only frameworks)
315 #sql data for import are supposed to be located in installer/data/<language>/<level>
316 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
317 # Where <level> is a category of requirement : required, recommended optional
318 # level should contain :
319 # SQL File for import With a readable name.
320 # txt File that explains what this SQL File is meant for.
321 # Could be VERY useful to have A Big file for a kind of library.
322 # But could also be useful to have some Authorised values data set prepared here.
323 # Framework Selection is achieved through checking boxes.
324 my $langchoice = $query->param('fwklanguage');
325 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
326 my $marcflavour = $query->param('marcflavour');
328 $installer->set_marcflavour_syspref($marcflavour);
330 $marcflavour = C4::Context->preference('marcflavour') unless ($marcflavour);
331 #Insert into database the selected marcflavour
333 my ($marc_defaulted_to_en, $fwklist) = $installer->marc_framework_sql_list($langchoice, $marcflavour);
334 $template->param('en_marc_frameworks' => $marc_defaulted_to_en);
335 $template->param( "frameworksloop" => $fwklist );
336 $template->param( "marcflavour" => ucfirst($marcflavour));
338 my ($sample_defaulted_to_en, $levellist) = $installer->sample_data_sql_list($langchoice, $marcflavour);
339 $template->param( "en_sample_data" => $sample_defaulted_to_en);
340 $template->param( "levelloop" => $levellist );
341 $template->param( "$op" => 1 );
343 elsif ( $op && $op eq 'choosemarc' ) {
346 # 1ST install, 2nd sub-step : show the user the marcflavour available.
351 #sql data are supposed to be located in installer/data/<dbms>/<language>/marcflavour/marcflavourname
352 # Where <dbms> is database type according to DBD syntax
353 # Where <language> is en|fr or any international abbreviation (provided language hash is updated... This will be a problem with internationlisation.)
354 # Where <level> is a category of requirement : required, recommended optional
355 # level should contain :
356 # SQL File for import With a readable name.
357 # txt File taht explains what this SQL File is meant for.
358 # Could be VERY useful to have A Big file for a kind of library.
359 # But could also be useful to have some Authorised values data set prepared here.
360 # Marcflavour Selection is achieved through radiobuttons.
361 my $langchoice = $query->param('fwklanguage');
362 $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice);
364 C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/$langchoice/marcflavour";
365 unless (opendir( MYDIR, $dir )) {
366 if ($langchoice eq 'en') {
367 warn "cannot open MARC frameworks directory $dir";
369 # if no translated MARC framework is available,
371 $dir = C4::Context->config('intranetdir') . "/installer/data/$info{dbms}/en/marcflavour";
372 opendir(MYDIR, $dir) or warn "cannot open English MARC frameworks directory $dir";
375 my @listdir = grep { !/^\./ && -d "$dir/$_" } readdir(MYDIR);
377 my $marcflavour=C4::Context->preference("marcflavour");
379 foreach my $marc (@listdir) {
381 "label"=> ucfirst($marc),
383 "checked"=> defined($marcflavour) ? uc($marc) eq $marcflavour : 0);
384 # $cell{"description"}= do { local $/ = undef; open INPUT "<$dir/$marc.txt"||"";<INPUT> };
385 push @flavourlist, \%cell;
387 $template->param( "flavourloop" => \@flavourlist );
388 $template->param( "$op" => 1 );
390 elsif ( $op && $op eq 'importdatastructure' ) {
393 # 1st install, 1st "sub-step" : import kohastructure
396 my $error = $installer->load_db_schema();
402 elsif ( $op && $op eq 'updatestructure' ) {
404 # Not 1st install, the only sub-step : update database
406 #Do updatedatabase And report
407 my $cmd = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl";
408 my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) = IPC::Cmd::run(command => $cmd, verbose => 0);
411 $template->param(update_report => [ map { { line => $_ } } split(/\n/, join('', @$stdout_buf)) ] );
412 $template->param(has_update_succeeds => 1);
415 $template->param(update_errors => [ map { { line => $_ } } split(/\n/, join('', @$stderr_buf)) ] );
416 $template->param(has_update_errors => 1);
417 warn "The following errors were returned while attempting to run the updatedatabase.pl script:\n";
418 foreach my $line (@$stderr_buf) {warn "$line\n";}
421 $template->param( $op => 1 );
425 # check wether it's a 1st install or an update
427 #Check if there are enough tables.
428 # Paul has cleaned up tables so reduced the count
429 #I put it there because it implied a data import if condition was not satisfied.
430 my $dbh = DBI->connect(
431 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
432 . ( $info{port} ? ";port=$info{port}" : "" ),
433 $info{'user'}, $info{'password'}
436 if ( $info{dbms} eq 'mysql' ) { $rq = $dbh->prepare( "SHOW TABLES FROM " . $info{'dbname'} ); }
437 elsif ( $info{dbms} eq 'Pg' ) { $rq = $dbh->prepare( "SELECT *
438 FROM information_schema.tables
439 WHERE table_schema='public' and table_type='BASE TABLE';" ); }
441 my $data = $rq->fetchall_arrayref( {} );
442 my $count = scalar(@$data);
444 # we don't have tables, propose DB import
447 $template->param( "count" => $count, "proposeimport" => 1 );
451 # we have tables, propose to select files to upload or updatedatabase
453 $template->param( "count" => $count, "default" => 1 );
455 # 1st part of step 3 : check if there is a databaseversion systempreference
456 # if there is, then we just need to upgrade
457 # if there is none, then we need to install the database
459 if (C4::Context->preference('Version')) {
460 my $dbversion = C4::Context->preference('Version');
461 $dbversion =~ /(.*)\.(..)(..)(...)/;
462 $dbversion = "$1.$2.$3.$4";
463 $template->param("upgrading" => 1,
464 "dbversion" => $dbversion,
465 "kohaversion" => C4::Context->KOHAVERSION,
475 # LANGUAGE SELECTION page by default
476 # using opendir + language Hash
477 my $languages_loop = getTranslatedLanguages('intranet');
478 $template->param( installer_languages_loop => $languages_loop );
482 "SELECT * from systempreferences WHERE variable='Version'");
483 if ( $rq->execute ) {
484 my ($version) = $rq->fetchrow;
486 $query->redirect("install.pl?step=3");
492 output_html_with_http_headers $query, $cookie, $template->output;