Bug 7317: Handle backend absense more gracefuly
[koha-equinox.git] / about.pl
1 #!/usr/bin/perl
2
3 # Copyright Pat Eyler 2003
4 # Copyright Biblibre 2006
5 # Parts Copyright Liblime 2008
6 # Parts Copyright Chris Nighswonger 2010
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use Modern::Perl;
24
25 use CGI qw ( -utf8 );
26 use List::MoreUtils qw/ any /;
27 use LWP::Simple;
28 use XML::Simple;
29 use Config;
30
31 use C4::Output;
32 use C4::Auth;
33 use C4::Context;
34 use C4::Installer;
35
36 use Koha;
37 use Koha::Acquisition::Currencies;
38 use Koha::Patrons;
39 use Koha::Caches;
40 use Koha::Config::SysPrefs;
41 use Koha::Illrequest::Config;
42 use C4::Members::Statistics;
43
44 #use Smart::Comments '####';
45
46 my $query = new CGI;
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48     {
49         template_name   => "about.tt",
50         query           => $query,
51         type            => "intranet",
52         authnotrequired => 0,
53         flagsrequired   => { catalogue => 1 },
54         debug           => 1,
55     }
56 );
57
58 my $perl_path = $^X;
59 if ($^O ne 'VMS') {
60     $perl_path .= $Config{_exe} unless $perl_path =~ m/$Config{_exe}$/i;
61 }
62
63 my $zebraVersion = `zebraidx -V`;
64
65 # Check running PSGI env
66 if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
67     $template->param(
68         is_psgi => 1,
69         psgi_server => ($ENV{ PLACK_ENV }) ? "Plack ($ENV{PLACK_ENV})" :
70                        ($ENV{ MOD_PERL })  ? "mod_perl ($ENV{MOD_PERL})" :
71                                              'Unknown'
72     );
73 }
74
75 # Memcached configuration
76 my $memcached_servers   = $ENV{MEMCACHED_SERVERS} || C4::Context->config('memcached_servers');
77 my $memcached_namespace = $ENV{MEMCACHED_NAMESPACE} || C4::Context->config('memcached_namespace') // 'koha';
78
79 my $cache = Koha::Caches->get_instance;
80 my $effective_caching_method = ref($cache->cache);
81 # Memcached may have been running when plack has been initialized but could have been stopped since
82 # FIXME What are the consequences of that??
83 my $is_memcached_still_active = $cache->set_in_cache('test_for_about_page', "just a simple value");
84
85 my $where_is_memcached_config = 'nowhere';
86 if ( $ENV{MEMCACHED_SERVERS} and C4::Context->config('memcached_servers') ) {
87     $where_is_memcached_config = 'both';
88 } elsif ( $ENV{MEMCACHED_SERVERS} and not C4::Context->config('memcached_servers') ) {
89     $where_is_memcached_config = 'ENV_only';
90 } elsif ( C4::Context->config('memcached_servers') ) {
91     $where_is_memcached_config = 'config_only';
92 }
93
94 $template->param(
95     effective_caching_method => $effective_caching_method,
96     memcached_servers   => $memcached_servers,
97     memcached_namespace => $memcached_namespace,
98     is_memcached_still_active => $is_memcached_still_active,
99     where_is_memcached_config => $where_is_memcached_config,
100     memcached_running   => Koha::Caches->get_instance->memcached_cache,
101 );
102
103 # Additional system information for warnings
104
105 my $warnStatisticsFieldsError;
106 my $prefStatisticsFields = C4::Context->preference('StatisticsFields');
107 if ($prefStatisticsFields) {
108     $warnStatisticsFieldsError = $prefStatisticsFields
109         unless ( $prefStatisticsFields eq C4::Members::Statistics->get_fields() );
110 }
111
112 my $prefAutoCreateAuthorities = C4::Context->preference('AutoCreateAuthorities');
113 my $prefBiblioAddsAuthorities = C4::Context->preference('BiblioAddsAuthorities');
114 my $warnPrefBiblioAddsAuthorities = ( $prefAutoCreateAuthorities && ( !$prefBiblioAddsAuthorities) );
115
116 my $prefEasyAnalyticalRecords  = C4::Context->preference('EasyAnalyticalRecords');
117 my $prefUseControlNumber  = C4::Context->preference('UseControlNumber');
118 my $warnPrefEasyAnalyticalRecords  = ( $prefEasyAnalyticalRecords  && $prefUseControlNumber );
119 my $warnPrefAnonymousPatron = (
120     C4::Context->preference('OPACPrivacy')
121         and not C4::Context->preference('AnonymousPatron')
122 );
123
124 my $anonymous_patron = Koha::Patrons->find( C4::Context->preference('AnonymousPatron') );
125 my $warnPrefAnonymousPatron_PatronDoesNotExist = ( not $anonymous_patron and Koha::Patrons->search({ privacy => 2 })->count );
126
127 my $errZebraConnection = C4::Context->Zconn("biblioserver",0)->errcode();
128
129 my $warnIsRootUser   = (! $loggedinuser);
130
131 my $warnNoActiveCurrency = (! defined Koha::Acquisition::Currencies->get_active);
132
133 my @xml_config_warnings;
134
135 my $context = new C4::Context;
136
137 if ( ! defined C4::Context->config('zebra_bib_index_mode') ) {
138     push @xml_config_warnings, {
139         error => 'zebra_bib_index_mode_warn'
140     };
141     if ($context->{'server'}->{'biblioserver'}->{'config'} !~ /zebra-biblios-dom.cfg/) {
142         push @xml_config_warnings, {
143             error => 'zebra_bib_mode_seems_grs1'
144         };
145     }
146     else {
147         push @xml_config_warnings, {
148             error => 'zebra_bib_mode_seems_dom'
149         };
150     }
151 } else {
152     push @xml_config_warnings, { error => 'zebra_bib_grs_warn' }
153         if C4::Context->config('zebra_bib_index_mode') eq 'grs1';
154 }
155
156 if ( (C4::Context->config('zebra_bib_index_mode') eq 'dom') &&
157      ($context->{'server'}->{'biblioserver'}->{'config'} !~ /zebra-biblios-dom.cfg/) ) {
158
159     push @xml_config_warnings, {
160         error => 'zebra_bib_index_mode_mismatch_warn'
161     };
162 }
163
164 if ( (C4::Context->config('zebra_bib_index_mode') eq 'grs1') &&
165      ($context->{'server'}->{'biblioserver'}->{'config'} =~ /zebra-biblios-dom.cfg/) ) {
166
167     push @xml_config_warnings, {
168         error => 'zebra_bib_index_mode_mismatch_warn'
169     };
170 }
171
172 if ( ! defined C4::Context->config('zebra_auth_index_mode') ) {
173     push @xml_config_warnings, {
174         error => 'zebra_auth_index_mode_warn'
175     };
176     if ($context->{'server'}->{'authorityserver'}->{'config'} !~ /zebra-authorities-dom.cfg/) {
177         push @xml_config_warnings, {
178             error => 'zebra_auth_mode_seems_grs1'
179         };
180     }
181     else {
182         push @xml_config_warnings, {
183             error => 'zebra_auth_mode_seems_dom'
184         };
185     }
186 } else {
187     push @xml_config_warnings, { error => 'zebra_auth_grs_warn' }
188         if C4::Context->config('zebra_auth_index_mode') eq 'grs1';
189 }
190
191 if ( (C4::Context->config('zebra_auth_index_mode') eq 'dom') && ($context->{'server'}->{'authorityserver'}->{'config'} !~ /zebra-authorities-dom.cfg/) ) {
192     push @xml_config_warnings, {
193         error => 'zebra_auth_index_mode_mismatch_warn'
194     };
195 }
196
197 if ( (C4::Context->config('zebra_auth_index_mode') eq 'grs1') && ($context->{'server'}->{'authorityserver'}->{'config'} =~ /zebra-authorities-dom.cfg/) ) {
198     push @xml_config_warnings, {
199         error => 'zebra_auth_index_mode_mismatch_warn'
200     };
201 }
202
203 if ( ! defined C4::Context->config('log4perl_conf') ) {
204     push @xml_config_warnings, {
205         error => 'log4perl_entry_missing'
206     }
207 }
208
209 if ( ! defined C4::Context->config('upload_path') ) {
210     if ( Koha::Config::SysPrefs->find('OPACBaseURL')->value ) {
211         # OPACBaseURL seems to be set
212         push @xml_config_warnings, {
213             error => 'uploadpath_entry_missing'
214         }
215     } else {
216         push @xml_config_warnings, {
217             error => 'uploadpath_and_opacbaseurl_entry_missing'
218         }
219     }
220 }
221
222 # Test QueryParser configuration sanity
223 if ( C4::Context->preference( 'UseQueryParser' ) ) {
224     # Get the QueryParser configuration file name
225     my $queryparser_file          = C4::Context->config( 'queryparser_config' );
226     my $queryparser_fallback_file = '/etc/koha/searchengine/queryparser.yaml';
227     # Check QueryParser is functional
228     my $QParser = C4::Context->queryparser();
229     my $queryparser_error = {};
230     if ( ! defined $QParser || ref($QParser) ne 'Koha::QueryParser::Driver::PQF' ) {
231         # Error initializing the QueryParser object
232         # Get the used queryparser.yaml file path to report the user
233         $queryparser_error->{ fallback } = ( defined $queryparser_file ) ? 0 : 1;
234         $queryparser_error->{ file }     = ( defined $queryparser_file )
235                                                 ? $queryparser_file
236                                                 : $queryparser_fallback_file;
237         # Report error data to the template
238         $template->param( QueryParserError => $queryparser_error );
239     } else {
240         # Check for an absent queryparser_config entry in koha-conf.xml
241         if ( ! defined $queryparser_file ) {
242             # Not an error but a warning for the missing entry in koha-conf-xml
243             push @xml_config_warnings, {
244                     error => 'queryparser_entry_missing',
245                     file  => $queryparser_fallback_file
246             };
247         }
248     }
249 }
250
251 # Test Zebra facets configuration
252 if ( !defined C4::Context->config('use_zebra_facets') ) {
253     push @xml_config_warnings, { error => 'use_zebra_facets_entry_missing' };
254 } else {
255     if ( C4::Context->config('use_zebra_facets') &&
256          C4::Context->config('zebra_bib_index_mode') ) {
257         # use_zebra_facets works with DOM
258         push @xml_config_warnings, {
259             error => 'use_zebra_facets_needs_dom'
260         } if C4::Context->config('zebra_bib_index_mode') ne 'dom' ;
261     }
262 }
263
264 if ( C4::Context->preference('ILLModule') ) {
265     my $warnILLConfiguration = 0;
266     my $available_ill_backends =
267       ( scalar @{ Koha::Illrequest::Config->new->available_backends } > 0 );
268
269     if ( !$available_ill_backends ) {
270         $template->param( no_ill_backends => 1 );
271         $warnILLConfiguration = 1;
272     }
273
274     $template->param( warnILLConfiguration => $warnILLConfiguration );
275 }
276
277 # Sco Patron should not contain any other perms than circulate => self_checkout
278 if (  C4::Context->preference('WebBasedSelfCheck')
279       and C4::Context->preference('AutoSelfCheckAllowed')
280 ) {
281     my $userid = C4::Context->preference('AutoSelfCheckID');
282     my $all_permissions = C4::Auth::get_user_subpermissions( $userid );
283     my ( $has_self_checkout_perm, $has_other_permissions );
284     while ( my ( $module, $permissions ) = each %$all_permissions ) {
285         if ( $module eq 'circulate' ) {
286             while ( my ( $permission, $flag ) = each %$permissions ) {
287                 if ( $permission eq 'self_checkout' ) {
288                     $has_self_checkout_perm = 1;
289                 } else {
290                     $has_other_permissions = 1;
291                 }
292             }
293         } else {
294             $has_other_permissions = 1;
295         }
296     }
297     $template->param(
298         AutoSelfCheckPatronDoesNotHaveSelfCheckPerm => not ( $has_self_checkout_perm ),
299         AutoSelfCheckPatronHasTooManyPerm => $has_other_permissions,
300     );
301 }
302
303 {
304     my $dbh       = C4::Context->dbh;
305     my $patrons = $dbh->selectall_arrayref(
306         q|select b.borrowernumber from borrowers b join deletedborrowers db on b.borrowernumber=db.borrowernumber|,
307         { Slice => {} }
308     );
309     my $biblios = $dbh->selectall_arrayref(
310         q|select b.biblionumber from biblio b join deletedbiblio db on b.biblionumber=db.biblionumber|,
311         { Slice => {} }
312     );
313     my $items = $dbh->selectall_arrayref(
314         q|select i.itemnumber from items i join deleteditems di on i.itemnumber=di.itemnumber|,
315         { Slice => {} }
316     );
317     my $checkouts = $dbh->selectall_arrayref(
318         q|select i.issue_id from issues i join old_issues oi on i.issue_id=oi.issue_id|,
319         { Slice => {} }
320     );
321     my $holds = $dbh->selectall_arrayref(
322         q|select r.reserve_id from reserves r join old_reserves o on r.reserve_id=o.reserve_id|,
323         { Slice => {} }
324     );
325     if ( @$patrons or @$biblios or @$items or @$checkouts or @$holds ) {
326         $template->param(
327             has_ai_issues => 1,
328             ai_patrons    => $patrons,
329             ai_biblios    => $biblios,
330             ai_items      => $items,
331             ai_checkouts  => $checkouts,
332             ai_holds      => $holds,
333         );
334     }
335 }
336 my %versions = C4::Context::get_versions();
337
338 $template->param(
339     kohaVersion   => $versions{'kohaVersion'},
340     osVersion     => $versions{'osVersion'},
341     perlPath      => $perl_path,
342     perlVersion   => $versions{'perlVersion'},
343     perlIncPath   => [ map { perlinc => $_ }, @INC ],
344     mysqlVersion  => $versions{'mysqlVersion'},
345     apacheVersion => $versions{'apacheVersion'},
346     zebraVersion  => $zebraVersion,
347     prefBiblioAddsAuthorities => $prefBiblioAddsAuthorities,
348     prefAutoCreateAuthorities => $prefAutoCreateAuthorities,
349     warnPrefBiblioAddsAuthorities => $warnPrefBiblioAddsAuthorities,
350     warnPrefEasyAnalyticalRecords  => $warnPrefEasyAnalyticalRecords,
351     warnPrefAnonymousPatron => $warnPrefAnonymousPatron,
352     warnPrefAnonymousPatron_PatronDoesNotExist => $warnPrefAnonymousPatron_PatronDoesNotExist,
353     errZebraConnection => $errZebraConnection,
354     warnIsRootUser => $warnIsRootUser,
355     warnNoActiveCurrency => $warnNoActiveCurrency,
356     warnNoTemplateCaching => ( C4::Context->config('template_cache_dir') ? 0 : 1 ),
357     xml_config_warnings => \@xml_config_warnings,
358     warnStatisticsFieldsError => $warnStatisticsFieldsError,
359 );
360
361 my @components = ();
362
363 my $perl_modules = C4::Installer::PerlModules->new;
364 $perl_modules->versions_info;
365
366 my @pm_types = qw(missing_pm upgrade_pm current_pm);
367
368 foreach my $pm_type(@pm_types) {
369     my $modules = $perl_modules->get_attr($pm_type);
370     foreach (@$modules) {
371         my ($module, $stats) = each %$_;
372         push(
373             @components,
374             {
375                 name    => $module,
376                 version => $stats->{'cur_ver'},
377                 missing => ($pm_type eq 'missing_pm' ? 1 : 0),
378                 upgrade => ($pm_type eq 'upgrade_pm' ? 1 : 0),
379                 current => ($pm_type eq 'current_pm' ? 1 : 0),
380                 require => $stats->{'required'},
381                 reqversion => $stats->{'min_ver'},
382             }
383         );
384     }
385 }
386
387 @components = sort {$a->{'name'} cmp $b->{'name'}} @components;
388
389 my $counter=0;
390 my $row = [];
391 my $table = [];
392 foreach (@components) {
393     push (@$row, $_);
394     unless (++$counter % 4) {
395         push (@$table, {row => $row});
396         $row = [];
397     }
398 }
399 # Processing the last line (if there are any modules left)
400 if (scalar(@$row) > 0) {
401     # Extending $row to the table size
402     $$row[3] = '';
403     # Pushing the last line
404     push (@$table, {row => $row});
405 }
406 ## ## $table
407
408 $template->param( table => $table );
409
410
411 ## ------------------------------------------
412 ## Koha time line code
413
414 #get file location
415 my $docdir;
416 if ( defined C4::Context->config('docdir') ) {
417     $docdir = C4::Context->config('docdir');
418 } else {
419     # if no <docdir> is defined in koha-conf.xml, use the default location
420     # this is a work-around to stop breakage on upgraded Kohas, bug 8911
421     $docdir = C4::Context->config('intranetdir') . '/docs';
422 }
423
424 if ( open( my $file, "<:encoding(UTF-8)", "$docdir" . "/history.txt" ) ) {
425
426     my $i = 0;
427
428     my @rows2 = ();
429     my $row2  = [];
430
431     my @lines = <$file>;
432     close($file);
433
434     shift @lines; #remove header row
435
436     foreach (@lines) {
437         my ( $epoch, $date, $desc, $tag ) = split(/\t/);
438         if(!$desc && $date=~ /(?<=\d{4})\s+/) {
439             ($date, $desc)= ($`, $');
440         }
441         push(
442             @rows2,
443             {
444                 date => $date,
445                 desc => $desc,
446             }
447         );
448     }
449
450     my $table2 = [];
451     #foreach my $row2 (@rows2) {
452     foreach  (@rows2) {
453         push (@$row2, $_);
454         push( @$table2, { row2 => $row2 } );
455         $row2 = [];
456     }
457
458     $template->param( table2 => $table2 );
459 } else {
460     $template->param( timeline_read_error => 1 );
461 }
462
463 output_html_with_http_headers $query, $cookie, $template->output;