|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
#----------------------------------- |
| 3 |
# Copyright 2013 ByWater Solutions |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 2 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
#----------------------------------- |
| 20 |
|
| 21 |
=head1 NAME |
| 22 |
|
| 23 |
import_lexile.pl Import lexile scores for records from csv. |
| 24 |
|
| 25 |
=cut |
| 26 |
|
| 27 |
use Modern::Perl; |
| 28 |
|
| 29 |
use Getopt::Long; |
| 30 |
use Text::CSV; |
| 31 |
|
| 32 |
use C4::Context; |
| 33 |
use C4::Biblio; |
| 34 |
use C4::Koha qw( GetVariationsOfISBN ); |
| 35 |
use Koha::Database; |
| 36 |
|
| 37 |
BEGIN { |
| 38 |
|
| 39 |
# find Koha's Perl modules |
| 40 |
# test carefully before changing this |
| 41 |
use FindBin; |
| 42 |
eval { require "$FindBin::Bin/../kohalib.pl" }; |
| 43 |
} |
| 44 |
|
| 45 |
my $file; |
| 46 |
my $verbose; |
| 47 |
my $field_number = "521"; |
| 48 |
my $subfield_target_audience_note = "a"; |
| 49 |
my $subfield_source = "b"; |
| 50 |
my $subfield_source_value = "Lexile"; |
| 51 |
|
| 52 |
GetOptions( |
| 53 |
'f|file=s' => \$file, |
| 54 |
'v|verbose' => \$verbose, |
| 55 |
'field=s' => \$field_number, |
| 56 |
'target-audience-note=s' => $subfield_target_audience_note, |
| 57 |
'source=s' => $subfield_source, |
| 58 |
'source-value=s' => $subfield_source_value, |
| 59 |
); |
| 60 |
|
| 61 |
my $usage = << 'ENDUSAGE'; |
| 62 |
import_lexile.pl: Import lexile scores for records from csv. |
| 63 |
|
| 64 |
import_lexile.pl -f /path/to/LexileTitles.txt |
| 65 |
|
| 66 |
This script takes the following parameters : |
| 67 |
|
| 68 |
-f | --file CSV file of lexile scores ( acquired from Lexile.com ) |
| 69 |
-v | --verbose Print data on found matches |
| 70 |
--field Defines the field number for the Lexile data ( default: 521 ) |
| 71 |
--target-audience-note Defines the subfield for the lexile score ( default: a ) |
| 72 |
--source Defines the "Source" subfield ( default: b ) |
| 73 |
--source-value Defines the value to put stored in the "Source" subfield ( default: "Lexile" ) |
| 74 |
|
| 75 |
ENDUSAGE |
| 76 |
|
| 77 |
unless ($file) { |
| 78 |
print $usage; |
| 79 |
exit(1); |
| 80 |
} |
| 81 |
|
| 82 |
my $schema = Koha::Database->new()->schema(); |
| 83 |
|
| 84 |
my $csv = Text::CSV->new( { sep_char => "\t" } ) |
| 85 |
or die "Cannot use CSV: " . Text::CSV->error_diag(); |
| 86 |
|
| 87 |
open my $fh, "<:encoding(utf8)", $file or die "test.csv: $!"; |
| 88 |
|
| 89 |
my $column_names = $csv->getline($fh); |
| 90 |
$csv->column_names(@$column_names); |
| 91 |
|
| 92 |
while ( my $row = $csv->getline_hr($fh) ) { |
| 93 |
|
| 94 |
# Match by ISBN |
| 95 |
my @isbns = ( |
| 96 |
$row->{ISBN}, $row->{ISBN13}, |
| 97 |
GetVariationsOfISBN( $row->{ISBN} ), |
| 98 |
GetVariationsOfISBN( $row->{ISBN13} ) |
| 99 |
); |
| 100 |
|
| 101 |
my @likes = map { { isbn => { like => '%' . $_ . '%' } } } @isbns; |
| 102 |
|
| 103 |
my @biblionumbers = |
| 104 |
$schema->resultset('Biblioitem')->search( -or => \@likes ) |
| 105 |
->get_column('biblionumber')->all(); |
| 106 |
|
| 107 |
foreach my $biblionumber (@biblionumbers) { |
| 108 |
my $record = GetMarcBiblio($biblionumber); |
| 109 |
|
| 110 |
if ($verbose) { |
| 111 |
say "Found matching record! Biblionumber: $biblionumber"; |
| 112 |
say "Title: " . $row->{Title}; |
| 113 |
say "Author: " . $row->{Author}; |
| 114 |
say "ISBN10: " . $row->{ISBN}; |
| 115 |
say "ISBN13: " . $row->{ISBN13}; |
| 116 |
say q{}; |
| 117 |
} |
| 118 |
|
| 119 |
# Check for existing embedded lexile score |
| 120 |
my $lexile_score_field; |
| 121 |
for my $field ( $record->field($field_number) ) { |
| 122 |
if ( $field->subfield($subfield_source) eq $subfield_source_value ) |
| 123 |
{ |
| 124 |
$lexile_score_field = $field; |
| 125 |
last; # Each item can only have one lexile score |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if ($lexile_score_field) { |
| 130 |
$lexile_score_field->update( |
| 131 |
ind1 => '8', |
| 132 |
ind2 => '#', |
| 133 |
$subfield_target_audience_note => $row->{Lexile}, |
| 134 |
$subfield_source => $subfield_source_value, |
| 135 |
); |
| 136 |
} |
| 137 |
else { |
| 138 |
my $field = MARC::Field->new( |
| 139 |
$field_number, '8', '#', |
| 140 |
$subfield_target_audience_note => $row->{Lexile}, |
| 141 |
$subfield_source => $subfield_source_value, |
| 142 |
); |
| 143 |
$record->append_fields($field); |
| 144 |
} |
| 145 |
|
| 146 |
ModBiblio( $record, $biblionumber ); |
| 147 |
} |
| 148 |
|
| 149 |
} |