| Lines 15-104
          BEGIN {
      
      
        Link Here | 
        
          | 15 | # Koha modules used | 15 | # Koha modules used | 
        
          | 16 | use Koha::Script; | 16 | use Koha::Script; | 
        
          | 17 | use MARC::Record; | 17 | use MARC::Record; | 
            
              |  |  | 18 | use C4::Charset; | 
        
          | 18 | use C4::Context; | 19 | use C4::Context; | 
        
          | 19 | use C4::Biblio; | 20 | use C4::Biblio; | 
        
          | 20 | use Time::HiRes qw(gettimeofday); | 21 | use Time::HiRes qw(gettimeofday); | 
        
          | 21 |  | 22 |  | 
        
          | 22 | use Getopt::Long; | 23 | use Getopt::Long; | 
          
            
              | 23 | my ( $input_marc_file, $number) = ('', 0); | 24 |  | 
            
              | 24 | my ($version, $confirm, $test_parameter); | 25 | my ($version, $confirm); | 
        
          | 25 | GetOptions( | 26 | GetOptions( | 
        
          | 26 |     'c' => \$confirm, | 27 |     'c' => \$confirm, | 
          
            
              | 27 |     'h' => \$version, | 28 |     'h' => \$version | 
            
              | 28 |     't' => \$test_parameter, |  |  | 
        
          | 29 | ); | 29 | ); | 
        
          | 30 |  | 30 |  | 
        
          | 31 | if ($version || (!$confirm)) { | 31 | if ($version || (!$confirm)) { | 
        
          | 32 |     print <<EOF | 32 |     print <<EOF | 
          
            
              | 33 | This script rebuilds the non-MARC DB from the MARC values. | 33 | This script rebuilds the non-MARC fields from the MARC values. | 
        
          | 34 | You can/must use it when you change your mapping. | 34 | You can/must use it when you change your mapping. | 
        
          | 35 |  | 35 |  | 
        
          | 36 | Example: you decide to map biblio.title to 200\$a (it was previously mapped to 610\$a). | 36 | Example: you decide to map biblio.title to 200\$a (it was previously mapped to 610\$a). | 
          
            
              | 37 | Run this script or you will have strange results in OPAC ! | 37 | Run this script or you will have strange results in the UI! | 
        
          | 38 |  | 38 |  | 
        
          | 39 | Syntax: | 39 | Syntax: | 
          
            
              | 40 | \t./batchRebuildBiblioTables.pl -h (or without arguments => shows this screen) | 40 | \t./batchRebuildBiblioTables.pl -h (or without arguments => show this screen) | 
            
              | 41 | \t./batchRebuildBiblioTables.pl -c (c like confirm => rebuild non marc DB (may be long) | 41 | \t./batchRebuildBiblioTables.pl -c (c like confirm => rebuild non-MARC fields (may take long) | 
            
              | 42 | \t-t => test only, change nothing in DB |  |  | 
        
          | 43 | EOF | 42 | EOF | 
        
          | 44 | ; | 43 | ; | 
        
          | 45 |     exit; | 44 |     exit; | 
        
          | 46 | } | 45 | } | 
        
          | 47 |  | 46 |  | 
            
              |  |  | 47 | $|=1; # non-buffered output | 
            
              | 48 |  | 
        
          | 48 | my $dbh = C4::Context->dbh; | 49 | my $dbh = C4::Context->dbh; | 
        
          | 49 | my $i=0; | 50 | my $i=0; | 
          
            
              | 50 | my $starttime = time(); | 51 | my $starttime = gettimeofday; | 
            
              | 51 |  | 52 | my $marcflavour = C4::Context->preference('marcflavour'); | 
            
              | 52 | $|=1; # flushes output | 53 | my $sth = $dbh->prepare('SELECT biblionumber, frameworkcode FROM biblio'); | 
            
              | 53 | $starttime = gettimeofday; | 54 | $sth->execute(); | 
        
          | 54 |  | 55 |  | 
            
              | 55 | #1st of all, find item MARC tag. |  |  | 
            
              | 56 | my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.itemnumber",''); | 
            
              | 57 | # $dbh->do("lock tables biblio write, biblioitems write, items write, marc_biblio write, marc_subfield_table write, marc_blob_subfield write, marc_word write, marc_subfield_structure write"); | 
            
              | 58 | my $sth = $dbh->prepare("SELECT biblionumber FROM biblio"); | 
            
              | 59 | $sth->execute; | 
            
              | 60 | # my ($biblionumbermax) =  $sth->fetchrow; | 
            
              | 61 | # warn "$biblionumbermax <<=="; | 
        
          | 62 | my @errors; | 56 | my @errors; | 
          
            
              | 63 | while (my ($biblionumber)= $sth->fetchrow) { | 57 | while (my ($biblionumber, $frameworkcode) = $sth->fetchrow) { | 
            
              | 64 |     #now, parse the record, extract the item fields, and store them in somewhere else. | 58 |     my $marcxml = GetXmlBiblio($biblionumber); | 
            
              | 65 |     my $record = GetMarcBiblio({ biblionumber => $biblionumber }); | 59 |     if (not defined $marcxml) { | 
            
              | 66 |     if (not defined $record) { | 60 |         push @errors, $biblionumber; | 
            
              | 67 | 	push @errors, $biblionumber; | 61 |         next; | 
            
              | 68 | 	next; |  |  | 
        
          | 69 |     } | 62 |     } | 
          
            
              | 70 |     my @fields = $record->field($tagfield); | 63 |  | 
            
              | 71 |     my @items; | 64 |     $marcxml = C4::Charset::StripNonXmlChars( $marcxml ); | 
            
              | 72 |     my $nbitems=0; | 65 |     my $record = eval { | 
            
              | 73 |     print "."; | 66 |         MARC::Record::new_from_xml($marcxml, 'utf8', $marcflavour); | 
            
              | 74 |     my $timeneeded = gettimeofday - $starttime; | 67 |     }; | 
            
              | 75 |     print "$i in $timeneeded s\n" unless ($i % 50); | 68 |     if ($@) { | 
            
              | 76 |     $i++; | 69 |         push @errors, $biblionumber; | 
            
              | 77 |     foreach my $field (@fields) { | 70 |         next; | 
            
              | 78 |         my $item = MARC::Record->new(); |  |  | 
            
              | 79 |         $item->append_fields($field); | 
            
              | 80 |         push @items,$item; | 
            
              | 81 |         $record->delete_field($field); | 
            
              | 82 |         $nbitems++; | 
        
          | 83 |     } | 71 |     } | 
          
            
              | 84 | #     print "$biblionumber\n"; | 72 |  | 
            
              | 85 |     my $frameworkcode = GetFrameworkCode($biblionumber); | 73 |     my $biblio = TransformMarcToKoha($record); | 
            
              | 86 |     localNEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode) unless $test_parameter; | 74 |     C4::Biblio::_koha_modify_biblio($dbh, $biblio, $frameworkcode); | 
            
              | 87 | } | 75 |     C4::Biblio::_koha_modify_biblioitem_nonmarc($dbh, $biblio); | 
            
              | 88 | # $dbh->do("unlock tables"); | 76 |  | 
            
              | 89 | my $timeneeded = time() - $starttime; | 77 |     $i++; | 
            
              | 90 | print "$i MARC record done in $timeneeded seconds\n"; | 78 |     printf("%lu records processed in %.2f seconds\n", $i, gettimeofday() - $starttime) unless ($i % 100); | 
            
              | 91 | if (scalar(@errors) > 0) { |  |  | 
            
              | 92 |     print "Some biblionumber could not be processed though: ", join(" ", @errors); | 
        
          | 93 | } | 79 | } | 
        
          | 94 |  | 80 |  | 
          
            
              | 95 | # modified NEWmodbiblio to jump the MARC part of the biblio modif | 81 | printf("\n%lu records processed in %.2f seconds\n", $i, gettimeofday() - $starttime); | 
            
              | 96 | # highly faster | 82 | if (scalar(@errors) > 0) { | 
            
              | 97 | sub localNEWmodbiblio { | 83 |     print "Some records could not be processed though: ", join(' ', @errors); | 
            
              | 98 |     my ($dbh,$record,$biblionumber,$frameworkcode) =@_; |  |  | 
            
              | 99 |     $frameworkcode="" unless $frameworkcode; | 
            
              | 100 |     my $oldbiblio = TransformMarcToKoha($record,$frameworkcode); | 
            
              | 101 |     C4::Biblio::_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode ); | 
            
              | 102 |     C4::Biblio::_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio ); | 
            
              | 103 |     return 1; | 
        
          | 104 | } | 84 | } | 
            
              | 105 | -  |  |  |