-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathimport_lexile.pl
executable file
·200 lines (163 loc) · 6.6 KB
/
import_lexile.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/perl
#-----------------------------------
# Copyright 2015 ByWater Solutions
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
#-----------------------------------
=head1 NAME
import_lexile.pl Import lexile scores for records from csv.
=cut
use utf8;
use Modern::Perl;
use Getopt::Long qw( GetOptions );
use Text::CSV;
use Koha::Script;
use C4::Context;
use C4::Biblio qw( ModBiblio );
use C4::Koha qw( GetVariationsOfISBN );
use Koha::Biblios;
use Koha::Database;
binmode STDOUT, ':encoding(UTF-8)';
my $help;
my $confirm;
my $test;
my $file;
my $verbose;
my $start;
my $end;
my $field_number = "521";
my $subfield_target_audience_note = "a";
my $subfield_source = "b";
my $subfield_source_value = "Lexile";
GetOptions(
'h|help' => \$help,
'c|confirm' => \$confirm,
't|test' => \$test,
'f|file=s' => \$file,
'v|verbose+' => \$verbose,
's|start=s' => \$start,
'e|end=s' => \$end,
'field=s' => \$field_number,
'target-audience-note=s' => $subfield_target_audience_note,
'source=s' => $subfield_source,
'source-value=s' => $subfield_source_value,
);
my $usage = << 'ENDUSAGE';
import_lexile.pl: Import lexile scores for records from csv.
import_lexile.pl -f /path/to/LexileTitles.txt
This script takes the following parameters :
-h --help Display this help
-c --confirm Confirms you want to really run this script ( otherwise print help )
-t --test Runs the script in test mode ( no changes will be made to your database )
-f --file CSV file of lexile scores ( acquired from Lexile.com )
-v --verbose Print data on found matches. Use -v -v for more data, and -v -v -v will give the most data.
--field Defines the field number for the Lexile data ( default: 521 )
--target-audience-note Defines the subfield for the lexile score ( default: a )
--source Defines the "Source" subfield ( default: b )
--source-value Defines the value to put stored in the "Source" subfield ( default: "Lexile" )
The CSV file must have the following columns ( with the first line being the column headers ) in tab delimited format:
Title, Author, ISBN, ISBN13, Lexile
ENDUSAGE
if ( $help || !$file || !$confirm ) {
say $usage;
exit(1);
}
my $schema = Koha::Database->new()->schema();
my $csv = Text::CSV->new( { binary => 1, sep_char => "\t", formula => 'empty' } )
or die "Cannot use CSV: " . Text::CSV->error_diag();
open my $fh, "<:encoding(utf8)", $file or die "test.csv: $!";
my $column_names = $csv->getline($fh);
$csv->column_names(@$column_names);
my $counter = 0;
my $i = 0;
while ( my $row = $csv->getline_hr($fh) ) {
$i++;
next if ( $start && $i < $start );
last if ( $end && $i >= $end );
if ( $verbose > 1 ) {
say "Searching for matching record for row $i...";
say "Title: " . $row->{Title};
say "Author: " . $row->{Author};
say "ISBN10: " . $row->{ISBN};
say "ISBN13: " . $row->{ISBN13};
say q{};
}
# Match by ISBN
my @isbns;
for ( 'ISBN', 'ISBN13' ) {
if ( $row->{$_} && $row->{$_} ne "None" ) {
push( @isbns, $row->{$_} );
eval { push( @isbns, GetVariationsOfISBN( $row->{$_} ) ) };
}
}
@isbns = grep( $_, @isbns );
next unless @isbns;
say "Searching for ISBNs: " . join( ' : ', @isbns ) if ( $verbose > 2 );
my @likes = map { { isbn => { like => '%' . $_ . '%' } } } @isbns;
my @biblionumbers =
$schema->resultset('Biblioitem')->search( { -or => \@likes } )->get_column('biblionumber')->all();
say "Found matching records! Biblionumbers: " . join( " ,", @biblionumbers )
if ( @biblionumbers && $verbose > 2 );
foreach my $biblionumber (@biblionumbers) {
$counter++;
my $biblio = Koha::Biblios->find($biblionumber);
my $record = $biblio->metadata->record;
if ($verbose) {
say "Found matching record! Biblionumber: $biblionumber";
if ( $verbose > 2 ) {
my $biblio = Koha::Biblios->find($biblionumber);
say "Title from record: " . $biblio->title
if $biblio->title;
say "Author from record: " . $biblio->author
if $biblio->author;
say "ISBN from record: " . $biblio->biblioitem->isbn
if $biblio->biblioitem->isbn;
}
say "Title: " . $row->{Title};
say "Author: " . $row->{Author};
say "ISBN10: " . $row->{ISBN};
say "ISBN13: " . $row->{ISBN13};
say q{};
}
# Check for existing embedded lexile score
my $lexile_score_field;
for my $field ( $record->field($field_number) ) {
if ( defined( $field->subfield($subfield_source) )
&& $field->subfield($subfield_source) eq $subfield_source_value )
{
$lexile_score_field = $field;
last; # Each item can only have one lexile score
}
}
if ($lexile_score_field) {
$lexile_score_field->update(
ind1 => '8',
ind2 => '#',
$subfield_target_audience_note => $row->{Lexile},
$subfield_source => $subfield_source_value,
);
} else {
my $field = MARC::Field->new(
$field_number, '8', '#',
$subfield_target_audience_note => $row->{Lexile},
$subfield_source => $subfield_source_value,
);
$record->append_fields($field);
}
ModBiblio( $record, $biblionumber, undef, { overlay_context => { source => 'import_lexile' } } ) unless ($test);
}
}
say "Update $counter records" if $verbose;