| 
      
            Lines 2-8
          
      
      
        Link Here
      
     | 
  
        
          | 2 | 
          #  | 
          2 | 
          #  | 
        
        
          | 3 | 
          # This script should be used only with UNIMARC flavour  | 
          3 | 
          # This script should be used only with UNIMARC flavour  | 
        
        
          | 4 | 
          # It is designed to report some missing information from biblio  | 
          4 | 
          # It is designed to report some missing information from biblio  | 
        
          
            
              | 5 | 
              # table into  marc data  | 
              5 | 
              # table into marc data  | 
            
        
          | 6 | 
          #  | 
          6 | 
          #  | 
        
        
          | 7 | 
          use strict;  | 
          7 | 
          use strict;  | 
        
        
          | 8 | 
          use warnings;  | 
          8 | 
          use warnings;  | 
        
  
    | 
      
            Lines 13-75
          BEGIN {
      
      
        Link Here
      
     | 
  
        
          | 13 | 
          }  | 
          13 | 
          }  | 
        
        
          | 14 | 
           | 
          14 | 
           | 
        
        
          | 15 | 
          use C4::Biblio;  | 
          15 | 
          use C4::Biblio;  | 
        
            
               | 
               | 
              16 | 
              use Getopt::Long;  | 
            
            
              | 17 | 
               | 
            
            
              | 18 | 
              sub _read_marc_code { | 
            
            
              | 19 | 
                  my $input = shift;  | 
            
            
              | 20 | 
                  my ( $field, $subfield );  | 
            
            
              | 21 | 
                  if ( $input =~ /^(\d{3})$/ ) { | 
            
            
              | 22 | 
                      $field = $1;  | 
            
            
              | 23 | 
                  }  | 
            
            
              | 24 | 
                  elsif ( $input =~ /^(\d{3})(\w)$/ ) { | 
            
            
              | 25 | 
                      $field    = $1;  | 
            
            
              | 26 | 
                      $subfield = $2;  | 
            
            
              | 27 | 
                  }  | 
            
            
              | 28 | 
                  return ( $field, $subfield );  | 
            
            
              | 29 | 
              }  | 
            
            
              | 30 | 
               | 
            
            
              | 31 | 
              my ( $run, $help, $verbose, $where, $date_created_marc, $date_modified_marc );  | 
            
            
              | 32 | 
              GetOptions(  | 
            
            
              | 33 | 
                  'help|h'                 => \$help,  | 
            
            
              | 34 | 
                  'verbose|v'              => \$verbose,  | 
            
            
              | 35 | 
                  'run'                    => \$run,  | 
            
            
              | 36 | 
                  'where:s'                => \$where,  | 
            
            
              | 37 | 
                  'date-created-marc|c:s'  => \$date_created_marc,  | 
            
            
              | 38 | 
                  'date-modified-marc|m:s' => \$date_modified_marc,  | 
            
            
              | 39 | 
              );  | 
            
            
              | 40 | 
              my $debug = $ENV{DEBUG}; | 
            
            
              | 41 | 
              $verbose = 1 if $debug;  | 
            
            
              | 42 | 
               | 
            
            
              | 43 | 
              # display help ?  | 
            
            
              | 44 | 
              if ($help) { | 
            
            
              | 45 | 
                  print_usage();  | 
            
            
              | 46 | 
                  exit 0;  | 
            
            
              | 47 | 
              }  | 
            
            
              | 48 | 
               | 
            
            
              | 49 | 
              $verbose and print "================================\n";  | 
            
            
              | 50 | 
               | 
            
            
              | 51 | 
              # date created MARC field/subfield  | 
            
            
              | 52 | 
              $date_created_marc = '099c' unless $date_created_marc;  | 
            
            
              | 53 | 
              my ( $c_field, $c_subfield ) = _read_marc_code($date_created_marc);  | 
            
            
              | 54 | 
              die "date-created-marc '$date_created_marc' is not correct." unless $c_field;  | 
            
            
              | 55 | 
              if ($verbose) { | 
            
            
              | 56 | 
                  print "Date created on $c_field";  | 
            
            
              | 57 | 
                  print $c_subfield if defined $c_subfield;    # use of defined to allow 0  | 
            
            
              | 58 | 
                  print "\n";  | 
            
            
              | 59 | 
              }  | 
            
            
              | 60 | 
               | 
            
            
              | 61 | 
              # date last modified MARC field/subfield  | 
            
            
              | 62 | 
              $date_modified_marc = '099d' unless $date_modified_marc;  | 
            
            
              | 63 | 
              my ( $m_field, $m_subfield ) = _read_marc_code($date_modified_marc);  | 
            
            
              | 64 | 
              die "date-modified-marc '$date_modified_marc' is not correct." unless $m_field;  | 
            
            
              | 65 | 
              die  | 
            
            
              | 66 | 
              "When date-created-marc and date-modified-marc are on same field, they should have distinct subfields"  | 
            
            
              | 67 | 
                if ( $c_field eq $m_field )  | 
            
            
              | 68 | 
                && ( !defined $c_subfield  | 
            
            
              | 69 | 
                  || !defined $m_subfield  | 
            
            
              | 70 | 
                  || $c_subfield eq $m_subfield );  | 
            
            
              | 71 | 
              if ($verbose) { | 
            
            
              | 72 | 
                  print "Date last modified on $m_field";  | 
            
            
              | 73 | 
                  print $m_subfield if defined $m_subfield;    # use of defined to allow 0  | 
            
            
              | 74 | 
                  print "\n";  | 
            
            
              | 75 | 
              }  | 
            
            
              | 76 | 
               | 
            
            
              | 77 | 
              my $dbh;  | 
            
            
              | 78 | 
              my $sth_prepared;  | 
            
        
          | 16 | 
           | 
          79 | 
           | 
        
        
          | 17 | 
          sub updateMarc { | 
          80 | 
          sub updateMarc { | 
        
          
            
              | 18 | 
                  my $id = shift;  | 
              81 | 
                  my $id     = shift;  | 
            
            
              | 19 | 
                  my $dbh = C4::Context->dbh;  | 
               | 
               | 
            
            
              | 20 | 
                  my $field;  | 
            
        
          | 21 | 
              my $biblio = GetMarcBiblio($id);  | 
          82 | 
              my $biblio = GetMarcBiblio($id);  | 
        
        
          | 22 | 
           | 
          83 | 
           | 
        
          
            
              | 23 | 
                  return unless $biblio;  | 
              84 | 
                  unless ($biblio) { | 
            
            
              | 24 | 
               | 
              85 | 
                      $debug and warn '[ERROR] GetMarcBiblio did not return any biblio.';  | 
            
            
              | 25 | 
                  if(!$biblio->field('099')) | 
              86 | 
                      return;  | 
            
            
              | 26 | 
                  { | 
               | 
               | 
            
            
              | 27 | 
                      $field = new MARC::Field('099','','', | 
            
            
              | 28 | 
                                  'c' => '',  | 
            
            
              | 29 | 
                                  'd'=>'');  | 
            
            
              | 30 | 
                      $biblio->add_fields($field);  | 
            
        
          | 31 | 
              }  | 
          87 | 
              }  | 
        
        
          | 32 | 
           | 
          88 | 
           | 
        
          
            
              | 33 | 
                  $field = $biblio->field('099'); | 
              89 | 
                  my $c_marc_field = $biblio->field($c_field);  | 
            
            
               | 
               | 
              90 | 
                  my $m_marc_field = $biblio->field($m_field);  | 
            
        
          | 34 | 
           | 
          91 | 
           | 
        
          
            
              | 35 | 
                  my $sth = $dbh->prepare("SELECT DATE_FORMAT(datecreated,'%Y-%m-%d') as datecreated, | 
              92 | 
                  my $c_marc_value;  | 
            
            
              | 36 | 
                                                  DATE_FORMAT(timestamp,'%Y-%m-%d') as timestamp,  | 
              93 | 
                  if ($c_marc_field) { | 
            
            
              | 37 | 
                                                  frameworkcode  | 
              94 | 
                      $c_marc_value =  | 
            
            
              | 38 | 
                                                  FROM biblio  | 
              95 | 
                        defined $c_subfield  | 
            
            
              | 39 | 
                                                  WHERE biblionumber = ?");  | 
              96 | 
                        ? $c_marc_field->subfield($c_subfield)  | 
            
            
              | 40 | 
                  $sth->execute($id);  | 
              97 | 
                        : $c_marc_field->data();  | 
            
            
              | 41 | 
                  (my $bibliorow = $sth->fetchrow_hashref);  | 
              98 | 
                  }  | 
            
            
               | 
               | 
              99 | 
                  $c_marc_value = '' unless defined $c_marc_value;  | 
            
            
              | 100 | 
               | 
            
            
              | 101 | 
                  my $m_marc_value;  | 
            
            
              | 102 | 
                  if ($m_marc_field) { | 
            
            
              | 103 | 
                      $m_marc_value =  | 
            
            
              | 104 | 
                        defined $m_subfield  | 
            
            
              | 105 | 
                        ? $m_marc_field->subfield($m_subfield)  | 
            
            
              | 106 | 
                        : $m_marc_field->data();  | 
            
            
              | 107 | 
                  }  | 
            
            
              | 108 | 
                  $m_marc_value ||= '';  | 
            
            
              | 109 | 
               | 
            
            
              | 110 | 
                  $sth_prepared = $dbh->prepare(  | 
            
            
              | 111 | 
                      q{ | 
            
            
              | 112 | 
                      SELECT  | 
            
            
              | 113 | 
                          DATE_FORMAT(datecreated,'%Y-%m-%d') AS datecreatediso,  | 
            
            
              | 114 | 
                          DATE_FORMAT(timestamp,'%Y-%m-%d') AS datemodifiediso,  | 
            
            
              | 115 | 
                          frameworkcode  | 
            
            
              | 116 | 
                      FROM biblio  | 
            
            
              | 117 | 
                      WHERE biblionumber = ?  | 
            
            
              | 118 | 
                  }  | 
            
            
              | 119 | 
                  ) unless $sth_prepared;  | 
            
            
              | 120 | 
                  $sth_prepared->execute($id);  | 
            
            
              | 121 | 
                  my $bibliorow     = $sth_prepared->fetchrow_hashref;  | 
            
        
          | 42 | 
              my $frameworkcode = $bibliorow->{'frameworkcode'}; | 
          122 | 
              my $frameworkcode = $bibliorow->{'frameworkcode'}; | 
        
            
               | 
               | 
              123 | 
                  my $c_db_value    = $bibliorow->{'datecreatediso'} || ''; | 
            
            
              | 124 | 
                  my $m_db_value    = $bibliorow->{'datemodifiediso'} || ''; | 
            
            
              | 125 | 
               | 
            
            
              | 126 | 
                  # do nothing if already sync  | 
            
            
              | 127 | 
                  return if ( $c_marc_value eq $c_db_value && $m_marc_value eq $m_db_value );  | 
            
        
          | 43 | 
           | 
          128 | 
           | 
        
          
            
              | 44 | 
                  $field->update( 'c' => $bibliorow->{'datecreated'}, | 
              129 | 
                  # do apply to database ?  | 
            
            
              | 45 | 
                                  'd' => $bibliorow->{'timestamp'} | 
              130 | 
                  return 1 unless $run;  | 
            
            
              | 46 | 
                                  );  | 
               | 
               | 
            
        
          | 47 | 
           | 
          131 | 
           | 
        
          
            
              | 48 | 
                   if(&ModBiblio($biblio, $id, $frameworkcode))  | 
              132 | 
                  # update MARC record  | 
            
            
              | 49 | 
                   { | 
               | 
               | 
            
            
              | 50 | 
                      print "\r$id";  | 
            
            
              | 51 | 
                   }  | 
            
        
          | 52 | 
           | 
          133 | 
           | 
        
            
               | 
               | 
              134 | 
                  # date created field  | 
            
            
              | 135 | 
                  unless ($c_marc_field) { | 
            
            
              | 136 | 
                      $biblio->add_fields( new MARC::Field( $c_field, '', '' ) );  | 
            
            
              | 137 | 
                      $debug and warn "[WARN] $c_field did not exist.";  | 
            
            
              | 138 | 
                      $c_marc_field = $biblio->field($c_field);  | 
            
            
              | 139 | 
               | 
            
            
              | 140 | 
                      # when on same field  | 
            
            
              | 141 | 
                      if ( $m_field eq $c_field ) { | 
            
            
              | 142 | 
                          $m_marc_field = $c_marc_field;  | 
            
            
              | 143 | 
                      }  | 
            
            
              | 144 | 
                  }  | 
            
            
              | 145 | 
                  if ( defined $c_subfield ) { | 
            
            
              | 146 | 
                      $c_marc_field->update( $c_subfield, $c_db_value );  | 
            
            
              | 147 | 
                  }  | 
            
            
              | 148 | 
                  else { | 
            
            
              | 149 | 
                      $c_marc_field->update($c_db_value);  | 
            
            
              | 150 | 
                  }  | 
            
            
              | 151 | 
               | 
            
            
              | 152 | 
                  # date last modified field  | 
            
            
              | 153 | 
                  unless ($m_marc_field) { | 
            
            
              | 154 | 
                      $biblio->add_fields( new MARC::Field( $m_field, '', '' ) );  | 
            
            
              | 155 | 
                      $debug and warn "[WARN] $m_field did not exist.";  | 
            
            
              | 156 | 
                      $m_marc_field = $biblio->field($m_field);  | 
            
            
              | 157 | 
                  }  | 
            
            
              | 158 | 
                  if ( defined $m_subfield ) { | 
            
            
              | 159 | 
                      $m_marc_field->update( $m_subfield, $m_db_value );  | 
            
            
              | 160 | 
                  }  | 
            
            
              | 161 | 
                  else { | 
            
            
              | 162 | 
                      $m_marc_field->update($m_db_value);  | 
            
            
              | 163 | 
                  }  | 
            
            
              | 164 | 
               | 
            
            
              | 165 | 
                  # apply to databse  | 
            
            
              | 166 | 
                  if ( &ModBiblio( $biblio, $id, $frameworkcode ) ) { | 
            
            
              | 167 | 
                      return 1;  | 
            
            
              | 168 | 
                  }  | 
            
            
              | 169 | 
               | 
            
            
              | 170 | 
                  $debug and warn '[ERROR] ModBiblio failed.';  | 
            
            
              | 171 | 
                  return;  | 
            
        
          | 53 | 
          }  | 
          172 | 
          }  | 
        
        
          | 54 | 
           | 
          173 | 
           | 
        
        
          | 55 | 
          sub process { | 
          174 | 
          sub process { | 
        
        
          | 56 | 
           | 
          175 | 
           | 
        
          
            
              | 57 | 
                  my $dbh = C4::Context->dbh;  | 
              176 | 
                  $dbh = C4::Context->dbh;  | 
            
            
               | 
               | 
              177 | 
                  my $mod_count = 0;  | 
            
        
          | 58 | 
           | 
          178 | 
           | 
        
          
            
              | 59 | 
                  my $sth = $dbh->prepare("SELECT biblionumber FROM biblio"); | 
              179 | 
                  my $query = q{ | 
            
            
               | 
               | 
              180 | 
                      SELECT biblionumber  | 
            
            
              | 181 | 
                      FROM biblio  | 
            
            
              | 182 | 
                      JOIN biblioitems USING (biblionumber)  | 
            
            
              | 183 | 
                  };  | 
            
            
              | 184 | 
                  $query .= qq{ WHERE $where} if $where; | 
            
            
              | 185 | 
                  my $sth = $dbh->prepare($query);  | 
            
        
          | 60 | 
              $sth->execute();  | 
          186 | 
              $sth->execute();  | 
        
        
          | 61 | 
           | 
          187 | 
           | 
        
          
            
              | 62 | 
                  while(my $biblios = $sth->fetchrow_hashref)  | 
              188 | 
                  $verbose and print "Number of concerned biblios: " . $sth->rows . "\n";  | 
            
            
              | 63 | 
                  { | 
              189 | 
               | 
            
            
              | 64 | 
                      updateMarc($biblios->{'biblionumber'}); | 
              190 | 
                  while ( my $biblios = $sth->fetchrow_hashref ) { | 
            
            
              | 65 | 
                      print ".";  | 
              191 | 
                      $verbose and print 'Bib ' . $biblios->{'biblionumber'} . ':'; | 
            
            
               | 
               | 
              192 | 
                      my $ret = updateMarc( $biblios->{'biblionumber'} ); | 
            
            
              | 193 | 
                      if ($ret) { | 
            
            
              | 194 | 
                          $verbose and print 'modified';  | 
            
            
              | 195 | 
                          $mod_count++;  | 
            
            
              | 196 | 
                      }  | 
            
            
              | 197 | 
                      $verbose and print "\n";  | 
            
        
          | 66 | 
              }  | 
          198 | 
              }  | 
        
        
          | 67 | 
           | 
          199 | 
           | 
        
            
               | 
               | 
              200 | 
                  $verbose and print "Number of modified biblios: " . $mod_count . "\n";  | 
            
        
          | 68 | 
          }  | 
          201 | 
          }  | 
        
        
          | 69 | 
           | 
          202 | 
           | 
        
          
            
              | 70 | 
              if (lc(C4::Context->preference('marcflavour')) eq "unimarc"){ | 
              203 | 
              if ( lc( C4::Context->preference('marcflavour') ) eq "unimarc" ) { | 
            
            
              | 71 | 
              process();  | 
              204 | 
                  $verbose  | 
            
            
              | 72 | 
              }   | 
              205 | 
                    and !$run  | 
            
            
               | 
               | 
              206 | 
                    and print "*** Not in run mode, modifications will not be applyed ***\n";  | 
            
            
              | 207 | 
               | 
            
            
              | 208 | 
                  $verbose and print "================================\n";  | 
            
            
              | 209 | 
                  process();  | 
            
            
              | 210 | 
              }  | 
            
        
          | 73 | 
          else { | 
          211 | 
          else { | 
        
          
            
              | 74 | 
              	print "this script is UNIMARC only and should be used only on unimarc databases\n";  | 
              212 | 
                  print  | 
            
            
               | 
               | 
              213 | 
              "This script is UNIMARC only and should be used only on UNIMARC databases\n";  | 
            
            
              | 214 | 
              }  | 
            
            
              | 215 | 
               | 
            
            
              | 216 | 
              sub print_usage { | 
            
            
              | 217 | 
                  print <<_USAGE_;  | 
            
            
              | 218 | 
              Synchronizes date created and date last modified from biblio table to MARC data.  | 
            
            
              | 219 | 
              Does not update biblio if dates are already synchronized.  | 
            
            
              | 220 | 
              UNIMARC specific.  | 
            
            
              | 221 | 
               | 
            
            
              | 222 | 
              Parameters:  | 
            
            
              | 223 | 
                  --help or -h                show this message  | 
            
            
              | 224 | 
                  --verbose or -v             verbose logging  | 
            
            
              | 225 | 
                  --run                       run the command else modifications will not be applyed to database  | 
            
            
              | 226 | 
                  --where                     (optional) use this to limit execution to some biblios :  | 
            
            
              | 227 | 
                                              write a SQL where clause using biblio and/or biblioitems fields  | 
            
            
              | 228 | 
                  --date-created-marc or c    (optional) date created MARC field and optional subfield,  | 
            
            
              | 229 | 
                                              099c by default  | 
            
            
              | 230 | 
                  --date-modified-marc or m   (optional) date last modified MARC field and optional subfield,  | 
            
            
              | 231 | 
                                              099d by default  | 
            
            
              | 232 | 
              _USAGE_  | 
            
        
          | 75 | 
          }  | 
          233 | 
          }  | 
        
            
              | 76 | 
              -   | 
               | 
               |