2 #-----------------------------------
3 # Copyright 2015 ByWater Solutions
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 #-----------------------------------
23 import_lexile.pl Import lexile scores for records from csv.
31 use Getopt::Long qw( GetOptions );
36 use C4::Biblio qw( GetMarcBiblio ModBiblio );
37 use C4::Koha qw( GetVariationsOfISBN );
42 binmode STDOUT, ':encoding(UTF-8)';
51 my $field_number = "521";
52 my $subfield_target_audience_note = "a";
53 my $subfield_source = "b";
54 my $subfield_source_value = "Lexile";
58 'c|confirm' => \$confirm,
61 'v|verbose+' => \$verbose,
62 's|start=s' => \$start,
64 'field=s' => \$field_number,
65 'target-audience-note=s' => $subfield_target_audience_note,
66 'source=s' => $subfield_source,
67 'source-value=s' => $subfield_source_value,
70 my $usage = << 'ENDUSAGE';
71 import_lexile.pl: Import lexile scores for records from csv.
73 import_lexile.pl -f /path/to/LexileTitles.txt
75 This script takes the following parameters :
77 -h --help Display this help
78 -c --confirm Confirms you want to really run this script ( otherwise print help )
79 -t --test Runs the script in test mode ( no changes will be made to your database )
80 -f --file CSV file of lexile scores ( acquired from Lexile.com )
81 -v --verbose Print data on found matches. Use -v -v for more data, and -v -v -v will give the most data.
82 --field Defines the field number for the Lexile data ( default: 521 )
83 --target-audience-note Defines the subfield for the lexile score ( default: a )
84 --source Defines the "Source" subfield ( default: b )
85 --source-value Defines the value to put stored in the "Source" subfield ( default: "Lexile" )
87 The CSV file must have the following columns ( with the first line being the column headers ) in tab delimited format:
88 Title, Author, ISBN, ISBN13, Lexile
92 if ( $help || !$file || !$confirm ) {
97 my $schema = Koha::Database->new()->schema();
99 my $csv = Text::CSV->new( { binary => 1, sep_char => "\t" } )
100 or die "Cannot use CSV: " . Text::CSV->error_diag();
102 open my $fh, "<:encoding(utf8)", $file or die "test.csv: $!";
104 my $column_names = $csv->getline($fh);
105 $csv->column_names(@$column_names);
109 while ( my $row = $csv->getline_hr($fh) ) {
112 next if ( $start && $i < $start );
113 last if ( $end && $i >= $end );
115 if ( $verbose > 1 ) {
116 say "Searching for matching record for row $i...";
117 say "Title: " . $row->{Title};
118 say "Author: " . $row->{Author};
119 say "ISBN10: " . $row->{ISBN};
120 say "ISBN13: " . $row->{ISBN13};
126 for ( 'ISBN', 'ISBN13' ) {
127 if ( $row->{$_} && $row->{$_} ne "None" ) {
128 push( @isbns, $row->{$_} );
129 eval { push( @isbns, GetVariationsOfISBN( $row->{$_} ) ) };
132 @isbns = grep( $_, @isbns );
135 say "Searching for ISBNs: " . join( ' : ', @isbns ) if ( $verbose > 2 );
137 my @likes = map { { isbn => { like => '%' . $_ . '%' } } } @isbns;
140 $schema->resultset('Biblioitem')->search( { -or => \@likes } )
141 ->get_column('biblionumber')->all();
143 say "Found matching records! Biblionumbers: " . join( " ,", @biblionumbers )
144 if ( @biblionumbers && $verbose > 2 );
146 foreach my $biblionumber (@biblionumbers) {
148 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
151 say "Found matching record! Biblionumber: $biblionumber";
153 if ( $verbose > 2 ) {
154 my $biblio = Koha::Biblios->find( $biblionumber );
155 say "Title from record: " . $biblio->title
157 say "Author from record: " . $biblio->author
159 say "ISBN from record: " . $biblio->biblioitem->isbn
160 if $biblio->biblioitem->isbn;
162 say "Title: " . $row->{Title};
163 say "Author: " . $row->{Author};
164 say "ISBN10: " . $row->{ISBN};
165 say "ISBN13: " . $row->{ISBN13};
169 # Check for existing embedded lexile score
170 my $lexile_score_field;
171 for my $field ( $record->field($field_number) ) {
172 if ( defined( $field->subfield($subfield_source) )
173 && $field->subfield($subfield_source) eq
174 $subfield_source_value )
176 $lexile_score_field = $field;
177 last; # Each item can only have one lexile score
181 if ($lexile_score_field) {
182 $lexile_score_field->update(
185 $subfield_target_audience_note => $row->{Lexile},
186 $subfield_source => $subfield_source_value,
190 my $field = MARC::Field->new(
191 $field_number, '8', '#',
192 $subfield_target_audience_note => $row->{Lexile},
193 $subfield_source => $subfield_source_value,
195 $record->append_fields($field);
198 ModBiblio( $record, $biblionumber, undef, { overlay_context => { source => 'import_lexile' } } ) unless ( $test );
202 say "Update $counter records" if $verbose;