Bug 8745 - Disallow rebuild_zebra.pl from executing, when run by root user.
[koha.git] / misc / migration_tools / rebuild_zebra.pl
index b72ab2c..14fea3c 100755 (executable)
@@ -11,8 +11,8 @@ use C4::Biblio;
 use C4::AuthoritiesMarc;
 use C4::Items;
 use Koha::RecordProcessor;
+use XML::LibXML;
 
-# 
 # script that checks zebradir structure & create directories & mandatory files if needed
 #
 #
@@ -39,6 +39,9 @@ my $do_not_clear_zebraqueue;
 my $length;
 my $where;
 my $offset;
+my $run_as_root;
+my $run_user = (getpwuid($<))[0];
+
 my $verbose_logging = 0;
 my $zebraidx_log_opt = " -v none,fatal,warn ";
 my $result = GetOptions(
@@ -46,7 +49,7 @@ my $result = GetOptions(
     'r|reset'       => \$reset,
     's'             => \$skip_export,
     'k'             => \$keep_export,
-    'I|skip-index'    => \$skip_index,
+    'I|skip-index'  => \$skip_index,
     'nosanitize'    => \$nosanitize,
     'b'             => \$biblios,
     'noxml'         => \$noxml,
@@ -61,14 +64,21 @@ my $result = GetOptions(
     'length:i'        => \$length,
     'offset:i'      => \$offset,
     'v+'             => \$verbose_logging,
+    'run-as-root'    => \$run_as_root,
 );
 
-
 if (not $result or $want_help) {
     print_usage();
     exit 0;
 }
 
+if( not defined $run_as_root and $run_user eq 'root') {
+    my $msg = "Warning: You are running this script as the user 'root'.\n";
+    $msg   .= "If this is intentional you must explicitly specify this using the -run-as-root switch\n";
+    $msg   .= "Please do '$0 --help' to see usage.\n";
+    die $msg;
+}
+
 if (not $biblios and not $authorities) {
     my $msg = "Must specify -b or -a to reindex bibs or authorities\n";
     $msg   .= "Please do '$0 --help' to see usage.\n";
@@ -140,6 +150,8 @@ if ($do_munge) {
     munge_config();
 }
 
+my $tester = XML::LibXML->new();
+
 if ($authorities) {
     index_records('authority', $directory, $skip_export, $skip_index, $process_zebraqueue, $as_xml, $noxml, $nosanitize, $do_not_clear_zebraqueue, $verbose_logging, $zebraidx_log_opt, $authorityserverdir);
 } else {
@@ -377,8 +389,18 @@ sub export_marc_records_from_sth {
                         substr($itemsxml, index($itemsxml, "</leader>\n", 0) + 10);
                 }
             }
+            # extra test to ensure that result is valid XML; otherwise
+            # Zebra won't parse it in DOM mode
+            eval {
+                my $doc = $tester->parse_string($marcxml);
+            };
+            if ($@) {
+                warn "Error exporting record $record_number ($record_type): $@\n";
+                next;
+            }
             if ( $marcxml ) {
-                print {$fh} $marcxml if $marcxml;
+                $marcxml =~ s!<\?xml version="1.0" encoding="UTF-8"\?>\n!!;
+                print {$fh} $marcxml;
                 $num_exported++;
             }
             next;
@@ -389,6 +411,12 @@ sub export_marc_records_from_sth {
                 my $rec;
                 if ($as_xml) {
                     $rec = $marc->as_xml_record(C4::Context->preference('marcflavour'));
+                    eval {
+                        my $doc = $tester->parse_string($rec);
+                    };
+                    if ($@) {
+                        die "invalid XML: $@";
+                    }
                     $rec =~ s!<\?xml version="1.0" encoding="UTF-8"\?>\n!!;
                 } else {
                     $rec = $marc->as_usmarc();
@@ -397,7 +425,8 @@ sub export_marc_records_from_sth {
                 $num_exported++;
             };
             if ($@) {
-              warn "Error exporting record $record_number ($record_type) ".($noxml ? "not XML" : "XML");
+                warn "Error exporting record $record_number ($record_type) ".($noxml ? "not XML" : "XML");
+                warn "... specific error is $@" if $verbose_logging;
             }
         }
     }
@@ -441,7 +470,6 @@ sub export_marc_records_from_list {
             if ($@) {
               warn "Error exporting record $record_number ($record_type) ".($noxml ? "not XML" : "XML");
             }
-            $num_exported++;
         }
     }
     print "\nRecords exported: $num_exported\n" if ( $verbose_logging );
@@ -656,11 +684,10 @@ sub print_usage {
 $0: reindex MARC bibs and/or authorities in Zebra.
 
 Use this batch job to reindex all biblio or authority
-records in your Koha database.  This job is useful
-only if you are using Zebra; if you are using the 'NoZebra'
-mode, this job should not be used.
+records in your Koha database.
 
 Parameters:
+
     -b                      index bibliographic records
 
     -a                      index authority records
@@ -720,6 +747,9 @@ Parameters:
 
     --munge-config          Deprecated option to try
                             to fix Zebra config files.
+
+    --run-as-root           explicitily allow script to run as 'root' user
+
     --help or -h            show this message.
 _USAGE_
 }